Skip to content

Commit

Permalink
add util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Mar 19, 2020
1 parent d0a65bc commit d20ca0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions js/zksync.js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export const IERC20_INTERFACE = new utils.Interface(
export const SYNC_MAIN_CONTRACT_INTERFACE = new utils.Interface(
require("../abi/SyncMain.json").interface
);
export const SYNC_PRIOR_QUEUE_INTERFACE = new utils.Interface(
require("../abi/SyncPriorityQueue.json").interface
);

export const SYNC_GOV_CONTRACT_INTERFACE = new utils.Interface(
require("../abi/SyncGov.json").interface
Expand Down Expand Up @@ -220,6 +217,12 @@ export function closestPackableTransactionAmount(
);
}

export function isTransactionAmountPackable(
amount: utils.BigNumberish
): boolean {
return closestPackableTransactionAmount(amount).eq(amount);
}

/**
* packs and unpacks the amount, returning the closest packed value.
* e.g 1000000003 => 1000000000
Expand All @@ -240,6 +243,10 @@ export function closestPackableTransactionFee(
);
}

export function isTransactionFeePackable(amount: utils.BigNumberish): boolean {
return closestPackableTransactionAmount(amount).eq(amount);
}

export function buffer2bitsLE(buff) {
const res = new Array(buff.length * 8);
for (let i = 0; i < buff.length; i++) {
Expand Down
11 changes: 11 additions & 0 deletions js/zksync.js/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ export class Wallet {
throw new Error("Current signing key is set already");
}

const isAccountInTheTree = await this.getAccountId();
if (isAccountInTheTree === undefined) {
throw new Error(
"Account should exits in the ZK Sync network before setting signing key"
);
}

const numNonce = await this.getNonce(nonce);
const ethSignature = onchainAuth
? null
Expand Down Expand Up @@ -274,6 +281,10 @@ export class Wallet {
}
}

async getAccountId(): Promise<number | undefined> {
return (await this.provider.getState(this.address())).id;
}

address(): Address {
return this.cachedAddress;
}
Expand Down
10 changes: 10 additions & 0 deletions js/zksync.js/tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import BN = require("bn.js");
import {
closestPackableTransactionAmount,
closestPackableTransactionFee,
isTransactionAmountPackable,
isTransactionFeePackable,
TokenSet
} from "../src/utils";
import { pedersenHash } from "../src/crypto";
Expand All @@ -22,9 +24,17 @@ describe("Packing and unpacking", function() {
expect(
closestPackableTransactionFee(bigNumberAmount).toString()
).equal(bigNumberAmount.toString(), "fee packing");
expect(
isTransactionAmountPackable(bigNumberAmount),
"check amount pack"
).eq(true);
expect(
closestPackableTransactionAmount(bigNumberAmount).toString()
).equal(bigNumberAmount.toString(), "amount packing");
expect(
isTransactionFeePackable(bigNumberAmount),
"check fee pack"
).eq(true);
}
});
});
Expand Down

0 comments on commit d20ca0f

Please sign in to comment.