Skip to content

Commit

Permalink
initialize genesis denom with 18 decimals by default (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Aug 21, 2024
1 parent 88ad9b7 commit 4adcd17
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
7 changes: 6 additions & 1 deletion x/evm/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"context"
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -141,6 +142,7 @@ func (f *TestFaucet) NewFundedAccount(ctx sdk.Context, amounts ...sdk.Coin) sdk.
}

type TestKeepers struct {
Decimals uint8
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CommunityPoolKeeper *MockCommunityPoolKeeper
Expand Down Expand Up @@ -270,16 +272,19 @@ func _createTestInput(
*erc20Keeper = *evmKeeper.ERC20Keeper().(*evmkeeper.ERC20Keeper)
faucet := NewTestFaucet(t, ctx, bankKeeper, authtypes.Minter)

decimals := uint8(evmtypes.EtherDecimals)
if withInitialize {
decimals = uint8(rand.Intn(int(evmtypes.EtherDecimals) + 1))
evmParams := evmtypes.DefaultParams()
evmParams.AllowCustomERC20 = false
require.NoError(t, evmKeeper.Params.Set(ctx, evmParams))
require.NoError(t, evmKeeper.Initialize(ctx))
require.NoError(t, evmKeeper.InitializeWithDecimals(ctx, decimals))

faucet.NewFundedAccount(ctx, sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1_000_000)))
}

keepers := TestKeepers{
Decimals: decimals,
AccountKeeper: accountKeeper,
CommunityPoolKeeper: communityPoolKeeper,
EVMKeeper: *evmKeeper,
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (k ERC20Keeper) MintCoins(ctx context.Context, addr sdk.AccAddress, amount
if found, err := k.ERC20ContractAddrsByDenom.Has(ctx, denom); err != nil {
return err
} else if !found {
err := k.CreateERC20(ctx, denom)
err := k.CreateERC20(ctx, denom, 0)
if err != nil {
return err
}
Expand All @@ -324,7 +324,7 @@ func (k ERC20Keeper) MintCoins(ctx context.Context, addr sdk.AccAddress, amount
return nil
}

func (k ERC20Keeper) CreateERC20(ctx context.Context, denom string) error {
func (k ERC20Keeper) CreateERC20(ctx context.Context, denom string, decimals uint8) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
factoryAddr, err := k.GetERC20FactoryAddr(ctx)
if err != nil {
Expand All @@ -343,7 +343,7 @@ func (k ERC20Keeper) CreateERC20(ctx context.Context, denom string) error {
return err
}

inputBz, err := k.ERC20FactoryABI.Pack("createERC20", denom, denom, uint8(0))
inputBz, err := k.ERC20FactoryABI.Pack("createERC20", denom, denom, uint8(decimals))
if err != nil {
return types.ErrFailedToPackABI.Wrap(err.Error())
}
Expand Down
10 changes: 9 additions & 1 deletion x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import (
"github.com/initia-labs/minievm/x/evm/types"
)

// Initialize initializes the EVM state at genesis
// 1. deploy and store erc20 factory contract
// 2. deploy fee denom erc20 coins at genesis bootstrapping with 18 decimals
func (k Keeper) Initialize(ctx context.Context) error {
return k.InitializeWithDecimals(ctx, types.EtherDecimals)
}

// InitializeWithDecimals initializes the EVM state at genesis with the given decimals
func (k Keeper) InitializeWithDecimals(ctx context.Context, decimals uint8) error {
// 1. deploy and store erc20 factory contract
code, err := hexutil.Decode(erc20_factory.Erc20FactoryBin)
if err != nil {
Expand All @@ -37,7 +45,7 @@ func (k Keeper) Initialize(ctx context.Context) error {
}

// 2. deploy fee denom erc20 coins at genesis bootstrapping
err = k.erc20Keeper.CreateERC20(ctx, params.FeeDenom)
err = k.erc20Keeper.CreateERC20(ctx, params.FeeDenom, decimals)
if err != nil {
return err
}
Expand Down
30 changes: 19 additions & 11 deletions x/evm/keeper/txutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func Test_DynamicFeeTxConversion(t *testing.T) {
_, _, addr := keyPubAddr()
input.Faucet.Mint(ctx, addr, sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1000000000000000000)))

feeAmount := int64(150000)
gasLimit := uint64(1000000)
decimals := input.Decimals
gasLimit := uint64(1_000_000)
feeAmount := new(big.Int).Mul(
big.NewInt(int64(gasLimit)),
new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)-8), nil), // gas price is 1e-8
)

ethFactoryAddr, err := input.EVMKeeper.GetERC20FactoryAddr(ctx)
require.NoError(t, err)
Expand All @@ -41,9 +45,9 @@ func Test_DynamicFeeTxConversion(t *testing.T) {
inputBz, err := abi.Pack("createERC20", "bar", "bar", uint8(6))
require.NoError(t, err)

gasFeeCap := types.ToEthersUint(0, big.NewInt(feeAmount))
gasFeeCap := types.ToEthersUint(decimals, feeAmount)
gasFeeCap = gasFeeCap.Quo(gasFeeCap, new(big.Int).SetUint64(gasLimit))
value := types.ToEthersUint(0, big.NewInt(100))
value := types.ToEthersUint(decimals, big.NewInt(100))

ethChainID := types.ConvertCosmosChainIDToEthereumChainID(ctx.ChainID())
ethTx := coretypes.NewTx(&coretypes.DynamicFeeTx{
Expand Down Expand Up @@ -90,7 +94,7 @@ func Test_DynamicFeeTxConversion(t *testing.T) {
})

authTx := sdkTx.(authsigning.Tx)
require.Equal(t, authTx.GetFee(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(feeAmount+1))))
require.Equal(t, authTx.GetFee(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewIntFromBigInt(feeAmount).AddRaw(1))))

sigs, err := authTx.GetSignaturesV2()
require.NoError(t, err)
Expand All @@ -117,8 +121,13 @@ func Test_LegacyTxConversion(t *testing.T) {
_, _, addr := keyPubAddr()
input.Faucet.Mint(ctx, addr, sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1000000000000000000)))

feeAmount := int64(150000)
gasLimit := uint64(1000000)
decimals := input.Decimals
gasLimit := uint64(1_000_000)
feeAmount := new(big.Int).Mul(
big.NewInt(int64(gasLimit)),
new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)-8), nil), // gas price is 1e-8
)

ethFactoryAddr, err := input.EVMKeeper.GetERC20FactoryAddr(ctx)
require.NoError(t, err)

Expand All @@ -128,9 +137,9 @@ func Test_LegacyTxConversion(t *testing.T) {
inputBz, err := abi.Pack("createERC20", "bar", "bar", uint8(6))
require.NoError(t, err)

gasFeeCap := types.ToEthersUint(0, big.NewInt(feeAmount))
gasFeeCap := types.ToEthersUint(decimals, feeAmount)
gasFeeCap = gasFeeCap.Quo(gasFeeCap, new(big.Int).SetUint64(gasLimit))
value := types.ToEthersUint(0, big.NewInt(100))
value := types.ToEthersUint(decimals, big.NewInt(100))

ethChainID := types.ConvertCosmosChainIDToEthereumChainID(ctx.ChainID())
ethTx := coretypes.NewTx(&coretypes.LegacyTx{
Expand Down Expand Up @@ -175,7 +184,7 @@ func Test_LegacyTxConversion(t *testing.T) {
})

authTx := sdkTx.(authsigning.Tx)
require.Equal(t, authTx.GetFee(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(feeAmount+1))))
require.Equal(t, authTx.GetFee(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewIntFromBigInt(feeAmount).AddRaw(1))))

sigs, err := authTx.GetSignaturesV2()
require.NoError(t, err)
Expand All @@ -200,7 +209,6 @@ func Test_LegacyTxConversion(t *testing.T) {
ethTx2, _, err := keeper.NewTxUtils(&input.EVMKeeper).ConvertCosmosTxToEthereumTx(ctx, sdkTx)
require.NoError(t, err)
EqualEthTransaction(t, signedTx, ethTx2)

}

func EqualEthTransaction(t *testing.T, expected, actual *coretypes.Transaction) {
Expand Down
2 changes: 1 addition & 1 deletion x/evm/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type IERC20Keeper interface {
GetDecimals(ctx context.Context, denom string) (uint8, error)

// contract creation
CreateERC20(ctx context.Context, denom string) error
CreateERC20(ctx context.Context, denom string, decimals uint8) error
}

type IERC721Keeper interface {
Expand Down

0 comments on commit 4adcd17

Please sign in to comment.