diff --git a/package.json b/package.json index f16a176f..0ec2e497 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.22.0-rc19", + "version": "0.22.0-rc20", "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..0be8b87e 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,12 @@ export type GetSwapInfoParams = { options?: { instantSettlement?: boolean poolOnly?: boolean - }, - walletAddress?: string, + } + walletAddress?: string + isTradeBuy?: boolean } export default async function getSwapInfo({ - type, assetIn, assetOut, amount, @@ -32,6 +31,7 @@ export default async function getSwapInfo({ aggregator, options, walletAddress, + isTradeBuy = false, }: GetSwapInfoParams) { if (amount === '') throw new Error('Amount can not be empty'); if (assetIn === '') throw new Error('AssetIn can not be empty'); @@ -61,7 +61,6 @@ export default async function getSwapInfo({ } const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -69,6 +68,7 @@ export default async function getSwapInfo({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isTradeBuy, ); const { exchanges: swapExchanges } = swapInfo; @@ -76,16 +76,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 32c773b1..ae19a0e1 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -22,7 +22,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 @@ -39,6 +38,7 @@ export type SwapLimitParams = { route?: 'aggregator' | 'pool' } } + isTradeBuy?: boolean } type AggregatorOrder = { @@ -62,7 +62,6 @@ const isValidSingleSwap = (singleSwap: Omit & { factory: } export default async function swapLimit({ - type, assetIn, assetOut, price, @@ -71,6 +70,7 @@ export default async function swapLimit({ signer, unit, options, + 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'); @@ -142,7 +142,6 @@ export default async function swapLimit({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -150,6 +149,7 @@ export default async function swapLimit({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isTradeBuy, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -158,11 +158,11 @@ export default async function swapLimit({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo.type === 'exactReceive' && 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.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) { + if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -204,9 +204,9 @@ 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 = isTradeBuy + ? 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}`); @@ -250,7 +250,7 @@ export default async function swapLimit({ if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`); } - const amountSpend = swapInfo.type === 'exactSpend' + const amountSpend = !(swapInfo?.isTradeBuy) ? swapInfo.amountIn : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) @@ -265,7 +265,7 @@ export default async function swapLimit({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo.type === 'exactReceive' + 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 2cbc3e59..71d18d13 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -46,7 +46,6 @@ const isValidSingleSwap = (singleSwap: Omit & { factory: } export default async function swapMarket({ - type, assetIn, assetOut, amount, @@ -55,6 +54,7 @@ export default async function swapMarket({ signer, unit, options, + isTradeBuy = false, }: SwapMarketParams): Promise { if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); @@ -128,7 +128,6 @@ export default async function swapMarket({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -136,6 +135,7 @@ export default async function swapMarket({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isTradeBuy, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -144,11 +144,11 @@ export default async function swapMarket({ if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); - if (swapInfo.type === 'exactReceive' && 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.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) { + if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) { throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); } @@ -206,7 +206,7 @@ export default async function swapMarket({ .multipliedBy(new BigNumber(1).plus(percent)) .toString(); - const amountSpend = swapInfo.type === 'exactSpend' ? swapInfo.amountIn : amountInWithSlippage; + const amountSpend = swapInfo?.isTradeBuy ? amountInWithSlippage : swapInfo.amountIn; balanceGuard.registerRequirement({ reason: 'Amount spend', @@ -219,7 +219,7 @@ export default async function swapMarket({ sources: getAvailableSources('amount', assetInAddress, 'pool'), }); - const amountReceive = swapInfo.type === 'exactReceive' ? swapInfo.amountOut : amountOutWithSlippage; + 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 bf721635..f1a05166 100644 --- a/src/services/Aggregator/index.ts +++ b/src/services/Aggregator/index.ts @@ -315,21 +315,22 @@ class Aggregator { ); getSwapInfo = ( - type: 'exactSpend' | 'exactReceive', assetIn: string, assetOut: string, amount: string, instantSettlement?: boolean, exchanges?: string[] | 'cex' | 'pools', + isTradeBuy?: boolean, ) => { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); url.searchParams.append('assetOut', assetOut); - if (type === 'exactSpend') { + if (isTradeBuy !== true) { url.searchParams.append('amountIn', amount); } else { url.searchParams.append('amountOut', 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 07fc5026..8de1e8db 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -64,7 +64,7 @@ const swapInfoByAmountIn = swapInfoBase.extend({ marketAmountIn: z.null(), }).transform((val) => ({ ...val, - type: 'exactSpend' as const, + isTradeBuy: false as const, })); const swapInfoByAmountOut = swapInfoBase.extend({ @@ -74,7 +74,7 @@ const swapInfoByAmountOut = swapInfoBase.extend({ marketAmountIn: z.number().nullable(), }).transform((val) => ({ ...val, - type: 'exactReceive' 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 1ad60025..58852652 100644 --- a/src/services/Aggregator/ws/index.ts +++ b/src/services/Aggregator/ws/index.ts @@ -551,19 +551,19 @@ class AggregatorWS { autoSlippage: json.sl, }; - switch (json.k) { // kind - case 'exactSpend': + switch (json.tb) { // isTradeBuy + case false: this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - kind: json.k, + isTradeBuy: false, marketAmountOut: json.mo, availableAmountIn: json.aa, ...baseSwapInfo, }); break; - case 'exactReceive': + case true: this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ - kind: json.k, + 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 f153a9fe..66581e19 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -59,7 +59,7 @@ const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({ aa: z.number(), // available amount in }).transform((content) => ({ ...content, - k: 'exactSpend' as const, + tb: false as const, // isTradeBuy })); const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ @@ -67,7 +67,7 @@ const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ aao: z.number(), // available amount out }).transform((content) => ({ ...content, - k: 'exactReceive' as const, + tb: true as const, // isTradeBuy })); const swapInfoSchema = z.union([ diff --git a/src/types.ts b/src/types.ts index 5c6189cb..bd7e8125 100644 --- a/src/types.ts +++ b/src/types.ts @@ -247,13 +247,13 @@ export type SwapInfoBase = { } export type SwapInfoByAmountIn = SwapInfoBase & { - kind: 'exactSpend' + isTradeBuy: false availableAmountIn?: number | undefined marketAmountOut?: number | undefined } export type SwapInfoByAmountOut = SwapInfoBase & { - kind: 'exactReceive' + isTradeBuy: true marketAmountIn?: number | undefined availableAmountOut?: number | undefined } 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], }, }));