Skip to content

Commit

Permalink
Merge branch 'develop' into fix/query-system-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis authored Oct 12, 2023
2 parents bec2820 + 48f3a44 commit eb22930
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
11 changes: 8 additions & 3 deletions x/crosschain/keeper/evm_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ func (k Keeper) ProcessZRC20WithdrawalEvent(ctx sdk.Context, event *zrc20.ZRC20W
if err != nil {
return fmt.Errorf("cannot encode address %s: %s", event.To, err.Error())
}
gasLimit := foreignCoin.GasLimit
// gasLimit+uint64(event.Raw.Index) to genereate different cctx for multiple events in the same tx.

gasLimit, err := k.fungibleKeeper.QueryGasLimit(ctx, ethcommon.HexToAddress(foreignCoin.Zrc20ContractAddress))
if err != nil {
return fmt.Errorf("cannot query gas limit: %s", err.Error())
}

// gasLimit+uint64(event.Raw.Index) to generate different cctx for multiple events in the same tx.
msg := types.NewMsgVoteOnObservedInboundTx(
"",
emittingContract.Hex(),
Expand All @@ -142,7 +147,7 @@ func (k Keeper) ProcessZRC20WithdrawalEvent(ctx sdk.Context, event *zrc20.ZRC20W
"",
event.Raw.TxHash.String(),
event.Raw.BlockNumber,
gasLimit+uint64(event.Raw.Index),
gasLimit.Uint64()+uint64(event.Raw.Index),
foreignCoin.CoinType,
foreignCoin.Asset,
)
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/keeper/gas_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func setupGasCoin(
assetName,
symbol,
8,
nil,
)
require.NoError(t, err)
assertContractDeployment(t, evmk, ctx, addr)
Expand Down
5 changes: 5 additions & 0 deletions x/crosschain/keeper/msg_server_whitelist_erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func TestKeeper_WhitelistERC20(t *testing.T) {
require.True(t, found)
require.EqualValues(t, fmt.Sprintf("%s:%s", common.CmdWhitelistERC20, erc20Address), cctx.RelayedMessage)

// check gas limit is set
gasLimit, err := zk.FungibleKeeper.QueryGasLimit(ctx, ethcommon.HexToAddress(zrc20))
require.NoError(t, err)
require.Equal(t, uint64(100000), gasLimit.Uint64())

// Ensure that whitelist a new erc20 create a cctx with a different index
res, err = k.WhitelistERC20(ctx, &types.MsgWhitelistERC20{
Creator: admin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (k Keeper) BlockOneDeploySystemContracts(goCtx context.Context) error {
return err
}

ETHZRC20Addr, err := k.SetupChainGasCoinAndPool(ctx, common.GoerliChain().ChainId, "ETH", "gETH", 18)
ETHZRC20Addr, err := k.SetupChainGasCoinAndPool(ctx, common.GoerliChain().ChainId, "ETH", "gETH", 18, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
ctx.Logger().Info("Deployed ETH ZRC20 at " + ETHZRC20Addr.String())

BTCZRC20Addr, err := k.SetupChainGasCoinAndPool(ctx, common.BtcRegtestChain().ChainId, "BTC", "tBTC", 8)
BTCZRC20Addr, err := k.SetupChainGasCoinAndPool(ctx, common.BtcRegtestChain().ChainId, "BTC", "tBTC", 8, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ func (k Keeper) BlockOneDeploySystemContracts(goCtx context.Context) error {
if err != nil {
return err
}
_, err = k.SetupChainGasCoinAndPool(ctx, common.GoerliChain().ChainId, "ETH", "gETH", 18)
_, err = k.SetupChainGasCoinAndPool(ctx, common.GoerliChain().ChainId, "ETH", "gETH", 18, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}

_, err = k.SetupChainGasCoinAndPool(ctx, common.BscTestnetChain().ChainId, "BNB", "tBNB", 18)
_, err = k.SetupChainGasCoinAndPool(ctx, common.BscTestnetChain().ChainId, "BNB", "tBNB", 18, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
_, err = k.SetupChainGasCoinAndPool(ctx, common.MumbaiChain().ChainId, "MATIC", "tMATIC", 18)
_, err = k.SetupChainGasCoinAndPool(ctx, common.MumbaiChain().ChainId, "MATIC", "tMATIC", 18, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
_, err = k.SetupChainGasCoinAndPool(ctx, common.BtcTestNetChain().ChainId, "BTC", "tBTC", 8)
_, err = k.SetupChainGasCoinAndPool(ctx, common.BtcTestNetChain().ChainId, "BTC", "tBTC", 8, nil)
if err != nil {
return sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
Expand Down
20 changes: 16 additions & 4 deletions x/fungible/keeper/gas_coin_and_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@ import (
// SetupChainGasCoinAndPool setup gas ZRC20, and ZETA/gas pool for a chain
// add 0.1gas/0.1wzeta to the pool
// FIXME: add cointype and use proper gas limit based on cointype/chain
func (k Keeper) SetupChainGasCoinAndPool(ctx sdk.Context, chainID int64, gasAssetName string, symbol string, decimals uint8) (ethcommon.Address, error) {
func (k Keeper) SetupChainGasCoinAndPool(
ctx sdk.Context,
chainID int64,
gasAssetName string,
symbol string,
decimals uint8,
gasLimit *big.Int,
) (ethcommon.Address, error) {
chain := common.GetChainFromChainID(chainID)
if chain == nil {
return ethcommon.Address{}, zetaObserverTypes.ErrSupportedChains
}
name := fmt.Sprintf("%s-%s", gasAssetName, chain.ChainName)

transferGasLimit := big.NewInt(21_000)
if common.IsBitcoinChain(chain.ChainId) {
transferGasLimit = big.NewInt(100) // 100B for a typical tx
transferGasLimit := gasLimit

// default values
if transferGasLimit == nil {
transferGasLimit = big.NewInt(21_000)
if common.IsBitcoinChain(chain.ChainId) {
transferGasLimit = big.NewInt(100) // 100B for a typical tx
}
}

zrc20Addr, err := k.DeployZRC20Contract(ctx, name, symbol, decimals, chain.ChainId, common.CoinType_Gas, "", transferGasLimit)
Expand Down
1 change: 1 addition & 0 deletions x/fungible/keeper/gas_coin_and_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func setupGasCoin(
assetName,
symbol,
8,
nil,
)
require.NoError(t, err)
assertContractDeployment(t, evmk, ctx, addr)
Expand Down
2 changes: 1 addition & 1 deletion x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k msgServer) DeployFungibleCoinZRC20(goCtx context.Context, msg *types.Msg
}
if msg.CoinType == zetacommon.CoinType_Gas {
// #nosec G701 always in range
address, err = k.SetupChainGasCoinAndPool(ctx, msg.ForeignChainId, msg.Name, msg.Symbol, uint8(msg.Decimals))
address, err = k.SetupChainGasCoinAndPool(ctx, msg.ForeignChainId, msg.Name, msg.Symbol, uint8(msg.Decimals), big.NewInt(msg.GasLimit))
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to setupChainGasCoinAndPool")
}
Expand Down
12 changes: 11 additions & 1 deletion x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func TestMsgServer_DeployFungibleCoinZRC20(t *testing.T) {
require.Equal(t, foreignCoin.CoinType, common.CoinType_Gas)
require.Contains(t, foreignCoin.Name, "foo")

// check gas limit
gasLimit, err := k.QueryGasLimit(ctx, ethcommon.HexToAddress(foreignCoin.Zrc20ContractAddress))
require.NoError(t, err)
require.Equal(t, uint64(1000000), gasLimit.Uint64())

gas, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID))
require.NoError(t, err)
require.Equal(t, gasAddress, gas.Hex())
Expand All @@ -59,7 +64,7 @@ func TestMsgServer_DeployFungibleCoinZRC20(t *testing.T) {
"bar",
"bar",
common.CoinType_ERC20,
1000000,
2000000,
))
require.NoError(t, err)
assertContractDeployment(t, sdkk.EvmKeeper, ctx, ethcommon.HexToAddress(res.Address))
Expand All @@ -69,6 +74,11 @@ func TestMsgServer_DeployFungibleCoinZRC20(t *testing.T) {
require.Equal(t, foreignCoin.CoinType, common.CoinType_ERC20)
require.Contains(t, foreignCoin.Name, "bar")

// check gas limit
gasLimit, err = k.QueryGasLimit(ctx, ethcommon.HexToAddress(foreignCoin.Zrc20ContractAddress))
require.NoError(t, err)
require.Equal(t, uint64(2000000), gasLimit.Uint64())

// gas should remain the same
gas, err = k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID))
require.NoError(t, err)
Expand Down

0 comments on commit eb22930

Please sign in to comment.