Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use SpendableCoin instead of full balance when querying gas token #64

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading