From c04a708cd0239f2f10d8721eee4014288bea715e Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 26 Sep 2023 14:15:33 +0900 Subject: [PATCH] add switch --- x/cronos/keeper/precompiles/bank.go | 20 ++++++++++--- x/cronos/keeper/precompiles/ica.go | 16 ++++++++++- x/cronos/keeper/precompiles/relayer.go | 40 ++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/x/cronos/keeper/precompiles/bank.go b/x/cronos/keeper/precompiles/bank.go index 646cecc971..a665005de2 100644 --- a/x/cronos/keeper/precompiles/bank.go +++ b/x/cronos/keeper/precompiles/bank.go @@ -18,8 +18,7 @@ import ( ) const ( - EVMDenomPrefix = "evm/" - BankContractRequiredGas = 21000 + EVMDenomPrefix = "evm/" ) var ( @@ -53,8 +52,21 @@ func (bc *BankContract) Address() common.Address { // RequiredGas calculates the contract gas use func (bc *BankContract) RequiredGas(input []byte) uint64 { - // TODO estimate required gas - return BankContractRequiredGas + methodID := input[:4] + method, err := bankABI.MethodById(methodID) + if err != nil { + return 0 + } + switch method.Name { + case "mint", "burn": + return 1000 + case "balanceOf": + return 1000 + case "transfer": + return 1000 + default: + return 0 + } } func (bc *BankContract) IsStateful() bool { diff --git a/x/cronos/keeper/precompiles/ica.go b/x/cronos/keeper/precompiles/ica.go index 077627acb1..1f21932b6d 100644 --- a/x/cronos/keeper/precompiles/ica.go +++ b/x/cronos/keeper/precompiles/ica.go @@ -54,7 +54,21 @@ func (ic *IcaContract) Address() common.Address { // RequiredGas calculates the contract gas use func (ic *IcaContract) RequiredGas(input []byte) uint64 { - return ICAContractRequiredGas + methodID := input[:4] + method, err := icaABI.MethodById(methodID) + if err != nil { + return 0 + } + switch method.Name { + case "registerAccount": + return 1000 + case "queryAccount": + return 1000 + case "submitMsgs": + return 1000 + default: + return 0 + } } func (ic *IcaContract) IsStateful() bool { diff --git a/x/cronos/keeper/precompiles/relayer.go b/x/cronos/keeper/precompiles/relayer.go index 09ae70407a..8b92712743 100644 --- a/x/cronos/keeper/precompiles/relayer.go +++ b/x/cronos/keeper/precompiles/relayer.go @@ -12,8 +12,6 @@ import ( cronosevents "github.com/crypto-org-chain/cronos/v2/x/cronos/events" ) -const RelayerContractRequiredGas = 300000 - var RelayerContractAddress = common.BytesToAddress([]byte{101}) type RelayerContract struct { @@ -37,7 +35,43 @@ func (bc *RelayerContract) Address() common.Address { // RequiredGas calculates the contract gas use func (bc *RelayerContract) RequiredGas(input []byte) uint64 { - return RelayerContractRequiredGas + prefix := int(binary.LittleEndian.Uint32(input[:prefixSize4Bytes])) + switch prefix { + case prefixCreateClient: + return 300000 + case prefixUpdateClient: + return 300000 + case prefixUpgradeClient: + return 300000 + case prefixSubmitMisbehaviour: + return 300000 + case prefixConnectionOpenInit: + return 300000 + case prefixConnectionOpenTry: + return 300000 + case prefixConnectionOpenAck: + return 300000 + case prefixConnectionOpenConfirm: + return 300000 + case prefixChannelOpenInit: + return 300000 + case prefixChannelOpenTry: + return 300000 + case prefixChannelOpenAck: + return 300000 + case prefixChannelOpenConfirm: + return 300000 + case prefixRecvPacket: + return 300000 + case prefixAcknowledgement: + return 300000 + case prefixTimeout: + return 300000 + case prefixTimeoutOnClose: + return 300000 + default: + return 0 + } } func (bc *RelayerContract) IsStateful() bool {