Skip to content

Commit

Permalink
Merge pull request #198 from e-money/refactor-circulating-supply-calc
Browse files Browse the repository at this point in the history
Refactor circulating supply calc
  • Loading branch information
haasted authored Sep 29, 2022
2 parents 8088a21 + 33cd69b commit 36003fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion x/queries/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type AccountKeeper interface {
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
GetSupply(ctx sdk.Context) exported.SupplyI
IterateAllBalances(ctx sdk.Context, cb func(sdk.AccAddress, sdk.Coin) bool)
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
}

type SlashingKeeper interface {
Expand Down
29 changes: 13 additions & 16 deletions x/queries/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestCirculating(t *testing.T) {

gotRsp, err := queryClient.Circulating(ctx, &types.QueryCirculatingRequest{})
require.NoError(t, err)
assert.Equal(t, mustParseCoins("154blx,3"+stakingDenom), gotRsp.Total)
assert.Equal(t, mustParseCoins("154blx,153"+stakingDenom), gotRsp.Total)
}

func TestMissedBlocks(t *testing.T) {
Expand Down Expand Up @@ -122,6 +122,18 @@ type bankKeeperMock struct {
vesting sdk.Coins
}

func (b bankKeeperMock) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
if bal, found := b.balances[addr.String()]; found {
for _, b := range bal {
if b.Denom == denom {
return b
}
}
}

return sdk.NewCoin(denom, sdk.ZeroInt())
}

func (b bankKeeperMock) SpendableCoins(_ sdk.Context, addr sdk.AccAddress) sdk.Coins {
return b.balances[addr.String()]
}
Expand All @@ -136,21 +148,6 @@ func (b bankKeeperMock) GetSupply(_ sdk.Context) exported.SupplyI {
return banktypes.NewSupply(supply)
}

func (b bankKeeperMock) IterateAllBalances(_ sdk.Context, cb func(sdk.AccAddress, sdk.Coin) bool) {
for address, balance := range b.balances {
addr, err := sdk.AccAddressFromBech32(address)
if err != nil {
panic(err)
}

for _, coin := range balance {
if cb(addr, coin) {
return
}
}
}
}

var (
_ AccountKeeper = &accountKeeperMock{}
_ BankKeeper = &bankKeeperMock{}
Expand Down
34 changes: 11 additions & 23 deletions x/queries/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,25 @@ func querySpendableBalance(ctx sdk.Context, k BankKeeper, path []string) (res []
}

func calculateCirculatingSupply(ctx sdk.Context, accK AccountKeeper, bk BankKeeper) (circSupply sdk.Coins) {
total := bk.GetSupply(ctx).GetTotal()

stakingAccounts := map[string]bool{
accK.GetModuleAccount(ctx, stakingtypes.NotBondedPoolName).GetAddress().String(): true,
accK.GetModuleAccount(ctx, stakingtypes.BondedPoolName).GetAddress().String(): true,
stakingAccounts := []sdk.AccAddress{
accK.GetModuleAccount(ctx, stakingtypes.NotBondedPoolName).GetAddress(),
accK.GetModuleAccount(ctx, stakingtypes.BondedPoolName).GetAddress(),
}

ngmbalance := sdk.ZeroInt()

bk.IterateAllBalances(ctx, func(address sdk.AccAddress, coin sdk.Coin) bool {
if coin.Denom != stakingDenom {
return false
}

if _, exists := stakingAccounts[address.String()]; exists {
return false
}

spendableCoins := bk.SpendableCoins(ctx, address)
ngmbalance = ngmbalance.Add(spendableCoins.AmountOf("ungm"))

return false
})
bondedAndUnbondingBalance := sdk.ZeroInt()
for _, acc := range stakingAccounts {
bal := bk.GetBalance(ctx, acc, "ungm")
bondedAndUnbondingBalance = bondedAndUnbondingBalance.Add(bal.Amount)
}

// Replace staking token balance with the one calculated above, which omits vesting and staked tokens.
total := bk.GetSupply(ctx).GetTotal()
// Replace staking token balance with the one calculated above, which omits staked tokens.
for i, c := range total {
if c.Denom != stakingDenom {
continue
}

total[i] = sdk.NewCoin(stakingDenom, ngmbalance)
total[i] = total[i].Sub(sdk.NewCoin("ungm", bondedAndUnbondingBalance))
break
}

Expand Down

0 comments on commit 36003fd

Please sign in to comment.