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 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/evmutil/types"
|
2022-04-21 20:16:28 +00:00
|
|
|
)
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
const (
|
|
|
|
// EvmDenom is the gas denom used by the evm
|
|
|
|
EvmDenom = "neuron"
|
|
|
|
|
|
|
|
// CosmosDenom is the gas denom used by the 0g-chain app
|
|
|
|
CosmosDenom = "ua0gi"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConversionMultiplier is the conversion multiplier between neuron and ua0gi
|
|
|
|
var ConversionMultiplier = sdkmath.NewInt(1_000_000_000_000)
|
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-04 06:26:54 +00:00
|
|
|
neuronKeeper Keeper
|
|
|
|
bk types.BankKeeper
|
|
|
|
ak types.AccountKeeper
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
func NewEvmBankKeeper(neuronKeeper Keeper, bk types.BankKeeper, ak types.AccountKeeper) EvmBankKeeper {
|
2022-04-21 20:16:28 +00:00
|
|
|
return EvmBankKeeper{
|
2024-05-04 06:26:54 +00:00
|
|
|
neuronKeeper: neuronKeeper,
|
|
|
|
bk: bk,
|
|
|
|
ak: ak,
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04 06:26:54 +00:00
|
|
|
if denom != EvmDenom {
|
|
|
|
panic(fmt.Errorf("only evm denom %s is supported by EvmBankKeeper", EvmDenom))
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spendableCoins := k.bk.SpendableCoins(ctx, addr)
|
2024-05-04 06:26:54 +00:00
|
|
|
ua0gi := spendableCoins.AmountOf(CosmosDenom)
|
|
|
|
neuron := k.neuronKeeper.GetBalance(ctx, addr)
|
|
|
|
total := ua0gi.Mul(ConversionMultiplier).Add(neuron)
|
|
|
|
return sdk.NewCoin(EvmDenom, 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-04 06:26:54 +00:00
|
|
|
ua0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if ua0gi.Amount.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(ua0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
senderAddr := k.GetModuleAddress(senderModule)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, senderAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.neuronKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
return k.ConvertNeuronToUa0gi(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-04 06:26:54 +00:00
|
|
|
ua0gi, neuronNeeded, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if ua0gi.IsPositive() {
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, sdk.NewCoins(ua0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, senderAddr, neuronNeeded); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(recipientModule)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.neuronKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuronNeeded); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
return k.ConvertNeuronToUa0gi(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-04 06:26:54 +00:00
|
|
|
ua0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if ua0gi.IsPositive() {
|
|
|
|
if err := k.bk.MintCoins(ctx, moduleName, sdk.NewCoins(ua0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipientAddr := k.GetModuleAddress(moduleName)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.neuronKeeper.AddBalance(ctx, recipientAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
return k.ConvertNeuronToUa0gi(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-04 06:26:54 +00:00
|
|
|
ua0gi, neuron, err := SplitNeuronCoins(amt)
|
2022-04-21 20:16:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
if ua0gi.IsPositive() {
|
|
|
|
if err := k.bk.BurnCoins(ctx, moduleName, sdk.NewCoins(ua0gi)); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleAddr := k.GetModuleAddress(moduleName)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, moduleAddr, neuron); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
return k.neuronKeeper.RemoveBalance(ctx, moduleAddr, neuron)
|
2024-02-06 22:54:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
// ConvertOneUa0giToNeuronIfNeeded converts 1 a0gi to neuron for an address if
|
2024-05-01 05:53:58 +00:00
|
|
|
// its neuron balance is smaller than the neuronNeeded amount.
|
2024-05-04 06:26:54 +00:00
|
|
|
func (k EvmBankKeeper) ConvertOneUa0giToNeuronIfNeeded(ctx sdk.Context, addr sdk.AccAddress, neuronNeeded sdkmath.Int) error {
|
|
|
|
neuronBal := k.neuronKeeper.GetBalance(ctx, addr)
|
2024-05-01 05:53:58 +00:00
|
|
|
if neuronBal.GTE(neuronNeeded) {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
ua0giToStore := sdk.NewCoins(sdk.NewCoin(CosmosDenom, sdk.OneInt()))
|
|
|
|
if err := k.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, ua0giToStore); 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
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.neuronKeeper.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.
|
2024-05-04 06:26:54 +00:00
|
|
|
func (k EvmBankKeeper) ConvertNeuronToUa0gi(ctx sdk.Context, addr sdk.AccAddress) error {
|
|
|
|
totalNeuron := k.neuronKeeper.GetBalance(ctx, addr)
|
|
|
|
ua0gi, _, err := SplitNeuronCoins(sdk.NewCoins(sdk.NewCoin(EvmDenom, 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
|
2024-05-04 06:26:54 +00:00
|
|
|
ua0giToReceive := ua0gi.Amount
|
|
|
|
if !ua0giToReceive.IsPositive() {
|
2022-04-21 20:16:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
// remove neuron used for converting to ua0gi
|
|
|
|
neuronToBurn := ua0giToReceive.Mul(ConversionMultiplier)
|
2024-05-01 05:53:58 +00:00
|
|
|
finalBal := totalNeuron.Sub(neuronToBurn)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.neuronKeeper.SetBalance(ctx, addr, finalBal); err != nil {
|
2022-04-21 20:16:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr := k.GetModuleAddress(types.ModuleName)
|
2024-05-04 06:26:54 +00:00
|
|
|
if err := k.bk.SendCoins(ctx, fromAddr, addr, sdk.NewCoins(ua0gi)); 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()
|
2024-05-04 06:26:54 +00:00
|
|
|
ua0gi := sdk.NewCoin(CosmosDenom, sdk.ZeroInt())
|
2022-04-21 20:16:28 +00:00
|
|
|
|
|
|
|
if len(coins) == 0 {
|
2024-05-04 06:26:54 +00:00
|
|
|
return ua0gi, neuron, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidateEvmCoins(coins); err != nil {
|
2024-05-04 06:26:54 +00:00
|
|
|
return ua0gi, 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-04 06:26:54 +00:00
|
|
|
ua0giAmount := coin.Amount.Quo(ConversionMultiplier)
|
|
|
|
if ua0giAmount.IsPositive() {
|
|
|
|
ua0gi = sdk.NewCoin(CosmosDenom, ua0giAmount)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
return ua0gi, neuron, nil
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 06:26:54 +00:00
|
|
|
// ValidateEvmCoins validates the coins from evm is valid and is the EvmDenom (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
|
2024-05-04 06:26:54 +00:00
|
|
|
if len(coins) != 1 || coins[0].Denom != EvmDenom {
|
|
|
|
errMsg := fmt.Sprintf("invalid evm coin denom, only %s is supported", EvmDenom)
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, errMsg)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|