Skip to content

Commit 0c7f653

Browse files
committed
Make unit tests
1 parent d16648f commit 0c7f653

File tree

5 files changed

+82
-23
lines changed

5 files changed

+82
-23
lines changed

changelog/js-sdk.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ All notable changes to `zksync.js` will be documented in this file.
66

77
### Added
88

9-
- `getHashOfTx` function and a test for it.
9+
- Method for calculation of transaction hash.
1010

1111
### Changed
1212

core/tests/ts-tests/tests/main.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ describe(`ZkSync integration tests (token: ${token}, transport: ${transport})`,
254254
await tester.testCreate2BatchFail(hilda, david, token, TX_AMOUNT);
255255
});
256256
});
257-
258-
it('should test GetHashOfTx function', async () => {
259-
await tester.testGetHashOfTx(alice, bob, token, TX_AMOUNT);
260-
});
261257
});
262258

263259
// wBTC is chosen because it has decimals different from ETH (8 instead of 18).

core/tests/ts-tests/tests/misc.ts

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Tester } from './tester';
22
import { expect } from 'chai';
3-
import { Wallet, types, utils } from 'zksync';
3+
import { Wallet, types } from 'zksync';
44
import { BigNumber, ethers } from 'ethers';
55
import { SignedTransaction, TxEthSignature } from 'zksync/build/types';
66
import { submitSignedTransactionsBatch } from 'zksync/build/wallet';
@@ -14,7 +14,6 @@ declare module './tester' {
1414
testMultipleBatchSigners(wallets: Wallet[], token: TokenLike, amount: BigNumber): Promise<void>;
1515
testMultipleWalletsWrongSignature(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
1616
testBackwardCompatibleEthMessages(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
17-
testGetHashOfTx(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
1817
}
1918
}
2019

@@ -237,17 +236,3 @@ Tester.prototype.testBackwardCompatibleEthMessages = async function (
237236
await Promise.all(handles.map((handle) => handle.awaitReceipt()));
238237
this.runningFee = this.runningFee.add(totalFee);
239238
};
240-
241-
Tester.prototype.testGetHashOfTx = async function (from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber) {
242-
const signedTransfer = await from.signSyncTransfer({
243-
to: to.address(),
244-
token: token,
245-
amount,
246-
fee: amount.div(2),
247-
nonce: await from.getNonce()
248-
});
249-
let hashFromSubmitTx = await from.provider.submitTx(signedTransfer.tx, signedTransfer.ethereumSignature);
250-
let hashFromGetHash = utils.getHashOfTx(signedTransfer.tx);
251-
expect(hashFromSubmitTx === hashFromGetHash, 'Hashes from submitTx and getHashOfTx functions should be equal').to.be
252-
.true;
253-
};

sdk/zksync.js/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ export async function getPendingBalance(
728728
return zksyncContract.getPendingBalance(address, tokenAddress);
729729
}
730730

731-
export function getHashOfTx(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string {
731+
export function getTxHash(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string {
732732
if (tx.type == 'Close') {
733733
throw new Error('Close operation is disabled');
734734
}

sdk/zksync.js/tests/util.test.ts

+79-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
closestPackableTransactionFee,
77
isTransactionAmountPackable,
88
isTransactionFeePackable,
9-
TokenSet
9+
TokenSet,
10+
getTxHash
1011
} from '../src/utils';
12+
import { Transfer, ChangePubKey, Withdraw, ForcedExit } from '../src/types';
1113
import { BigNumber } from 'ethers';
1214

1315
describe('Packing and unpacking', function () {
@@ -66,3 +68,79 @@ describe('Token cache resolve', function () {
6668
expect(() => tokenCache.resolveTokenId('ERC20-2')).to.throw();
6769
});
6870
});
71+
72+
describe('Test getTxHash', function () {
73+
it('Test Transfer', async function () {
74+
const transfer = {
75+
type: 'Transfer',
76+
accountId: 123,
77+
from: '0xdddddddddddddddddddddddddddddddddddddddd',
78+
to: '0xeddddddddddddddddddddddddddddddddddddddd',
79+
token: 0,
80+
amount: 23,
81+
fee: 88,
82+
nonce: 123,
83+
validFrom: 12,
84+
validUntil: 1232321
85+
};
86+
const transferHash = getTxHash(transfer as Transfer);
87+
expect(
88+
'sync-tx:9aa2460771722dfc15fc371e11d8412b63acdd0a483b888336234fc4b825b00b' === transferHash,
89+
'Incorrect transfer hash'
90+
).to.be.true;
91+
});
92+
it('Test Withdraw', async function () {
93+
const withdraw = {
94+
type: 'Withdraw',
95+
accountId: 1,
96+
from: '0xddddddddddddddddddddddddddddddddddddddde',
97+
to: '0xadddddddddddddddddddddddddddddddddddddde',
98+
token: 12,
99+
amount: '123',
100+
fee: '897',
101+
nonce: 1,
102+
validFrom: 90809,
103+
validUntil: 873712938
104+
};
105+
const withdrawHash = getTxHash(withdraw as Withdraw);
106+
expect(
107+
'sync-tx:84365ebb70259b8f6d6d9729e660f1ea9ecb2dbeeefd449bed54ac144d80a315' === withdrawHash,
108+
'Incorrect withdrawal hash'
109+
).to.be.true;
110+
});
111+
it('Test ChangePubKey', async function () {
112+
const changePubKey = {
113+
type: 'ChangePubKey',
114+
accountId: 2,
115+
account: '0xaddddddddddddddddddddddddddddddddddddd0e',
116+
newPkHash: '0xadddddddd1234ddddddddddddddddddddddddd0e',
117+
feeToken: 20,
118+
fee: 98,
119+
nonce: 32,
120+
validFrom: 177,
121+
validUntil: 52443
122+
};
123+
const changePubKeyHash = getTxHash(changePubKey as ChangePubKey);
124+
expect(
125+
'sync-tx:486629437f43e9d9383431e2d075ba194d9e549c08b03db234ca4edaebb2200f' === changePubKeyHash,
126+
'Incorrect changePubKey hash'
127+
).to.be.true;
128+
});
129+
it('Test ForcedExit', async function () {
130+
const forcedExit = {
131+
type: 'ForcedExit',
132+
initiatorAccountId: 776,
133+
target: '0xadddddddd1234ddddd777ddddddddddddddddd0e',
134+
token: 5,
135+
fee: 123,
136+
nonce: 5,
137+
validFrom: 8978,
138+
validUntil: 57382678
139+
};
140+
const forcedExitHash = getTxHash(forcedExit as ForcedExit);
141+
expect(
142+
'sync-tx:0f5cba03550d1ab984d6f478c79aeb6f6961873df7a5876c9af4502364163d03' === forcedExitHash,
143+
'Incorrect forcedExit hash'
144+
).to.be.true;
145+
});
146+
});

0 commit comments

Comments
 (0)