Skip to content

Commit

Permalink
feat: Use SpendableCoin instead of full balance when querying and set…
Browse files Browse the repository at this point in the history
…ting gas token (#64)
  • Loading branch information
drklee3 authored Jul 3, 2024
1 parent badab86 commit 29f075c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ func (k *Keeper) GetBalance(ctx sdk.Context, addr common.Address) *big.Int {
if evmDenom == "" {
return big.NewInt(-1)
}
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, evmDenom)

// Only spendable coin is considered as balance from EVM perspective.
// Locked coins are not shown in the balance to prevent misleading
// insufficient balance errors for users.
coin := k.bankKeeper.SpendableCoin(ctx, cosmosAddr, evmDenom)
return coin.Amount.BigInt()
}

Expand Down
6 changes: 5 additions & 1 deletion x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ func (k *Keeper) SetBalance(ctx sdk.Context, addr common.Address, amount *big.In
cosmosAddr := sdk.AccAddress(addr.Bytes())

params := k.GetParams(ctx)
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, params.EvmDenom)

// Since spendable balances are fetched to get balances, when setting balances
// it should also compare with spendable coins as well to get the correct
// delta.
coin := k.bankKeeper.SpendableCoin(ctx, cosmosAddr, params.EvmDenom)
balance := coin.Amount.BigInt()
delta := new(big.Int).Sub(amount, balance)
switch delta.Sign() {
Expand Down
2 changes: 1 addition & 1 deletion x/evm/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type AccountKeeper interface {
// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
authtypes.BankKeeper
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SpendableCoin(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
Expand Down

0 comments on commit 29f075c

Please sign in to comment.