Skip to content

Commit

Permalink
Make unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
perekopskiy committed Feb 16, 2021
1 parent d16648f commit 0c7f653
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
2 changes: 1 addition & 1 deletion changelog/js-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to `zksync.js` will be documented in this file.

### Added

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

### Changed

Expand Down
4 changes: 0 additions & 4 deletions core/tests/ts-tests/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ describe(`ZkSync integration tests (token: ${token}, transport: ${transport})`,
await tester.testCreate2BatchFail(hilda, david, token, TX_AMOUNT);
});
});

it('should test GetHashOfTx function', async () => {
await tester.testGetHashOfTx(alice, bob, token, TX_AMOUNT);
});
});

// wBTC is chosen because it has decimals different from ETH (8 instead of 18).
Expand Down
17 changes: 1 addition & 16 deletions core/tests/ts-tests/tests/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tester } from './tester';
import { expect } from 'chai';
import { Wallet, types, utils } from 'zksync';
import { Wallet, types } from 'zksync';
import { BigNumber, ethers } from 'ethers';
import { SignedTransaction, TxEthSignature } from 'zksync/build/types';
import { submitSignedTransactionsBatch } from 'zksync/build/wallet';
Expand All @@ -14,7 +14,6 @@ declare module './tester' {
testMultipleBatchSigners(wallets: Wallet[], token: TokenLike, amount: BigNumber): Promise<void>;
testMultipleWalletsWrongSignature(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
testBackwardCompatibleEthMessages(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
testGetHashOfTx(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise<void>;
}
}

Expand Down Expand Up @@ -237,17 +236,3 @@ Tester.prototype.testBackwardCompatibleEthMessages = async function (
await Promise.all(handles.map((handle) => handle.awaitReceipt()));
this.runningFee = this.runningFee.add(totalFee);
};

Tester.prototype.testGetHashOfTx = async function (from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber) {
const signedTransfer = await from.signSyncTransfer({
to: to.address(),
token: token,
amount,
fee: amount.div(2),
nonce: await from.getNonce()
});
let hashFromSubmitTx = await from.provider.submitTx(signedTransfer.tx, signedTransfer.ethereumSignature);
let hashFromGetHash = utils.getHashOfTx(signedTransfer.tx);
expect(hashFromSubmitTx === hashFromGetHash, 'Hashes from submitTx and getHashOfTx functions should be equal').to.be
.true;
};
2 changes: 1 addition & 1 deletion sdk/zksync.js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ export async function getPendingBalance(
return zksyncContract.getPendingBalance(address, tokenAddress);
}

export function getHashOfTx(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string {
export function getTxHash(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string {
if (tx.type == 'Close') {
throw new Error('Close operation is disabled');
}
Expand Down
80 changes: 79 additions & 1 deletion sdk/zksync.js/tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
closestPackableTransactionFee,
isTransactionAmountPackable,
isTransactionFeePackable,
TokenSet
TokenSet,
getTxHash
} from '../src/utils';
import { Transfer, ChangePubKey, Withdraw, ForcedExit } from '../src/types';
import { BigNumber } from 'ethers';

describe('Packing and unpacking', function () {
Expand Down Expand Up @@ -66,3 +68,79 @@ describe('Token cache resolve', function () {
expect(() => tokenCache.resolveTokenId('ERC20-2')).to.throw();
});
});

describe('Test getTxHash', function () {
it('Test Transfer', async function () {
const transfer = {
type: 'Transfer',
accountId: 123,
from: '0xdddddddddddddddddddddddddddddddddddddddd',
to: '0xeddddddddddddddddddddddddddddddddddddddd',
token: 0,
amount: 23,
fee: 88,
nonce: 123,
validFrom: 12,
validUntil: 1232321
};
const transferHash = getTxHash(transfer as Transfer);
expect(
'sync-tx:9aa2460771722dfc15fc371e11d8412b63acdd0a483b888336234fc4b825b00b' === transferHash,
'Incorrect transfer hash'
).to.be.true;
});
it('Test Withdraw', async function () {
const withdraw = {
type: 'Withdraw',
accountId: 1,
from: '0xddddddddddddddddddddddddddddddddddddddde',
to: '0xadddddddddddddddddddddddddddddddddddddde',
token: 12,
amount: '123',
fee: '897',
nonce: 1,
validFrom: 90809,
validUntil: 873712938
};
const withdrawHash = getTxHash(withdraw as Withdraw);
expect(
'sync-tx:84365ebb70259b8f6d6d9729e660f1ea9ecb2dbeeefd449bed54ac144d80a315' === withdrawHash,
'Incorrect withdrawal hash'
).to.be.true;
});
it('Test ChangePubKey', async function () {
const changePubKey = {
type: 'ChangePubKey',
accountId: 2,
account: '0xaddddddddddddddddddddddddddddddddddddd0e',
newPkHash: '0xadddddddd1234ddddddddddddddddddddddddd0e',
feeToken: 20,
fee: 98,
nonce: 32,
validFrom: 177,
validUntil: 52443
};
const changePubKeyHash = getTxHash(changePubKey as ChangePubKey);
expect(
'sync-tx:486629437f43e9d9383431e2d075ba194d9e549c08b03db234ca4edaebb2200f' === changePubKeyHash,
'Incorrect changePubKey hash'
).to.be.true;
});
it('Test ForcedExit', async function () {
const forcedExit = {
type: 'ForcedExit',
initiatorAccountId: 776,
target: '0xadddddddd1234ddddd777ddddddddddddddddd0e',
token: 5,
fee: 123,
nonce: 5,
validFrom: 8978,
validUntil: 57382678
};
const forcedExitHash = getTxHash(forcedExit as ForcedExit);
expect(
'sync-tx:0f5cba03550d1ab984d6f478c79aeb6f6961873df7a5876c9af4502364163d03' === forcedExitHash,
'Incorrect forcedExit hash'
).to.be.true;
});
});

0 comments on commit 0c7f653

Please sign in to comment.