From ac6b331eca459741a36013d4fb36116d86b3f090 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Wed, 22 May 2024 22:51:03 +0300 Subject: [PATCH] is trade buy --- package.json | 2 +- src/Unit/Exchange/getSwapInfo.ts | 6 +++--- src/Unit/Exchange/swapLimit.ts | 16 ++++++++-------- src/Unit/Exchange/swapMarket.ts | 12 ++++++------ src/services/Aggregator/index.ts | 4 ++-- .../Aggregator/schemas/swapInfoSchema.ts | 4 ++-- src/services/Aggregator/ws/index.ts | 6 +++--- .../Aggregator/ws/schemas/swapInfoSchema.ts | 4 ++-- src/types.ts | 4 ++-- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index ecc901c..8de0c64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc6", + "version": "0.20.88-rc7", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/getSwapInfo.ts b/src/Unit/Exchange/getSwapInfo.ts index 33a2756..0be8b87 100644 --- a/src/Unit/Exchange/getSwapInfo.ts +++ b/src/Unit/Exchange/getSwapInfo.ts @@ -19,7 +19,7 @@ export type GetSwapInfoParams = { poolOnly?: boolean } walletAddress?: string - isExactReceive?: boolean + isTradeBuy?: boolean } export default async function getSwapInfo({ @@ -31,7 +31,7 @@ export default async function getSwapInfo({ aggregator, options, walletAddress, - isExactReceive = false, + isTradeBuy = false, }: GetSwapInfoParams) { if (amount === '') throw new Error('Amount can not be empty'); if (assetIn === '') throw new Error('AssetIn can not be empty'); @@ -68,7 +68,7 @@ export default async function getSwapInfo({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, - isExactReceive, + isTradeBuy, ); const { exchanges: swapExchanges } = swapInfo; diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index c914f1a..19ae300 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -34,7 +34,7 @@ export type SwapLimitParams = { route?: 'aggregator' | 'pool' } } - isExactReceive?: boolean + isTradeBuy?: boolean } type AggregatorOrder = { @@ -66,7 +66,7 @@ export default async function swapLimit({ signer, unit, options, - isExactReceive = false, + isTradeBuy = false, }: SwapLimitParams): Promise { if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); if (amount === '') throw new Error('Amount can not be empty'); @@ -145,7 +145,7 @@ export default async function swapLimit({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, - isExactReceive, + isTradeBuy, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -154,11 +154,11 @@ export default async function swapLimit({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { + if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) { throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); } - if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { + if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -200,7 +200,7 @@ export default async function swapLimit({ options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`); // BTEMP — better than or equal market price - const priceIsBTEMP = isExactReceive + const priceIsBTEMP = isTradeBuy ? priceBN.gte(swapInfo.orderInfo.safePrice) : priceBN.lte(swapInfo.orderInfo.safePrice); @@ -246,7 +246,7 @@ export default async function swapLimit({ if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`); } - const amountSpend = !(swapInfo?.isExactReceive) + const amountSpend = !(swapInfo?.isTradeBuy) ? swapInfo.amountIn : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) @@ -261,7 +261,7 @@ export default async function swapLimit({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo?.isExactReceive + const amountReceive = swapInfo?.isTradeBuy ? swapInfo.amountOut : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) const amountSpendBlockchainParam = normalizeNumber( diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index 4253bc7..934e2bb 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -50,7 +50,7 @@ export default async function swapMarket({ signer, unit, options, - isExactReceive = false, + isTradeBuy = false, }: SwapMarketParams): Promise { if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); @@ -131,7 +131,7 @@ export default async function swapMarket({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, - isExactReceive, + isTradeBuy, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -140,11 +140,11 @@ export default async function swapMarket({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { + if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) { throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); } - if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { + if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -202,7 +202,7 @@ export default async function swapMarket({ .multipliedBy(new BigNumber(1).plus(percent)) .toString(); - const amountSpend = swapInfo?.isExactReceive ? amountInWithSlippage : swapInfo.amountIn; + const amountSpend = swapInfo?.isTradeBuy ? amountInWithSlippage : swapInfo.amountIn; balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -215,7 +215,7 @@ export default async function swapMarket({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo?.isExactReceive ? amountOutWithSlippage : swapInfo.amountOut; + const amountReceive = swapInfo?.isTradeBuy ? amountOutWithSlippage : swapInfo.amountOut; const amountSpendBlockchainParam = normalizeNumber( amountSpend, INTERNAL_PROTOCOL_PRECISION, diff --git a/src/services/Aggregator/index.ts b/src/services/Aggregator/index.ts index 8ed790f..876fc26 100644 --- a/src/services/Aggregator/index.ts +++ b/src/services/Aggregator/index.ts @@ -268,12 +268,12 @@ class Aggregator { amount: string, instantSettlement?: boolean, exchanges?: string[] | 'cex' | 'pools', - isExactReceive?: boolean, + isTradeBuy?: boolean, ) => { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); url.searchParams.append('assetOut', assetOut); - if (isExactReceive !== true) { + if (isTradeBuy !== true) { url.searchParams.append('amountIn', amount); } else { url.searchParams.append('amountOut', amount); diff --git a/src/services/Aggregator/schemas/swapInfoSchema.ts b/src/services/Aggregator/schemas/swapInfoSchema.ts index 59de289..758e7af 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -59,7 +59,7 @@ const swapInfoByAmountIn = swapInfoBase.extend({ marketAmountIn: z.null(), }).transform((val) => ({ ...val, - isExactReceive: false as const, + isTradeBuy: false as const, })); const swapInfoByAmountOut = swapInfoBase.extend({ @@ -69,7 +69,7 @@ const swapInfoByAmountOut = swapInfoBase.extend({ marketAmountIn: z.number().nullable(), }).transform((val) => ({ ...val, - isExactReceive: true as const, + isTradeBuy: true as const, })); const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut); diff --git a/src/services/Aggregator/ws/index.ts b/src/services/Aggregator/ws/index.ts index d654e22..98629a1 100644 --- a/src/services/Aggregator/ws/index.ts +++ b/src/services/Aggregator/ws/index.ts @@ -544,10 +544,10 @@ class AggregatorWS { autoSlippage: json.sl, }; - switch (json.er) { // exactReceive + switch (json.tb) { // isTradeBuy case false: this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - isExactReceive: false, + isTradeBuy: false, marketAmountOut: json.mo, availableAmountIn: json.aa, ...baseSwapInfo, @@ -556,7 +556,7 @@ class AggregatorWS { break; case true: this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - isExactReceive: true, + isTradeBuy: true, ...baseSwapInfo, marketAmountIn: json.mi, availableAmountOut: json.aao, diff --git a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts index 828f6a1..8e37fd8 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -56,7 +56,7 @@ const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({ aa: z.number(), // available amount in }).transform((content) => ({ ...content, - er: false as const, + tb: false as const, // isTradeBuy })); const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ @@ -64,7 +64,7 @@ const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ aao: z.number(), // available amount out }).transform((content) => ({ ...content, - er: true as const, + tb: true as const, // isTradeBuy })); const swapInfoSchema = z.union([ diff --git a/src/types.ts b/src/types.ts index 1a03fcd..8696dec 100644 --- a/src/types.ts +++ b/src/types.ts @@ -216,13 +216,13 @@ export type SwapInfoBase = { } export type SwapInfoByAmountIn = SwapInfoBase & { - isExactReceive: false + isTradeBuy: false availableAmountIn?: number | undefined marketAmountOut?: number | undefined } export type SwapInfoByAmountOut = SwapInfoBase & { - isExactReceive: true + isTradeBuy: true marketAmountIn?: number | undefined availableAmountOut?: number | undefined }