Skip to content

Commit f874b87

Browse files
committed
query Balance
1 parent 289b1d7 commit f874b87

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

x/evm/keeper/grpc_query.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/NibiruChain/nibiru/x/common"
1010
"github.com/NibiruChain/nibiru/x/evm"
11+
sdk "github.com/cosmos/cosmos-sdk/types"
12+
gethcommon "github.com/ethereum/go-ethereum/common"
1113
)
1214

1315
// Compile-time interface assertion
@@ -88,10 +90,14 @@ func (k Keeper) ValidatorAccount(
8890
// - A pointer to the QueryBalanceResponse object containing the balance.
8991
// - An error if the balance retrieval process encounters any issues.
9092
func (k Keeper) Balance(goCtx context.Context, req *evm.QueryBalanceRequest) (*evm.QueryBalanceResponse, error) {
91-
// TODO: feat(evm): impl query Balance
93+
if err := req.Validate(); err != nil {
94+
return nil, err
95+
}
96+
ctx := sdk.UnwrapSDKContext(goCtx)
97+
balanceInt := k.GetEvmGasBalance(ctx, gethcommon.HexToAddress(req.Address))
9298
return &evm.QueryBalanceResponse{
93-
Balance: "",
94-
}, common.ErrNotImplementedGprc()
99+
Balance: balanceInt.String(),
100+
}, nil
95101
}
96102

97103
// BaseFee implements the Query/BaseFee gRPC method

x/evm/keeper/keeper.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ package keeper
33

44
import (
55
// "github.com/NibiruChain/nibiru/x/evm"
6+
"math/big"
7+
8+
"github.com/NibiruChain/nibiru/x/evm"
69
"github.com/cosmos/cosmos-sdk/codec"
710
storetypes "github.com/cosmos/cosmos-sdk/store/types"
811
sdk "github.com/cosmos/cosmos-sdk/types"
12+
gethcommon "github.com/ethereum/go-ethereum/common"
913
)
1014

1115
type Keeper struct {
@@ -20,6 +24,8 @@ type Keeper struct {
2024

2125
// the address capable of executing a MsgUpdateParams message. Typically, this should be the x/gov module account.
2226
authority sdk.AccAddress
27+
28+
bankKeeper evm.BankKeeper
2329
}
2430

2531
func NewKeeper(
@@ -38,3 +44,24 @@ func NewKeeper(
3844
EvmState: NewEvmState(cdc, storeKey, transientKey),
3945
}
4046
}
47+
48+
// GetEvmGasBalance: Implements `evm.EVMKeeper` from
49+
// "github.com/NibiruChain/nibiru/app/ante/evm": Load account's balance of gas
50+
// tokens for EVM execution
51+
func (k *Keeper) GetEvmGasBalance(ctx sdk.Context, addr gethcommon.Address) *big.Int {
52+
cosmosAddr := sdk.AccAddress(addr.Bytes())
53+
evmParams := k.GetParams(ctx)
54+
evmDenom := evmParams.GetEvmDenom()
55+
// if node is pruned, params is empty. Return invalid value
56+
if evmDenom == "" {
57+
return big.NewInt(-1)
58+
}
59+
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, evmDenom)
60+
return coin.Amount.BigInt()
61+
}
62+
63+
// GetParams returns the total set of evm parameters.
64+
func (k Keeper) GetParams(ctx sdk.Context) (params evm.Params) {
65+
params, _ = k.EvmState.ModuleParams.Get(ctx)
66+
return params
67+
}

0 commit comments

Comments
 (0)