Skip to content

Commit

Permalink
gas price
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Sep 13, 2023
1 parent b843da6 commit 9af09a8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
6 changes: 3 additions & 3 deletions x/fungible/keeper/gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/zeta-chain/zetacore/x/fungible/types"
)

// sets gas price on the system contract in zEVM; return the gasUsed and error code
// SetGasPrice sets gas price on the system contract in zEVM; return the gasUsed and error code
func (k Keeper) SetGasPrice(ctx sdk.Context, chainid *big.Int, gasPrice *big.Int) (uint64, error) {
system, found := k.GetSystemContract(ctx)
if !found {
Expand Down Expand Up @@ -66,9 +66,9 @@ func (k Keeper) SetGasZetaPool(ctx sdk.Context, chainid *big.Int, pool common.Ad
if err != nil {
return sdkerrors.Wrapf(types.ErrABIGet, "SystemContractMetaData")
}
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, oracle, BigIntZero, nil, true, false, "SetGasZetaPool", chainid, pool)
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, oracle, BigIntZero, nil, true, false, "setGasZetaPool", chainid, pool)
if err != nil || res.Failed() {
return sdkerrors.Wrapf(types.ErrContractCall, "SetGasZetaPool")
return sdkerrors.Wrapf(types.ErrContractCall, "setGasZetaPool")
}

return nil
Expand Down
74 changes: 74 additions & 0 deletions x/fungible/keeper/gas_price_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package keeper_test

import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/systemcontract.sol"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/x/fungible/types"
"math/big"
"testing"
)

func TestKeeper_SetGasPrice(t *testing.T) {
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

_, _, _, _, system := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)

queryGasPrice := func(chainID *big.Int) *big.Int {
abi, err := systemcontract.SystemContractMetaData.GetAbi()
require.NoError(t, err)
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, system, keeper.BigIntZero, nil, false, false, "gasPriceByChainId", chainID)
require.NoError(t, err)
unpacked, err := abi.Unpack("gasPriceByChainId", res.Ret)
require.NoError(t, err)
gasPrice, ok := unpacked[0].(*big.Int)
require.True(t, ok)
return gasPrice
}

_, err := k.SetGasPrice(ctx, big.NewInt(1), big.NewInt(42))
require.NoError(t, err)
require.Equal(t, big.NewInt(42), queryGasPrice(big.NewInt(1)))
}

func TestKeeper_SetGasCoin(t *testing.T) {
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
gas := sample.EthAddress()

deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)
err := k.SetGasCoin(ctx, big.NewInt(1), gas)
require.NoError(t, err)

found, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(1))
require.NoError(t, err)
require.Equal(t, gas.Hex(), found.Hex())
}

func TestKeeper_SetGasZetaPool(t *testing.T) {
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
zrc20 := sample.EthAddress()

_, _, _, _, system := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)

queryZetaPool := func(chainID *big.Int) ethcommon.Address {
abi, err := systemcontract.SystemContractMetaData.GetAbi()
require.NoError(t, err)
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, system, keeper.BigIntZero, nil, false, false, "gasZetaPoolByChainId", chainID)
require.NoError(t, err)
unpacked, err := abi.Unpack("gasZetaPoolByChainId", res.Ret)
require.NoError(t, err)
pool, ok := unpacked[0].(ethcommon.Address)
require.True(t, ok)
return pool
}

err := k.SetGasZetaPool(ctx, big.NewInt(1), zrc20)
require.NoError(t, err)
require.NotEqual(t, ethcommon.Address{}, queryZetaPool(big.NewInt(1)))
}

0 comments on commit 9af09a8

Please sign in to comment.