Skip to content

Commit

Permalink
test(go): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelTxFusion committed Dec 22, 2023
1 parent da2361b commit 617a09c
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 0 deletions.
55 changes: 55 additions & 0 deletions go/test/02_transfer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"github.com/zksync-sdk/zksync2-go/utils"
"math/big"
"testing"
)

func TestTransfer(t *testing.T) {
amount := big.NewInt(7_000_000_000)
receiver := common.HexToAddress(Receiver)

client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

ethClient, err := ethclient.Dial(EthereumProvider)
assert.NoError(t, err, "ethclient.Dial should not return an error")
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient)
assert.NoError(t, err, "NewWallet should not return an error")

balanceBeforeTransferSender, err := wallet.Balance(context.Background(), utils.EthAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

balanceBeforeTransferReceiver, err := client.BalanceAt(context.Background(), receiver, nil)
assert.NoError(t, err, "BalanceAt should not return an error")

tx, err := wallet.Transfer(nil, accounts.TransferTransaction{
To: receiver,
Amount: amount,
Token: utils.EthAddress,
})
assert.NoError(t, err, "Transfer should not return an error")

receipt, err := client.WaitMined(context.Background(), tx.Hash())
assert.NoError(t, err, "client.WaitMined should not return an error")
assert.NotNil(t, receipt.BlockHash, "Transaction should be mined")

balanceAfterTransferSender, err := wallet.Balance(context.Background(), utils.EthAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

balanceAfterTransferReceiver, err := wallet.BalanceL1(nil, utils.EthAddress)
assert.NoError(t, err, "BalanceL1 should not return an error")

assert.True(t, new(big.Int).Sub(balanceBeforeTransferSender, balanceAfterTransferSender).Cmp(amount) >= 0, "Sender balance should be decreased")
assert.True(t, new(big.Int).Sub(balanceAfterTransferReceiver, balanceBeforeTransferReceiver).Cmp(amount) >= 0, "Receiver balance should be increased")
}
54 changes: 54 additions & 0 deletions go/test/03_withdraw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"github.com/zksync-sdk/zksync2-go/utils"
"math/big"
"testing"
)

func TestWithdraw(t *testing.T) {
amount := big.NewInt(7_000_000_000)

client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

ethClient, err := ethclient.Dial(EthereumProvider)
assert.NoError(t, err, "ethclient.Dial should not return an error")
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient)
assert.NoError(t, err, "NewWallet should not return an error")

l2BalanceBeforeWithdrawal, err := wallet.Balance(context.Background(), utils.EthAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

//l1BalanceBeforeWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress)
//assert.NoError(t, err, "BalanceL1 should not return an error")

tx, err := wallet.Withdraw(nil, accounts.WithdrawalTransaction{
To: wallet.Address(),
Amount: amount,
Token: utils.EthAddress,
})
assert.NoError(t, err, "Withdraw should not return an error")

receipt, err := client.WaitMined(context.Background(), tx.Hash())
assert.NoError(t, err, "client.WaitMined should not return an error")
assert.NotNil(t, receipt.BlockHash, "Transaction should be mined")

l2BalanceAfterWithdrawal, err := wallet.Balance(context.Background(), utils.EthAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

//l1BalanceAfterWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress)
//assert.NoError(t, err, "BalanceL1 should not return an error")

assert.True(t, new(big.Int).Sub(l2BalanceBeforeWithdrawal, l2BalanceAfterWithdrawal).Cmp(amount) >= 0, "Balance on L2 should be decreased")
//assert.True(t, new(big.Int).Sub(l1BalanceBeforeDeposit, l1BalanceAfterDeposit).Cmp(amount) >= 0, "Balance on L1 should be decreased")
}
65 changes: 65 additions & 0 deletions go/test/05_deposit_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"math/big"
"testing"
)

func TestDepositToken(t *testing.T) {
tokenData := readToken()
amount := big.NewInt(5)
l1TokenAddress := common.HexToAddress(tokenData.L1Address)
l2TokenAddress := common.HexToAddress(tokenData.L2Address)

client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

ethClient, err := ethclient.Dial(EthereumProvider)
assert.NoError(t, err, "ethclient.Dial should not return an error")
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient)
assert.NoError(t, err, "NewWallet should not return an error")

l2BalanceBeforeDeposit, err := wallet.Balance(context.Background(), l2TokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

l1BalanceBeforeDeposit, err := wallet.BalanceL1(nil, l1TokenAddress)
assert.NoError(t, err, "BalanceL1 should not return an error")

tx, err := wallet.Deposit(nil, accounts.DepositTransaction{
To: wallet.Address(),
Token: l1TokenAddress,
Amount: amount,
ApproveERC20: true,
RefundRecipient: wallet.Address(),
})
assert.NoError(t, err, "Deposit should not return an error")

l1Receipt, err := bind.WaitMined(context.Background(), ethClient, tx)
assert.NoError(t, err, "bind.WaitMined should not return an error")

l2Tx, err := client.L2TransactionFromPriorityOp(context.Background(), l1Receipt)
assert.NoError(t, err, "L2TransactionFromPriorityOp should not return an error")

l2Receipt, err := client.WaitMined(context.Background(), l2Tx.Hash)
assert.NoError(t, err, "bind.WaitMined should not return an error")
assert.NotNil(t, l2Receipt.BlockHash, "Transaction should be mined")

l2BalanceAfterDeposit, err := wallet.Balance(context.Background(), l2TokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

l1BalanceAfterDeposit, err := wallet.BalanceL1(nil, l1TokenAddress)
assert.NoError(t, err, "BalanceL1 should not return an error")

assert.True(t, new(big.Int).Sub(l2BalanceAfterDeposit, l2BalanceBeforeDeposit).Cmp(amount) >= 0, "Balance on L2 should be increased")
assert.True(t, new(big.Int).Sub(l1BalanceBeforeDeposit, l1BalanceAfterDeposit).Cmp(amount) >= 0, "Balance on L1 should be decreased")
}
60 changes: 60 additions & 0 deletions go/test/06_transfer_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"github.com/zksync-sdk/zksync2-go/contracts/erc20"
"math/big"
"testing"
)

func TestTransferToken(t *testing.T) {
tokenData := readToken()
amount := big.NewInt(5)
tokenAddress := common.HexToAddress(tokenData.L2Address)
receiver := common.HexToAddress(Receiver)

client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

ethClient, err := ethclient.Dial(EthereumProvider)
assert.NoError(t, err, "ethclient.Dial should not return an error")
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient)
assert.NoError(t, err, "NewWallet should not return an error")

tokenContract, err := erc20.NewIERC20(tokenAddress, client)
assert.NoError(t, err, "NewIERC20 should not return an error")

balanceBeforeTransferSender, err := wallet.Balance(context.Background(), tokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

balanceBeforeTransferReceiver, err := tokenContract.BalanceOf(nil, receiver)
assert.NoError(t, err, "BalanceOf should not return an error")

tx, err := wallet.Transfer(nil, accounts.TransferTransaction{
To: receiver,
Amount: amount,
Token: tokenAddress,
})
assert.NoError(t, err, "Transfer should not return an error")

receipt, err := client.WaitMined(context.Background(), tx.Hash())
assert.NoError(t, err, "client.WaitMined should not return an error")
assert.NotNil(t, receipt.BlockHash, "Transaction should be mined")

balanceAfterTransferSender, err := wallet.Balance(context.Background(), tokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

balanceAfterTransferReceiver, err := tokenContract.BalanceOf(nil, receiver)
assert.NoError(t, err, "BalanceOf should not return an error")

assert.True(t, new(big.Int).Sub(balanceBeforeTransferSender, balanceAfterTransferSender).Cmp(amount) >= 0, "Sender balance should be decreased")
assert.True(t, new(big.Int).Sub(balanceAfterTransferReceiver, balanceBeforeTransferReceiver).Cmp(amount) >= 0, "Receiver balance should be increased")
}
56 changes: 56 additions & 0 deletions go/test/07_withdraw_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"math/big"
"testing"
)

func TestWithdrawToken(t *testing.T) {
tokenData := readToken()
amount := big.NewInt(5)
//l1TokenAddress := common.HexToAddress(tokenData.L1Address)
l2TokenAddress := common.HexToAddress(tokenData.L2Address)

client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

ethClient, err := ethclient.Dial(EthereumProvider)
assert.NoError(t, err, "ethclient.Dial should not return an error")
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient)
assert.NoError(t, err, "NewWallet should not return an error")

l2BalanceBeforeWithdrawal, err := wallet.Balance(context.Background(), l2TokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

//l1BalanceBeforeWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress)
//assert.NoError(t, err, "BalanceL1 should not return an error")

tx, err := wallet.Withdraw(nil, accounts.WithdrawalTransaction{
To: wallet.Address(),
Amount: amount,
Token: l2TokenAddress,
})
assert.NoError(t, err, "Withdraw should not return an error")

receipt, err := client.WaitMined(context.Background(), tx.Hash())
assert.NoError(t, err, "client.WaitMined should not return an error")
assert.NotNil(t, receipt.BlockHash, "Transaction should be mined")

l2BalanceAfterWithdrawal, err := wallet.Balance(context.Background(), l2TokenAddress, nil)
assert.NoError(t, err, "Balance should not return an error")

//l1BalanceAfterWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress)
//assert.NoError(t, err, "BalanceL1 should not return an error")

assert.True(t, new(big.Int).Sub(l2BalanceBeforeWithdrawal, l2BalanceAfterWithdrawal).Cmp(amount) >= 0, "Balance on L2 should be decreased")
//assert.True(t, new(big.Int).Sub(l1BalanceBeforeDeposit, l1BalanceAfterDeposit).Cmp(amount) >= 0, "Balance on L1 should be decreased")
}
83 changes: 83 additions & 0 deletions go/test/09_deploy_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package test

import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"math/big"
"os"
"testing"
"zksync2-examples/contracts/storage"
)

func TestDeployCreate(t *testing.T) {
client, err := clients.Dial(ZkSyncEraProvider)
defer client.Close()
assert.NoError(t, err, "clients.Dial should not return an error")

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, nil)
assert.NoError(t, err, "NewWallet should not return an error")

bytecode, err := os.ReadFile("../../solidity/storage/build/Storage.zbin")
assert.NoError(t, err, "ReadFile should not return an error")

hash, err := wallet.DeployWithCreate(nil, accounts.CreateTransaction{Bytecode: bytecode})
assert.NoError(t, err, "DeployWithCreate should not return an error")

receipt, err := client.WaitMined(context.Background(), hash)
assert.NoError(t, err, "client.WaitMined should not return an error")

contractAddress := receipt.ContractAddress
assert.NotNil(t, contractAddress, "Contract should be deployed")

storageContract, err := storage.NewStorage(contractAddress, client)
assert.NoError(t, err, "NewStorage should not return an error")

abi, err := storage.StorageMetaData.GetAbi()
assert.NoError(t, err, "GetAbi should not return an error")

setArguments, err := abi.Pack("set", big.NewInt(700))
assert.NoError(t, err, "Pack should not return an error")

result, err := wallet.CallContract(context.Background(), accounts.CallMsg{
To: &contractAddress,
Data: setArguments,
}, nil)
assert.NotNil(t, result, "Result from contract should be non-nil value")

value, err := storageContract.Get(nil)
assert.NoError(t, err, "Get should not return an error")
assert.True(t, value.Cmp(big.NewInt(0)) == 0, "Values should be the same")

opts, err := bind.NewKeyedTransactorWithChainID(wallet.Signer().PrivateKey(), wallet.Signer().Domain().ChainId)
assert.NoError(t, err, "NewKeyedTransactorWithChainID should not return an error")

tx, err := storageContract.Set(opts, big.NewInt(200))
assert.NoError(t, err, "Set should not return an error")

_, err = client.WaitMined(context.Background(), tx.Hash())
assert.NoError(t, err, "client.WaitMined should not return an error")

value, err = storageContract.Get(nil)
assert.NoError(t, err, "Get should not return an error")
assert.True(t, value.Cmp(big.NewInt(200)) == 0, "Values should be the same")

setArguments, err = abi.Pack("set", big.NewInt(500))
assert.NoError(t, err, "Pack should not return an error")

execute, err := wallet.SendTransaction(context.Background(), &accounts.Transaction{
To: &contractAddress,
Data: setArguments,
})
assert.NoError(t, err, "SendTransaction should not return an error")

_, err = client.WaitMined(context.Background(), execute)
assert.NoError(t, err, "client.WaitMined should not return an error")

value, err = storageContract.Get(nil)
assert.NoError(t, err, "Get should not return an error")
assert.True(t, value.Cmp(big.NewInt(500)) == 0, "Values should be the same")
}
Loading

0 comments on commit 617a09c

Please sign in to comment.