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-01 05:53:58 +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-01 05:53:58 +00:00
|
|
|
// ConversionMultiplier is the conversion multiplier between neuron and a0gi
|
|
|
|
var ConversionMultiplier = sdkmath.NewInt(chaincfg.ConversionMultiplier)
|
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-01 05:53:58 +00:00
|
|
|
// of the 18 decimal neuron coin on the evm.
|
|
|
|
// x/evm consumes gas and send coins by minting and burning neuron coins in its module
|
2022-04-21 20:16:28 +00:00
|
|
|
// account and then sending the funds to the target account.
|
2024-05-01 05:53:58 +00:00
|
|
|
// This keeper uses both the a0gi coin and a separate neuron balance to manage the
|
2022-04-21 20:16:28 +00:00
|
|
|
// extra percision needed by the evm.
|
|
|
|
type EvmBankKeeper struct {
|
2024-05-01 05:53:58 +00:00
|
|
|
baseKeeper Keeper
|
2022-04-21 20:16:28 +00:00
|
|
|
bk types.BankKeeper
|
|
|
|
ak types.AccountKeeper
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +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-01 05:53:58 +00:00
|
|
|
baseKeeper: baseKeeper,
|
2022-04-21 20:16:28 +00:00
|
|
|
bk: bk,
|
|
|
|
ak: ak,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// GetBalance returns the total **spendable** balance of neuron 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-01 05:53:58 +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-01 05:53:58 +00:00
|
|
|
a0gi := spendableCoins.AmountOf(chaincfg.DisplayDenom)
|
|
|
|
neuron := k.baseKeeper.GetBalance(ctx, addr)
|
|
|
|
total := a0gi.Mul(ConversionMultiplier).Add(neuron)
|
|
|
|
return sdk.NewCoin(chaincfg.BaseDenom, total)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// SendCoins transfers neuron 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-01 05:53:58 +00:00
|
|
|
// SendCoinsFromModuleToAccount transfers neuron 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-01 05:53:58 +00:00
|
|
|
a0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if a0gi.Amount.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(a0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
senderAddr := k.GetModuleAddress(senderModule)
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.ConvertOneA0giToNeuronIfNeeded(ctx, senderAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.baseKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
return k.ConvertNeuronToA0gi(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// SendCoinsFromAccountToModule transfers neuron 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-01 05:53:58 +00:00
|
|
|
a0gi, neuronNeeded, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if a0gi.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, sdk.NewCoins(a0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.ConvertOneA0giToNeuronIfNeeded(ctx, senderAddr, neuronNeeded); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(recipientModule)
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.baseKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuronNeeded); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
return k.ConvertNeuronToA0gi(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// MintCoins mints neuron coins by minting the equivalent a0gi coins and any remaining neuron 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-01 05:53:58 +00:00
|
|
|
a0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if a0gi.IsPositive() {
|
|
|
|
if err := k.bk.MintCoins(ctx, moduleName, sdk.NewCoins(a0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(moduleName)
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.baseKeeper.AddBalance(ctx, recipientAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
return k.ConvertNeuronToA0gi(ctx, recipientAddr)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// BurnCoins burns neuron coins by burning the equivalent a0gi coins and any remaining neuron 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-01 05:53:58 +00:00
|
|
|
a0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
if a0gi.IsPositive() {
|
|
|
|
if err := k.bk.BurnCoins(ctx, moduleName, sdk.NewCoins(a0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleAddr := k.GetModuleAddress(moduleName)
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.ConvertOneA0giToNeuronIfNeeded(ctx, moduleAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
return k.baseKeeper.RemoveBalance(ctx, moduleAddr, neuron)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// ConvertOneA0giToNeuronIfNeeded converts 1 a0gi to neuron for an address if
|
|
|
|
// its neuron balance is smaller than the neuronNeeded amount.
|
|
|
|
func (k EvmBankKeeper) ConvertOneA0giToNeuronIfNeeded(ctx sdk.Context, addr sdk.AccAddress, neuronNeeded sdkmath.Int) error {
|
|
|
|
neuronBal := k.baseKeeper.GetBalance(ctx, addr)
|
|
|
|
if neuronBal.GTE(neuronNeeded) {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
a0giToStore := sdk.NewCoins(sdk.NewCoin(chaincfg.DisplayDenom, sdk.OneInt()))
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, a0giToStore); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// add 1a0gi equivalent of neuron to addr
|
|
|
|
neuronToReceive := ConversionMultiplier
|
|
|
|
if err := k.baseKeeper.AddBalance(ctx, addr, neuronToReceive); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// ConvertNeuronToA0gi converts all available neuron to a0gi for a given AccAddress.
|
|
|
|
func (k EvmBankKeeper) ConvertNeuronToA0gi(ctx sdk.Context, addr sdk.AccAddress) error {
|
|
|
|
totalNeuron := k.baseKeeper.GetBalance(ctx, addr)
|
|
|
|
a0gi, _, err := SplitNeuronCoins(sdk.NewCoins(sdk.NewCoin(chaincfg.BaseDenom, totalNeuron)))
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// do nothing if account does not have enough neuron for a single a0gi
|
|
|
|
a0giToReceive := a0gi.Amount
|
|
|
|
if !a0giToReceive.IsPositive() {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// remove neuron used for converting to a0gi
|
|
|
|
neuronToBurn := a0giToReceive.Mul(ConversionMultiplier)
|
|
|
|
finalBal := totalNeuron.Sub(neuronToBurn)
|
|
|
|
if err := k.baseKeeper.SetBalance(ctx, addr, finalBal); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr := k.GetModuleAddress(types.ModuleName)
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := k.bk.SendCoins(ctx, fromAddr, addr, sdk.NewCoins(a0gi)); 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-01 05:53:58 +00:00
|
|
|
// SplitNeuronCoins splits neuron coins to the equivalent a0gi coins and any remaining neuron balance.
|
|
|
|
// An error will be returned if the coins are not valid or if the coins are not the neuron denom.
|
|
|
|
func SplitNeuronCoins(coins sdk.Coins) (sdk.Coin, sdkmath.Int, error) {
|
|
|
|
neuron := sdk.ZeroInt()
|
|
|
|
a0gi := sdk.NewCoin(chaincfg.DisplayDenom, sdk.ZeroInt())
|
2022-04-21 20:16:28 +00:00
|
|
|
|
|
|
|
if len(coins) == 0 {
|
2024-05-01 05:53:58 +00:00
|
|
|
return a0gi, neuron, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateEvmCoins(coins); err != nil {
|
2024-05-01 05:53:58 +00:00
|
|
|
return a0gi, neuron, 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-01 05:53:58 +00:00
|
|
|
neuron = remainingBalance
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
2024-05-01 05:53:58 +00:00
|
|
|
a0giAmount := coin.Amount.Quo(ConversionMultiplier)
|
|
|
|
if a0giAmount.IsPositive() {
|
|
|
|
a0gi = sdk.NewCoin(chaincfg.DisplayDenom, a0giAmount)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
return a0gi, neuron, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// ValidateEvmCoins validates the coins from evm is valid and is the chaincfg.BaseDenom (neuron).
|
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-01 05:53:58 +00:00
|
|
|
// validate that coin denom is neuron
|
|
|
|
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
|
|
|
|
}
|