From f53b45a0628401abf1ab8ebc3eff7e08b7c998b7 Mon Sep 17 00:00:00 2001 From: Marwen Abid Date: Fri, 24 Nov 2023 11:40:29 -0800 Subject: [PATCH] chore: Refactor assets to simplify Wallet Setup (#108) --- cmd/db_test.go | 27 +++++++++---------- internal/services/assets/assets_pubnet.go | 24 +++++++++++++++++ internal/services/assets/assets_testnet.go | 12 +++++++++ .../setup_assets_for_network_service.go | 22 +++++++-------- .../setup_assets_for_network_service_test.go | 12 ++++----- internal/services/wallets/wallets_pubnet.go | 20 +++++--------- internal/services/wallets/wallets_testnet.go | 20 +++++--------- 7 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 internal/services/assets/assets_pubnet.go create mode 100644 internal/services/assets/assets_testnet.go diff --git a/cmd/db_test.go b/cmd/db_test.go index 9e5f09c01..4a4855660 100644 --- a/cmd/db_test.go +++ b/cmd/db_test.go @@ -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" ) @@ -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") @@ -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) @@ -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", diff --git a/internal/services/assets/assets_pubnet.go b/internal/services/assets/assets_pubnet.go new file mode 100644 index 000000000..c3472fb86 --- /dev/null +++ b/internal/services/assets/assets_pubnet.go @@ -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: "", +} diff --git a/internal/services/assets/assets_testnet.go b/internal/services/assets/assets_testnet.go new file mode 100644 index 000000000..5dfb4ab0b --- /dev/null +++ b/internal/services/assets/assets_testnet.go @@ -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, +} diff --git a/internal/services/setup_assets_for_network_service.go b/internal/services/setup_assets_for_network_service.go index b608b40d6..3e0529e1c 100644 --- a/internal/services/setup_assets_for_network_service.go +++ b/internal/services/setup_assets_for_network_service.go @@ -5,6 +5,8 @@ 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" @@ -12,17 +14,11 @@ import ( "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 @@ -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()) diff --git a/internal/services/setup_assets_for_network_service_test.go b/internal/services/setup_assets_for_network_service_test.go index 14bd3620f..e809e38fd 100644 --- a/internal/services/setup_assets_for_network_service_test.go +++ b/internal/services/setup_assets_for_network_service_test.go @@ -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}, }, } @@ -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}, }, } diff --git a/internal/services/wallets/wallets_pubnet.go b/internal/services/wallets/wallets_pubnet.go index 50214b892..772c8b6e8 100644 --- a/internal/services/wallets/wallets_pubnet.go +++ b/internal/services/wallets/wallets_pubnet.go @@ -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{ { @@ -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, }, }, { @@ -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, }, }, { @@ -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, }, }, // { diff --git a/internal/services/wallets/wallets_testnet.go b/internal/services/wallets/wallets_testnet.go index 79231e975..bfde96118 100644 --- a/internal/services/wallets/wallets_testnet.go +++ b/internal/services/wallets/wallets_testnet.go @@ -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{ { @@ -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, }, }, { @@ -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, }, }, }