Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor assets to simplify Wallet Setup #108

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"
"testing"

"github.com/stellar/stellar-disbursement-platform-backend/internal/services/assets"

"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -158,12 +158,12 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
testnetUSDCIssuer := keypair.MustRandom().Address()
data.CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", testnetUSDCIssuer)

assets, err := models.Assets.GetAll(ctx)
actualAssets, err := models.Assets.GetAll(ctx)
require.NoError(t, err)

assert.Len(t, assets, 1)
assert.Equal(t, "USDC", assets[0].Code)
assert.Equal(t, testnetUSDCIssuer, assets[0].Issuer)
assert.Len(t, actualAssets, 1)
assert.Equal(t, "USDC", actualAssets[0].Code)
assert.Equal(t, testnetUSDCIssuer, actualAssets[0].Issuer)

// Wallets
data.CreateWalletFixture(t, ctx, dbConnectionPool, "Vibrant Assist", "https://vibrantapp.com", "api-dev.vibrantapp.com", "https://vibrantapp.com/sdp-dev")
Expand Down Expand Up @@ -196,15 +196,14 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
require.NoError(t, err)

// Validating assets
assets, err = models.Assets.GetAll(ctx)
actualAssets, err = models.Assets.GetAll(ctx)
require.NoError(t, err)

assert.Len(t, assets, 2)
assert.Equal(t, "USDC", assets[0].Code)
assert.NotEqual(t, testnetUSDCIssuer, assets[0].Issuer)
assert.Equal(t, services.DefaultAssetsNetworkMap[utils.PubnetNetworkType]["USDC"], assets[0].Issuer)
assert.Equal(t, "XLM", assets[1].Code)
assert.Empty(t, assets[1].Issuer)
require.Len(t, actualAssets, 2)
require.Equal(t, assets.USDCAssetPubnet.Code, actualAssets[0].Code)
require.Equal(t, assets.USDCAssetPubnet.Issuer, actualAssets[0].Issuer)
require.Equal(t, assets.XLMAsset.Code, actualAssets[1].Code)
require.Empty(t, assets.XLMAsset.Issuer)

// Validating wallets
wallets, err = models.Wallets.GetAll(ctx)
Expand Down Expand Up @@ -238,7 +237,7 @@ func Test_DatabaseCommand_db_setup_for_network(t *testing.T) {
expectedLogs := []string{
"updating/inserting assets for the 'pubnet' network",
"Code: USDC",
fmt.Sprintf("Issuer: %s", services.DefaultAssetsNetworkMap[utils.PubnetNetworkType]["USDC"]),
fmt.Sprintf("Issuer: %s", assets.USDCAssetPubnet.Issuer),
"Code: XLM",
"Issuer: ",
"updating/inserting wallets for the 'pubnet' network",
Expand Down
24 changes: 24 additions & 0 deletions internal/services/assets/assets_pubnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package assets

import "github.com/stellar/stellar-disbursement-platform-backend/internal/data"

// USDCAssetCode is the code for the USDC asset for pubnet and testnet
const USDCAssetCode = "USDC"

// XLMAssetCode is the code for the XLM asset for pubnet and testnet
const XLMAssetCode = "XLM"

// USDCAssetIssuerPubnet is the issuer for the USDC asset for pubnet
const USDCAssetIssuerPubnet = "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN"

// USDCAssetPubnet is the USDC asset for pubnet
var USDCAssetPubnet = data.Asset{
Code: USDCAssetCode,
Issuer: USDCAssetIssuerPubnet,
}

// XLMAsset is the XLM asset for pubnet
var XLMAsset = data.Asset{
Code: XLMAssetCode,
Issuer: "",
}
12 changes: 12 additions & 0 deletions internal/services/assets/assets_testnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package assets

import "github.com/stellar/stellar-disbursement-platform-backend/internal/data"

// USDCAssetIssuerTestnet is the issuer for the USDC asset for testnet
const USDCAssetIssuerTestnet = "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5"

// USDCAssetTestnet is the USDC asset for testnet
var USDCAssetTestnet = data.Asset{
Code: USDCAssetCode,
Issuer: USDCAssetIssuerTestnet,
}
22 changes: 9 additions & 13 deletions internal/services/setup_assets_for_network_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ import (
"fmt"
"strings"

"github.com/stellar/stellar-disbursement-platform-backend/internal/services/assets"

"github.com/lib/pq"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
)

type AssetsNetworkMapType map[utils.NetworkType]map[string]string
type AssetsNetworkMapType map[utils.NetworkType][]data.Asset

var DefaultAssetsNetworkMap = AssetsNetworkMapType{
utils.PubnetNetworkType: {
"USDC": "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
"XLM": "",
},
utils.TestnetNetworkType: {
"USDC": "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
"XLM": "",
},
utils.PubnetNetworkType: []data.Asset{assets.USDCAssetPubnet, assets.XLMAsset},
utils.TestnetNetworkType: []data.Asset{assets.USDCAssetTestnet, assets.XLMAsset},
}

// SetupAssetsForProperNetwork updates and inserts assets for the given Network Passphrase (`network`). So it avoids the application having
Expand All @@ -40,11 +36,11 @@ func SetupAssetsForProperNetwork(ctx context.Context, dbConnectionPool db.DBConn
separator := strings.Repeat("-", 20)
buf := new(strings.Builder)
buf.WriteString("assets' code that will be updated or inserted:\n\n")
for code, issuer := range assets {
codes = append(codes, code)
issuers = append(issuers, issuer)
for _, asset := range assets {
codes = append(codes, asset.Code)
issuers = append(issuers, asset.Issuer)

buf.WriteString(fmt.Sprintf("Code: %s\n%s\n\n", code, separator))
buf.WriteString(fmt.Sprintf("Code: %s\n%s\n\n", asset.Code, separator))
}

log.Ctx(ctx).Info(buf.String())
Expand Down
12 changes: 6 additions & 6 deletions internal/services/setup_assets_for_network_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func Test_SetupAssetsForProperNetwork(t *testing.T) {
assert.Equal(t, pubnetEUROCIssuer, assets[0].Issuer)

assetsNetworkMap := AssetsNetworkMapType{
utils.TestnetNetworkType: {
"EUROC": testnetEUROCIssuer,
"USDC": testnetUSDCIssuer,
utils.TestnetNetworkType: []data.Asset{
{Code: "EUROC", Issuer: testnetEUROCIssuer},
{Code: "USDC", Issuer: testnetUSDCIssuer},
},
}

Expand Down Expand Up @@ -146,9 +146,9 @@ func Test_SetupAssetsForProperNetwork(t *testing.T) {
assert.Equal(t, testnetEUROCIssuer, assets[1].Issuer)

assetsNetworkMap := AssetsNetworkMapType{
utils.PubnetNetworkType: {
"EUROC": pubnetEUROCIssuer,
"USDC": pubnetUSDCIssuer,
utils.PubnetNetworkType: []data.Asset{
{Code: "EUROC", Issuer: pubnetEUROCIssuer},
{Code: "USDC", Issuer: pubnetUSDCIssuer},
},
}

Expand Down
20 changes: 7 additions & 13 deletions internal/services/wallets/wallets_pubnet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package wallets

import "github.com/stellar/stellar-disbursement-platform-backend/internal/data"
import (
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services/assets"
)

var PubnetWallets = []data.Wallet{
{
Expand All @@ -9,10 +12,7 @@ var PubnetWallets = []data.Wallet{
DeepLinkSchema: "https://vibrantapp.com/sdp",
SEP10ClientDomain: "api.vibrantapp.com",
Assets: []data.Asset{
{
Code: "USDC",
Issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
},
assets.USDCAssetPubnet,
},
},
{
Expand All @@ -21,10 +21,7 @@ var PubnetWallets = []data.Wallet{
DeepLinkSchema: "https://vibrantapp.com/sdp-rc",
SEP10ClientDomain: "vibrantapp.com",
Assets: []data.Asset{
{
Code: "USDC",
Issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
},
assets.USDCAssetPubnet,
},
},
{
Expand All @@ -33,10 +30,7 @@ var PubnetWallets = []data.Wallet{
DeepLinkSchema: "https://freedom-public-uat.bpventures.us/disbursement/create",
SEP10ClientDomain: "freedom-public-uat.bpventures.us",
Assets: []data.Asset{
{
Code: "USDC",
Issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
},
assets.USDCAssetPubnet,
},
},
// {
Expand Down
20 changes: 7 additions & 13 deletions internal/services/wallets/wallets_testnet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package wallets

import "github.com/stellar/stellar-disbursement-platform-backend/internal/data"
import (
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/services/assets"
)

var TestnetWallets = []data.Wallet{
{
Expand All @@ -9,14 +12,8 @@ var TestnetWallets = []data.Wallet{
DeepLinkSchema: "https://demo-wallet.stellar.org",
SEP10ClientDomain: "demo-wallet-server.stellar.org",
Assets: []data.Asset{
{
Code: "USDC",
Issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
},
{
Code: "XLM",
Issuer: "",
},
assets.USDCAssetTestnet,
assets.XLMAsset,
},
},
{
Expand All @@ -25,10 +22,7 @@ var TestnetWallets = []data.Wallet{
DeepLinkSchema: "https://vibrantapp.com/sdp-dev",
SEP10ClientDomain: "api-dev.vibrantapp.com",
Assets: []data.Asset{
{
Code: "USDC",
Issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
},
assets.USDCAssetTestnet,
},
},
}
Loading