Skip to content

Commit

Permalink
deploy fee denom erc20 contract at genesis initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jul 15, 2024
1 parent edf9bc1 commit fc241d6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
67 changes: 38 additions & 29 deletions x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ func (k ERC20Keeper) IterateSupply(ctx context.Context, cb func(supply sdk.Coin)

// MintCoins implements IERC20Keeper.
func (k ERC20Keeper) MintCoins(ctx context.Context, addr sdk.AccAddress, amount sdk.Coins) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
evmAddr, err := k.convertToEVMAddress(ctx, addr, false)
if err != nil {
return err
Expand All @@ -294,37 +293,10 @@ 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 {
contractAddr, err := k.nextContractAddress(ctx, types.ERC20FactoryAddress())
err := k.CreateERC20(ctx, denom)
if err != nil {
return err
}

if err := k.ERC20DenomsByContractAddr.Set(ctx, contractAddr.Bytes(), denom); err != nil {
return err
}

if err := k.ERC20ContractAddrsByDenom.Set(ctx, denom, contractAddr.Bytes()); err != nil {
return err
}

inputBz, err := k.ERC20FactoryABI.Pack("createERC20", denom, denom, uint8(0))
if err != nil {
return types.ErrFailedToPackABI.Wrap(err.Error())
}

ret, _, err := k.EVMCall(ctx, types.StdAddress, types.ERC20FactoryAddress(), inputBz, nil)
if err != nil {
return err
}

// emit erc20 created event
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeERC20Created,
sdk.NewAttribute(types.AttributeKeyDenom, denom),
sdk.NewAttribute(types.AttributeKeyContract, hexutil.Encode(ret[12:])),
),
)
}

// mint coin
Expand All @@ -347,6 +319,43 @@ func (k ERC20Keeper) MintCoins(ctx context.Context, addr sdk.AccAddress, amount
return nil
}

func (k ERC20Keeper) CreateERC20(ctx context.Context, denom string) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
contractAddr, err := k.nextContractAddress(ctx, types.ERC20FactoryAddress())
if err != nil {
return err
}

if err := k.ERC20DenomsByContractAddr.Set(ctx, contractAddr.Bytes(), denom); err != nil {
return err
}

if err := k.ERC20ContractAddrsByDenom.Set(ctx, denom, contractAddr.Bytes()); err != nil {
return err
}

inputBz, err := k.ERC20FactoryABI.Pack("createERC20", denom, denom, uint8(0))
if err != nil {
return types.ErrFailedToPackABI.Wrap(err.Error())
}

ret, _, err := k.EVMCall(ctx, types.StdAddress, types.ERC20FactoryAddress(), inputBz, nil)
if err != nil {
return err
}

// emit erc20 created event
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeERC20Created,
sdk.NewAttribute(types.AttributeKeyDenom, denom),
sdk.NewAttribute(types.AttributeKeyContract, hexutil.Encode(ret[12:])),
),
)

return nil
}

// SendCoins implements IERC20Keeper.
func (k ERC20Keeper) SendCoins(ctx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
evmFromAddr, err := k.convertToEVMAddress(ctx, fromAddr, true)
Expand Down
18 changes: 18 additions & 0 deletions x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,30 @@ func (k Keeper) Initialize(ctx context.Context) error {
return err
}

// This is temporal alive flag to prevent checking fee denom erc20 coin existence
// during this function execution. It will be reverted to false after the erc20
// fee denom contract is deployed.
//
//
// This function is not pointer receiver, so it will not affect the original value.
k.initializing = true
_, _, _, err = k.EVMCreate2(ctx, types.StdAddress, code, nil, types.ERC20FactorySalt)
if err != nil {
return err
}

params, err := k.Params.Get(ctx)
if err != nil {
return err
}

// deploy fee denom erc20 coins at genesis bootstrapping
err = k.erc20Keeper.CreateERC20(ctx, params.FeeDenom)
if err != nil {
return err
}

// k.initialize will be reverted to false.
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions x/evm/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type IERC20Keeper interface {

// erc20 queries
GetDecimals(ctx context.Context, denom string) (uint8, error)

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

type IERC721Keeper interface {
Expand Down

0 comments on commit fc241d6

Please sign in to comment.