Skip to content

Commit

Permalink
extract zrc20 method call
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 8, 2024
1 parent d5190a5 commit e8f182c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 105 deletions.
9 changes: 0 additions & 9 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion precompiles/bank/method_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions precompiles/bank/method_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -108,7 +108,7 @@ func (c *Contract) withdraw(
}

// 3. Interactions: send ZRC20.
if err := c.fungibleKeeper.UnlockZRC20(ctx, c.zrc20ABI, zrc20Addr, caller, amount); err != nil {
if err := c.fungibleKeeper.UnlockZRC20(ctx, zrc20Addr, caller, amount); err != nil {
return nil, &ptypes.ErrUnexpected{
When: "UnlockZRC20InBank",
Got: err.Error(),
Expand Down
23 changes: 1 addition & 22 deletions x/fungible/keeper/zrc20_check_allowance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
24 changes: 1 addition & 23 deletions x/fungible/keeper/zrc20_check_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
25 changes: 2 additions & 23 deletions x/fungible/keeper/zrc20_lock_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
51 changes: 51 additions & 0 deletions x/fungible/keeper/zrc20_method_call.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 2 additions & 25 deletions x/fungible/keeper/zrc20_unlock_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ 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"

// 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 {
Expand All @@ -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
}
Expand Down

0 comments on commit e8f182c

Please sign in to comment.