diff --git a/core/tests/ts-tests/tests/swap.ts b/core/tests/ts-tests/tests/swap.ts index e7914bf64d..c70239557b 100644 --- a/core/tests/ts-tests/tests/swap.ts +++ b/core/tests/ts-tests/tests/swap.ts @@ -40,7 +40,7 @@ Tester.prototype.testSwapNFT = async function ( tokenSell: token, tokenBuy: nft, amount, - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: amount, tokenBuy: 1 }) @@ -50,7 +50,7 @@ Tester.prototype.testSwapNFT = async function ( tokenSell: nft, tokenBuy: token, amount: 1, - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: 1, tokenBuy: amount }) @@ -85,7 +85,7 @@ Tester.prototype.testSwap = async function ( tokenSell: tokenA, tokenBuy: tokenB, amount, - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: 1, tokenBuy: 2 }) @@ -95,7 +95,7 @@ Tester.prototype.testSwap = async function ( tokenSell: tokenB, tokenBuy: tokenA, amount: amount.mul(2), - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: 2, tokenBuy: 1 }) @@ -148,7 +148,7 @@ Tester.prototype.testSwapBatch = async function ( const orderA = await walletA.getLimitOrder({ tokenSell: tokenA, tokenBuy: tokenB, - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: 2, tokenBuy: 5 }) @@ -157,7 +157,7 @@ Tester.prototype.testSwapBatch = async function ( const orderB = await walletB.getLimitOrder({ tokenSell: tokenB, tokenBuy: tokenA, - ratio: utils.ratio({ + ratio: utils.weiRatio({ tokenSell: 4, tokenBuy: 1 }) diff --git a/sdk/zksync.js/src/types.ts b/sdk/zksync.js/src/types.ts index 4a10ade1c6..c26d700c41 100644 --- a/sdk/zksync.js/src/types.ts +++ b/sdk/zksync.js/src/types.ts @@ -102,6 +102,18 @@ export interface Signature { export type Ratio = [BigNumberish, BigNumberish]; +export type TokenRatio = { + type: 'Token'; + [token: string]: string; + [token: number]: string; +}; + +export type WeiRatio = { + type: 'Wei'; + [token: string]: BigNumberish; + [token: number]: BigNumberish; +}; + export interface Order { accountId: number; recipient: Address; diff --git a/sdk/zksync.js/src/utils.ts b/sdk/zksync.js/src/utils.ts index 550cee1b89..1a619ee99e 100644 --- a/sdk/zksync.js/src/utils.ts +++ b/sdk/zksync.js/src/utils.ts @@ -16,7 +16,8 @@ import { MintNFT, Order, Swap, - Ratio, + TokenRatio, + WeiRatio, WithdrawNFT } from './types'; import { rescueHashOrders } from './crypto'; @@ -58,8 +59,18 @@ const AMOUNT_MANTISSA_BIT_WIDTH = 35; const FEE_EXPONENT_BIT_WIDTH = 5; const FEE_MANTISSA_BIT_WIDTH = 11; -export function ratio(price: { tokenSell: BigNumberish; tokenBuy: BigNumberish }): Ratio { - return [BigNumber.from(price.tokenSell), BigNumber.from(price.tokenBuy)]; +export function tokenRatio(ratio: { [token: string]: string; [token: number]: string }): TokenRatio { + return { + type: 'Token', + ...ratio + }; +} + +export function weiRatio(ratio: { [token: string]: BigNumberish; [token: number]: BigNumberish }): WeiRatio { + return { + type: 'Wei', + ...ratio + }; } export function floatToInteger( diff --git a/sdk/zksync.js/src/wallet.ts b/sdk/zksync.js/src/wallet.ts index 6d6c4765a7..23b9e0cdf5 100644 --- a/sdk/zksync.js/src/wallet.ts +++ b/sdk/zksync.js/src/wallet.ts @@ -29,7 +29,9 @@ import { Transfer, TxEthSignature, Withdraw, - WithdrawNFT + WithdrawNFT, + TokenRatio, + WeiRatio } from './types'; import { ERC20_APPROVE_TRESHOLD, @@ -427,7 +429,7 @@ export class Wallet { async getLimitOrder(order: { tokenSell: TokenLike; tokenBuy: TokenLike; - ratio: Ratio; + ratio: TokenRatio | WeiRatio; recipient?: Address; nonce?: Nonce; validFrom?: number; @@ -442,7 +444,7 @@ export class Wallet { async getOrder(order: { tokenSell: TokenLike; tokenBuy: TokenLike; - ratio: Ratio; + ratio: TokenRatio | WeiRatio; amount: BigNumberish; recipient?: Address; nonce?: Nonce; @@ -456,6 +458,21 @@ export class Wallet { const nonce = order.nonce != null ? await this.getNonce(order.nonce) : await this.getNonce(); const recipient = order.recipient || this.address(); + let ratio: Ratio; + const sell = order.tokenSell; + const buy = order.tokenBuy; + + if (order.ratio.type == 'Wei') { + ratio = [order.ratio[sell], order.ratio[buy]]; + } else if (order.ratio.type == 'Token') { + ratio = [ + isNFT(sell) + ? BigNumber.from(order.ratio[sell]) + : this.provider.tokenSet.parseToken(sell, order.ratio[sell]), + isNFT(buy) ? BigNumber.from(order.ratio[buy]) : this.provider.tokenSet.parseToken(buy, order.ratio[buy]) + ]; + } + const signedOrder = await this.signer.signSyncOrder({ accountId: this.accountId, recipient, @@ -465,7 +482,7 @@ export class Wallet { tokenBuy: this.provider.tokenSet.resolveTokenId(order.tokenBuy), validFrom: order.validFrom || 0, validUntil: order.validUntil || MAX_TIMESTAMP, - ratio: order.ratio + ratio }); return this.signOrder(signedOrder);