mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
remove exactReceive
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.20.86",
|
||||
"version": "0.20.87-rc1",
|
||||
"description": "Orion Protocol SDK",
|
||||
"main": "./lib/index.cjs",
|
||||
"module": "./lib/index.js",
|
||||
|
||||
@@ -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,11 @@ export type GetSwapInfoParams = {
|
||||
options?: {
|
||||
instantSettlement?: boolean
|
||||
poolOnly?: boolean
|
||||
},
|
||||
walletAddress?: string,
|
||||
}
|
||||
walletAddress?: string
|
||||
}
|
||||
|
||||
export default async function getSwapInfo({
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
amount,
|
||||
@@ -61,7 +59,6 @@ export default async function getSwapInfo({
|
||||
}
|
||||
|
||||
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
amountBN.toString(),
|
||||
@@ -76,16 +73,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';
|
||||
|
||||
@@ -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
|
||||
@@ -58,7 +57,6 @@ const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory:
|
||||
}
|
||||
|
||||
export default async function swapLimit({
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
price,
|
||||
@@ -138,7 +136,6 @@ export default async function swapLimit({
|
||||
);
|
||||
|
||||
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
amountBN.toString(),
|
||||
@@ -154,11 +151,7 @@ export default async function swapLimit({
|
||||
|
||||
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
|
||||
|
||||
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)) {
|
||||
if (amountBN.lt(swapInfo.minAmountIn)) {
|
||||
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
|
||||
}
|
||||
|
||||
@@ -200,9 +193,7 @@ 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 = 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,9 +237,7 @@ export default async function swapLimit({
|
||||
if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`);
|
||||
}
|
||||
|
||||
const amountSpend = swapInfo.type === 'exactSpend'
|
||||
? swapInfo.amountIn
|
||||
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
|
||||
const amountSpend = swapInfo.amountIn;
|
||||
|
||||
balanceGuard.registerRequirement({
|
||||
reason: 'Amount spend',
|
||||
@@ -261,9 +250,7 @@ export default async function swapLimit({
|
||||
sources: getAvailableSources('amount', assetInAddress, 'pool'),
|
||||
});
|
||||
|
||||
const amountReceive = swapInfo.type === 'exactReceive'
|
||||
? swapInfo.amountOut
|
||||
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
|
||||
const amountReceive = new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
|
||||
const amountSpendBlockchainParam = normalizeNumber(
|
||||
amountSpend,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
|
||||
@@ -43,7 +43,6 @@ const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory:
|
||||
}
|
||||
|
||||
export default async function swapMarket({
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
amount,
|
||||
@@ -125,7 +124,6 @@ export default async function swapMarket({
|
||||
);
|
||||
|
||||
const swapInfo = await simpleFetch(aggregator.getSwapInfo)(
|
||||
type,
|
||||
assetIn,
|
||||
assetOut,
|
||||
amountBN.toString(),
|
||||
@@ -141,11 +139,7 @@ export default async function swapMarket({
|
||||
|
||||
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
|
||||
|
||||
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)) {
|
||||
if (amountBN.lt(swapInfo.minAmountIn)) {
|
||||
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
|
||||
}
|
||||
|
||||
@@ -199,11 +193,8 @@ export default async function swapMarket({
|
||||
const amountOutWithSlippage = new BigNumber(swapInfo.amountOut)
|
||||
.multipliedBy(new BigNumber(1).minus(percent))
|
||||
.toString();
|
||||
const amountInWithSlippage = new BigNumber(swapInfo.amountIn)
|
||||
.multipliedBy(new BigNumber(1).plus(percent))
|
||||
.toString();
|
||||
|
||||
const amountSpend = swapInfo.type === 'exactSpend' ? swapInfo.amountIn : amountInWithSlippage;
|
||||
const amountSpend = swapInfo.amountIn;
|
||||
|
||||
balanceGuard.registerRequirement({
|
||||
reason: 'Amount spend',
|
||||
@@ -216,14 +207,13 @@ export default async function swapMarket({
|
||||
sources: getAvailableSources('amount', assetInAddress, 'pool'),
|
||||
});
|
||||
|
||||
const amountReceive = swapInfo.type === 'exactReceive' ? swapInfo.amountOut : amountOutWithSlippage;
|
||||
const amountSpendBlockchainParam = normalizeNumber(
|
||||
amountSpend,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
BigNumber.ROUND_CEIL,
|
||||
);
|
||||
const amountReceiveBlockchainParam = normalizeNumber(
|
||||
amountReceive,
|
||||
amountOutWithSlippage,
|
||||
INTERNAL_PROTOCOL_PRECISION,
|
||||
BigNumber.ROUND_FLOOR,
|
||||
);
|
||||
|
||||
@@ -263,7 +263,6 @@ class Aggregator {
|
||||
);
|
||||
|
||||
getSwapInfo = (
|
||||
type: 'exactSpend' | 'exactReceive',
|
||||
assetIn: string,
|
||||
assetOut: string,
|
||||
amount: string,
|
||||
@@ -273,11 +272,8 @@ class Aggregator {
|
||||
const url = new URL(`${this.apiUrl}/api/v1/swap`);
|
||||
url.searchParams.append('assetIn', assetIn);
|
||||
url.searchParams.append('assetOut', assetOut);
|
||||
if (type === 'exactSpend') {
|
||||
url.searchParams.append('amountIn', amount);
|
||||
} else {
|
||||
url.searchParams.append('amountOut', amount);
|
||||
}
|
||||
url.searchParams.append('amountIn', amount);
|
||||
|
||||
if (exchanges !== undefined) {
|
||||
if (Array.isArray(exchanges)) {
|
||||
exchanges.forEach((exchange) => {
|
||||
|
||||
@@ -57,21 +57,8 @@ const swapInfoByAmountIn = swapInfoBase.extend({
|
||||
availableAmountIn: z.number(),
|
||||
marketAmountOut: z.number().nullable(),
|
||||
marketAmountIn: z.null(),
|
||||
}).transform((val) => ({
|
||||
...val,
|
||||
type: 'exactSpend' as const,
|
||||
}));
|
||||
});
|
||||
|
||||
const swapInfoByAmountOut = swapInfoBase.extend({
|
||||
availableAmountOut: z.number(),
|
||||
availableAmountIn: z.null(),
|
||||
marketAmountOut: z.null(),
|
||||
marketAmountIn: z.number().nullable(),
|
||||
}).transform((val) => ({
|
||||
...val,
|
||||
type: 'exactReceive' as const,
|
||||
}));
|
||||
|
||||
const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut);
|
||||
const swapInfoSchema = swapInfoByAmountIn;
|
||||
|
||||
export default swapInfoSchema;
|
||||
|
||||
@@ -544,27 +544,11 @@ class AggregatorWS {
|
||||
autoSlippage: json.sl,
|
||||
};
|
||||
|
||||
switch (json.k) { // kind
|
||||
case 'exactSpend':
|
||||
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
|
||||
kind: json.k,
|
||||
marketAmountOut: json.mo,
|
||||
availableAmountIn: json.aa,
|
||||
...baseSwapInfo,
|
||||
});
|
||||
|
||||
break;
|
||||
case 'exactReceive':
|
||||
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
|
||||
kind: json.k,
|
||||
...baseSwapInfo,
|
||||
marketAmountIn: json.mi,
|
||||
availableAmountOut: json.aao,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
|
||||
marketAmountOut: json.mo,
|
||||
availableAmountIn: json.aa,
|
||||
...baseSwapInfo,
|
||||
});
|
||||
}
|
||||
break;
|
||||
case MessageType.INITIALIZATION:
|
||||
|
||||
@@ -54,22 +54,9 @@ const swapInfoSchemaBase = baseMessageSchema.extend({
|
||||
const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({
|
||||
mo: z.number().optional(), // market amount out
|
||||
aa: z.number(), // available amount in
|
||||
}).transform((content) => ({
|
||||
...content,
|
||||
k: 'exactSpend' as const,
|
||||
}));
|
||||
});
|
||||
|
||||
const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({
|
||||
mi: z.number().optional(), // market amount in
|
||||
aao: z.number(), // available amount out
|
||||
}).transform((content) => ({
|
||||
...content,
|
||||
k: 'exactReceive' as const,
|
||||
}));
|
||||
|
||||
const swapInfoSchema = z.union([
|
||||
swapInfoSchemaByAmountIn,
|
||||
swapInfoSchemaByAmountOut,
|
||||
]);
|
||||
const swapInfoSchema = swapInfoSchemaByAmountIn;
|
||||
|
||||
export default swapInfoSchema;
|
||||
|
||||
@@ -214,18 +214,11 @@ export type SwapInfoBase = {
|
||||
}
|
||||
|
||||
export type SwapInfoByAmountIn = SwapInfoBase & {
|
||||
kind: 'exactSpend'
|
||||
availableAmountIn?: number | undefined
|
||||
marketAmountOut?: number | undefined
|
||||
}
|
||||
|
||||
export type SwapInfoByAmountOut = SwapInfoBase & {
|
||||
kind: 'exactReceive'
|
||||
marketAmountIn?: number | undefined
|
||||
availableAmountOut?: number | undefined
|
||||
}
|
||||
|
||||
export type SwapInfo = SwapInfoByAmountIn | SwapInfoByAmountOut;
|
||||
export type SwapInfo = SwapInfoByAmountIn;
|
||||
|
||||
export enum HistoryTransactionStatus {
|
||||
PENDING = 'Pending',
|
||||
|
||||
@@ -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],
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user