Implement serializers for transactions
import { utils, constants, ethers, BigNumber, BigNumberish } from 'ethers';
-import { PubKeyHash, TokenAddress, TokenLike, Tokens, TokenSymbol, EthSignerType, Address, Transfer } from './types';
+import {
+ PubKeyHash,
+ TokenAddress,
+ TokenLike,
+ Tokens,
+ TokenSymbol,
+ EthSignerType,
+ Address,
+ Transfer,
+ ForcedExit,
+ ChangePubKey,
+ Withdraw
+} from './types';
// Max number of tokens for the current version, it is determined by the zkSync circuit implementation.
const MAX_NUMBER_OF_TOKENS = 128;
@@ -433,6 +445,27 @@ export function serializeNonce(nonce: number): Uint8Array {
return numberToBytesBE(nonce, 4);
}
+export function serializeWithdraw(withdraw: Withdraw): Uint8Array {
+ const type = new Uint8Array([3]);
+ const accountId = serializeAccountId(withdraw.accountId);
+ const accountBytes = serializeAddress(withdraw.from);
+ const ethAddressBytes = serializeAddress(withdraw.to);
+ const tokenIdBytes = serializeTokenId(withdraw.token);
+ const amountBytes = serializeAmountFull(withdraw.amount);
+ const feeBytes = serializeFeePacked(withdraw.fee);
+ const nonceBytes = serializeNonce(withdraw.nonce);
+ return ethers.utils.concat([
+ type,
+ accountId,
+ accountBytes,
+ ethAddressBytes,
+ tokenIdBytes,
+ amountBytes,
+ feeBytes,
+ nonceBytes
+ ]);
+}
+
export function serializeTransfer(transfer: Transfer): Uint8Array {
const type = new Uint8Array([5]); // tx type
const accountId = serializeAccountId(transfer.accountId);
@@ -445,6 +478,35 @@ export function serializeTransfer(transfer: Transfer): Uint8Array {
return ethers.utils.concat([type, accountId, from, to, token, amount, fee, nonce]);
}
+export function serializeChangePubKey(changePubKey: ChangePubKey): Uint8Array {
+ const type = new Uint8Array([7]);
+ const accountIdBytes = serializeAccountId(changePubKey.accountId);
+ const accountBytes = serializeAddress(changePubKey.account);
+ const pubKeyHashBytes = serializeAddress(changePubKey.newPkHash);
+ const tokenIdBytes = serializeTokenId(changePubKey.feeToken);
+ const feeBytes = serializeFeePacked(changePubKey.fee);
+ const nonceBytes = serializeNonce(changePubKey.nonce);
+ return ethers.utils.concat([
+ type,
+ accountIdBytes,
+ accountBytes,
+ pubKeyHashBytes,
+ tokenIdBytes,
+ feeBytes,
+ nonceBytes
+ ]);
+}
+
+export function serializeForcedExit(forcedExit: ForcedExit): Uint8Array {
+ const type = new Uint8Array([8]);
+ const initiatorAccountIdBytes = serializeAccountId(forcedExit.initiatorAccountId);
+ const targetBytes = serializeAddress(forcedExit.target);
+ const tokenIdBytes = serializeTokenId(forcedExit.token);
+ const feeBytes = serializeFeePacked(forcedExit.fee);
+ const nonceBytes = serializeNonce(forcedExit.nonce);
+ return ethers.utils.concat([type, initiatorAccountIdBytes, targetBytes, tokenIdBytes, feeBytes, nonceBytes]);
+}
+
function numberToBytesBE(number: number, bytes: number): Uint8Array {
const result = new Uint8Array(bytes);
for (let i = bytes - 1; i >= 0; i--) {
