Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read gas limit from smart contract #1277

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
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
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
Loading