From ed9fe7651e72056db26454d2c92a087843840354 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Mon, 13 May 2024 15:19:31 +0300 Subject: [PATCH 1/9] remove exactReceive --- package.json | 2 +- src/Unit/Exchange/getSwapInfo.ts | 17 ++---------- src/Unit/Exchange/swapLimit.ts | 21 +++------------ src/Unit/Exchange/swapMarket.ts | 16 +++--------- src/services/Aggregator/index.ts | 8 ++---- .../Aggregator/schemas/swapInfoSchema.ts | 17 ++---------- src/services/Aggregator/ws/index.ts | 26 ++++--------------- .../Aggregator/ws/schemas/swapInfoSchema.ts | 17 ++---------- src/types.ts | 9 +------ src/utils/parseExchangeTradeTransaction.ts | 2 -- 10 files changed, 22 insertions(+), 113 deletions(-) diff --git a/package.json b/package.json index 6161bf5e..0f66d987 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.86", + "version": "0.20.87-rc1", "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 02cd26da..aa1f5c53 100644 --- a/src/Unit/Exchange/getSwapInfo.ts +++ b/src/Unit/Exchange/getSwapInfo.ts @@ -8,7 +8,6 @@ import type { BlockchainService } from '../../services/BlockchainService/index.j import { calculateFeeInFeeAsset, denormalizeNumber, getNativeCryptocurrencyName } from '../../utils/index.js'; export type GetSwapInfoParams = { - type: 'exactSpend' | 'exactReceive' assetIn: string assetOut: string amount: BigNumber.Value @@ -18,12 +17,11 @@ export type GetSwapInfoParams = { options?: { instantSettlement?: boolean poolOnly?: boolean - }, - walletAddress?: string, + } + walletAddress?: string } export default async function getSwapInfo({ - type, assetIn, assetOut, amount, @@ -61,7 +59,6 @@ export default async function getSwapInfo({ } const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -76,16 +73,6 @@ export default async function getSwapInfo({ const poolExchangesList = factories !== undefined ? Object.keys(factories) : []; const [firstSwapExchange] = swapExchanges; - // if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) { - // throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); - // } - - // if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) { - // throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); - // } - - // if (swapInfo.orderInfo === null) throw new Error(swapInfo.executionInfo); - let route: 'pool' | 'aggregator'; if (options?.poolOnly !== undefined && options.poolOnly) { route = 'pool'; diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index 6c9f4c8d..a0ddd810 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -18,7 +18,6 @@ import type { SingleSwap } from '../../types.js'; import { must, safeGet } from '../../utils/safeGetters.js'; export type SwapLimitParams = { - type: 'exactSpend' | 'exactReceive' assetIn: string assetOut: string price: BigNumber.Value @@ -58,7 +57,6 @@ const isValidSingleSwap = (singleSwap: Omit & { factory: } export default async function swapLimit({ - type, assetIn, assetOut, price, @@ -138,7 +136,6 @@ export default async function swapLimit({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -154,11 +151,7 @@ export default async function swapLimit({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) { - throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); - } - - if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) { + if (amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -200,9 +193,7 @@ export default async function swapLimit({ options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`); // BTEMP — better than or equal market price - const priceIsBTEMP = type === 'exactSpend' - ? priceBN.lte(swapInfo.orderInfo.safePrice) - : priceBN.gte(swapInfo.orderInfo.safePrice); + const priceIsBTEMP = priceBN.lte(swapInfo.orderInfo.safePrice); options?.logger?.(`Your price ${priceBN.toString()} is ${priceIsBTEMP ? 'better than or equal' : 'worse than'} market price ${swapInfo.orderInfo.safePrice}`); @@ -246,9 +237,7 @@ export default async function swapLimit({ if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`); } - const amountSpend = swapInfo.type === 'exactSpend' - ? swapInfo.amountIn - : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) + const amountSpend = swapInfo.amountIn; balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -261,9 +250,7 @@ export default async function swapLimit({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo.type === 'exactReceive' - ? swapInfo.amountOut - : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) + const amountReceive = new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) const amountSpendBlockchainParam = normalizeNumber( amountSpend, INTERNAL_PROTOCOL_PRECISION, diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index c4c0b051..21bf775b 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -43,7 +43,6 @@ const isValidSingleSwap = (singleSwap: Omit & { factory: } export default async function swapMarket({ - type, assetIn, assetOut, amount, @@ -125,7 +124,6 @@ export default async function swapMarket({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -141,11 +139,7 @@ export default async function swapMarket({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) { - throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); - } - - if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) { + if (amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -199,11 +193,8 @@ export default async function swapMarket({ const amountOutWithSlippage = new BigNumber(swapInfo.amountOut) .multipliedBy(new BigNumber(1).minus(percent)) .toString(); - const amountInWithSlippage = new BigNumber(swapInfo.amountIn) - .multipliedBy(new BigNumber(1).plus(percent)) - .toString(); - const amountSpend = swapInfo.type === 'exactSpend' ? swapInfo.amountIn : amountInWithSlippage; + const amountSpend = swapInfo.amountIn; balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -216,14 +207,13 @@ export default async function swapMarket({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo.type === 'exactReceive' ? swapInfo.amountOut : amountOutWithSlippage; const amountSpendBlockchainParam = normalizeNumber( amountSpend, INTERNAL_PROTOCOL_PRECISION, BigNumber.ROUND_CEIL, ); const amountReceiveBlockchainParam = normalizeNumber( - amountReceive, + amountOutWithSlippage, INTERNAL_PROTOCOL_PRECISION, BigNumber.ROUND_FLOOR, ); diff --git a/src/services/Aggregator/index.ts b/src/services/Aggregator/index.ts index 9a1d53a0..93869f94 100644 --- a/src/services/Aggregator/index.ts +++ b/src/services/Aggregator/index.ts @@ -263,7 +263,6 @@ class Aggregator { ); getSwapInfo = ( - type: 'exactSpend' | 'exactReceive', assetIn: string, assetOut: string, amount: string, @@ -273,11 +272,8 @@ class Aggregator { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); url.searchParams.append('assetOut', assetOut); - if (type === 'exactSpend') { - url.searchParams.append('amountIn', amount); - } else { - url.searchParams.append('amountOut', amount); - } + url.searchParams.append('amountIn', amount); + if (exchanges !== undefined) { if (Array.isArray(exchanges)) { exchanges.forEach((exchange) => { diff --git a/src/services/Aggregator/schemas/swapInfoSchema.ts b/src/services/Aggregator/schemas/swapInfoSchema.ts index aed6d098..792149f8 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -57,21 +57,8 @@ const swapInfoByAmountIn = swapInfoBase.extend({ availableAmountIn: z.number(), marketAmountOut: z.number().nullable(), marketAmountIn: z.null(), -}).transform((val) => ({ - ...val, - type: 'exactSpend' as const, -})); - -const swapInfoByAmountOut = swapInfoBase.extend({ - availableAmountOut: z.number(), - availableAmountIn: z.null(), - marketAmountOut: z.null(), - marketAmountIn: z.number().nullable(), -}).transform((val) => ({ - ...val, - type: 'exactReceive' as const, -})); +}); -const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut); +const swapInfoSchema = swapInfoByAmountIn; export default swapInfoSchema; diff --git a/src/services/Aggregator/ws/index.ts b/src/services/Aggregator/ws/index.ts index fb1f88a0..34a19a59 100644 --- a/src/services/Aggregator/ws/index.ts +++ b/src/services/Aggregator/ws/index.ts @@ -544,27 +544,11 @@ class AggregatorWS { autoSlippage: json.sl, }; - switch (json.k) { // kind - case 'exactSpend': - this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - kind: json.k, - marketAmountOut: json.mo, - availableAmountIn: json.aa, - ...baseSwapInfo, - }); - - break; - case 'exactReceive': - this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - kind: json.k, - ...baseSwapInfo, - marketAmountIn: json.mi, - availableAmountOut: json.aao, - }); - break; - default: - break; - } + this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ + marketAmountOut: json.mo, + availableAmountIn: json.aa, + ...baseSwapInfo, + }); } break; case MessageType.INITIALIZATION: diff --git a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts index eb23200f..32f7fc1d 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -54,22 +54,9 @@ const swapInfoSchemaBase = baseMessageSchema.extend({ const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({ mo: z.number().optional(), // market amount out aa: z.number(), // available amount in -}).transform((content) => ({ - ...content, - k: 'exactSpend' as const, -})); +}); -const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ - mi: z.number().optional(), // market amount in - aao: z.number(), // available amount out -}).transform((content) => ({ - ...content, - k: 'exactReceive' as const, -})); -const swapInfoSchema = z.union([ - swapInfoSchemaByAmountIn, - swapInfoSchemaByAmountOut, -]); +const swapInfoSchema = swapInfoSchemaByAmountIn; export default swapInfoSchema; diff --git a/src/types.ts b/src/types.ts index 6a3513ac..edbbff6d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -214,18 +214,11 @@ export type SwapInfoBase = { } export type SwapInfoByAmountIn = SwapInfoBase & { - kind: 'exactSpend' availableAmountIn?: number | undefined marketAmountOut?: number | undefined } -export type SwapInfoByAmountOut = SwapInfoBase & { - kind: 'exactReceive' - marketAmountIn?: number | undefined - availableAmountOut?: number | undefined -} - -export type SwapInfo = SwapInfoByAmountIn | SwapInfoByAmountOut; +export type SwapInfo = SwapInfoByAmountIn; export enum HistoryTransactionStatus { PENDING = 'Pending', diff --git a/src/utils/parseExchangeTradeTransaction.ts b/src/utils/parseExchangeTradeTransaction.ts index efd0235b..9b59093b 100644 --- a/src/utils/parseExchangeTradeTransaction.ts +++ b/src/utils/parseExchangeTradeTransaction.ts @@ -8,7 +8,6 @@ const swapThroughOrionPoolSchema = z.object({ z.bigint(), // amount_spend z.bigint(), // amount_receive z.string().refine(ethers.isAddress).array().nonempty(), // path - z.boolean(), // is_exact_spend ]), }).transform((data) => ({ name: data.name, @@ -16,7 +15,6 @@ const swapThroughOrionPoolSchema = z.object({ amount_spend: data.args[0], amount_receive: data.args[1], path: data.args[2], - is_exact_spend: data.args[3], }, })); From 71ba0ca5fca5ff40fd066fc708bf102502d57028 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Mon, 13 May 2024 15:22:05 +0300 Subject: [PATCH 2/9] fix version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0f66d987..98968f5a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.87-rc1", + "version": "0.20.88-rc1", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", From 275547340e726a6630ffc19eb474db730c204d49 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Mon, 13 May 2024 17:21:51 +0300 Subject: [PATCH 3/9] fix schemas --- package-lock.json | 4 ++-- package.json | 2 +- src/services/Aggregator/schemas/swapInfoSchema.ts | 4 +--- src/services/Aggregator/ws/schemas/swapInfoSchema.ts | 5 +---- src/types.ts | 4 +--- 5 files changed, 6 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7ab175b0..182ba5aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.86", + "version": "0.20.88-rc2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.86", + "version": "0.20.88-rc2", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index 98968f5a..0cf52dd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc1", + "version": "0.20.88-rc2", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/services/Aggregator/schemas/swapInfoSchema.ts b/src/services/Aggregator/schemas/swapInfoSchema.ts index 792149f8..ad1219d6 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -52,13 +52,11 @@ const swapInfoBase = z.object({ autoSlippage: z.number().optional(), }); -const swapInfoByAmountIn = swapInfoBase.extend({ +const swapInfoSchema = swapInfoBase.extend({ availableAmountOut: z.null(), availableAmountIn: z.number(), marketAmountOut: z.number().nullable(), marketAmountIn: z.null(), }); -const swapInfoSchema = swapInfoByAmountIn; - export default swapInfoSchema; diff --git a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts index 32f7fc1d..38eeae91 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -51,12 +51,9 @@ const swapInfoSchemaBase = baseMessageSchema.extend({ sl: z.number().optional(), }); -const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({ +const swapInfoSchema = swapInfoSchemaBase.extend({ mo: z.number().optional(), // market amount out aa: z.number(), // available amount in }); - -const swapInfoSchema = swapInfoSchemaByAmountIn; - export default swapInfoSchema; diff --git a/src/types.ts b/src/types.ts index edbbff6d..5329925a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -213,13 +213,11 @@ export type SwapInfoBase = { autoSlippage: number | undefined } -export type SwapInfoByAmountIn = SwapInfoBase & { +export type SwapInfo = SwapInfoBase & { availableAmountIn?: number | undefined marketAmountOut?: number | undefined } -export type SwapInfo = SwapInfoByAmountIn; - export enum HistoryTransactionStatus { PENDING = 'Pending', DONE = 'Done', From b44a42dd28c8f82f95980bbb9bb7db13ac9eacf9 Mon Sep 17 00:00:00 2001 From: TheJuze Date: Wed, 15 May 2024 16:58:17 +0300 Subject: [PATCH 4/9] update package-lock.json --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 182ba5aa..b7ab5724 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc2", + "version": "0.20.88-rc3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc2", + "version": "0.20.88-rc3", "hasInstallScript": true, "license": "ISC", "dependencies": { From 4c63f330b856bfd46356c681bb9007038357c412 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Wed, 22 May 2024 18:14:39 +0300 Subject: [PATCH 5/9] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index debd4e6c..29de6813 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc4", + "version": "0.20.88-rc5", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", From 32836a2bde03ba784978bac0aea8d6ca97b20482 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Wed, 22 May 2024 22:09:11 +0300 Subject: [PATCH 6/9] is exact receive --- package.json | 2 +- src/Unit/Exchange/getSwapInfo.ts | 3 +++ src/Unit/Exchange/swapLimit.ts | 21 ++++++++++++--- src/Unit/Exchange/swapMarket.ts | 17 +++++++++--- src/services/Aggregator/index.ts | 7 ++++- .../Aggregator/schemas/swapInfoSchema.ts | 19 ++++++++++++-- src/services/Aggregator/ws/index.ts | 26 +++++++++++++++---- .../Aggregator/ws/schemas/swapInfoSchema.ts | 20 ++++++++++++-- src/types.ts | 11 +++++++- 9 files changed, 106 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 29de6813..ecc901c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc5", + "version": "0.20.88-rc6", "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 aa1f5c53..33a2756d 100644 --- a/src/Unit/Exchange/getSwapInfo.ts +++ b/src/Unit/Exchange/getSwapInfo.ts @@ -19,6 +19,7 @@ export type GetSwapInfoParams = { poolOnly?: boolean } walletAddress?: string + isExactReceive?: boolean } export default async function getSwapInfo({ @@ -30,6 +31,7 @@ export default async function getSwapInfo({ aggregator, options, walletAddress, + isExactReceive = false, }: GetSwapInfoParams) { if (amount === '') throw new Error('Amount can not be empty'); if (assetIn === '') throw new Error('AssetIn can not be empty'); @@ -66,6 +68,7 @@ export default async function getSwapInfo({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isExactReceive, ); const { exchanges: swapExchanges } = swapInfo; diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index a0ddd810..c914f1ad 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -34,6 +34,7 @@ export type SwapLimitParams = { route?: 'aggregator' | 'pool' } } + isExactReceive?: boolean } type AggregatorOrder = { @@ -65,6 +66,7 @@ export default async function swapLimit({ signer, unit, options, + isExactReceive = 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'); @@ -143,6 +145,7 @@ export default async function swapLimit({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isExactReceive, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -151,7 +154,11 @@ export default async function swapLimit({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (amountBN.lt(swapInfo.minAmountIn)) { + if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { + throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); + } + + if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -193,7 +200,9 @@ export default async function swapLimit({ options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`); // BTEMP — better than or equal market price - const priceIsBTEMP = priceBN.lte(swapInfo.orderInfo.safePrice); + const priceIsBTEMP = isExactReceive + ? priceBN.gte(swapInfo.orderInfo.safePrice) + : priceBN.lte(swapInfo.orderInfo.safePrice); options?.logger?.(`Your price ${priceBN.toString()} is ${priceIsBTEMP ? 'better than or equal' : 'worse than'} market price ${swapInfo.orderInfo.safePrice}`); @@ -237,7 +246,9 @@ export default async function swapLimit({ if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`); } - const amountSpend = swapInfo.amountIn; + const amountSpend = !(swapInfo?.isExactReceive) + ? swapInfo.amountIn + : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -250,7 +261,9 @@ export default async function swapLimit({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) + const amountReceive = swapInfo?.isExactReceive + ? swapInfo.amountOut + : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) const amountSpendBlockchainParam = normalizeNumber( amountSpend, INTERNAL_PROTOCOL_PRECISION, diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index 21bf775b..4253bc70 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -37,7 +37,6 @@ type PoolSwap = { export type Swap = AggregatorOrder | PoolSwap; - const isValidSingleSwap = (singleSwap: Omit & { factory: string }): singleSwap is SingleSwap => { return isValidFactory(singleSwap.factory); } @@ -51,6 +50,7 @@ export default async function swapMarket({ signer, unit, options, + isExactReceive = false, }: SwapMarketParams): Promise { if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); @@ -131,6 +131,7 @@ export default async function swapMarket({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isExactReceive, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -139,7 +140,11 @@ export default async function swapMarket({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (amountBN.lt(swapInfo.minAmountIn)) { + if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { + throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); + } + + if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -193,8 +198,11 @@ export default async function swapMarket({ const amountOutWithSlippage = new BigNumber(swapInfo.amountOut) .multipliedBy(new BigNumber(1).minus(percent)) .toString(); + const amountInWithSlippage = new BigNumber(swapInfo.amountIn) + .multipliedBy(new BigNumber(1).plus(percent)) + .toString(); - const amountSpend = swapInfo.amountIn; + const amountSpend = swapInfo?.isExactReceive ? amountInWithSlippage : swapInfo.amountIn; balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -207,13 +215,14 @@ export default async function swapMarket({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); + const amountReceive = swapInfo?.isExactReceive ? amountOutWithSlippage : swapInfo.amountOut; const amountSpendBlockchainParam = normalizeNumber( amountSpend, INTERNAL_PROTOCOL_PRECISION, BigNumber.ROUND_CEIL, ); const amountReceiveBlockchainParam = normalizeNumber( - amountOutWithSlippage, + amountReceive, INTERNAL_PROTOCOL_PRECISION, BigNumber.ROUND_FLOOR, ); diff --git a/src/services/Aggregator/index.ts b/src/services/Aggregator/index.ts index 93869f94..8ed790fa 100644 --- a/src/services/Aggregator/index.ts +++ b/src/services/Aggregator/index.ts @@ -268,11 +268,16 @@ class Aggregator { amount: string, instantSettlement?: boolean, exchanges?: string[] | 'cex' | 'pools', + isExactReceive?: boolean, ) => { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); url.searchParams.append('assetOut', assetOut); - url.searchParams.append('amountIn', amount); + if (isExactReceive !== true) { + url.searchParams.append('amountIn', amount); + } else { + url.searchParams.append('amountOut', amount); + } if (exchanges !== undefined) { if (Array.isArray(exchanges)) { diff --git a/src/services/Aggregator/schemas/swapInfoSchema.ts b/src/services/Aggregator/schemas/swapInfoSchema.ts index ad1219d6..59de289d 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -52,11 +52,26 @@ const swapInfoBase = z.object({ autoSlippage: z.number().optional(), }); -const swapInfoSchema = swapInfoBase.extend({ +const swapInfoByAmountIn = swapInfoBase.extend({ availableAmountOut: z.null(), availableAmountIn: z.number(), marketAmountOut: z.number().nullable(), marketAmountIn: z.null(), -}); +}).transform((val) => ({ + ...val, + isExactReceive: false as const, +})); + +const swapInfoByAmountOut = swapInfoBase.extend({ + availableAmountOut: z.number(), + availableAmountIn: z.null(), + marketAmountOut: z.null(), + marketAmountIn: z.number().nullable(), +}).transform((val) => ({ + ...val, + isExactReceive: true as const, +})); + +const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut); export default swapInfoSchema; diff --git a/src/services/Aggregator/ws/index.ts b/src/services/Aggregator/ws/index.ts index 34a19a59..d654e22c 100644 --- a/src/services/Aggregator/ws/index.ts +++ b/src/services/Aggregator/ws/index.ts @@ -544,11 +544,27 @@ class AggregatorWS { autoSlippage: json.sl, }; - this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - marketAmountOut: json.mo, - availableAmountIn: json.aa, - ...baseSwapInfo, - }); + switch (json.er) { // exactReceive + case false: + this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ + isExactReceive: false, + marketAmountOut: json.mo, + availableAmountIn: json.aa, + ...baseSwapInfo, + }); + + break; + case true: + this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ + isExactReceive: true, + ...baseSwapInfo, + marketAmountIn: json.mi, + availableAmountOut: json.aao, + }); + break; + default: + break; + } } break; case MessageType.INITIALIZATION: diff --git a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts index 38eeae91..828f6a13 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -51,9 +51,25 @@ const swapInfoSchemaBase = baseMessageSchema.extend({ sl: z.number().optional(), }); -const swapInfoSchema = swapInfoSchemaBase.extend({ +const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({ mo: z.number().optional(), // market amount out aa: z.number(), // available amount in -}); +}).transform((content) => ({ + ...content, + er: false as const, +})); + +const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ + mi: z.number().optional(), // market amount in + aao: z.number(), // available amount out +}).transform((content) => ({ + ...content, + er: true as const, +})); + +const swapInfoSchema = z.union([ + swapInfoSchemaByAmountIn, + swapInfoSchemaByAmountOut, +]); export default swapInfoSchema; diff --git a/src/types.ts b/src/types.ts index 946675a4..1a03fcd2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -215,11 +215,20 @@ export type SwapInfoBase = { autoSlippage: number | undefined } -export type SwapInfo = SwapInfoBase & { +export type SwapInfoByAmountIn = SwapInfoBase & { + isExactReceive: false availableAmountIn?: number | undefined marketAmountOut?: number | undefined } +export type SwapInfoByAmountOut = SwapInfoBase & { + isExactReceive: true + marketAmountIn?: number | undefined + availableAmountOut?: number | undefined +} + +export type SwapInfo = SwapInfoByAmountIn | SwapInfoByAmountOut; + export enum HistoryTransactionStatus { PENDING = 'Pending', DONE = 'Done', From ac6b331eca459741a36013d4fb36116d86b3f090 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Wed, 22 May 2024 22:51:03 +0300 Subject: [PATCH 7/9] 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 ecc901c0..8de0c64a 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 33a2756d..0be8b87e 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 c914f1ad..19ae3008 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 4253bc70..934e2bb6 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 8ed790fa..876fc26b 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 59de289d..758e7afc 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 d654e22c..98629a1f 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 828f6a13..8e37fd8c 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 1a03fcd2..8696decb 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 } From f9e8443e8c202188d2300d47e6d48b967c140549 Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Thu, 30 May 2024 15:49:40 +0300 Subject: [PATCH 8/9] bump version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b166e41c..6c7095a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc8", + "version": "0.21.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc8", + "version": "0.21.4", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index afeddfe9..e6dd48bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.88-rc9", + "version": "0.21.4", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", From 2c89b2ec4c721988e0e9a758f829de966ae38acc Mon Sep 17 00:00:00 2001 From: Kirill Litvinov Date: Thu, 30 May 2024 16:12:01 +0300 Subject: [PATCH 9/9] bump version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6c7095a5..72ed3951 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.21.4", + "version": "0.22.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.21.4", + "version": "0.22.0", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index e6dd48bb..e4fd7016 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.21.4", + "version": "0.22.0", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js",