Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: erc20 auth calls #3012

Merged
merged 18 commits into from
Oct 18, 2024
Merged
1 change: 1 addition & 0 deletions cmd/zetae2e/local/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func startV2Tests(eg *errgroup.Group, conf config.Config, deployerRunner *runner
e2etests.TestV2ERC20DepositName,
e2etests.TestV2ERC20DepositAndCallName,
e2etests.TestV2ERC20WithdrawName,
e2etests.TestV2ERC20WithdrawAndArbitraryCallName,
e2etests.TestV2ERC20WithdrawAndCallName,
))

Expand Down
11 changes: 10 additions & 1 deletion e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const (
TestV2ERC20DepositAndCallRevertName = "v2_erc20_deposit_and_call_revert"
TestV2ERC20DepositAndCallRevertWithCallName = "v2_erc20_deposit_and_call_revert_with_call"
TestV2ERC20WithdrawName = "v2_erc20_withdraw"
TestV2ERC20WithdrawAndArbitraryCallName = "v2_erc20_withdraw_and_arbitrary_call"
TestV2ERC20WithdrawAndCallName = "v2_erc20_withdraw_and_call"
TestV2ERC20WithdrawAndCallRevertName = "v2_erc20_withdraw_and_call_revert"
TestV2ERC20WithdrawAndCallRevertWithCallName = "v2_erc20_withdraw_and_call_revert_with_call"
Expand Down Expand Up @@ -834,9 +835,17 @@ var AllE2ETests = []runner.E2ETest{
},
TestV2ERC20Withdraw,
),
runner.NewE2ETest(
TestV2ERC20WithdrawAndArbitraryCallName,
"withdraw ERC20 from ZEVM and arbitrary call a contract using V2 contract",
[]runner.ArgDefinition{
{Description: "amount", DefaultValue: "1000"},
},
TestV2ERC20WithdrawAndArbitraryCall,
),
runner.NewE2ETest(
TestV2ERC20WithdrawAndCallName,
"withdraw ERC20 from ZEVM and call a contract using V2 contract",
"withdraw ERC20 from ZEVM and authenticated call a contract using V2 contract",
[]runner.ArgDefinition{
{Description: "amount", DefaultValue: "1000"},
},
Expand Down
41 changes: 41 additions & 0 deletions e2e/e2etests/test_v2_erc20_withdraw_and_arbitrary_call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package e2etests

import (
"math/big"

"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayzevm.sol"

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

const payloadMessageWithdrawERC20 = "this is a test ERC20 withdraw and call payload"

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

amount, ok := big.NewInt(0).SetString(args[0], 10)
require.True(r, ok, "Invalid amount specified for TestV2ERC20WithdrawAndCall")
skosito marked this conversation as resolved.
Show resolved Hide resolved

r.AssertTestDAppEVMCalled(false, payloadMessageWithdrawERC20, amount)

r.ApproveERC20ZRC20(r.GatewayZEVMAddr)
r.ApproveETHZRC20(r.GatewayZEVMAddr)
skosito marked this conversation as resolved.
Show resolved Hide resolved

// perform the withdraw
tx := r.V2ERC20WithdrawAndArbitraryCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeERC20Call(r.ERC20Addr, amount, payloadMessageWithdrawERC20),
gatewayzevm.RevertOptions{OnRevertGasLimit: big.NewInt(0)},
)

skosito marked this conversation as resolved.
Show resolved Hide resolved
// wait for the cctx to be mined
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
skosito marked this conversation as resolved.
Show resolved Hide resolved
r.Logger.CCTX(*cctx, "withdraw")
require.Equal(r, crosschaintypes.CctxStatus_OutboundMined, cctx.CctxStatus.Status)

r.AssertTestDAppEVMCalled(true, payloadMessageWithdrawERC20, amount)
}
skosito marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 19 additions & 5 deletions e2e/e2etests/test_v2_erc20_withdraw_and_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayzevm.sol"

Expand All @@ -11,24 +12,29 @@ import (
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
)

const payloadMessageWithdrawERC20 = "this is a test ERC20 withdraw and call payload"
const payloadMessageWithdrawAuthenticatedCallERC20 = "this is a test ERC20 withdraw and authenticated call payload"

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

amount, ok := big.NewInt(0).SetString(args[0], 10)
require.True(r, ok, "Invalid amount specified for TestV2ERC20WithdrawAndCall")

r.AssertTestDAppEVMCalled(false, payloadMessageWithdrawERC20, amount)
r.AssertTestDAppEVMCalled(false, payloadMessageWithdrawAuthenticatedCallERC20, amount)

r.ApproveERC20ZRC20(r.GatewayZEVMAddr)
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// set expected sender
tx, err := r.TestDAppV2EVM.SetExpectedOnCallSender(r.EVMAuth, r.ZEVMAuth.From)
require.NoError(r, err)
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// perform the withdraw
tx := r.V2ERC20WithdrawAndCall(
tx = r.V2ERC20WithdrawAndCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeERC20Call(r.ERC20Addr, amount, payloadMessageWithdrawERC20),
r.EncodeERC20Call(r.ERC20Addr, amount, payloadMessageWithdrawAuthenticatedCallERC20),
gatewayzevm.RevertOptions{OnRevertGasLimit: big.NewInt(0)},
)

Expand All @@ -37,5 +43,13 @@ func TestV2ERC20WithdrawAndCall(r *runner.E2ERunner, args []string) {
r.Logger.CCTX(*cctx, "withdraw")
require.Equal(r, crosschaintypes.CctxStatus_OutboundMined, cctx.CctxStatus.Status)

r.AssertTestDAppEVMCalled(true, payloadMessageWithdrawERC20, amount)
r.AssertTestDAppEVMCalled(true, payloadMessageWithdrawAuthenticatedCallERC20, amount)

// check expected sender was used
senderForMsg, err := r.TestDAppV2EVM.SenderWithMessage(
&bind.CallOpts{},
[]byte(payloadMessageAuthenticatedWithdrawETH),
)
require.NoError(r, err)
require.Equal(r, r.ZEVMAuth.From, senderForMsg)
}
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_erc20_withdraw_and_call_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestV2ERC20WithdrawAndCallRevert(r *runner.E2ERunner, args []string) {
require.EqualValues(r, int64(0), balance.Int64())

// perform the withdraw
tx := r.V2ERC20WithdrawAndCall(
tx := r.V2ERC20WithdrawAndArbitraryCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeERC20CallRevert(r.ERC20Addr, amount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestV2ERC20WithdrawAndCallRevertWithCall(r *runner.E2ERunner, args []string
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// perform the withdraw
tx := r.V2ERC20WithdrawAndCall(
tx := r.V2ERC20WithdrawAndArbitraryCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeERC20CallRevert(r.ERC20Addr, amount),
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_eth_withdraw_and_arbitrary_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestV2ETHWithdrawAndArbitraryCall(r *runner.E2ERunner, args []string) {
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// perform the withdraw
tx := r.V2ETHWithdrawAndCall(
tx := r.V2ETHWithdrawAndArbitraryCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeGasCall(payloadMessageWithdrawETH),
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_eth_withdraw_and_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestV2ETHWithdrawAndCall(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// perform the withdraw
tx = r.V2ETHWithdrawAndAuthenticatedCall(
tx = r.V2ETHWithdrawAndCall(
r.TestDAppV2EVMAddr,
amount,
[]byte(payloadMessageAuthenticatedWithdrawETH),
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_eth_withdraw_and_call_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestV2ETHWithdrawAndCallRevert(r *runner.E2ERunner, args []string) {
require.EqualValues(r, int64(0), balance.Int64())

// perform the withdraw
tx := r.V2ETHWithdrawAndCall(r.TestDAppV2EVMAddr, amount, r.EncodeGasCall("revert"), gatewayzevm.RevertOptions{
tx := r.V2ETHWithdrawAndArbitraryCall(r.TestDAppV2EVMAddr, amount, r.EncodeGasCall("revert"), gatewayzevm.RevertOptions{
RevertAddress: revertAddress,
OnRevertGasLimit: big.NewInt(0),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestV2ETHWithdrawAndCallRevertWithCall(r *runner.E2ERunner, args []string)
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// perform the withdraw
tx := r.V2ETHWithdrawAndCall(
tx := r.V2ETHWithdrawAndArbitraryCall(
r.TestDAppV2EVMAddr,
amount,
r.EncodeGasCall("revert"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestV2ETHWithdrawAndCallThroughContract(r *runner.E2ERunner, args []string)
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// perform the authenticated call
tx = r.V2ETHWithdrawAndAuthenticatedCallThroughContract(gatewayCaller, r.TestDAppV2EVMAddr,
tx = r.V2ETHWithdrawAndCallThroughContract(gatewayCaller, r.TestDAppV2EVMAddr,
amount,
[]byte(payloadMessageAuthenticatedWithdrawETHThroughContract),
gatewayzevmcaller.RevertOptions{OnRevertGasLimit: big.NewInt(0)})
skosito marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -72,7 +72,7 @@ func TestV2ETHWithdrawAndCallThroughContract(r *runner.E2ERunner, args []string)
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// repeat authenticated call through contract, should revert because of wrong sender
tx = r.V2ETHWithdrawAndAuthenticatedCallThroughContract(gatewayCaller, r.TestDAppV2EVMAddr,
tx = r.V2ETHWithdrawAndCallThroughContract(gatewayCaller, r.TestDAppV2EVMAddr,
amount,
[]byte(payloadMessageAuthenticatedWithdrawETHThroughContract),
gatewayzevmcaller.RevertOptions{OnRevertGasLimit: big.NewInt(0)})
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_zevm_to_evm_arbitrary_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestV2ZEVMToEVMArbitraryCall(r *runner.E2ERunner, args []string) {
r.ApproveETHZRC20(r.GatewayZEVMAddr)

// perform the call
tx := r.V2ZEVMToEMVCall(r.TestDAppV2EVMAddr, r.EncodeSimpleCall(payloadMessageEVMCall), gatewayzevm.RevertOptions{
tx := r.V2ZEVMToEMVArbitraryCall(r.TestDAppV2EVMAddr, r.EncodeSimpleCall(payloadMessageEVMCall), gatewayzevm.RevertOptions{
OnRevertGasLimit: big.NewInt(0),
})

Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_v2_zevm_to_evm_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestV2ZEVMToEVMCall(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// perform the authenticated call
tx = r.V2ZEVMToEMVAuthenticatedCall(
tx = r.V2ZEVMToEMVCall(
r.TestDAppV2EVMAddr,
[]byte(payloadMessageEVMAuthenticatedCall),
gatewayzevm.RevertOptions{
Expand Down
4 changes: 2 additions & 2 deletions e2e/e2etests/test_v2_zevm_to_evm_call_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestV2ZEVMToEVMCallThroughContract(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// perform the authenticated call
tx = r.V2ZEVMToEMVAuthenticatedCallThroughContract(
tx = r.V2ZEVMToEMVCallThroughContract(
gatewayCaller,
r.TestDAppV2EVMAddr,
[]byte(payloadMessageEVMAuthenticatedCallThroughContract),
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestV2ZEVMToEVMCallThroughContract(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)

// repeat authenticated call through contract, should revert because of wrong sender
tx = r.V2ZEVMToEMVAuthenticatedCallThroughContract(
tx = r.V2ZEVMToEMVCallThroughContract(
gatewayCaller,
r.TestDAppV2EVMAddr,
[]byte(payloadMessageEVMAuthenticatedCallThroughContract),
Expand Down
65 changes: 47 additions & 18 deletions e2e/runner/v2_zevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ func (r *E2ERunner) V2ETHWithdraw(
return tx
}

// V2ETHWithdrawAndCall calls WithdrawAndCall of Gateway with gas token on ZEVM
func (r *E2ERunner) V2ETHWithdrawAndCall(
// V2ETHWithdrawAndCall calls WithdrawAndCall of Gateway with gas token on ZEVM using arbitrary call
func (r *E2ERunner) V2ETHWithdrawAndArbitraryCall(
receiver ethcommon.Address,
amount *big.Int,
payload []byte,
revertOptions gatewayzevm.RevertOptions,
) *ethtypes.Transaction {
tx, err := r.GatewayZEVM.WithdrawAndCall(
tx, err := r.GatewayZEVM.WithdrawAndCall0(
r.ZEVMAuth,
receiver.Bytes(),
amount,
r.ETHZRC20Addr,
payload,
gasLimit,
gatewayzevm.CallOptions{GasLimit: gasLimit, IsArbitraryCall: true},
revertOptions,
)
require.NoError(r, err)
Expand All @@ -53,13 +53,13 @@ func (r *E2ERunner) V2ETHWithdrawAndCall(
}

// V2ETHWithdrawAndCall calls WithdrawAndCall of Gateway with gas token on ZEVM using authenticated call
func (r *E2ERunner) V2ETHWithdrawAndAuthenticatedCall(
func (r *E2ERunner) V2ETHWithdrawAndCall(
receiver ethcommon.Address,
amount *big.Int,
payload []byte,
revertOptions gatewayzevm.RevertOptions,
) *ethtypes.Transaction {
tx, err := r.GatewayZEVM.WithdrawAndCall2(
tx, err := r.GatewayZEVM.WithdrawAndCall0(
r.ZEVMAuth,
receiver.Bytes(),
amount,
Expand All @@ -78,7 +78,7 @@ func (r *E2ERunner) V2ETHWithdrawAndAuthenticatedCall(

// V2ETHWithdrawAndCall calls WithdrawAndCall of Gateway with gas token on ZEVM using authenticated call
// through contract
func (r *E2ERunner) V2ETHWithdrawAndAuthenticatedCallThroughContract(
func (r *E2ERunner) V2ETHWithdrawAndCallThroughContract(
gatewayZEVMCaller *gatewayzevmcaller.GatewayZEVMCaller,
receiver ethcommon.Address,
amount *big.Int,
Expand Down Expand Up @@ -120,7 +120,36 @@ func (r *E2ERunner) V2ERC20Withdraw(
return tx
}

// V2ERC20WithdrawAndCall calls WithdrawAndCall of Gateway with erc20 token on ZEVM
// V2ERC20WithdrawAndCall calls WithdrawAndCall of Gateway with erc20 token on ZEVM using arbitrary call
func (r *E2ERunner) V2ERC20WithdrawAndArbitraryCall(
receiver ethcommon.Address,
amount *big.Int,
payload []byte,
revertOptions gatewayzevm.RevertOptions,
) *ethtypes.Transaction {
// this function take more gas than default 500k
// so we need to increase the gas limit
previousGasLimit := r.ZEVMAuth.GasLimit
r.ZEVMAuth.GasLimit = 10000000
defer func() {
r.ZEVMAuth.GasLimit = previousGasLimit
}()
skosito marked this conversation as resolved.
Show resolved Hide resolved

tx, err := r.GatewayZEVM.WithdrawAndCall0(
r.ZEVMAuth,
receiver.Bytes(),
amount,
r.ERC20ZRC20Addr,
payload,
gatewayzevm.CallOptions{GasLimit: gasLimit, IsArbitraryCall: true},
revertOptions,
)
require.NoError(r, err)

return tx
}

// V2ERC20WithdrawAndCall calls WithdrawAndCall of Gateway with erc20 token on ZEVM using authenticated call
func (r *E2ERunner) V2ERC20WithdrawAndCall(
receiver ethcommon.Address,
amount *big.Int,
Expand All @@ -135,41 +164,41 @@ func (r *E2ERunner) V2ERC20WithdrawAndCall(
r.ZEVMAuth.GasLimit = previousGasLimit
}()

tx, err := r.GatewayZEVM.WithdrawAndCall(
tx, err := r.GatewayZEVM.WithdrawAndCall0(
r.ZEVMAuth,
receiver.Bytes(),
amount,
r.ERC20ZRC20Addr,
payload,
gasLimit,
gatewayzevm.CallOptions{GasLimit: gasLimit, IsArbitraryCall: false},
revertOptions,
)
require.NoError(r, err)

return tx
}

// V2ZEVMToEMVCall calls Call of Gateway on ZEVM
func (r *E2ERunner) V2ZEVMToEMVCall(
// V2ZEVMToEMVCall calls Call of Gateway on ZEVM using arbitrary call
func (r *E2ERunner) V2ZEVMToEMVArbitraryCall(
receiver ethcommon.Address,
payload []byte,
revertOptions gatewayzevm.RevertOptions,
) *ethtypes.Transaction {
tx, err := r.GatewayZEVM.Call0(
tx, err := r.GatewayZEVM.Call(
r.ZEVMAuth,
receiver.Bytes(),
r.ETHZRC20Addr,
payload,
gasLimit,
gatewayzevm.CallOptions{GasLimit: gasLimit, IsArbitraryCall: true},
revertOptions,
skosito marked this conversation as resolved.
Show resolved Hide resolved
)
require.NoError(r, err)

return tx
}

// V2ZEVMToEMVCall calls authenticated Call of Gateway on ZEVM
func (r *E2ERunner) V2ZEVMToEMVAuthenticatedCall(
// V2ZEVMToEMVCall calls authenticated Call of Gateway on ZEVM using authenticated call
func (r *E2ERunner) V2ZEVMToEMVCall(
receiver ethcommon.Address,
payload []byte,
revertOptions gatewayzevm.RevertOptions,
Expand All @@ -190,8 +219,8 @@ func (r *E2ERunner) V2ZEVMToEMVAuthenticatedCall(
return tx
}

// V2ZEVMToEMVCall calls authenticated Call of Gateway on ZEVM through contract
func (r *E2ERunner) V2ZEVMToEMVAuthenticatedCallThroughContract(
// V2ZEVMToEMVCall calls authenticated Call of Gateway on ZEVM through contract using authenticated call
func (r *E2ERunner) V2ZEVMToEMVCallThroughContract(
gatewayZEVMCaller *gatewayzevmcaller.GatewayZEVMCaller,
receiver ethcommon.Address,
payload []byte,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/zeta-chain/ethermint v0.0.0-20241010181243-044e22bdb7e7
github.com/zeta-chain/keystone/keys v0.0.0-20240826165841-3874f358c138
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20241014093550-f7f6d9fd971a
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20241016134610-c70fec4d66a6
gitlab.com/thorchain/tss/go-tss v1.6.5
go.nhat.io/grpcmock v0.25.0
golang.org/x/crypto v0.23.0
Expand Down
Loading
Loading