Skip to content

Commit

Permalink
clean up all occurences of Amount and NumAccounts (#5438)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikiyer56 committed Sep 27, 2024
1 parent 43cdce9 commit 811ae43
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 105 deletions.
2 changes: 0 additions & 2 deletions clients/horizonclient/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 0 additions & 10 deletions services/horizon/internal/actions/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions services/horizon/internal/integration/liquidity_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions services/horizon/internal/integration/sac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 0 additions & 2 deletions services/horizon/internal/resourceadapter/asset_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down
4 changes: 0 additions & 4 deletions services/horizon/internal/resourceadapter/asset_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
30 changes: 23 additions & 7 deletions services/ticker/internal/scraper/asset_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ 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" {
return true
}
// [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
}

Expand Down Expand Up @@ -143,14 +145,28 @@ 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(
asset hProtocol.AssetStat,
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
}
Expand All @@ -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,
Expand Down
73 changes: 2 additions & 71 deletions services/ticker/internal/scraper/asset_scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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{}
Expand All @@ -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{}
Expand Down

0 comments on commit 811ae43

Please sign in to comment.