Skip to content

Commit f793acd

Browse files
committed
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
# Conflicts: # package-lock.json # package.json
2 parents ab4a674 + 2c89b2e commit f793acd

File tree

10 files changed

+36
-47
lines changed

10 files changed

+36
-47
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@orionprotocol/sdk",
3-
"version": "0.22.0-rc19",
3+
"version": "0.22.0-rc20",
44
"description": "Orion Protocol SDK",
55
"main": "./lib/index.cjs",
66
"module": "./lib/index.js",

src/Unit/Exchange/getSwapInfo.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { BlockchainService } from '../../services/BlockchainService/index.j
88
import { calculateFeeInFeeAsset, denormalizeNumber, getNativeCryptocurrencyName } from '../../utils/index.js';
99

1010
export type GetSwapInfoParams = {
11-
type: 'exactSpend' | 'exactReceive'
1211
assetIn: string
1312
assetOut: string
1413
amount: BigNumber.Value
@@ -18,12 +17,12 @@ export type GetSwapInfoParams = {
1817
options?: {
1918
instantSettlement?: boolean
2019
poolOnly?: boolean
21-
},
22-
walletAddress?: string,
20+
}
21+
walletAddress?: string
22+
isTradeBuy?: boolean
2323
}
2424

2525
export default async function getSwapInfo({
26-
type,
2726
assetIn,
2827
assetOut,
2928
amount,
@@ -32,6 +31,7 @@ export default async function getSwapInfo({
3231
aggregator,
3332
options,
3433
walletAddress,
34+
isTradeBuy = false,
3535
}: GetSwapInfoParams) {
3636
if (amount === '') throw new Error('Amount can not be empty');
3737
if (assetIn === '') throw new Error('AssetIn can not be empty');
@@ -61,31 +61,21 @@ export default async function getSwapInfo({
6161
}
6262

6363
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
64-
type,
6564
assetIn,
6665
assetOut,
6766
amountBN.toString(),
6867
options?.instantSettlement,
6968
options?.poolOnly !== undefined && options.poolOnly
7069
? 'pools'
7170
: undefined,
71+
isTradeBuy,
7272
);
7373

7474
const { exchanges: swapExchanges } = swapInfo;
7575
const { factories } = await simpleFetch(blockchainService.getPoolsConfig)();
7676
const poolExchangesList = factories !== undefined ? Object.keys(factories) : [];
7777
const [firstSwapExchange] = swapExchanges;
7878

79-
// if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) {
80-
// throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
81-
// }
82-
83-
// if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) {
84-
// throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
85-
// }
86-
87-
// if (swapInfo.orderInfo === null) throw new Error(swapInfo.executionInfo);
88-
8979
let route: 'pool' | 'aggregator';
9080
if (options?.poolOnly !== undefined && options.poolOnly) {
9181
route = 'pool';

src/Unit/Exchange/swapLimit.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import type { SingleSwap } from '../../types.js';
2222
import { must, safeGet } from '../../utils/safeGetters.js';
2323

2424
export type SwapLimitParams = {
25-
type: 'exactSpend' | 'exactReceive'
2625
assetIn: string
2726
assetOut: string
2827
price: BigNumber.Value
@@ -39,6 +38,7 @@ export type SwapLimitParams = {
3938
route?: 'aggregator' | 'pool'
4039
}
4140
}
41+
isTradeBuy?: boolean
4242
}
4343

4444
type AggregatorOrder = {
@@ -62,7 +62,6 @@ const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory:
6262
}
6363

6464
export default async function swapLimit({
65-
type,
6665
assetIn,
6766
assetOut,
6867
price,
@@ -71,6 +70,7 @@ export default async function swapLimit({
7170
signer,
7271
unit,
7372
options,
73+
isTradeBuy = false,
7474
}: SwapLimitParams): Promise<Swap> {
7575
if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!');
7676
if (amount === '') throw new Error('Amount can not be empty');
@@ -142,14 +142,14 @@ export default async function swapLimit({
142142
);
143143

144144
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
145-
type,
146145
assetIn,
147146
assetOut,
148147
amountBN.toString(),
149148
options?.instantSettlement,
150149
options?.poolOnly !== undefined && options.poolOnly
151150
? 'pools'
152151
: undefined,
152+
isTradeBuy,
153153
);
154154

155155
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo;
@@ -158,11 +158,11 @@ export default async function swapLimit({
158158

159159
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
160160

161-
if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) {
161+
if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) {
162162
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
163163
}
164164

165-
if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) {
165+
if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) {
166166
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
167167
}
168168

@@ -204,9 +204,9 @@ export default async function swapLimit({
204204

205205
options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`);
206206
// BTEMP — better than or equal market price
207-
const priceIsBTEMP = type === 'exactSpend'
208-
? priceBN.lte(swapInfo.orderInfo.safePrice)
209-
: priceBN.gte(swapInfo.orderInfo.safePrice);
207+
const priceIsBTEMP = isTradeBuy
208+
? priceBN.gte(swapInfo.orderInfo.safePrice)
209+
: priceBN.lte(swapInfo.orderInfo.safePrice);
210210

211211
options?.logger?.(`Your price ${priceBN.toString()} is ${priceIsBTEMP ? 'better than or equal' : 'worse than'} market price ${swapInfo.orderInfo.safePrice}`);
212212

@@ -250,7 +250,7 @@ export default async function swapLimit({
250250
if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`);
251251
}
252252

253-
const amountSpend = swapInfo.type === 'exactSpend'
253+
const amountSpend = !(swapInfo?.isTradeBuy)
254254
? swapInfo.amountIn
255255
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
256256

@@ -265,7 +265,7 @@ export default async function swapLimit({
265265
sources: getAvailableSources('amount', assetInAddress, 'pool'),
266266
});
267267

268-
const amountReceive = swapInfo.type === 'exactReceive'
268+
const amountReceive = swapInfo?.isTradeBuy
269269
? swapInfo.amountOut
270270
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
271271
const amountSpendBlockchainParam = normalizeNumber(

src/Unit/Exchange/swapMarket.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory:
4646
}
4747

4848
export default async function swapMarket({
49-
type,
5049
assetIn,
5150
assetOut,
5251
amount,
@@ -55,6 +54,7 @@ export default async function swapMarket({
5554
signer,
5655
unit,
5756
options,
57+
isTradeBuy = false,
5858
}: SwapMarketParams): Promise<Swap> {
5959
if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!');
6060

@@ -128,14 +128,14 @@ export default async function swapMarket({
128128
);
129129

130130
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
131-
type,
132131
assetIn,
133132
assetOut,
134133
amountBN.toString(),
135134
options?.instantSettlement,
136135
options?.poolOnly !== undefined && options.poolOnly
137136
? 'pools'
138137
: undefined,
138+
isTradeBuy,
139139
);
140140

141141
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo;
@@ -144,11 +144,11 @@ export default async function swapMarket({
144144

145145
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
146146

147-
if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) {
147+
if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) {
148148
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
149149
}
150150

151-
if (swapInfo.type === 'exactSpend' && amountBN.lt(swapInfo.minAmountIn)) {
151+
if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) {
152152
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
153153
}
154154

@@ -206,7 +206,7 @@ export default async function swapMarket({
206206
.multipliedBy(new BigNumber(1).plus(percent))
207207
.toString();
208208

209-
const amountSpend = swapInfo.type === 'exactSpend' ? swapInfo.amountIn : amountInWithSlippage;
209+
const amountSpend = swapInfo?.isTradeBuy ? amountInWithSlippage : swapInfo.amountIn;
210210

211211
balanceGuard.registerRequirement({
212212
reason: 'Amount spend',
@@ -219,7 +219,7 @@ export default async function swapMarket({
219219
sources: getAvailableSources('amount', assetInAddress, 'pool'),
220220
});
221221

222-
const amountReceive = swapInfo.type === 'exactReceive' ? swapInfo.amountOut : amountOutWithSlippage;
222+
const amountReceive = swapInfo?.isTradeBuy ? amountOutWithSlippage : swapInfo.amountOut;
223223
const amountSpendBlockchainParam = normalizeNumber(
224224
amountSpend,
225225
INTERNAL_PROTOCOL_PRECISION,

src/services/Aggregator/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,22 @@ class Aggregator {
315315
);
316316

317317
getSwapInfo = (
318-
type: 'exactSpend' | 'exactReceive',
319318
assetIn: string,
320319
assetOut: string,
321320
amount: string,
322321
instantSettlement?: boolean,
323322
exchanges?: string[] | 'cex' | 'pools',
323+
isTradeBuy?: boolean,
324324
) => {
325325
const url = new URL(`${this.apiUrl}/api/v1/swap`);
326326
url.searchParams.append('assetIn', assetIn);
327327
url.searchParams.append('assetOut', assetOut);
328-
if (type === 'exactSpend') {
328+
if (isTradeBuy !== true) {
329329
url.searchParams.append('amountIn', amount);
330330
} else {
331331
url.searchParams.append('amountOut', amount);
332332
}
333+
333334
if (exchanges !== undefined) {
334335
if (Array.isArray(exchanges)) {
335336
exchanges.forEach((exchange) => {

src/services/Aggregator/schemas/swapInfoSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const swapInfoByAmountIn = swapInfoBase.extend({
6464
marketAmountIn: z.null(),
6565
}).transform((val) => ({
6666
...val,
67-
type: 'exactSpend' as const,
67+
isTradeBuy: false as const,
6868
}));
6969

7070
const swapInfoByAmountOut = swapInfoBase.extend({
@@ -74,7 +74,7 @@ const swapInfoByAmountOut = swapInfoBase.extend({
7474
marketAmountIn: z.number().nullable(),
7575
}).transform((val) => ({
7676
...val,
77-
type: 'exactReceive' as const,
77+
isTradeBuy: true as const,
7878
}));
7979

8080
const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut);

src/services/Aggregator/ws/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,19 @@ class AggregatorWS {
551551
autoSlippage: json.sl,
552552
};
553553

554-
switch (json.k) { // kind
555-
case 'exactSpend':
554+
switch (json.tb) { // isTradeBuy
555+
case false:
556556
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
557-
kind: json.k,
557+
isTradeBuy: false,
558558
marketAmountOut: json.mo,
559559
availableAmountIn: json.aa,
560560
...baseSwapInfo,
561561
});
562562

563563
break;
564-
case 'exactReceive':
564+
case true:
565565
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
566-
kind: json.k,
566+
isTradeBuy: true,
567567
...baseSwapInfo,
568568
marketAmountIn: json.mi,
569569
availableAmountOut: json.aao,

src/services/Aggregator/ws/schemas/swapInfoSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({
5959
aa: z.number(), // available amount in
6060
}).transform((content) => ({
6161
...content,
62-
k: 'exactSpend' as const,
62+
tb: false as const, // isTradeBuy
6363
}));
6464

6565
const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({
6666
mi: z.number().optional(), // market amount in
6767
aao: z.number(), // available amount out
6868
}).transform((content) => ({
6969
...content,
70-
k: 'exactReceive' as const,
70+
tb: true as const, // isTradeBuy
7171
}));
7272

7373
const swapInfoSchema = z.union([

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ export type SwapInfoBase = {
247247
}
248248

249249
export type SwapInfoByAmountIn = SwapInfoBase & {
250-
kind: 'exactSpend'
250+
isTradeBuy: false
251251
availableAmountIn?: number | undefined
252252
marketAmountOut?: number | undefined
253253
}
254254

255255
export type SwapInfoByAmountOut = SwapInfoBase & {
256-
kind: 'exactReceive'
256+
isTradeBuy: true
257257
marketAmountIn?: number | undefined
258258
availableAmountOut?: number | undefined
259259
}

src/utils/parseExchangeTradeTransaction.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ const swapThroughOrionPoolSchema = z.object({
88
z.bigint(), // amount_spend
99
z.bigint(), // amount_receive
1010
z.string().refine(ethers.isAddress).array().nonempty(), // path
11-
z.boolean(), // is_exact_spend
1211
]),
1312
}).transform((data) => ({
1413
name: data.name,
1514
args: {
1615
amount_spend: data.args[0],
1716
amount_receive: data.args[1],
1817
path: data.args[2],
19-
is_exact_spend: data.args[3],
2018
},
2119
}));
2220

0 commit comments

Comments
 (0)