Skip to content

Commit

Permalink
Better ratio utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed May 25, 2021
1 parent c5d2446 commit 004e04c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
12 changes: 6 additions & 6 deletions core/tests/ts-tests/tests/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Tester.prototype.testSwapNFT = async function (
tokenSell: token,
tokenBuy: nft,
amount,
ratio: utils.ratio({
ratio: utils.weiRatio({
tokenSell: amount,
tokenBuy: 1
})
Expand All @@ -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
})
Expand Down Expand Up @@ -85,7 +85,7 @@ Tester.prototype.testSwap = async function (
tokenSell: tokenA,
tokenBuy: tokenB,
amount,
ratio: utils.ratio({
ratio: utils.weiRatio({
tokenSell: 1,
tokenBuy: 2
})
Expand All @@ -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
})
Expand Down Expand Up @@ -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
})
Expand All @@ -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
})
Expand Down
12 changes: 12 additions & 0 deletions sdk/zksync.js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions sdk/zksync.js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
MintNFT,
Order,
Swap,
Ratio,
TokenRatio,
WeiRatio,
WithdrawNFT
} from './types';
import { rescueHashOrders } from './crypto';
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 21 additions & 4 deletions sdk/zksync.js/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
Transfer,
TxEthSignature,
Withdraw,
WithdrawNFT
WithdrawNFT,
TokenRatio,
WeiRatio
} from './types';
import {
ERC20_APPROVE_TRESHOLD,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 004e04c

Please sign in to comment.