Skip to content

Commit

Permalink
initiate impl of Solana restricted address
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Aug 29, 2024
1 parent 26c4fb1 commit 94163a1
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 49 deletions.
4 changes: 2 additions & 2 deletions cmd/zetaclientd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

var InitCmd = &cobra.Command{
Expand Down Expand Up @@ -110,7 +110,7 @@ func Initialize(_ *cobra.Command, _ []string) error {
configData.HsmMode = initArgs.HsmMode
configData.HsmHotKey = initArgs.HsmHotKey
configData.RelayerKeyPath = initArgs.RelayerKeyPath
configData.ComplianceConfig = testutils.ComplianceConfigTest()
configData.ComplianceConfig = sample.ComplianceConfig()

// Save config file
return config.Save(&configData, rootArgs.zetaCoreHome)
Expand Down
2 changes: 2 additions & 0 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestSolanaWithdrawName,
e2etests.TestSolanaDepositAndCallName,
e2etests.TestSolanaDepositAndCallRefundName,
e2etests.TestSolanaDepositRestrictedName,
e2etests.TestSolanaWithdrawRestrictedName,
}
eg.Go(solanaTestRoutine(conf, deployerRunner, verbose, solanaTests...))
}
Expand Down
23 changes: 22 additions & 1 deletion e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2etests

import (
"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/testutil/sample"
)

// List of all e2e test names to be used in zetae2e
Expand Down Expand Up @@ -58,6 +59,8 @@ const (
TestSolanaWithdrawName = "solana_withdraw"
TestSolanaDepositAndCallName = "solana_deposit_and_call"
TestSolanaDepositAndCallRefundName = "solana_deposit_and_call_refund"
TestSolanaDepositRestrictedName = "solana_deposit_restricted"
TestSolanaWithdrawRestrictedName = "solana_withdraw_restricted"

/*
Bitcoin tests
Expand Down Expand Up @@ -372,7 +375,7 @@ var AllE2ETests = []runner.E2ETest{
TestSolanaDepositName,
"deposit SOL into ZEVM",
[]runner.ArgDefinition{
{Description: "amount in lamport", DefaultValue: "1200000"},
{Description: "amount in lamport", DefaultValue: "12000000"},
},
TestSolanaDeposit,
),
Expand Down Expand Up @@ -400,6 +403,24 @@ var AllE2ETests = []runner.E2ETest{
},
TestSolanaDepositAndCallRefund,
),
runner.NewE2ETest(
TestSolanaDepositRestrictedName,
"deposit SOL into ZEVM restricted address",
[]runner.ArgDefinition{
{Description: "receiver", DefaultValue: sample.RestrictedEVMAddressTest},
{Description: "amount in lamport", DefaultValue: "1200000"},
},
TestSolanaDepositRestricted,
),
runner.NewE2ETest(
TestSolanaWithdrawRestrictedName,
"withdraw SOL from ZEVM to restricted address",
[]runner.ArgDefinition{
{Description: "receiver", DefaultValue: sample.RestrictedSolAddressTest},
{Description: "amount in lamport", DefaultValue: "1000000"},
},
TestSolanaWithdrawRestricted,
),
/*
Bitcoin tests
*/
Expand Down
29 changes: 29 additions & 0 deletions e2e/e2etests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
"github.com/zeta-chain/zetacore/pkg/chains"
solanacontracts "github.com/zeta-chain/zetacore/pkg/contracts/solana"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

Expand Down Expand Up @@ -93,6 +96,32 @@ func verifyTransferAmountFromCCTX(r *runner.E2ERunner, cctx *crosschaintypes.Cro
}
}

// verifySolanaWithdrawalAmountFromCCTX verifies the withdrawn amount on Solana for given CCTX
func verifySolanaWithdrawalAmountFromCCTX(r *runner.E2ERunner, cctx *crosschaintypes.CrossChainTx, amount uint64) {
txHash := cctx.GetCurrentOutboundParam().Hash
r.Logger.Info("outbound hash %s", txHash)

// convert txHash to signature
sig, err := solana.SignatureFromBase58(txHash)
require.NoError(r, err)

// query transaction by signature
txResult, err := r.SolanaClient.GetTransaction(r.Ctx, sig, &rpc.GetTransactionOpts{})
require.NoError(r, err)

// unmarshal transaction
tx, err := txResult.Transaction.GetTransaction()
require.NoError(r, err)

// 1st instruction is the withdraw
instruction := tx.Message.Instructions[0]
instWithdrae, err := solanacontracts.ParseInstructionWithdraw(instruction)
require.NoError(r, err)

// verify the amount
require.Equal(r, amount, instWithdrae.TokenAmount(), "withdraw amount is not correct")
}

// Parse helpers ==========================================>

func parseFloat(t require.TestingT, s string) float64 {
Expand Down
4 changes: 2 additions & 2 deletions e2e/e2etests/test_bitcoin_withdraw_restricted_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestBitcoinWithdrawRestricted(r *runner.E2ERunner, args []string) {
Expand All @@ -24,7 +24,7 @@ func TestBitcoinWithdrawRestricted(r *runner.E2ERunner, args []string) {
func withdrawBitcoinRestricted(r *runner.E2ERunner, amount *big.Int) {
// use restricted BTC P2WPKH address
addressRestricted, err := chains.DecodeBtcAddress(
testutils.RestrictedBtcAddressTest,
sample.RestrictedBtcAddressTest,
chains.BitcoinRegtest.ChainId,
)
require.NoError(r, err)
Expand Down
4 changes: 2 additions & 2 deletions e2e/e2etests/test_erc20_deposit_restricted_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestERC20DepositRestricted(r *runner.E2ERunner, args []string) {
Expand All @@ -15,5 +15,5 @@ func TestERC20DepositRestricted(r *runner.E2ERunner, args []string) {
amount := parseBigInt(r, args[0])

// deposit ERC20 to restricted address
r.DepositERC20WithAmountAndMessage(ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest), amount, []byte{})
r.DepositERC20WithAmountAndMessage(ethcommon.HexToAddress(sample.RestrictedEVMAddressTest), amount, []byte{})
}
4 changes: 2 additions & 2 deletions e2e/e2etests/test_eth_withdraw_restricted_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

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

// TestEtherWithdrawRestricted tests the withdrawal to a restricted receiver address
Expand All @@ -31,7 +31,7 @@ func TestEtherWithdrawRestricted(r *runner.E2ERunner, args []string) {
r.Logger.EVMReceipt(*receipt, "approve")

// withdraw
restrictedAddress := ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest)
restrictedAddress := ethcommon.HexToAddress(sample.RestrictedEVMAddressTest)
tx, err = r.ETHZRC20.Withdraw(r.ZEVMAuth, restrictedAddress.Bytes(), withdrawalAmount)
require.NoError(r, err)

Expand Down
21 changes: 21 additions & 0 deletions e2e/e2etests/test_solana_deposit_restricted_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package e2etests

import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
)

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

// parse restricted address
receiverRestricted := ethcommon.HexToAddress(args[0])

// parse deposit amount (in lamports)
depositAmount := parseBigInt(r, args[1])

// execute the deposit transaction
r.SOLDepositAndCall(nil, receiverRestricted, depositAmount, nil)
}
36 changes: 36 additions & 0 deletions e2e/e2etests/test_solana_withdraw_restricted_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package e2etests

import (
"fmt"
"math/big"

"github.com/gagliardetto/solana-go"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/pkg/chains"
)

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

// parse restricted address
receiverRestricted, err := chains.DecodeSolanaWalletAddress(args[0])
require.NoError(r, err, fmt.Sprintf("unable to decode solana wallet address: %s", args[0]))

// parse withdraw amount (in lamports), approve amount is 1 SOL
approvedAmount := new(big.Int).SetUint64(solana.LAMPORTS_PER_SOL)
withdrawAmount := parseBigInt(r, args[1])
require.Equal(
r,
-1,
withdrawAmount.Cmp(approvedAmount),
"Withdrawal amount must be less than the approved amount (1e9).",
)

// withdraw
cctx := r.WithdrawSOLZRC20(receiverRestricted, withdrawAmount, approvedAmount)

// the cctx should be cancelled with zero value
verifySolanaWithdrawalAmountFromCCTX(r, cctx, 0)
}
4 changes: 2 additions & 2 deletions e2e/e2etests/test_zeta_deposit_restricted_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestZetaDepositRestricted(r *runner.E2ERunner, args []string) {
Expand All @@ -15,5 +15,5 @@ func TestZetaDepositRestricted(r *runner.E2ERunner, args []string) {
amount := parseBigInt(r, args[0])

// Deposit amount to restricted address
r.DepositZetaWithAmount(ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest), amount)
r.DepositZetaWithAmount(ethcommon.HexToAddress(sample.RestrictedEVMAddressTest), amount)
}
61 changes: 61 additions & 0 deletions e2e/runner/poste2e_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package runner

import (
"fmt"
"math/big"

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

"github.com/zeta-chain/zetacore/testutil/sample"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

// EnsureNoTrackers ensures that there are no trackers left on zetacore
func (r *E2ERunner) EnsureNoTrackers() {
// get all trackers
res, err := r.CctxClient.OutTxTrackerAll(
r.Ctx,
&crosschaintypes.QueryAllOutboundTrackerRequest{},
)
require.NoError(r, err)
require.Empty(r, res.OutboundTracker, "there should be no trackers at the end of the test")
}

// EnsureZeroBalanceOnRestrictedAddress ensures that the balance of the restricted address is zero
func (r *E2ERunner) EnsureZeroBalanceOnRestrictedAddress() {
restrictedAddress := ethcommon.HexToAddress(sample.RestrictedEVMAddressTest)

// ensure ZETA balance is zero
balance, err := r.WZeta.BalanceOf(&bind.CallOpts{}, restrictedAddress)
require.NoError(r, err)
require.Zero(r, balance.Cmp(big.NewInt(0)), "the wZETA balance of the restricted address should be zero")

// ensure ZRC20 ETH balance is zero
ensureZRC20ZeroBalance(r, r.ETHZRC20, restrictedAddress)

// ensure ZRC20 ERC20 balance is zero
ensureZRC20ZeroBalance(r, r.ERC20ZRC20, restrictedAddress)

// ensure ZRC20 BTC balance is zero
ensureZRC20ZeroBalance(r, r.BTCZRC20, restrictedAddress)

// ensure ZRC20 SOL balance is zero
ensureZRC20ZeroBalance(r, r.SOLZRC20, restrictedAddress)
}

// ensureZRC20ZeroBalance ensures that the balance of the ZRC20 token is zero on given address
func ensureZRC20ZeroBalance(r *E2ERunner, zrc20 *zrc20.ZRC20, address ethcommon.Address) {
balance, err := zrc20.BalanceOf(&bind.CallOpts{}, address)
require.NoError(r, err)

zrc20Name, err := zrc20.Name(&bind.CallOpts{})
require.NoError(r, err)
require.Zero(
r,
balance.Cmp(big.NewInt(0)),
fmt.Sprintf("the balance of address %s should be zero on ZRC20: %s", address, zrc20Name),
)
}
4 changes: 2 additions & 2 deletions e2e/runner/setup_zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *E2ERunner) SetTSSAddresses() error {

// SetZEVMSystemContracts set system contracts for the ZEVM
func (r *E2ERunner) SetZEVMSystemContracts() {
r.Logger.Print("⚙️ deploying system contracts and ZRC20s on ZEVM")
r.Logger.Print("⚙️ deploying system contracts on ZEVM")
startTime := time.Now()
defer func() {
r.Logger.Info("System contract deployments took %s\n", time.Since(startTime))
Expand Down Expand Up @@ -168,7 +168,7 @@ func (r *E2ERunner) SetZEVMSystemContracts() {

// SetZEVMZRC20s set ZRC20 for the ZEVM
func (r *E2ERunner) SetZEVMZRC20s() {
r.Logger.Print("⚙️ deploying system contracts and ZRC20s on ZEVM")
r.Logger.Print("⚙️ deploying ZRC20s on ZEVM")
startTime := time.Now()
defer func() {
r.Logger.Info("System contract deployments took %s\n", time.Since(startTime))
Expand Down
8 changes: 7 additions & 1 deletion e2e/runner/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ func (r *E2ERunner) SOLDepositAndCall(
}

// WithdrawSOLZRC20 withdraws an amount of ZRC20 SOL tokens
func (r *E2ERunner) WithdrawSOLZRC20(to solana.PublicKey, amount *big.Int, approveAmount *big.Int) {
func (r *E2ERunner) WithdrawSOLZRC20(
to solana.PublicKey,
amount *big.Int,
approveAmount *big.Int,
) *crosschaintypes.CrossChainTx {
// approve
tx, err := r.SOLZRC20.Approve(r.ZEVMAuth, r.SOLZRC20Addr, approveAmount)
require.NoError(r, err)
Expand All @@ -165,4 +169,6 @@ func (r *E2ERunner) WithdrawSOLZRC20(to solana.PublicKey, amount *big.Int, appro
// wait for the cctx to be mined
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined)

return cctx
}
18 changes: 0 additions & 18 deletions e2e/runner/trackers.go

This file was deleted.

Loading

0 comments on commit 94163a1

Please sign in to comment.