2022-04-21 20:16:28 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-04-21 20:16:28 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2023-04-04 00:08:45 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-04-21 20:16:28 +00:00
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/evmutil/types"
|
2022-04-21 20:16:28 +00:00
|
|
|
)
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// ConversionMultiplier is the conversion multiplier between base denom and auxiliary denom
|
|
|
|
var ConversionMultiplier = sdkmath.NewInt(chaincfg.AuxiliaryDenomConversionMultiplier)
|
2022-04-21 20:16:28 +00:00
|
|
|
|
|
|
|
var _ evmtypes.BankKeeper = EvmBankKeeper{}
|
|
|
|
|
|
|
|
// EvmBankKeeper is a BankKeeper wrapper for the x/evm module to allow the use
|
2024-05-09 11:35:16 +00:00
|
|
|
// of the 18 decimal base denom coin on the evm.
|
|
|
|
// x/evm consumes gas and send coins by minting and burning base denom coins in its module
|
2022-04-21 20:16:28 +00:00
|
|
|
// account and then sending the funds to the target account.
|
2024-05-09 11:35:16 +00:00
|
|
|
// This keeper uses both the auxiliary denom coin and a separate base denom balance to manage the
|
2022-04-21 20:16:28 +00:00
|
|
|
// extra percision needed by the evm.
|
|
|
|
type EvmBankKeeper struct {
|
2024-05-09 11:35:16 +00:00
|
|
|
baseDenomKeeper Keeper
|
|
|
|
bk types.BankKeeper
|
|
|
|
ak types.AccountKeeper
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
func NewEvmBankKeeper(baseKeeper Keeper, bk types.BankKeeper, ak types.AccountKeeper) EvmBankKeeper {
|
2022-04-21 20:16:28 +00:00
|
|
|
return EvmBankKeeper{
|
2024-05-09 11:35:16 +00:00
|
|
|
baseDenomKeeper: baseKeeper,
|
|
|
|
bk: bk,
|
|
|
|
ak: ak,
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// GetBalance returns the total **spendable** balance of base denom for a given account by address.
|
2022-04-21 20:16:28 +00:00
|
|
|
func (k EvmBankKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
|
2024-05-09 11:35:16 +00:00
|
|
|
if denom != chaincfg.BaseDenom {
|
|
|
|
panic(fmt.Errorf("only evm denom %s is supported by EvmBankKeeper", chaincfg.BaseDenom))
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spendableCoins := k.bk.SpendableCoins(ctx, addr)
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomFromBank := spendableCoins.AmountOf(chaincfg.AuxiliaryDenom)
|
|
|
|
baseDenomFromBank := spendableCoins.AmountOf(chaincfg.BaseDenom)
|
|
|
|
baseDenomFromEvmBank := k.baseDenomKeeper.GetBalance(ctx, addr)
|
2024-05-05 06:17:37 +00:00
|
|
|
|
|
|
|
var total sdkmath.Int
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if auxiliaryDenomFromBank.IsPositive() {
|
|
|
|
total = auxiliaryDenomFromBank.Mul(ConversionMultiplier).Add(baseDenomFromBank).Add(baseDenomFromEvmBank)
|
2024-05-05 06:17:37 +00:00
|
|
|
} else {
|
2024-05-09 11:35:16 +00:00
|
|
|
total = baseDenomFromBank.Add(baseDenomFromEvmBank)
|
2024-05-05 06:17:37 +00:00
|
|
|
}
|
2024-05-09 11:35:16 +00:00
|
|
|
return sdk.NewCoin(chaincfg.BaseDenom, total)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// SendCoins transfers base denom coins from a AccAddress to an AccAddress.
|
2023-04-04 00:08:45 +00:00
|
|
|
func (k EvmBankKeeper) SendCoins(ctx sdk.Context, senderAddr sdk.AccAddress, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
|
|
|
|
// SendCoins method is not used by the evm module, but is required by the
|
|
|
|
// evmtypes.BankKeeper interface. This must be updated if the evm module
|
|
|
|
// is updated to use SendCoins.
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// SendCoinsFromModuleToAccount transfers base denom coins from a ModuleAccount to an AccAddress.
|
2022-04-21 20:16:28 +00:00
|
|
|
// It will panic if the module account does not exist. An error is returned if the recipient
|
|
|
|
// address is black-listed or if sending the tokens fails.
|
|
|
|
func (k EvmBankKeeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if auxiliaryDenomCoin.Amount.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
senderAddr := k.GetModuleAddress(senderModule)
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, senderAddr, baseDemonCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.baseDenomKeeper.SendBalance(ctx, senderAddr, recipientAddr, baseDemonCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// SendCoinsFromAccountToModule transfers base denom coins from an AccAddress to a ModuleAccount.
|
2022-04-21 20:16:28 +00:00
|
|
|
// It will panic if the module account does not exist.
|
|
|
|
func (k EvmBankKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomCoin, baseDenomCnt, err := SplitBaseDenomCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if auxiliaryDenomCoin.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, senderAddr, baseDenomCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(recipientModule)
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.baseDenomKeeper.SendBalance(ctx, senderAddr, recipientAddr, baseDenomCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// MintCoins mints base denom coins by minting the equivalent auxiliary denom coins and any remaining base denom coins.
|
2022-04-21 20:16:28 +00:00
|
|
|
// It will panic if the module account does not exist or is unauthorized.
|
|
|
|
func (k EvmBankKeeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if auxiliaryDenomCoin.IsPositive() {
|
|
|
|
if err := k.bk.MintCoins(ctx, moduleName, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(moduleName)
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.baseDenomKeeper.AddBalance(ctx, recipientAddr, baseDemonCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// BurnCoins burns base denom coins by burning the equivalent auxiliary denom coins and any remaining base denom coins.
|
2022-04-21 20:16:28 +00:00
|
|
|
// It will panic if the module account does not exist or is unauthorized.
|
|
|
|
func (k EvmBankKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
if auxiliaryDenomCoin.IsPositive() {
|
|
|
|
if err := k.bk.BurnCoins(ctx, moduleName, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleAddr := k.GetModuleAddress(moduleName)
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, moduleAddr, baseDemonCnt); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
return k.baseDenomKeeper.RemoveBalance(ctx, moduleAddr, baseDemonCnt)
|
2024-02-06 22:54:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// ConvertOneauxiliaryDenomToBaseDenomIfNeeded converts 1 auxiliary denom to base denom for an address if
|
|
|
|
// its base denom balance is smaller than the baseDenomCnt amount.
|
|
|
|
func (k EvmBankKeeper) ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx sdk.Context, addr sdk.AccAddress, baseDenomCnt sdkmath.Int) error {
|
|
|
|
baseDenomBal := k.baseDenomKeeper.GetBalance(ctx, addr)
|
|
|
|
if baseDenomBal.GTE(baseDenomCnt) {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomToStore := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdk.OneInt()))
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, auxiliaryDenomToStore); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// add 1 auxiliary denom equivalent of base denom to addr
|
|
|
|
baseDenomToReceive := ConversionMultiplier
|
|
|
|
if err := k.baseDenomKeeper.AddBalance(ctx, addr, baseDenomToReceive); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// ConvertBaseDenomToauxiliaryDenom converts all available base denom to auxiliary denom for a given AccAddress.
|
|
|
|
func (k EvmBankKeeper) ConvertBaseDenomToAuxiliaryDenom(ctx sdk.Context, addr sdk.AccAddress) error {
|
|
|
|
totalBaseDenom := k.baseDenomKeeper.GetBalance(ctx, addr)
|
|
|
|
auxiliaryDenomCoin, _, err := SplitBaseDenomCoins(sdk.NewCoins(sdk.NewCoin(chaincfg.BaseDenom, totalBaseDenom)))
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// do nothing if account does not have enough base denom for a single auxiliary denom
|
|
|
|
auxiliaryDenomToReceive := auxiliaryDenomCoin.Amount
|
|
|
|
if !auxiliaryDenomToReceive.IsPositive() {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// remove base denom used for converting to auxiliary denom
|
|
|
|
baseDenomToBurn := auxiliaryDenomToReceive.Mul(ConversionMultiplier)
|
|
|
|
finalBal := totalBaseDenom.Sub(baseDenomToBurn)
|
|
|
|
if err := k.baseDenomKeeper.SetBalance(ctx, addr, finalBal); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr := k.GetModuleAddress(types.ModuleName)
|
2024-05-09 11:35:16 +00:00
|
|
|
if err := k.bk.SendCoins(ctx, fromAddr, addr, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k EvmBankKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
|
|
|
|
addr := k.ak.GetModuleAddress(moduleName)
|
|
|
|
if addr == nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
panic(errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName))
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// SplitBaseDenomCoins splits base denom coins to the equivalent auxiliary denom coins and any remaining base denom balance.
|
|
|
|
// An error will be returned if the coins are not valid or if the coins are not the base denom.
|
|
|
|
func SplitBaseDenomCoins(coins sdk.Coins) (sdk.Coin, sdkmath.Int, error) {
|
|
|
|
baseDemonCnt := sdk.ZeroInt()
|
|
|
|
auxiliaryDenomAmt := sdk.NewCoin(chaincfg.AuxiliaryDenom, sdk.ZeroInt())
|
2022-04-21 20:16:28 +00:00
|
|
|
|
|
|
|
if len(coins) == 0 {
|
2024-05-09 11:35:16 +00:00
|
|
|
return auxiliaryDenomAmt, baseDemonCnt, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateEvmCoins(coins); err != nil {
|
2024-05-09 11:35:16 +00:00
|
|
|
return auxiliaryDenomAmt, baseDemonCnt, err
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// note: we should always have len(coins) == 1 here since coins cannot have dup denoms after we validate.
|
|
|
|
coin := coins[0]
|
|
|
|
remainingBalance := coin.Amount.Mod(ConversionMultiplier)
|
|
|
|
if remainingBalance.IsPositive() {
|
2024-05-09 11:35:16 +00:00
|
|
|
baseDemonCnt = remainingBalance
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
2024-05-09 11:35:16 +00:00
|
|
|
auxiliaryDenomAmount := coin.Amount.Quo(ConversionMultiplier)
|
|
|
|
if auxiliaryDenomAmount.IsPositive() {
|
|
|
|
auxiliaryDenomAmt = sdk.NewCoin(chaincfg.AuxiliaryDenom, auxiliaryDenomAmount)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
return auxiliaryDenomAmt, baseDemonCnt, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// ValidateEvmCoins validates the coins from evm is valid and is the base denom.
|
2022-04-21 20:16:28 +00:00
|
|
|
func ValidateEvmCoins(coins sdk.Coins) error {
|
|
|
|
if len(coins) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate that coins are non-negative, sorted, and no dup denoms
|
|
|
|
if err := coins.Validate(); err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coins.String())
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 11:35:16 +00:00
|
|
|
// validate that coin denom is base denom
|
|
|
|
if len(coins) != 1 || coins[0].Denom != chaincfg.BaseDenom {
|
|
|
|
errMsg := fmt.Sprintf("invalid evm coin denom, only %s is supported", chaincfg.BaseDenom)
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, errMsg)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|