Skip to content

Commit

Permalink
simple call implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Aug 10, 2024
1 parent ec88dd9 commit 17e084e
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 186 deletions.
8 changes: 4 additions & 4 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
//e2etests.TestV2ETHDepositAndCallName,
//e2etests.TestV2ETHWithdrawName,
//e2etests.TestV2ETHWithdrawAndCallName,
e2etests.TestV2ERC20DepositName,
//e2etests.TestV2ERC20DepositName,
//e2etests.TestV2ERC20DepositAndCallName,
//e2etests.TestV2ERC20WithdrawName,
e2etests.TestV2ERC20WithdrawAndCallName,
//e2etests.TestV2ZEVMToEVMCallName,
//e2etests.TestV2EVMToZEVMCallName,
//e2etests.TestV2ERC20WithdrawAndCallName,
e2etests.TestV2ZEVMToEVMCallName,
e2etests.TestV2EVMToZEVMCallName,
))
}

Expand Down
4 changes: 3 additions & 1 deletion docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57071,11 +57071,13 @@ definitions:
- Gas
- ERC20
- Cmd
- NoAssetCall
default: Zeta
title: |-
- Gas: Ether, BNB, Matic, Klay, BTC, etc
- ERC20: ERC20 token
- Cmd: not a real coin, rather a command
- Cmd: not asset, used for admin command
- NoAssetCall: no asset, used for contract call
crosschainCctxStatus:
type: string
enum:
Expand Down
12 changes: 8 additions & 4 deletions e2e/e2etests/test_v2_evm_to_zevm_call.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package e2etests

import (
"math/big"

"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

const payloadMessageZEVMCall = "this is a test ZEVM call payload"

func TestV2EVMToZEVMCall(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0)

// TODO: set payload
payload := []byte("")

// perform the withdraw
tx := r.V2EVMToZEMVCall(r.EVMAddress(), payload)
tx := r.V2EVMToZEMVCall(r.TestDAppV2ZEVMAddr, []byte(payloadMessageZEVMCall))

// wait for the cctx to be mined
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
r.Logger.CCTX(*cctx, "call")
require.Equal(r, crosschaintypes.CctxStatus_OutboundMined, cctx.CctxStatus.Status)

// check the payload was received on the contract
r.AssertTestDAppZEVMValues(true, payloadMessageZEVMCall, big.NewInt(0))
}
13 changes: 10 additions & 3 deletions e2e/e2etests/test_v2_zevm_to_evm_call.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package e2etests

import (
"math/big"

"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

const payloadMessageEVMCall = "this is a test EVM call payload"

func TestV2ZEVMToEVMCall(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0)

// TODO: set payload
payload := []byte("")
// Necessary approval for fee payment
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// perform the withdraw
tx := r.V2ZEVMToEMVCall(r.EVMAddress(), payload)
tx := r.V2ZEVMToEMVCall(r.TestDAppV2EVMAddr, r.EncodeSimpleCall(payloadMessageEVMCall))

// wait for the cctx to be mined
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
r.Logger.CCTX(*cctx, "call")
require.Equal(r, crosschaintypes.CctxStatus_OutboundMined, cctx.CctxStatus.Status)

// check the payload was received on the contract
r.AssertTestDAppEVMValues(true, payloadMessageEVMCall, big.NewInt(0))
}
1 change: 1 addition & 0 deletions e2e/runner/v2_setup_zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (r *E2ERunner) UpdateChainParamsERC20CustodyContract() {
require.NoError(r, err)

evmChainID, err := r.EVMClient.ChainID(r.Ctx)
require.NoError(r, err)

// find old chain params
var (
Expand Down
1 change: 1 addition & 0 deletions pkg/contracts/testdappv2/TestDAppV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ contract TestDAppV2 {
require(!isRevertMessage(bytes(message)));

lastMessage = message;
lastAmount = 0;
}

function isRevertMessage(bytes memory message) internal pure returns (bool) {
Expand Down
9 changes: 8 additions & 1 deletion typescript/zetachain/zetacore/pkg/coin/coin_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ export declare enum CoinType {
ERC20 = 2,

/**
* not a real coin, rather a command
* not asset, used for admin command
*
* @generated from enum value: Cmd = 3;
*/
Cmd = 3,

/**
* no asset, used for contract call
*
* @generated from enum value: NoAssetCall = 4;
*/
NoAssetCall = 4,
}

8 changes: 6 additions & 2 deletions x/fungible/keeper/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (k Keeper) ZRC20DepositAndCallContract(
var found bool

// get foreign coin
if coinType == coin.CoinType_Gas {
// retrieve the gas token of the chain for no asset call
// this simplify the current workflow and allow to pause calls by pausing the gas token
// TODO: refactor this logic and create specific workflow for no asset call
// https://github.com/zeta-chain/node/issues/2627
if coinType == coin.CoinType_Gas || coinType == coin.CoinType_NoAssetCall {
foreignCoin, found = k.GetGasCoinForForeignCoin(ctx, senderChainID)
if !found {
return nil, false, crosschaintypes.ErrGasCoinNotFound
Expand Down Expand Up @@ -74,7 +78,7 @@ func (k Keeper) ZRC20DepositAndCallContract(

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

// check if the receiver is a contract
Expand Down
14 changes: 12 additions & 2 deletions x/fungible/keeper/v2_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/zeta-chain/protocol-contracts/v2/pkg/systemcontract.sol"

"github.com/zeta-chain/zetacore/pkg/coin"
)

// ProcessV2Deposit handles a deposit from an inbound tx with protocol version 2
Expand All @@ -20,14 +22,22 @@ func (k Keeper) ProcessV2Deposit(
to ethcommon.Address,
amount *big.Int,
message []byte,
coinType coin.CoinType,
) (*evmtypes.MsgEthereumTxResponse, bool, error) {
// simple deposit
if len(message) == 0 {
// simple deposit
res, err := k.DepositZRC20(ctx, zrc20Addr, to, amount)
return res, false, err
} else if coinType == coin.CoinType_NoAssetCall {
// simple call
context := systemcontract.ZContext{
Origin: from,
Sender: ethcommon.Address{},
ChainID: big.NewInt(senderChainID),

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

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_deposits.go#L33-L36

Added lines #L33 - L36 were not covered by tests
}
res, err := k.CallExecute(ctx, context, zrc20Addr, amount, to, message)
return res, true, err

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

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_deposits.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}

// deposit and call
context := systemcontract.ZContext{
Origin: from,
Expand Down
3 changes: 3 additions & 0 deletions x/fungible/keeper/v2_deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/pkg/contracts/testdappv2"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
Expand Down Expand Up @@ -101,6 +102,7 @@ func TestKeeper_ProcessV2Deposit(t *testing.T) {
receiver,
big.NewInt(42),
[]byte{},
coin.CoinType_Gas,
)

// ASSERT
Expand Down Expand Up @@ -135,6 +137,7 @@ func TestKeeper_ProcessV2Deposit(t *testing.T) {
testDapp,
big.NewInt(82),
[]byte("foo"),
coin.CoinType_Gas,
)

// ASSERT
Expand Down
50 changes: 50 additions & 0 deletions x/fungible/keeper/v2_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,53 @@ func (k Keeper) CallDepositAndCallZRC20(
message,
)
}

// CallExecute calls the execute function on the gateway contract
// function execute(
//
// zContext calldata context,
// address zrc20,
// uint256 amount,
// address target,
// bytes calldata message
//
// )
func (k Keeper) CallExecute(
ctx sdk.Context,
context systemcontract.ZContext,
zrc20 common.Address,
amount *big.Int,
target common.Address,
message []byte,
) (*evmtypes.MsgEthereumTxResponse, error) {
gatewayABI, err := gatewayzevm.GatewayZEVMMetaData.GetAbi()
if err != nil {
return nil, err

Check warning on line 89 in x/fungible/keeper/v2_evm.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_evm.go#L86-L89

Added lines #L86 - L89 were not covered by tests
}

systemContract, found := k.GetSystemContract(ctx)
if !found {
return nil, types.ErrSystemContractNotFound

Check warning on line 94 in x/fungible/keeper/v2_evm.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_evm.go#L92-L94

Added lines #L92 - L94 were not covered by tests
}
gatewayAddr := common.HexToAddress(systemContract.Gateway)
if gatewayAddr == (common.Address{}) {
return nil, types.ErrGatewayContractNotSet

Check warning on line 98 in x/fungible/keeper/v2_evm.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_evm.go#L96-L98

Added lines #L96 - L98 were not covered by tests
}

return k.CallEVM(
ctx,
*gatewayABI,
types.ModuleAddressEVM,
gatewayAddr,
BigIntZero,
nil,
true,
false,
"execute",
context,
zrc20,
amount,
target,
message,
)

Check warning on line 116 in x/fungible/keeper/v2_evm.go

View check run for this annotation

Codecov / codecov/patch

x/fungible/keeper/v2_evm.go#L101-L116

Added lines #L101 - L116 were not covered by tests
}
4 changes: 4 additions & 0 deletions zetaclient/chains/evm/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func ParseOutboundTypeFromCCTX(cctx types.CrossChainTx) OutboundTypes {
return OutboundTypeERC20WithdrawAndCall

Check warning on line 57 in zetaclient/chains/evm/cctx.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/cctx.go#L51-L57

Added lines #L51 - L57 were not covered by tests
}
}
case coin.CoinType_NoAssetCall:
if cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
return OutboundTypeCall

Check warning on line 62 in zetaclient/chains/evm/cctx.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/cctx.go#L60-L62

Added lines #L60 - L62 were not covered by tests
}
}

return OutboundTypeUnknown

Check warning on line 66 in zetaclient/chains/evm/cctx.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/cctx.go#L66

Added line #L66 was not covered by tests
Expand Down
17 changes: 13 additions & 4 deletions zetaclient/chains/evm/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,17 @@ func (ob *Observer) ObserveInbound(ctx context.Context, sampledLogger zerolog.Lo
// query the gateway logs
// TODO: refactor in a more declarative design. Example: storing the list of contract and events to listen in an array
// https://github.com/zeta-chain/node/issues/2493
lastScannedGateway, err := ob.ObserveGateway(ctx, startBlock, toBlock)
lastScannedGatewayDeposit, err := ob.ObserveGatewayDeposit(ctx, startBlock, toBlock)

Check warning on line 223 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L223

Added line #L223 was not covered by tests
if err != nil {
ob.Logger().Inbound.Error().
Err(err).
Msgf("ObserveInbound: error observing events from Gateway contract")
Msgf("ObserveInbound: error observing deposit events from Gateway contract")

Check warning on line 227 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L227

Added line #L227 was not covered by tests
}
lastScannedGatewayCall, err := ob.ObserveGatewayCall(ctx, startBlock, toBlock)
if err != nil {
ob.Logger().Inbound.Error().
Err(err).
Msgf("ObserveInbound: error observing call events from Gateway contract")

Check warning on line 233 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L229-L233

Added lines #L229 - L233 were not covered by tests
}

// note: using lowest height for all 3 events is not perfect, but it's simple and good enough
Expand All @@ -235,8 +241,11 @@ func (ob *Observer) ObserveInbound(ctx context.Context, sampledLogger zerolog.Lo
if lastScannedTssRecvd < lastScannedLowest {
lastScannedLowest = lastScannedTssRecvd
}
if lastScannedGateway < lastScannedLowest {
lastScannedLowest = lastScannedGateway
if lastScannedGatewayDeposit < lastScannedLowest {
lastScannedLowest = lastScannedGatewayDeposit

Check warning on line 245 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L244-L245

Added lines #L244 - L245 were not covered by tests
}
if lastScannedGatewayCall < lastScannedLowest {
lastScannedLowest = lastScannedGatewayCall

Check warning on line 248 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L247-L248

Added lines #L247 - L248 were not covered by tests
}

// update last scanned block height for all 3 events (ZetaSent, Deposited, TssRecvd), ignore db error
Expand Down
Loading

0 comments on commit 17e084e

Please sign in to comment.