Prepare txs before sending
-import { BigNumberish, ethers } from 'ethers';
+import { BigNumber, BigNumberish, ethers } from 'ethers';
import { EthMessageSigner } from './eth-message-signer';
import { SyncProvider } from './provider-interface';
import { BatchBuilderInternalTx } from './batch-builder';
@@ -378,6 +378,36 @@ export class RemoteWallet extends AbstractWallet {
// Internal methods
//
+ /**
+ *
+ * Makes all fields that represent amount to be of `string` type
+ * and all fields that represent tokens to be token ids i.e. of `number` type.
+ *
+ * @param txs A list of transactions
+ *
+ * @protected A list of prepared transactions
+ */
+ protected prepareTxsBeforeSending(txs: any[]): any[] {
+ const amountFields = ['amount', 'fee'];
+ const tokenFields = ['token', 'feeToken', 'tokenSell', 'tokenBuy'];
+ return txs.map((tx) => {
+ for (const field of amountFields) {
+ if (field in tx) {
+ tx[field] = BigNumber.from(tx[field]).toString();
+ }
+ }
+ for (const field of tokenFields) {
+ if (field in tx) {
+ tx[field] = this.provider.tokenSet.resolveTokenId(tx[field]);
+ }
+ }
+ if ('amounts' in tx) {
+ tx.amounts = [BigNumber.from(tx.amounts[0]).toString(), BigNumber.from(tx.amounts[1]).toString()];
+ }
+ return tx;
+ });
+ }
+
/**
* Performs an RPC call to the custom `zkSync_signBatch` method.
* This method is specified here: https://github.com/argentlabs/argent-contracts-l2/discussions/4
@@ -392,10 +422,11 @@ export class RemoteWallet extends AbstractWallet {
*/
protected async callExtSignZkSyncBatch(txs: any[]): Promise<SignedTransaction[]> {
try {
+ const preparedTxs = this.prepareTxsBeforeSending(txs);
// Response must be an array of signed transactions.
// Transactions are flattened (ethereum signatures are on the same level as L2 signatures),
// so we need to "unflat" each one.
- const response: any[] = await this.web3Provider.send('zkSync_signBatch', [txs]);
+ const response: any[] = await this.web3Provider.send('zkSync_signBatch', [preparedTxs]);
const transactions = response.map((tx) => {
const ethereumSignature = tx['ethereumSignature'];
@@ -423,8 +454,9 @@ export class RemoteWallet extends AbstractWallet {
*/
protected async callExtSignOrder(order: any): Promise<Order> {
try {
+ const preparedOrder = this.prepareTxsBeforeSending([order]);
// For now, we assume that the same method will be used for both signing transactions and orders.
- const signedOrder: any = await this.web3Provider.send('zkSync_signBatch', [order]);
+ const signedOrder: any = (await this.web3Provider.send('zkSync_signBatch', preparedOrder))[0];
// Sanity check
if (!signedOrder['signature']) {
