From 533e18901e0847c4aa0069f3fea395264d2c0fb1 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Fri, 19 Aug 2022 11:52:49 +0400 Subject: [PATCH] Instant settlement (#22) * instantSettlement impl * Fixed poolOnly --- README.md | 14 ++++++++++---- package.json | 2 +- src/OrionUnit/Exchange/getSwapMarketFeeInfo.ts | 3 ++- src/OrionUnit/Exchange/swapMarket.ts | 2 ++ src/services/OrionAggregator/index.ts | 16 ++++++++++++---- src/services/OrionAggregator/ws/index.ts | 3 ++- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1095f0e..9412e13 100644 --- a/README.md +++ b/README.md @@ -139,13 +139,16 @@ orionUnit.exchange // Each trading pair has its own quantity precision // You need to prepare (round) the quantity according to quantity precision -const pairConfig = await simpleFetch(orionAggregator.getPairConfig)('ORN-USDT'); +const pairConfig = await simpleFetch(orionAggregator.getPairConfig)("ORN-USDT"); if (!pairConfig) throw new Error(`Pair config ORN-USDT not found`); const { qtyPrecision } = pairConfig; const amount = 23.5346563; -const roundedAmount = new BigNumber(amount).dp(qtyPrecision, BigNumber.ROUND_FLOOR); // You can use you own Math lib +const roundedAmount = new BigNumber(amount).dp( + qtyPrecision, + BigNumber.ROUND_FLOOR +); // You can use you own Math lib orionUnit.exchange .swapMarket({ @@ -159,6 +162,7 @@ orionUnit.exchange options: { // All options are optional 🙂 poolOnly: true, // You can specify whether you want to perform the exchange only through the pool + instantSettlement: true, // Set true to ensure that funds can be instantly transferred to wallet (otherwise, there is a possibility of receiving funds to the balance of the exchange contract) logger: console.log, // Set it to true if you want the issues associated with // the lack of allowance to be automatically corrected @@ -195,9 +199,11 @@ orionUnit.farmingManager.removeAllLiquidity({ ```ts import { simpleFetch } from "@orionprotocol/sdk"; -const orderbook = await simpleFetch(orionUnit.orionAggregator.getAggregatedOrderbook)( +const orderbook = await simpleFetch( + orionUnit.orionAggregator.getAggregatedOrderbook +)( "ORN-USDT", - 20, // Depth + 20 // Depth ); ``` diff --git a/package.json b/package.json index 4769172..acfa1a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.13.2", + "version": "0.14.0-rc.1", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/OrionUnit/Exchange/getSwapMarketFeeInfo.ts b/src/OrionUnit/Exchange/getSwapMarketFeeInfo.ts index 4e26f06..274ccd4 100644 --- a/src/OrionUnit/Exchange/getSwapMarketFeeInfo.ts +++ b/src/OrionUnit/Exchange/getSwapMarketFeeInfo.ts @@ -61,7 +61,8 @@ export default async function getSwapMarketFeeInfo({ assetIn, assetOut, amount.toString(), - options?.poolOnly ? ['ORION_POOL'] : undefined, + false, + options?.poolOnly ? 'pools' : undefined, ); if (swapInfo.orderInfo !== null && options?.poolOnly === true && options.poolOnly !== swapInfo.isThroughPoolOptimal) { diff --git a/src/OrionUnit/Exchange/swapMarket.ts b/src/OrionUnit/Exchange/swapMarket.ts index 4195bba..b688cd0 100644 --- a/src/OrionUnit/Exchange/swapMarket.ts +++ b/src/OrionUnit/Exchange/swapMarket.ts @@ -22,6 +22,7 @@ export type SwapMarketParams = { orionUnit: OrionUnit, options?: { poolOnly?: boolean, + instantSettlement?: boolean, logger?: (message: string) => void, autoApprove?: boolean, developer?: { @@ -124,6 +125,7 @@ export default async function swapMarket({ assetIn, assetOut, amount.toString(), + options?.instantSettlement, options?.poolOnly ? ['ORION_POOL'] : undefined, ); diff --git a/src/services/OrionAggregator/index.ts b/src/services/OrionAggregator/index.ts index 545663d..ecf1169 100644 --- a/src/services/OrionAggregator/index.ts +++ b/src/services/OrionAggregator/index.ts @@ -151,7 +151,8 @@ class OrionAggregator { assetIn: string, assetOut: string, amount: string, - exchanges?: Exchange[], + instantSettlement?: boolean, + exchanges?: Exchange[] | 'cex' | 'pools', ) => { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); @@ -162,9 +163,16 @@ class OrionAggregator { url.searchParams.append('amountOut', amount); } if (exchanges) { - exchanges.forEach((exchange) => { - url.searchParams.append('exchanges', exchange); - }); + if (Array.isArray(exchanges)) { + exchanges.forEach((exchange) => { + url.searchParams.append('exchanges', exchange); + }); + } else { + url.searchParams.append('exchanges', exchanges); + } + } + if (instantSettlement) { + url.searchParams.append('instantSettlement', 'true'); } return fetchWithValidation( diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 55921cf..c45a0cc 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -70,8 +70,9 @@ type SwapSubscriptionRequest = { i: string, // asset in o: string, // asset out a: number // amount IN/OUT - es?: Exchange[], // exchange list + es?: Exchange[] | 'cex' | 'pools', // exchange list of all cex or all pools (ORION_POOL, UNISWAP, PANCAKESWAP etc) e?: boolean; // is amount IN? Value `false` means a = amount OUT, `true` if omitted + is?: boolean; // instant settlement } type BrokerTradableAtomicSwapBalanceSubscription = {