Skip to content

Commit

Permalink
deposit gas token
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Aug 1, 2024
1 parent 166767c commit 1779e98
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 20 deletions.
1 change: 1 addition & 0 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func MockRevertForHandleEVMDeposit(
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{VmError: "reverted"}, false, errDeposit)
}

Expand Down
24 changes: 13 additions & 11 deletions testutil/keeper/mocks/crosschain/fungible.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/crosschain/keeper/evm_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (k Keeper) HandleEVMDeposit(ctx sdk.Context, cctx *types.CrossChainTx) (boo
data,
inboundCoinType,
cctx.InboundParams.Asset,
cctx.ProtocolContractVersion,
)
if fungibletypes.IsContractReverted(evmTxResponse, err) || errShouldRevertCctx(err) {
return true, err
Expand Down
12 changes: 11 additions & 1 deletion x/crosschain/keeper/evm_deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
fungibleMock.On("ZETADepositAndCallContract", ctx, ethcommon.HexToAddress(sender.String()), receiver, senderChainId, amount, mock.Anything, mock.Anything).
Return(nil, errDeposit)

// call HandleEVMDeposit
// call HandleEVMDeposit

cctx.InboundParams.Sender = sender.String()
cctx.GetCurrentOutboundParam().Receiver = receiver.String()
Expand Down Expand Up @@ -106,6 +106,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, nil)

// call HandleEVMDeposit
Expand Down Expand Up @@ -151,6 +152,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{
Logs: []*evmtypes.Log{
{
Expand Down Expand Up @@ -213,6 +215,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{
Logs: []*evmtypes.Log{
{
Expand Down Expand Up @@ -302,6 +305,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, errDeposit)

// call HandleEVMDeposit
Expand Down Expand Up @@ -346,6 +350,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{VmError: "reverted"}, false, errDeposit)

// call HandleEVMDeposit
Expand Down Expand Up @@ -389,6 +394,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, fungibletypes.ErrForeignCoinCapReached)

// call HandleEVMDeposit
Expand Down Expand Up @@ -432,6 +438,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, fungibletypes.ErrPausedZRC20)

// call HandleEVMDeposit
Expand Down Expand Up @@ -475,6 +482,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, fungibletypes.ErrCallNonContract)

// call HandleEVMDeposit
Expand Down Expand Up @@ -544,6 +552,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
data,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, nil)

cctx.GetCurrentOutboundParam().Receiver = sample.EthAddress().String()
Expand Down Expand Up @@ -586,6 +595,7 @@ func TestMsgServer_HandleEVMDeposit(t *testing.T) {
data,
coin.CoinType_ERC20,
mock.Anything,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{}, false, nil)

cctx := sample.CrossChainTx(t, "foo")
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type FungibleKeeper interface {
data []byte,
coinType coin.CoinType,
asset string,
protocolContractVersion ProtocolContractVersion,
) (*evmtypes.MsgEthereumTxResponse, bool, error)
CallUniswapV2RouterSwapExactTokensForTokens(
ctx sdk.Context,
Expand Down
21 changes: 13 additions & 8 deletions x/fungible/keeper/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func (k Keeper) DepositCoinsToFungibleModule(ctx sdk.Context, amount *big.Int) e

// ZRC20DepositAndCallContract deposits ZRC20 to the EVM account and calls the contract
// returns [txResponse, isContractCall, error]
// isContractCall is true if the receiver is a contract and a contract call was made
func (k Keeper) ZRC20DepositAndCallContract(
ctx sdk.Context,
from []byte,
to eth.Address,
amount *big.Int,
senderChainID int64,
data []byte,
message []byte,
coinType coin.CoinType,
asset string,
protocolContractVersion crosschaintypes.ProtocolContractVersion,
) (*evmtypes.MsgEthereumTxResponse, bool, error) {
var ZRC20Contract eth.Address
var zrc20Contract eth.Address
var foreignCoin types.ForeignCoins
var found bool

Expand All @@ -52,7 +52,7 @@ func (k Keeper) ZRC20DepositAndCallContract(
return nil, false, crosschaintypes.ErrForeignCoinNotFound
}
}
ZRC20Contract = eth.HexToAddress(foreignCoin.Zrc20ContractAddress)
zrc20Contract = eth.HexToAddress(foreignCoin.Zrc20ContractAddress)

// check if foreign coin is paused
if foreignCoin.Paused {
Expand All @@ -62,7 +62,7 @@ func (k Keeper) ZRC20DepositAndCallContract(
// check foreign coins cap if it has a cap
if !foreignCoin.LiquidityCap.IsNil() && !foreignCoin.LiquidityCap.IsZero() {
liquidityCap := foreignCoin.LiquidityCap.BigInt()
totalSupply, err := k.TotalSupplyZRC4(ctx, ZRC20Contract)
totalSupply, err := k.TotalSupplyZRC4(ctx, zrc20Contract)
if err != nil {
return nil, false, err
}
Expand All @@ -72,6 +72,11 @@ func (k Keeper) ZRC20DepositAndCallContract(
}
}

// handle the deposit for protocol contract version 2
if protocolContractVersion == crosschaintypes.ProtocolContractVersion_V2 {
return k.ProcessV2Deposit(ctx, zrc20Contract, to, amount, message)

Check warning on line 77 in x/fungible/keeper/deposits.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/deposits.go#L77

Added line #L77 was not covered by tests
}

// check if the receiver is a contract
// if it is, then the hook onCrossChainCall() will be called
// if not, the zrc20 are simply transferred to the receiver
Expand All @@ -82,15 +87,15 @@ func (k Keeper) ZRC20DepositAndCallContract(
Sender: eth.Address{},
ChainID: big.NewInt(senderChainID),
}
res, err := k.DepositZRC20AndCallContract(ctx, context, ZRC20Contract, to, amount, data)
res, err := k.DepositZRC20AndCallContract(ctx, context, zrc20Contract, to, amount, message)
return res, true, err
}

// if the account is a EOC, no contract call can be made with the data
if len(data) > 0 {
if len(message) > 0 {
return nil, false, types.ErrCallNonContract
}

res, err := k.DepositZRC20(ctx, ZRC20Contract, to, amount)
res, err := k.DepositZRC20(ctx, zrc20Contract, to, amount)
return res, false, err
}
10 changes: 10 additions & 0 deletions x/fungible/keeper/deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.NoError(t, err)
require.False(t, contractCall)
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_ERC20,
assetAddress,
crosschaintypes.ProtocolContractVersion_V1,
)
require.NoError(t, err)
require.False(t, contractCall)
Expand Down Expand Up @@ -108,6 +110,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte("DEADBEEF"),
coin.CoinType_ERC20,
assetAddress,
crosschaintypes.ProtocolContractVersion_V1,
)
require.ErrorIs(t, err, types.ErrCallNonContract)
})
Expand Down Expand Up @@ -149,6 +152,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.NoError(t, err)
require.False(t, contractCall)
Expand Down Expand Up @@ -186,6 +190,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.ErrorIs(t, err, types.ErrPausedZRC20)
})
Expand Down Expand Up @@ -227,6 +232,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.ErrorIs(t, err, types.ErrForeignCoinCapReached)
})
Expand All @@ -253,6 +259,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.ErrorIs(t, err, crosschaintypes.ErrGasCoinNotFound)
})
Expand All @@ -279,6 +286,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_ERC20,
assetAddress,
crosschaintypes.ProtocolContractVersion_V1,
)
require.ErrorIs(t, err, crosschaintypes.ErrForeignCoinNotFound)
})
Expand Down Expand Up @@ -309,6 +317,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.NoError(t, err)
require.True(t, contractCall)
Expand Down Expand Up @@ -347,6 +356,7 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
[]byte{},
coin.CoinType_Gas,
sample.EthAddress().String(),
crosschaintypes.ProtocolContractVersion_V1,
)
require.Error(t, err)
require.True(t, contractCall)
Expand Down
29 changes: 29 additions & 0 deletions x/fungible/keeper/v2_deposits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper

import (
"errors"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

// ProcessV2Deposit handles a deposit from an inbound tx with protocol version 2
// returns [txResponse, isContractCall, error]
// isContractCall is true if the message is non empty
func (k Keeper) ProcessV2Deposit(
ctx sdk.Context,
zrc20Addr ethcommon.Address,
to ethcommon.Address,
amount *big.Int,
message []byte,
) (*evmtypes.MsgEthereumTxResponse, bool, error) {
if len(message) == 0 {

Check warning on line 22 in x/fungible/keeper/v2_deposits.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_deposits.go#L21-L22

Added lines #L21 - L22 were not covered by tests
// simple deposit
res, err := k.DepositZRC20(ctx, zrc20Addr, to, amount)
return res, false, err
} else {

Check failure on line 26 in x/fungible/keeper/v2_deposits.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return nil, true, errors.New("not implemented")

Check warning on line 27 in x/fungible/keeper/v2_deposits.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_deposits.go#L24-L27

Added lines #L24 - L27 were not covered by tests
}
}

0 comments on commit 1779e98

Please sign in to comment.