From 785db68938e2ed662c3971aa66782ea6d20e48fd Mon Sep 17 00:00:00 2001 From: Francisco de Borja Aranda Castillejo Date: Tue, 8 Oct 2024 11:34:23 +0200 Subject: [PATCH] extract zrc20 method call --- precompiles/bank/bank.go | 9 ---- precompiles/bank/method_deposit.go | 2 +- precompiles/bank/method_withdraw.go | 2 +- x/fungible/keeper/zrc20_check_allowance.go | 23 +--------- x/fungible/keeper/zrc20_check_balance.go | 24 +--------- x/fungible/keeper/zrc20_lock_token.go | 25 +---------- x/fungible/keeper/zrc20_method_call.go | 51 ++++++++++++++++++++++ x/fungible/keeper/zrc20_unlock_token.go | 27 +----------- 8 files changed, 59 insertions(+), 104 deletions(-) create mode 100644 x/fungible/keeper/zrc20_method_call.go diff --git a/precompiles/bank/bank.go b/precompiles/bank/bank.go index 436364dfa6..f99c8fff66 100644 --- a/precompiles/bank/bank.go +++ b/precompiles/bank/bank.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" - "github.com/zeta-chain/protocol-contracts/v2/pkg/zrc20.sol" ptypes "github.com/zeta-chain/node/precompiles/types" fungiblekeeper "github.com/zeta-chain/node/x/fungible/keeper" @@ -70,18 +69,10 @@ func NewIBankContract( fungibleKeeper.GetAuthKeeper().SetAccount(ctx, authtypes.NewBaseAccount(accAddress, nil, 0, 0)) } - // Instantiate the ZRC20 ABI only one time. - // This avoids instantiating it every time deposit or withdraw are called. - zrc20ABI, err := zrc20.ZRC20MetaData.GetAbi() - if err != nil { - return nil - } - return &Contract{ BaseContract: ptypes.NewBaseContract(ContractAddress), bankKeeper: bankKeeper, fungibleKeeper: fungibleKeeper, - zrc20ABI: zrc20ABI, cdc: cdc, kvGasConfig: kvGasConfig, } diff --git a/precompiles/bank/method_deposit.go b/precompiles/bank/method_deposit.go index 62cb59dd51..72534b2b9b 100644 --- a/precompiles/bank/method_deposit.go +++ b/precompiles/bank/method_deposit.go @@ -100,7 +100,7 @@ func (c *Contract) deposit( } // 2. Effect: subtract balance. - if err := c.fungibleKeeper.LockZRC20(ctx, c.zrc20ABI, zrc20Addr, caller, amount); err != nil { + if err := c.fungibleKeeper.LockZRC20(ctx, zrc20Addr, caller, amount); err != nil { return nil, &ptypes.ErrUnexpected{ When: "LockZRC20InBank", Got: err.Error(), diff --git a/precompiles/bank/method_withdraw.go b/precompiles/bank/method_withdraw.go index e0893b08c2..63e026678c 100644 --- a/precompiles/bank/method_withdraw.go +++ b/precompiles/bank/method_withdraw.go @@ -85,7 +85,7 @@ func (c *Contract) withdraw( } // Check if fungible address has enough ZRC20 balance. - if err := c.fungibleKeeper.CheckFungibleZRC20Balance(ctx, c.zrc20ABI, zrc20Addr, amount); err != nil { + if err := c.fungibleKeeper.CheckFungibleZRC20Balance(ctx, zrc20Addr, amount); err != nil { return nil, &ptypes.ErrUnexpected{ When: "balanceOf", Got: err.Error(), diff --git a/x/fungible/keeper/zrc20_check_allowance.go b/x/fungible/keeper/zrc20_check_allowance.go index 538f985c47..c652e04c9a 100644 --- a/x/fungible/keeper/zrc20_check_allowance.go +++ b/x/fungible/keeper/zrc20_check_allowance.go @@ -5,7 +5,6 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" fungibletypes "github.com/zeta-chain/node/x/fungible/types" @@ -17,7 +16,6 @@ const allowance = "allowance" // is equal or greater than the provided amount. func (k Keeper) CheckFungibleZRC20Allowance( ctx sdk.Context, - zrc20ABI *abi.ABI, from, zrc20Address common.Address, amount *big.Int, ) error { @@ -34,27 +32,8 @@ func (k Keeper) CheckFungibleZRC20Allowance( } args := []interface{}{from, fungibletypes.ModuleAddressZEVM} - res, err := k.CallEVM( - ctx, - *zrc20ABI, - fungibletypes.ModuleAddressZEVM, - zrc20Address, - big.NewInt(0), - nil, - true, - true, - allowance, - args..., - ) - if err != nil { - return err - } - - if res.VmError != "" { - return fmt.Errorf("%s", res.VmError) - } - ret, err := zrc20ABI.Methods[allowance].Outputs.Unpack(res.Ret) + ret, err := k.CallZRC20Method(ctx, zrc20Address, allowance, args...) if err != nil { return err } diff --git a/x/fungible/keeper/zrc20_check_balance.go b/x/fungible/keeper/zrc20_check_balance.go index 34e393d6b9..2986d66846 100644 --- a/x/fungible/keeper/zrc20_check_balance.go +++ b/x/fungible/keeper/zrc20_check_balance.go @@ -5,7 +5,6 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" fungibletypes "github.com/zeta-chain/node/x/fungible/types" @@ -17,7 +16,6 @@ const balanceOf = "balanceOf" // is equal or greater than the provided amount. func (k Keeper) CheckFungibleZRC20Balance( ctx sdk.Context, - zrc20ABI *abi.ABI, zrc20Address common.Address, amount *big.Int, ) error { @@ -29,27 +27,7 @@ func (k Keeper) CheckFungibleZRC20Balance( return fmt.Errorf("zrc20 address cannot be zero") } - res, err := k.CallEVM( - ctx, - *zrc20ABI, - fungibletypes.ModuleAddressZEVM, - zrc20Address, - big.NewInt(0), - nil, - true, - true, - balanceOf, - fungibletypes.ModuleAddressZEVM, - ) - if err != nil { - return err - } - - if res.VmError != "" { - return fmt.Errorf("%s", res.VmError) - } - - ret, err := zrc20ABI.Methods[balanceOf].Outputs.Unpack(res.Ret) + ret, err := k.CallZRC20Method(ctx, zrc20Address, balanceOf, fungibletypes.ModuleAddressZEVM) if err != nil { return err } diff --git a/x/fungible/keeper/zrc20_lock_token.go b/x/fungible/keeper/zrc20_lock_token.go index 1f96e20e55..08c7282bb3 100644 --- a/x/fungible/keeper/zrc20_lock_token.go +++ b/x/fungible/keeper/zrc20_lock_token.go @@ -5,7 +5,6 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" fungibletypes "github.com/zeta-chain/node/x/fungible/types" @@ -19,7 +18,6 @@ var zeroAddress common.Address = common.HexToAddress("0x000000000000000000000000 // The caller must have approved the bank contract to spend the amount of ZRC20 tokens. func (k Keeper) LockZRC20( ctx sdk.Context, - zrc20ABI *abi.ABI, zrc20Address, from common.Address, amount *big.Int, ) error { @@ -39,32 +37,13 @@ func (k Keeper) LockZRC20( return err } - if err := k.CheckFungibleZRC20Allowance(ctx, zrc20ABI, from, zrc20Address, amount); err != nil { + if err := k.CheckFungibleZRC20Allowance(ctx, from, zrc20Address, amount); err != nil { return err } args := []interface{}{from, fungibletypes.ModuleAddressZEVM, amount} - res, err := k.CallEVM( - ctx, - *zrc20ABI, - fungibletypes.ModuleAddressZEVM, - zrc20Address, - big.NewInt(0), - nil, - true, - true, - transferFrom, - args..., - ) - if err != nil { - return err - } - - if res.VmError != "" { - return fmt.Errorf("%s", res.VmError) - } - ret, err := zrc20ABI.Methods[transferFrom].Outputs.Unpack(res.Ret) + ret, err := k.CallZRC20Method(ctx, zrc20Address, transferFrom, args...) if err != nil { return err } diff --git a/x/fungible/keeper/zrc20_method_call.go b/x/fungible/keeper/zrc20_method_call.go new file mode 100644 index 0000000000..912db1ecc2 --- /dev/null +++ b/x/fungible/keeper/zrc20_method_call.go @@ -0,0 +1,51 @@ +package keeper + +import ( + "fmt" + "math/big" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/ethereum/go-ethereum/common" + "github.com/zeta-chain/protocol-contracts/v2/pkg/zrc20.sol" + + fungibletypes "github.com/zeta-chain/node/x/fungible/types" +) + +func (k Keeper) CallZRC20Method( + ctx sdk.Context, + zrc20Address common.Address, + method string, + args ...interface{}, +) ([]interface{}, error) { + zrc20ABI, err := zrc20.ZRC20MetaData.GetAbi() + if err != nil { + return nil, err + } + + res, err := k.CallEVM( + ctx, + *zrc20ABI, + fungibletypes.ModuleAddressZEVM, + zrc20Address, + big.NewInt(0), + nil, + true, + true, + method, + args..., + ) + if err != nil { + return nil, err + } + + if res.VmError != "" { + return nil, fmt.Errorf("%s", res.VmError) + } + + ret, err := zrc20ABI.Methods[method].Outputs.Unpack(res.Ret) + if err != nil { + return nil, err + } + + return ret, nil +} diff --git a/x/fungible/keeper/zrc20_unlock_token.go b/x/fungible/keeper/zrc20_unlock_token.go index 26f917fe82..82d3679a10 100644 --- a/x/fungible/keeper/zrc20_unlock_token.go +++ b/x/fungible/keeper/zrc20_unlock_token.go @@ -5,10 +5,7 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - - fungibletypes "github.com/zeta-chain/node/x/fungible/types" ) const transfer = "transfer" @@ -16,7 +13,6 @@ const transfer = "transfer" // UnlockZRC20 unlocks ZRC20 tokens and sends them to the "to" address. func (k Keeper) UnlockZRC20( ctx sdk.Context, - zrc20ABI *abi.ABI, zrc20Address, to common.Address, amount *big.Int, ) error { @@ -36,32 +32,13 @@ func (k Keeper) UnlockZRC20( return err } - if err := k.CheckFungibleZRC20Balance(ctx, zrc20ABI, zrc20Address, amount); err != nil { + if err := k.CheckFungibleZRC20Balance(ctx, zrc20Address, amount); err != nil { return err } args := []interface{}{to, amount} - res, err := k.CallEVM( - ctx, - *zrc20ABI, - fungibletypes.ModuleAddressZEVM, - zrc20Address, - big.NewInt(0), - nil, - true, - true, - transfer, - args..., - ) - if err != nil { - return err - } - - if res.VmError != "" { - return fmt.Errorf("%s", res.VmError) - } - ret, err := zrc20ABI.Methods[transfer].Outputs.Unpack(res.Ret) + ret, err := k.CallZRC20Method(ctx, zrc20Address, transfer, args...) if err != nil { return err }