From b8c1d76a9a0426095469d0ecc16e28296f6ca879 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Wed, 7 Sep 2022 15:53:55 +0100 Subject: [PATCH] Add query for the total supply of a coin (#903) * add bank supply query * remove a local dev dependency --- x/wasm/keeper/query_plugins.go | 10 ++++++++++ x/wasm/keeper/query_plugins_test.go | 8 ++++++++ x/wasm/types/expected_keepers.go | 1 + 3 files changed, 19 insertions(+) diff --git a/x/wasm/keeper/query_plugins.go b/x/wasm/keeper/query_plugins.go index b0e095b12c..371242d00a 100644 --- a/x/wasm/keeper/query_plugins.go +++ b/x/wasm/keeper/query_plugins.go @@ -189,6 +189,16 @@ func BankQuerier(bankKeeper types.BankViewKeeper) func(ctx sdk.Context, request } return json.Marshal(res) } + if request.Supply != nil { + coin := bankKeeper.GetSupply(ctx, request.Supply.Denom) + res := wasmvmtypes.SupplyResponse{ + Amount: wasmvmtypes.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + }, + } + return json.Marshal(res) + } return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown BankQuery variant"} } } diff --git a/x/wasm/keeper/query_plugins_test.go b/x/wasm/keeper/query_plugins_test.go index c58932d67e..f9282ac152 100644 --- a/x/wasm/keeper/query_plugins_test.go +++ b/x/wasm/keeper/query_plugins_test.go @@ -511,10 +511,18 @@ func (m mockWasmQueryKeeper) IsPinnedCode(ctx sdk.Context, codeID uint64) bool { } type bankKeeperMock struct { + GetSupplyFn func(ctx sdk.Context, denom string) sdk.Coin GetBalanceFn func(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin GetAllBalancesFn func(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } +func (m bankKeeperMock) GetSupply(ctx sdk.Context, denom string) sdk.Coin { + if m.GetSupplyFn == nil { + panic("not expected to be called") + } + return m.GetSupplyFn(ctx, denom) +} + func (m bankKeeperMock) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin { if m.GetBalanceFn == nil { panic("not expected to be called") diff --git a/x/wasm/types/expected_keepers.go b/x/wasm/types/expected_keepers.go index 0cdd4d80e0..da00047aa4 100644 --- a/x/wasm/types/expected_keepers.go +++ b/x/wasm/types/expected_keepers.go @@ -17,6 +17,7 @@ import ( type BankViewKeeper interface { GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin + GetSupply(ctx sdk.Context, denom string) sdk.Coin } // Burner is a subset of the sdk bank keeper methods