diff --git a/package-lock.json b/package-lock.json index 3176675..b166e41 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.76-rc110", + "version": "0.20.88-rc8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.76-rc110", + "version": "0.20.88-rc8", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index 769c078..afeddfe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.21.3", + "version": "0.20.88-rc9", "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 02cd26d..0be8b87 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 6c9f4c8..19ae300 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 @@ -35,6 +34,7 @@ export type SwapLimitParams = { route?: 'aggregator' | 'pool' } } + isTradeBuy?: boolean } type AggregatorOrder = { @@ -58,7 +58,6 @@ const isValidSingleSwap = (singleSwap: Omit & { factory: } export default async function swapLimit({ - type, assetIn, assetOut, price, @@ -67,6 +66,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'); @@ -138,7 +138,6 @@ export default async function swapLimit({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -146,6 +145,7 @@ export default async function swapLimit({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + 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.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}`); } @@ -200,9 +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 = 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}`); @@ -246,7 +246,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) @@ -261,7 +261,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 c4c0b05..934e2bb 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -37,13 +37,11 @@ type PoolSwap = { export type Swap = AggregatorOrder | PoolSwap; - const isValidSingleSwap = (singleSwap: Omit & { factory: string }): singleSwap is SingleSwap => { return isValidFactory(singleSwap.factory); } export default async function swapMarket({ - type, assetIn, assetOut, amount, @@ -52,6 +50,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!'); @@ -125,7 +124,6 @@ export default async function swapMarket({ ); const swapInfo = await simpleFetch(aggregator.getSwapInfo)( - type, assetIn, assetOut, amountBN.toString(), @@ -133,6 +131,7 @@ export default async function swapMarket({ options?.poolOnly !== undefined && options.poolOnly ? 'pools' : undefined, + isTradeBuy, ); const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; @@ -141,11 +140,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}`); } @@ -203,7 +202,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', @@ -216,7 +215,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 9a1d53a..876fc26 100644 --- a/src/services/Aggregator/index.ts +++ b/src/services/Aggregator/index.ts @@ -263,21 +263,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 940612f..46a663f 100644 --- a/src/services/Aggregator/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/schemas/swapInfoSchema.ts @@ -61,7 +61,7 @@ const swapInfoByAmountIn = swapInfoBase.extend({ marketAmountIn: z.null(), }).transform((val) => ({ ...val, - type: 'exactSpend' as const, + isTradeBuy: false as const, })); const swapInfoByAmountOut = swapInfoBase.extend({ @@ -71,7 +71,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 ee64bd3..8443458 100644 --- a/src/services/Aggregator/ws/index.ts +++ b/src/services/Aggregator/ws/index.ts @@ -547,19 +547,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 f153a9f..66581e1 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 f26da80..c02bc8f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -219,13 +219,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 efd0235..9b59093 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], }, }));