From 811ae435ce9c0bed18eb0029d2204b11918d7218 Mon Sep 17 00:00:00 2001 From: Karthik Iyer Date: Thu, 26 Sep 2024 20:19:42 -0700 Subject: [PATCH] clean up all occurences of Amount and NumAccounts (#5438) --- clients/horizonclient/main_test.go | 2 - protocols/horizon/main.go | 4 +- .../horizon/internal/actions/asset_test.go | 10 --- .../integration/liquidity_pool_test.go | 2 - .../horizon/internal/integration/sac_test.go | 2 - .../internal/resourceadapter/asset_stat.go | 2 - .../resourceadapter/asset_stat_test.go | 4 - .../configureissuer/configureissuer_test.go | 7 +- .../ticker/internal/scraper/asset_scraper.go | 30 ++++++-- .../internal/scraper/asset_scraper_test.go | 73 +------------------ 10 files changed, 31 insertions(+), 105 deletions(-) diff --git a/clients/horizonclient/main_test.go b/clients/horizonclient/main_test.go index f7b4ba0788..b11b807bee 100644 --- a/clients/horizonclient/main_test.go +++ b/clients/horizonclient/main_test.go @@ -480,8 +480,6 @@ func TestAssetsRequest(t *testing.T) { assert.Equal(t, record.Asset.Code, "ABC") assert.Equal(t, record.Asset.Issuer, "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU") assert.Equal(t, record.PT, "1") - assert.Equal(t, record.NumAccounts, int32(3)) - assert.Equal(t, record.Amount, "105.0000000") assert.Equal(t, record.Flags.AuthRevocable, false) assert.Equal(t, record.Flags.AuthRequired, true) assert.Equal(t, record.Flags.AuthImmutable, false) diff --git a/protocols/horizon/main.go b/protocols/horizon/main.go index df4aee78cf..72c4269f8e 100644 --- a/protocols/horizon/main.go +++ b/protocols/horizon/main.go @@ -178,13 +178,13 @@ type AssetStat struct { PT string `json:"paging_token"` ContractID string `json:"contract_id,omitempty"` // Action needed in release: horizon-v3.0.0: deprecated field - NumAccounts int32 `json:"num_accounts"` + //NumAccounts int32 `json:"num_accounts"` NumClaimableBalances int32 `json:"num_claimable_balances"` NumLiquidityPools int32 `json:"num_liquidity_pools"` NumContracts int32 `json:"num_contracts"` NumArchivedContracts int32 `json:"num_archived_contracts"` // Action needed in release: horizon-v3.0.0: deprecated field - Amount string `json:"amount"` + // Amount string `json:"amount"` Accounts AssetStatAccounts `json:"accounts"` ClaimableBalancesAmount string `json:"claimable_balances_amount"` LiquidityPoolsAmount string `json:"liquidity_pools_amount"` diff --git a/services/horizon/internal/actions/asset_test.go b/services/horizon/internal/actions/asset_test.go index 0e4995d93c..68b56d94b2 100644 --- a/services/horizon/internal/actions/asset_test.go +++ b/services/horizon/internal/actions/asset_test.go @@ -157,8 +157,6 @@ func TestAssetStats(t *testing.T) { }, ClaimableBalancesAmount: "0.0000010", LiquidityPoolsAmount: "0.0000020", - Amount: "0.0000001", - NumAccounts: usdAssetStat.NumAccounts, ContractsAmount: "0.0000000", ArchivedContractsAmount: "0.0000000", Asset: base.Asset{ @@ -206,8 +204,6 @@ func TestAssetStats(t *testing.T) { LiquidityPoolsAmount: "0.0000000", ContractsAmount: "0.0000000", ArchivedContractsAmount: "0.0000000", - Amount: "0.0000023", - NumAccounts: etherAssetStat.NumAccounts, Asset: base.Asset{ Type: "credit_alphanum4", Code: etherAssetStat.AssetCode, @@ -251,10 +247,8 @@ func TestAssetStats(t *testing.T) { }, ClaimableBalancesAmount: "0.0000000", LiquidityPoolsAmount: "0.0000000", - Amount: "0.0000001", ContractsAmount: "0.0000000", ArchivedContractsAmount: "0.0000000", - NumAccounts: otherUSDAssetStat.NumAccounts, Asset: base.Asset{ Type: "credit_alphanum4", Code: otherUSDAssetStat.AssetCode, @@ -300,10 +294,8 @@ func TestAssetStats(t *testing.T) { }, ClaimableBalancesAmount: "0.0000000", LiquidityPoolsAmount: "0.0000000", - Amount: "0.0000111", ContractsAmount: "0.0000000", ArchivedContractsAmount: "0.0000000", - NumAccounts: eurAssetStat.NumAccounts, Asset: base.Asset{ Type: "credit_alphanum4", Code: eurAssetStat.AssetCode, @@ -478,10 +470,8 @@ func TestAssetStatsIssuerDoesNotExist(t *testing.T) { }, ClaimableBalancesAmount: "0.0000000", LiquidityPoolsAmount: "0.0000000", - Amount: "0.0000001", ContractsAmount: "0.0000000", ArchivedContractsAmount: "0.0000000", - NumAccounts: usdAssetStat.NumAccounts, Asset: base.Asset{ Type: "credit_alphanum4", Code: usdAssetStat.AssetCode, diff --git a/services/horizon/internal/integration/liquidity_pool_test.go b/services/horizon/internal/integration/liquidity_pool_test.go index 9106003179..bb7e39a7c6 100644 --- a/services/horizon/internal/integration/liquidity_pool_test.go +++ b/services/horizon/internal/integration/liquidity_pool_test.go @@ -156,8 +156,6 @@ func TestLiquidityPoolHappyPath(t *testing.T) { tt.Equal("credit_alphanum4", stat.Asset.Type) tt.Equal("USD", stat.Asset.Code) tt.Equal(master.Address(), stat.Asset.Issuer) - tt.Equal(int32(2), stat.NumAccounts) - tt.Equal("225.0000000", stat.Amount) tt.Equal(int32(1), stat.NumLiquidityPools) tt.Equal("775.0000000", stat.LiquidityPoolsAmount) diff --git a/services/horizon/internal/integration/sac_test.go b/services/horizon/internal/integration/sac_test.go index 7eb91a201c..a7311d173d 100644 --- a/services/horizon/internal/integration/sac_test.go +++ b/services/horizon/internal/integration/sac_test.go @@ -1104,9 +1104,7 @@ func assertAssetStats(itest *integration.Test, expected assetStats) { asset := assets.Embedded.Records[0] assert.Equal(itest.CurrentTest(), expected.code, asset.Code) assert.Equal(itest.CurrentTest(), expected.issuer, asset.Issuer) - assert.Equal(itest.CurrentTest(), expected.numAccounts, asset.NumAccounts) assert.Equal(itest.CurrentTest(), expected.numAccounts, asset.Accounts.Authorized) - assert.Equal(itest.CurrentTest(), expected.balanceAccounts, amount.MustParse(asset.Amount)) assert.Equal(itest.CurrentTest(), expected.numContracts, asset.NumContracts) assert.Equal(itest.CurrentTest(), expected.numArchivedContracts, asset.NumArchivedContracts) assert.Equal(itest.CurrentTest(), expected.balanceContracts.String(), parseBalance(itest, asset.ContractsAmount).String()) diff --git a/services/horizon/internal/resourceadapter/asset_stat.go b/services/horizon/internal/resourceadapter/asset_stat.go index 994cce99a0..8cc2c12740 100644 --- a/services/horizon/internal/resourceadapter/asset_stat.go +++ b/services/horizon/internal/resourceadapter/asset_stat.go @@ -39,7 +39,6 @@ func PopulateAssetStat( res.NumLiquidityPools = row.Accounts.LiquidityPools res.NumContracts = row.Contracts.ActiveHolders res.NumArchivedContracts = row.Contracts.ArchivedHolders - res.NumAccounts = row.NumAccounts err = populateAssetStatBalances(res, row) if err != nil { return err @@ -63,7 +62,6 @@ func PopulateAssetStat( } func populateAssetStatBalances(res *protocol.AssetStat, row history.AssetAndContractStat) (err error) { - res.Amount, err = amount.IntStringToAmount(row.Balances.Authorized) if err != nil { return errors.Wrap(err, "Invalid amount in PopulateAssetStat") } diff --git a/services/horizon/internal/resourceadapter/asset_stat_test.go b/services/horizon/internal/resourceadapter/asset_stat_test.go index d3b14d62e6..e36b7bef85 100644 --- a/services/horizon/internal/resourceadapter/asset_stat_test.go +++ b/services/horizon/internal/resourceadapter/asset_stat_test.go @@ -67,8 +67,6 @@ func TestPopulateExpAssetStat(t *testing.T) { assert.Equal(t, "770000000000.0000000", res.LiquidityPoolsAmount) assert.Equal(t, "90000000000.0000000", res.ContractsAmount) assert.Equal(t, "70000000000.0000000", res.ArchivedContractsAmount) - assert.Equal(t, "10000000000000.0000000", res.Amount) - assert.Equal(t, int32(429), res.NumAccounts) assert.Equal(t, horizon.AccountFlags{}, res.Flags) assert.Equal(t, "https://xim.com/.well-known/stellar.toml", res.Links.Toml.Href) assert.Equal(t, "", res.ContractID) @@ -90,8 +88,6 @@ func TestPopulateExpAssetStat(t *testing.T) { assert.Equal(t, "credit_alphanum4", res.Type) assert.Equal(t, "XIM", res.Code) assert.Equal(t, "GBZ35ZJRIKJGYH5PBKLKOZ5L6EXCNTO7BKIL7DAVVDFQ2ODJEEHHJXIM", res.Issuer) - assert.Equal(t, "10000000000000.0000000", res.Amount) - assert.Equal(t, int32(429), res.NumAccounts) assert.Equal( t, horizon.AccountFlags{ diff --git a/services/regulated-assets-approval-server/internal/configureissuer/configureissuer_test.go b/services/regulated-assets-approval-server/internal/configureissuer/configureissuer_test.go index f26afdae11..a24afb9318 100644 --- a/services/regulated-assets-approval-server/internal/configureissuer/configureissuer_test.go +++ b/services/regulated-assets-approval-server/internal/configureissuer/configureissuer_test.go @@ -45,6 +45,9 @@ func TestSetup_accountAlreadyConfigured(t *testing.T) { HomeDomain: "domain.test.com", Sequence: 10, }, nil) + + dummyAsset := horizon.AssetStat{} + dummyAsset.Asset.Code = "FOO" horizonMock. On("Assets", horizonclient.AssetRequest{ ForAssetCode: opts.AssetCode, @@ -53,9 +56,7 @@ func TestSetup_accountAlreadyConfigured(t *testing.T) { }). Return(horizon.AssetsPage{ Embedded: struct{ Records []horizon.AssetStat }{ - Records: []horizon.AssetStat{ - {Amount: "0.0000001"}, - }, + Records: []horizon.AssetStat{dummyAsset}, }, }, nil) diff --git a/services/ticker/internal/scraper/asset_scraper.go b/services/ticker/internal/scraper/asset_scraper.go index ca7899a25d..c4c6d009cc 100644 --- a/services/ticker/internal/scraper/asset_scraper.go +++ b/services/ticker/internal/scraper/asset_scraper.go @@ -23,15 +23,17 @@ import ( // shouldDiscardAsset maps the criteria for discarding an asset from the asset index func shouldDiscardAsset(asset hProtocol.AssetStat, shouldValidateTOML bool) bool { - if asset.Amount == "" { + amt, _ := AddStringsToSum(asset.Balances.AuthorizedToMaintainLiabilities + asset.Balances.Unauthorized + asset.Balances.Authorized) + amount := strconv.FormatFloat(amt, 'f', -1, 64) + + if amount == "" { return true } - f, _ := strconv.ParseFloat(asset.Amount, 64) - if f == 0.0 { + if amt == 0.0 { return true } // [StellarX Ticker]: assets need at least some adoption to show up - if asset.NumAccounts < 10 { + if asset.Accounts.Unauthorized+asset.Accounts.Authorized+asset.Accounts.AuthorizedToMaintainLiabilities < 10 { return true } if asset.Code == "REMOVE" { @@ -39,7 +41,7 @@ func shouldDiscardAsset(asset hProtocol.AssetStat, shouldValidateTOML bool) bool } // [StellarX Ticker]: assets with at least 100 accounts get a pass, // even with toml issues - if asset.NumAccounts >= 100 { + if asset.Accounts.Unauthorized+asset.Accounts.Authorized+asset.Accounts.AuthorizedToMaintainLiabilities >= 100 { return false } @@ -143,6 +145,20 @@ func isDomainVerified(orgURL string, tomlURL string, hasCurrency bool) bool { } return true } +func AddStringsToSum(values ...string) (float64, error) { + var sum float64 + for _, s := range values { + if s == "" { + continue // Treat empty string as zero + } + value, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0, fmt.Errorf("failed to convert '%s': %w", s, err) + } + sum += value + } + return sum, nil +} // makeTomlAsset aggregates Horizon Data with TOML Data func makeFinalAsset( @@ -150,7 +166,7 @@ func makeFinalAsset( issuer TOMLIssuer, errors []error, ) (t FinalAsset, err error) { - amount, err := strconv.ParseFloat(asset.Amount, 64) + amount, err := AddStringsToSum(asset.Balances.Authorized + asset.Balances.Unauthorized + asset.Balances.Unauthorized) if err != nil { return } @@ -159,7 +175,7 @@ func makeFinalAsset( Type: asset.Type, Code: asset.Code, Issuer: asset.Issuer, - NumAccounts: asset.NumAccounts, + NumAccounts: asset.Accounts.Authorized + asset.Accounts.Unauthorized + asset.Accounts.AuthorizedToMaintainLiabilities, AuthRequired: asset.Flags.AuthRequired, AuthRevocable: asset.Flags.AuthRevocable, Amount: amount, diff --git a/services/ticker/internal/scraper/asset_scraper_test.go b/services/ticker/internal/scraper/asset_scraper_test.go index 39f3e0a2ee..96918c6c83 100644 --- a/services/ticker/internal/scraper/asset_scraper_test.go +++ b/services/ticker/internal/scraper/asset_scraper_test.go @@ -14,69 +14,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestShouldDiscardAsset(t *testing.T) { - testAsset := hProtocol.AssetStat{ - Amount: "", - } - - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "0.0", - } - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "0", - } - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 8, - } - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 12, - } - testAsset.Code = "REMOVE" - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 100, - } - testAsset.Code = "SOMETHINGVALID" - testAsset.Links.Toml.Href = "" - assert.Equal(t, shouldDiscardAsset(testAsset, true), false) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 40, - } - testAsset.Code = "SOMETHINGVALID" - testAsset.Links.Toml.Href = "http://www.stellar.org/.well-known/stellar.toml" - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 40, - } - testAsset.Code = "SOMETHINGVALID" - testAsset.Links.Toml.Href = "" - assert.Equal(t, shouldDiscardAsset(testAsset, true), true) - - testAsset = hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 40, - } - testAsset.Code = "SOMETHINGVALID" - testAsset.Links.Toml.Href = "https://www.stellar.org/.well-known/stellar.toml" - assert.Equal(t, shouldDiscardAsset(testAsset, true), false) -} - func TestDomainsMatch(t *testing.T) { tomlURL, _ := url.Parse("https://stellar.org/stellar.toml") orgURL, _ := url.Parse("https://stellar.org/") @@ -154,10 +91,7 @@ func TestProcessAsset_notCached(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `SIGNING_KEY="not cached signing key"`) })) - asset := hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 100, - } + asset := hProtocol.AssetStat{} asset.Code = "SOMETHINGVALID" asset.Links.Toml.Href = server.URL tomlCache := &TOMLCache{} @@ -172,10 +106,7 @@ func TestProcessAsset_notCached(t *testing.T) { func TestProcessAsset_cached(t *testing.T) { logger := log.DefaultLogger - asset := hProtocol.AssetStat{ - Amount: "123901.0129310", - NumAccounts: 100, - } + asset := hProtocol.AssetStat{} asset.Code = "SOMETHINGVALID" asset.Links.Toml.Href = "url" tomlCache := &TOMLCache{}