From 63a9fe5ebf3b7160c554da073d91fa945161023a Mon Sep 17 00:00:00 2001 From: danijelTxFusion Date: Fri, 22 Dec 2023 19:19:03 +0100 Subject: [PATCH] test: add more tests --- .gitignore | 2 +- scripts/wait.go | 33 --- test/setup_test.go | 26 +++ test/testfiles/Demo.zbin | Bin 0 -> 3488 bytes test/testfiles/Foo.zbin | Bin 0 -> 1824 bytes test/testfiles/Incrementer.zbin | Bin 0 -> 1504 bytes test/testfiles/Storage.zbin | Bin 0 -> 864 bytes test/wallet_test.go | 383 ++++++++++++++------------------ 8 files changed, 195 insertions(+), 249 deletions(-) delete mode 100644 scripts/wait.go create mode 100644 test/testfiles/Demo.zbin create mode 100644 test/testfiles/Foo.zbin create mode 100644 test/testfiles/Incrementer.zbin create mode 100644 test/testfiles/Storage.zbin diff --git a/.gitignore b/.gitignore index 136496c..ab4e8fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea delete-me -test/testfiles/token.json \ No newline at end of file +test/token.json \ No newline at end of file diff --git a/scripts/wait.go b/scripts/wait.go deleted file mode 100644 index 4a5aa62..0000000 --- a/scripts/wait.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "context" - "fmt" - "github.com/zksync-sdk/zksync2-go/clients" - "log" - "time" -) - -const maxAttempts = 30 - -func main() { - nodeURL := "http://localhost:3050" - client, err := clients.Dial(nodeURL) - if err != nil { - log.Fatal(err) - } - defer client.Close() - - for i := 0; i < maxAttempts; i++ { - _, err := client.NetworkID(context.Background()) - if err == nil { - fmt.Println("Node is ready to receive traffic.") - return - } - - fmt.Println("Node not ready yet. Retrying...") - time.Sleep(20 * time.Second) - } - - log.Fatal("Maximum retries exceeded.") -} diff --git a/test/setup_test.go b/test/setup_test.go index 2d2a576..e8649e8 100644 --- a/test/setup_test.go +++ b/test/setup_test.go @@ -15,6 +15,7 @@ import ( "math/big" "os" "testing" + "time" ) const TokenPath = "./token.json" @@ -191,7 +192,32 @@ func createTokenL2(wallet *accounts.Wallet, client clients.Client, ethClient *et return tokenL2Address, tx.Hash(), l2Tx.Hash } +func wait() { + const maxAttempts = 30 + + nodeURL := "http://localhost:3050" + client, err := clients.Dial(nodeURL) + if err != nil { + log.Fatal(err) + } + defer client.Close() + + for i := 0; i < maxAttempts; i++ { + _, err := client.NetworkID(context.Background()) + if err == nil { + log.Println("Node is ready to receive traffic.") + return + } + + log.Println("Node not ready yet. Retrying...") + time.Sleep(20 * time.Second) + } + + log.Fatal("Maximum retries exceeded.") +} + func TestMain(m *testing.M) { + wait() client, err := clients.Dial(ZkSyncEraProvider) if err != nil { diff --git a/test/testfiles/Demo.zbin b/test/testfiles/Demo.zbin new file mode 100644 index 0000000000000000000000000000000000000000..dfab04ae42893784db92780bbe55a90fc61f4a95 GIT binary patch literal 3488 zcmcguPiS0a5TAM9_g<2NWRu2HTWa2#R`g)INfT)-p>GYL0j;LSigdNzZPO+xNUISQ z{KJYOSc9#oRItSq1aBTZNl&eae-0wO=tYWP5%FZfUc{omnQz|O-6U;1=!16h&6{uj z{N^|FmORN5kuwS5m~Mfnx>9r$vf@j8Qs0-|ZdejAB=l6`)5Q3Ts2Bh0_;VOCqk`WK z$(-^D$h(Nnk3foPKZNH3%|pIQR2F^2^F&j*e#XsW5JF-w0mOL1M|l%?>eQ5C+b#Ng??){F@F9-slIJ<#Sb*G%yuO6I6zMx20v2pL->;_zF9X+MM`#}kSJC1Z3S&JO`@@lSC{^PV-o5=U?#E(bsUUiIt1Z`H@% zZ_#^0{B4lYX0=y7qUsX8uIGN!_Xj)P#UF^CdNiMk^$~PYo?~u8KOAd6fJ5G&OmNDq zKh?pe{WBNlCuxHuHa@@qc48|D)9S zUo?GjN&fe8N#`-`$oKDCd_?k(UJi9|x6Y|BLh53s4E(?}hq?HZs3`a_a0gz5^LaT> z86Q$ig?u*%`$uu{oQa%@1#djbgw_rJ3qe0&efQ_&oY}slMf(;2^$jva&s)-$0_U;<)uqP9OtWY zU-r$l-!mFzKM3_S@D=+Ur|pI}vEj@`^)sC)-}w%rGlJiY;U{w_d$WwQ)Zy)WGaKGF z=DV?{@!dZTyBPCm%<$iZWm=Ce5xGeBH@|o~Dvwc&w?;gRAeD4~+ zk1*fDhf0nM{OCNy8-h%FOeTiQ!B8*yF7;A8sQh|3$A$ZXJ-(;Hd${)aEy^`2`l6gr zkKPM<`4QZ(7rR5S>sx`{4DcF&z17i}BZ+w3=Px|Kc=&SfNavB_A2+Rfb8^SKFLiD1 z{P~_Uo3_31MD@0J_MF-|S-JSpH^)vM?|$R$9qWIefAQ?6r;l#!=&JnCHQ!gcD(!7~ zz&EP5==3P@uJn LXZtR!n_c@i#mzp4 literal 0 HcmV?d00001 diff --git a/test/testfiles/Foo.zbin b/test/testfiles/Foo.zbin new file mode 100644 index 0000000000000000000000000000000000000000..550a9af154d2d4567c3c0ac19dd1a61c62c74d46 GIT binary patch literal 1824 zcmb7EJ!n)x5T4oHJ48?)L4%3N?U^)zGkLlE8G_^;8VN?UvCnDHLWHPo3NKhFDkzB9 zC|;u^F(9N8iEvh0S_w9GHex4=7NXzG?voI5pj*6QXTF{NzL|MTOtK8+78}dd1yi@F zxddCGlpl$;DL;T^j>CSX!d6hFubJq&Th<0{vwxP(q@ z@HRfB}Gw*g0%lowf(3BPL0PcFs{HKSkeBent0rY?R0kB#>Uk8W|>MD?&q zRPR;i`HtX?({m-~RUf%WK+HGV(_!@5Bdgq9^ob> z0moi(E$ElLO{Tio1*DdGHljcKi{H9_lZv&H zpc&wf6P=O!M}e!s67CAsUqvNS#C^;^r}Kaygzv`}OV3v#9z*AL!&$LD z_AB(Y1>On#2f^nJ^u@{8YIj=`EoRf&^ zC+L+qydL=v#q;@mk#&KuW1Y`tAr1U!O3&wfJ*us|3%#oAO61)_{l#C3x;*L<=B})B zH;n4m5FbIES4CBO2pWjBg^4fHkCw@3MA_4GDdV z{}UC4Rd2JH1zfS{hJYhFr{}$k{-_ggPfhdNYN!(mCUZFpJ#vq?QLm}0-U{}jw}Sgc zFB#kD(tUWB@BrvOi*r}U+{pY9ckpAFyst}Cc0JCb-i^3dpZBcs g$f@-g*M7L?%ag0;I~QjssQcyETI=1n?R$Uz0mGKVBme*a literal 0 HcmV?d00001 diff --git a/test/testfiles/Incrementer.zbin b/test/testfiles/Incrementer.zbin new file mode 100644 index 0000000000000000000000000000000000000000..e886dfadd5fd01886ca14416177649b92d172912 GIT binary patch literal 1504 zcmcgsziX615S`iG@6v7>Au71>a!LF_OfEgU8&H_vF?gCJ*P5~TyS_3{HYKb2+*&9IoaOVKqDeg(*=>_;rqU+*! z3ipKLp2kN!cLFB*Z`B1;UQ|dpAFjac*Y1k$2IS$K$j!0z2V zjsqqNNMorV`^~}sn%hUY->6IGInT-Ndu*IzeC#^UWa@GLZBzT4b;TT&GOuhW^}BzX z;_jW#!Ke%Mn@^0j>W5Bio~+X|=+{L2%yXSE{oVSBRqcNTe-nSKQf4o&rHc>mX-uwBlEPg%T<1D~d^|1FeefqY1e`e+5tD)~-j;;=Nw~oJk^L}T#J@Fwl Ke!QLOG=gq}cn^t>Y^zf|l}e&e%Oo^p6AEmzn^38@@B@^h zQtDJ15``Y;zBf-i#%fG4XYRRk?!9M*6v1IkbvF*F-;uL_;S&=vJ`6)}NteBm|v z9C}{*)i8Pjv{BxdBBCM5M=BPqvEi1)IMV7l0NM0jUBe0sf``%{Rffyw896UiAM__*#Xp38~MdhXKz2 z|KPW|m*Hm$`NGGiPPDz&I~MZz#?p$jw<=NLV5{*s6VBvp`nJ?{y)J}v7PJeeezNW6 h(dEI}?p5XWx%0BK?@rclx_!5I7pJZJ(I;#6J^(~(LU;fG literal 0 HcmV?d00001 diff --git a/test/wallet_test.go b/test/wallet_test.go index 7dff4dd..e3394f1 100644 --- a/test/wallet_test.go +++ b/test/wallet_test.go @@ -11,6 +11,7 @@ import ( "github.com/zksync-sdk/zksync2-go/contracts/erc20" "github.com/zksync-sdk/zksync2-go/utils" "math/big" + "os" "testing" ) @@ -272,203 +273,178 @@ func TestIntegrationWallet_DepositToken(t *testing.T) { assert.True(t, new(big.Int).Sub(l1BalanceBeforeDeposit, l1BalanceAfterDeposit).Cmp(amount) >= 0, "Balance on L1 should be decreased") } -func TestIntegrationWallet_EstimateGasDeposit(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} +func TestIntegrationWallet_Withdraw(t *testing.T) { + amount := big.NewInt(7_000_000_000) -func TestIntegrationWallet_FullRequiredDepositFee(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + client, err := clients.Dial(ZkSyncEraProvider) + defer client.Close() + assert.NoError(t, err, "clients.Dial should not return an error") -func TestIntegrationWallet_IsWithdrawFinalized(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + ethClient, err := ethclient.Dial(EthereumProvider) + assert.NoError(t, err, "ethclient.Dial should not return an error") + defer ethClient.Close() -// WRITE NEGATIVE TEST -func TestIntegrationWallet_ClaimFailedDeposit(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, ethClient) + assert.NoError(t, err, "NewWallet should not return an error") -func TestIntegrationWallet_RequestExecute(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + l2BalanceBeforeWithdrawal, err := wallet.Balance(context.Background(), utils.EthAddress, nil) + assert.NoError(t, err, "Balance should not return an error") -func TestIntegrationWallet_EstimateGasRequestExecute(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + //l1BalanceBeforeWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress) + //assert.NoError(t, err, "BalanceL1 should not return an error") -func TestIntegrationWallet_Address(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + 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") -func TestIntegrationWallet_Signer(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + 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") -func TestIntegrationWallet_Balance(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + l2BalanceAfterWithdrawal, err := wallet.Balance(context.Background(), utils.EthAddress, nil) + assert.NoError(t, err, "Balance should not return an error") -func TestIntegrationWallet_AllBalances(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + //l1BalanceAfterWithdrawal, err := wallet.BalanceL1(nil, l1TokenAddress) + //assert.NoError(t, err, "BalanceL1 should not return an error") -func TestIntegrationWallet_L2BridgeContracts(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") + 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") } -func TestIntegrationWallet_Withdraw(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} +func TestIntegrationWallet_WithdrawToken(t *testing.T) { + tokenData := readToken() + amount := big.NewInt(5) + //l1TokenAddress := common.HexToAddress(tokenData.L1Address) + l2TokenAddress := common.HexToAddress(tokenData.L2Address) -func TestIntegrationWallet_EstimateGasWithdraw(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") + 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") } func TestIntegrationWallet_Transfer(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + amount := big.NewInt(7_000_000_000) + receiver := common.HexToAddress(Receiver) -func TestIntegrationWallet_EstimateGasTransfer(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + client, err := clients.Dial(ZkSyncEraProvider) + defer client.Close() + assert.NoError(t, err, "clients.Dial should not return an error") -func TestIntegrationWallet_CallContract(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + 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") -func TestIntegrationWallet_PopulateTransaction(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") + 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") } -func TestIntegrationWallet_SignTransaction(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") +func TestIntegrationWallet_TransferToken(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") } func TestIntegrationWallet_SendTransaction(t *testing.T) { @@ -500,46 +476,23 @@ func TestIntegrationWallet_SendTransaction(t *testing.T) { assert.NotNil(t, txReceipt.BlockHash, "Transaction should be mined") } -func TestIntegrationWallet_Deploy(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} - func TestIntegrationWallet_DeployWithCreate(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + client, err := clients.Dial(ZkSyncEraProvider) + defer client.Close() + assert.NoError(t, err, "clients.Dial should not return an error") -func TestIntegrationWallet_DeployAccount(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") -} + wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, nil) + assert.NoError(t, err, "NewWallet should not return an error") + + bytecode, err := os.ReadFile("./testfiles/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") -func TestIntegrationWallet_DeployAccountWithCreate(t *testing.T) { - //client, err := clients.Dial(ZkSyncEraProvider) - //defer client.Close() - //assert.NoError(t, err, "clients.Dial should not return an error") - // - //block, err := client.BlockByNumber(context.Background(), nil) - // - //assert.NoError(t, err, "BlockByNumber should not return an error") - //assert.NotNil(t, block, "BlockByNumber should return a non-nil block") + contractAddress := receipt.ContractAddress + assert.NotNil(t, contractAddress, "Contract should be deployed") }