Skip to content

Commit

Permalink
add review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 16, 2024
1 parent 10dcfd6 commit f20129d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions precompiles/bank/method_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (c *Contract) deposit(
// The process of creating a new cosmos coin is:
// - Generate the new coin denom using ZRC20 address,
// this way we map ZRC20 addresses to cosmos denoms "zevm/0x12345".
// - Mint coins.
// - Send coins to the caller.
// - Mint coins to the fungible module.
// - Send coins from fungible to the caller.
coinSet, err := createCoinSet(ZRC20ToCosmosDenom(zrc20Addr), amount)
if err != nil {
return nil, err
Expand Down
22 changes: 11 additions & 11 deletions precompiles/bank/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func Test_Methods(t *testing.T) {
t.Run("should fail when trying to run deposit as read only method", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
methodID := ts.bankABI.Methods[DepositMethodName]

// Set CallerAddress and evm.Origin to the caller address.
Expand All @@ -55,7 +55,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when trying to run withdraw as read only method", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
methodID := ts.bankABI.Methods[WithdrawMethodName]

// Set CallerAddress and evm.Origin to the caller address.
Expand All @@ -82,7 +82,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when caller has 0 token balance", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
methodID := ts.bankABI.Methods[DepositMethodName]

// Set CallerAddress and evm.Origin to the caller address.
Expand Down Expand Up @@ -116,7 +116,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when bank has 0 token allowance", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))

methodID := ts.bankABI.Methods[DepositMethodName]
Expand Down Expand Up @@ -146,7 +146,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when trying to deposit 0", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))

methodID := ts.bankABI.Methods[DepositMethodName]
Expand All @@ -173,7 +173,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when trying to deposit more than allowed to bank", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))

// Allow bank to spend 500 ZRC20 tokens.
Expand Down Expand Up @@ -225,7 +225,7 @@ func Test_Methods(t *testing.T) {

t.Run("should fail when trying to deposit more than user balance", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))

// Allow bank to spend 500 ZRC20 tokens.
Expand Down Expand Up @@ -279,7 +279,7 @@ func Test_Methods(t *testing.T) {

t.Run("should deposit tokens and retrieve balance of cosmos coin", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))
methodID := ts.bankABI.Methods[DepositMethodName]

Expand Down Expand Up @@ -325,7 +325,7 @@ func Test_Methods(t *testing.T) {

t.Run("should deposit tokens, withdraw and check with balanceOf", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))
methodID := ts.bankABI.Methods[DepositMethodName]

Expand Down Expand Up @@ -404,7 +404,7 @@ func Test_Methods(t *testing.T) {

t.Run("should deposit tokens and fail when withdrawing more than depositted", func(t *testing.T) {
ts := setupChain(t)
caller := fungibletypes.ModuleAddressZEVM
caller := fungibletypes.ModuleAddressEVM
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, caller, big.NewInt(1000))
methodID := ts.bankABI.Methods[DepositMethodName]

Expand Down Expand Up @@ -573,7 +573,7 @@ func allowBank(t *testing.T, ts testSuite, amount *big.Int) {
ts.ctx,
ts.fungibleKeeper,
&ts.zrc20ABI,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
ts.zrc20Address,
"approve",
[]interface{}{ts.bankContract.Address(), amount},
Expand Down
8 changes: 4 additions & 4 deletions x/fungible/keeper/zrc20_cosmos_coin_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_LockZRC20(t *testing.T) {

ts := setupChain(t)

owner := fungibletypes.ModuleAddressZEVM
owner := fungibletypes.ModuleAddressEVM
locker := sample.EthAddress()
depositTotal := big.NewInt(1000)
allowanceTotal := big.NewInt(100)
Expand Down Expand Up @@ -148,7 +148,7 @@ func Test_UnlockZRC20(t *testing.T) {

ts := setupChain(t)

owner := fungibletypes.ModuleAddressZEVM
owner := fungibletypes.ModuleAddressEVM
locker := sample.EthAddress()
depositTotal := big.NewInt(1000)
allowanceTotal := big.NewInt(100)
Expand Down Expand Up @@ -217,7 +217,7 @@ func Test_CheckZRC20Allowance(t *testing.T) {

ts := setupChain(t)

owner := fungibletypes.ModuleAddressZEVM
owner := fungibletypes.ModuleAddressEVM
spender := sample.EthAddress()
depositTotal := big.NewInt(1000)
allowanceTotal := big.NewInt(100)
Expand All @@ -229,7 +229,7 @@ func Test_CheckZRC20Allowance(t *testing.T) {
ts.fungibleKeeper.GetAuthKeeper().SetAccount(ts.ctx, authtypes.NewBaseAccount(accAddress, nil, 0, 0))

// Deposit ZRC20 tokens into the fungible.
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressZEVM, depositTotal)
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressEVM, depositTotal)

t.Run("should fail when checking zero amount", func(t *testing.T) {
err = ts.fungibleKeeper.CheckZRC20Allowance(ts.ctx, zrc20ABI, owner, spender, ts.zrc20Address, big.NewInt(0))
Expand Down
6 changes: 3 additions & 3 deletions x/fungible/keeper/zrc20_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k Keeper) ZRC20Allowance(
res, err := k.CallEVM(
ctx,
*zrc20ABI,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
zrc20Address,
big.NewInt(0),
nil,
Expand Down Expand Up @@ -101,7 +101,7 @@ func (k Keeper) ZRC20BalanceOf(
res, err := k.CallEVM(
ctx,
*zrc20ABI,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
zrc20Address,
big.NewInt(0),
nil,
Expand Down Expand Up @@ -153,7 +153,7 @@ func (k Keeper) ZRC20TotalSupply(
res, err := k.CallEVM(
ctx,
*zrc20ABI,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
zrc20Address,
big.NewInt(0),
nil,
Expand Down
28 changes: 14 additions & 14 deletions x/fungible/keeper/zrc20_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_ZRC20Allowance(t *testing.T) {
zrc20ABI,
common.Address{},
sample.EthAddress(),
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
)
require.Error(t, err)
require.ErrorAs(t, err, &fungibletypes.ErrZRC20ZeroAddress)
Expand All @@ -68,7 +68,7 @@ func Test_ZRC20Allowance(t *testing.T) {
ts.ctx,
zrc20ABI,
ts.zrc20Address,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
sample.EthAddress(),
)
require.NoError(t, err)
Expand Down Expand Up @@ -107,7 +107,7 @@ func Test_ZRC20BalanceOf(t *testing.T) {
ts.ctx,
zrc20ABI,
ts.zrc20Address,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
)
require.NoError(t, err)
require.Equal(t, uint64(0), balance.Uint64())
Expand Down Expand Up @@ -198,20 +198,20 @@ func Test_ZRC20Transfer(t *testing.T) {
zrc20ABI,
common.Address{},
sample.EthAddress(),
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
big.NewInt(0),
)
require.Error(t, err)
require.ErrorAs(t, err, &fungibletypes.ErrZRC20ZeroAddress)
})

t.Run("should pass with correct input", func(t *testing.T) {
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressZEVM, big.NewInt(10))
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressEVM, big.NewInt(10))
transferred, err := ts.fungibleKeeper.ZRC20Transfer(
ts.ctx,
zrc20ABI,
ts.zrc20Address,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
sample.EthAddress(),
big.NewInt(10),
)
Expand Down Expand Up @@ -295,7 +295,7 @@ func Test_ZRC20TransferFrom(t *testing.T) {
common.Address{},
sample.EthAddress(),
sample.EthAddress(),
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
big.NewInt(0),
)
require.Error(t, err)
Expand All @@ -304,36 +304,36 @@ func Test_ZRC20TransferFrom(t *testing.T) {

t.Run("should fail without an allowance approval", func(t *testing.T) {
// Deposit ZRC20 into fungible EOA.
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressZEVM, big.NewInt(1000))
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressEVM, big.NewInt(1000))

// Transferring the tokens with transferFrom without approval should fail.
_, err = ts.fungibleKeeper.ZRC20TransferFrom(
ts.ctx,
zrc20ABI,
ts.zrc20Address,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
sample.EthAddress(),
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
big.NewInt(10),
)
require.Error(t, err)
})

t.Run("should success with an allowance approval", func(t *testing.T) {
// Deposit ZRC20 into fungible EOA.
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressZEVM, big.NewInt(1000))
ts.fungibleKeeper.DepositZRC20(ts.ctx, ts.zrc20Address, fungibletypes.ModuleAddressEVM, big.NewInt(1000))

// Approve allowance to sample.EthAddress() to spend 10 ZRC20 tokens.
approveAllowance(t, ts, zrc20ABI, fungibletypes.ModuleAddressZEVM, sample.EthAddress(), big.NewInt(10))
approveAllowance(t, ts, zrc20ABI, fungibletypes.ModuleAddressEVM, sample.EthAddress(), big.NewInt(10))

// Transferring the tokens with transferFrom without approval should fail.
_, err = ts.fungibleKeeper.ZRC20TransferFrom(
ts.ctx,
zrc20ABI,
ts.zrc20Address,
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
sample.EthAddress(),
fungibletypes.ModuleAddressZEVM,
fungibletypes.ModuleAddressEVM,
big.NewInt(10),
)
require.Error(t, err)
Expand Down
4 changes: 0 additions & 4 deletions x/fungible/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func KeyPrefix(p string) []byte {
var (
ModuleAddress = authtypes.NewModuleAddress(ModuleName)
ModuleAddressEVM = common.BytesToAddress(ModuleAddress.Bytes())
// ModuleAddressZEVM is calculated in the same way as ModuleAddressEVM.
// Maintain it for legibility in functions calling fungible module address from zEVM.
ModuleAddressZEVM = common.BytesToAddress(ModuleAddress.Bytes())
AdminAddress = "zeta1rx9r8hff0adaqhr5tuadkzj4e7ns2ntg446vtt"
)

const (
Expand Down

0 comments on commit f20129d

Please sign in to comment.