From fd212d23668f67004b669937ef426e1f1343d827 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Sat, 28 May 2022 11:17:18 +0400 Subject: [PATCH] poolOnly --- .vscode/settings.json | 4 +++- README.md | 3 +++ package.json | 2 +- src/OrionUnit/Exchange/swapMarket.ts | 13 ++++++++++++- src/services/OrionAggregator/index.ts | 6 +++++- src/services/OrionAggregator/ws/index.ts | 3 ++- src/types.ts | 4 ++++ 7 files changed, 30 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3c5bb9b..131a6c4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,8 @@ { "cSpell.words": [ + "ASCENDEX", "bignumber", - "denormalized" + "denormalized", + "KUCOIN" ] } \ No newline at end of file diff --git a/README.md b/README.md index bb09aa1..362fc7b 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ orionUnit.exchange slippagePercent: 1, signer: wallet, // or signer when UI options: { + // All options are optional 🙂 + poolOnly: true, // You can specify whether you want to perform the exchange only through the pool logger: console.log, // Set it to true if you want the issues associated with // the lack of allowance to be automatically corrected @@ -252,6 +254,7 @@ orionUnit.orionAggregator.ws.subscribe( i: assetIn, // asset in o: assetOut, // asset out e: true, // true when type of swap is exactSpend, can be omitted (true by default) + es: ['ORION_POOL'] // OPTIONAL! Specify ['ORION_POOL'] if you want "pool only" swap execution a: 5.62345343, // amount }, // Handle data update in your way diff --git a/package.json b/package.json index 237383f..9b06ce2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.10.4-rc.2", + "version": "0.10.4-rc.3", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/OrionUnit/Exchange/swapMarket.ts b/src/OrionUnit/Exchange/swapMarket.ts index 87abbde..9ce4faa 100644 --- a/src/OrionUnit/Exchange/swapMarket.ts +++ b/src/OrionUnit/Exchange/swapMarket.ts @@ -21,6 +21,7 @@ export type SwapMarketParams = { signer: ethers.Signer, orionUnit: OrionUnit, options?: { + poolOnly?: boolean, logger?: (message: string) => void, autoApprove?: boolean, developer?: { @@ -115,7 +116,17 @@ export default async function swapMarket({ signer, ); - const swapInfo = await simpleFetch(orionAggregator.getSwapInfo)(type, assetIn, assetOut, amount.toString()); + const swapInfo = await simpleFetch(orionAggregator.getSwapInfo)( + type, + assetIn, + assetOut, + amount.toString(), + options?.poolOnly ? ['ORION_POOL'] : undefined, + ); + + if (options?.poolOnly === true && options.poolOnly !== swapInfo.isThroughPoolOptimal) { + throw new Error(`Unexpected Orion Aggregator response. Please, contact support. Report swap request id: ${swapInfo.id}`); + } if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) { throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); diff --git a/src/services/OrionAggregator/index.ts b/src/services/OrionAggregator/index.ts index 55bfe84..2a3c909 100644 --- a/src/services/OrionAggregator/index.ts +++ b/src/services/OrionAggregator/index.ts @@ -9,7 +9,7 @@ import errorSchema from './schemas/errorSchema'; import placeAtomicSwapSchema from './schemas/placeAtomicSwapSchema'; import { OrionAggregatorWS } from './ws'; import { atomicSwapHistorySchema } from './schemas/atomicSwapHistorySchema'; -import { SignedCancelOrderRequest, SignedOrder } from '../../types'; +import { Exchange, SignedCancelOrderRequest, SignedOrder } from '../../types'; import { pairConfigSchema } from './schemas'; import { aggregatedOrderbookSchema, exchangeOrderbookSchema, @@ -151,6 +151,7 @@ class OrionAggregator { assetIn: string, assetOut: string, amount: string, + exchanges?: Exchange[], ) => { const url = new URL(`${this.apiUrl}/api/v1/swap`); url.searchParams.append('assetIn', assetIn); @@ -160,6 +161,9 @@ class OrionAggregator { } else { url.searchParams.append('amountOut', amount); } + if (exchanges) { + url.searchParams.append('exchanges', exchanges.join(',')); + } return fetchWithValidation( url.toString(), diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index ef979ad..64d0a24 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -12,7 +12,7 @@ import { import UnsubscriptionType from './UnsubscriptionType'; import { SwapInfoByAmountIn, SwapInfoByAmountOut, SwapInfoBase, - FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance, + FullOrder, OrderUpdate, AssetPairUpdate, OrderbookItem, Balance, Exchange, } from '../../../types'; import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema'; // import errorSchema from './schemas/errorSchema'; @@ -69,6 +69,7 @@ type SwapSubscriptionRequest = { i: string, // asset in o: string, // asset out a: number // amount IN/OUT + es?: Exchange[], // exchange list e?: boolean; // is amount IN? Value `false` means a = amount OUT, `true` if omitted } diff --git a/src/types.ts b/src/types.ts index 2bf243b..37aa007 100644 --- a/src/types.ts +++ b/src/types.ts @@ -195,3 +195,7 @@ export type BalanceIssue = { readonly sources: Source[], readonly fixes?: Fix[], } + +const availableExchanges = ['ORION_POOL', 'ASCENDEX', 'BINANCE', 'KUCOIN'] as const; + +export type Exchange = typeof availableExchanges[number];