is trade buy

This commit is contained in:
Kirill Litvinov
2024-05-22 22:51:03 +03:00
parent 32836a2bde
commit ac6b331eca
9 changed files with 29 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@orionprotocol/sdk", "name": "@orionprotocol/sdk",
"version": "0.20.88-rc6", "version": "0.20.88-rc7",
"description": "Orion Protocol SDK", "description": "Orion Protocol SDK",
"main": "./lib/index.cjs", "main": "./lib/index.cjs",
"module": "./lib/index.js", "module": "./lib/index.js",

View File

@@ -19,7 +19,7 @@ export type GetSwapInfoParams = {
poolOnly?: boolean poolOnly?: boolean
} }
walletAddress?: string walletAddress?: string
isExactReceive?: boolean isTradeBuy?: boolean
} }
export default async function getSwapInfo({ export default async function getSwapInfo({
@@ -31,7 +31,7 @@ export default async function getSwapInfo({
aggregator, aggregator,
options, options,
walletAddress, walletAddress,
isExactReceive = false, isTradeBuy = false,
}: GetSwapInfoParams) { }: GetSwapInfoParams) {
if (amount === '') throw new Error('Amount can not be empty'); if (amount === '') throw new Error('Amount can not be empty');
if (assetIn === '') throw new Error('AssetIn can not be empty'); if (assetIn === '') throw new Error('AssetIn can not be empty');
@@ -68,7 +68,7 @@ export default async function getSwapInfo({
options?.poolOnly !== undefined && options.poolOnly options?.poolOnly !== undefined && options.poolOnly
? 'pools' ? 'pools'
: undefined, : undefined,
isExactReceive, isTradeBuy,
); );
const { exchanges: swapExchanges } = swapInfo; const { exchanges: swapExchanges } = swapInfo;

View File

@@ -34,7 +34,7 @@ export type SwapLimitParams = {
route?: 'aggregator' | 'pool' route?: 'aggregator' | 'pool'
} }
} }
isExactReceive?: boolean isTradeBuy?: boolean
} }
type AggregatorOrder = { type AggregatorOrder = {
@@ -66,7 +66,7 @@ export default async function swapLimit({
signer, signer,
unit, unit,
options, options,
isExactReceive = false, isTradeBuy = false,
}: SwapLimitParams): Promise<Swap> { }: SwapLimitParams): Promise<Swap> {
if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!');
if (amount === '') throw new Error('Amount can not be empty'); if (amount === '') throw new Error('Amount can not be empty');
@@ -145,7 +145,7 @@ export default async function swapLimit({
options?.poolOnly !== undefined && options.poolOnly options?.poolOnly !== undefined && options.poolOnly
? 'pools' ? 'pools'
: undefined, : undefined,
isExactReceive, isTradeBuy,
); );
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; 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 (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) {
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
} }
if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) {
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
} }
@@ -200,7 +200,7 @@ export default async function swapLimit({
options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`); options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`);
// BTEMP — better than or equal market price // BTEMP — better than or equal market price
const priceIsBTEMP = isExactReceive const priceIsBTEMP = isTradeBuy
? priceBN.gte(swapInfo.orderInfo.safePrice) ? priceBN.gte(swapInfo.orderInfo.safePrice)
: priceBN.lte(swapInfo.orderInfo.safePrice); : priceBN.lte(swapInfo.orderInfo.safePrice);
@@ -246,7 +246,7 @@ export default async function swapLimit({
if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`); if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`);
} }
const amountSpend = !(swapInfo?.isExactReceive) const amountSpend = !(swapInfo?.isTradeBuy)
? swapInfo.amountIn ? swapInfo.amountIn
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
@@ -261,7 +261,7 @@ export default async function swapLimit({
sources: getAvailableSources('amount', assetInAddress, 'pool'), sources: getAvailableSources('amount', assetInAddress, 'pool'),
}); });
const amountReceive = swapInfo?.isExactReceive const amountReceive = swapInfo?.isTradeBuy
? swapInfo.amountOut ? swapInfo.amountOut
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice) : new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
const amountSpendBlockchainParam = normalizeNumber( const amountSpendBlockchainParam = normalizeNumber(

View File

@@ -50,7 +50,7 @@ export default async function swapMarket({
signer, signer,
unit, unit,
options, options,
isExactReceive = false, isTradeBuy = false,
}: SwapMarketParams): Promise<Swap> { }: SwapMarketParams): Promise<Swap> {
if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!');
@@ -131,7 +131,7 @@ export default async function swapMarket({
options?.poolOnly !== undefined && options.poolOnly options?.poolOnly !== undefined && options.poolOnly
? 'pools' ? 'pools'
: undefined, : undefined,
isExactReceive, isTradeBuy,
); );
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo; const { exchanges: swapExchanges, exchangeContractPath } = swapInfo;
@@ -140,11 +140,11 @@ export default async function swapMarket({
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`); if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) { if (swapInfo?.isTradeBuy && amountBN.lt(swapInfo.minAmountOut)) {
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`); throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
} }
if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) { if (!(swapInfo?.isTradeBuy) && amountBN.lt(swapInfo.minAmountIn)) {
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`); throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
} }
@@ -202,7 +202,7 @@ export default async function swapMarket({
.multipliedBy(new BigNumber(1).plus(percent)) .multipliedBy(new BigNumber(1).plus(percent))
.toString(); .toString();
const amountSpend = swapInfo?.isExactReceive ? amountInWithSlippage : swapInfo.amountIn; const amountSpend = swapInfo?.isTradeBuy ? amountInWithSlippage : swapInfo.amountIn;
balanceGuard.registerRequirement({ balanceGuard.registerRequirement({
reason: 'Amount spend', reason: 'Amount spend',
@@ -215,7 +215,7 @@ export default async function swapMarket({
sources: getAvailableSources('amount', assetInAddress, 'pool'), sources: getAvailableSources('amount', assetInAddress, 'pool'),
}); });
const amountReceive = swapInfo?.isExactReceive ? amountOutWithSlippage : swapInfo.amountOut; const amountReceive = swapInfo?.isTradeBuy ? amountOutWithSlippage : swapInfo.amountOut;
const amountSpendBlockchainParam = normalizeNumber( const amountSpendBlockchainParam = normalizeNumber(
amountSpend, amountSpend,
INTERNAL_PROTOCOL_PRECISION, INTERNAL_PROTOCOL_PRECISION,

View File

@@ -268,12 +268,12 @@ class Aggregator {
amount: string, amount: string,
instantSettlement?: boolean, instantSettlement?: boolean,
exchanges?: string[] | 'cex' | 'pools', exchanges?: string[] | 'cex' | 'pools',
isExactReceive?: boolean, isTradeBuy?: boolean,
) => { ) => {
const url = new URL(`${this.apiUrl}/api/v1/swap`); const url = new URL(`${this.apiUrl}/api/v1/swap`);
url.searchParams.append('assetIn', assetIn); url.searchParams.append('assetIn', assetIn);
url.searchParams.append('assetOut', assetOut); url.searchParams.append('assetOut', assetOut);
if (isExactReceive !== true) { if (isTradeBuy !== true) {
url.searchParams.append('amountIn', amount); url.searchParams.append('amountIn', amount);
} else { } else {
url.searchParams.append('amountOut', amount); url.searchParams.append('amountOut', amount);

View File

@@ -59,7 +59,7 @@ const swapInfoByAmountIn = swapInfoBase.extend({
marketAmountIn: z.null(), marketAmountIn: z.null(),
}).transform((val) => ({ }).transform((val) => ({
...val, ...val,
isExactReceive: false as const, isTradeBuy: false as const,
})); }));
const swapInfoByAmountOut = swapInfoBase.extend({ const swapInfoByAmountOut = swapInfoBase.extend({
@@ -69,7 +69,7 @@ const swapInfoByAmountOut = swapInfoBase.extend({
marketAmountIn: z.number().nullable(), marketAmountIn: z.number().nullable(),
}).transform((val) => ({ }).transform((val) => ({
...val, ...val,
isExactReceive: true as const, isTradeBuy: true as const,
})); }));
const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut); const swapInfoSchema = swapInfoByAmountIn.or(swapInfoByAmountOut);

View File

@@ -544,10 +544,10 @@ class AggregatorWS {
autoSlippage: json.sl, autoSlippage: json.sl,
}; };
switch (json.er) { // exactReceive switch (json.tb) { // isTradeBuy
case false: case false:
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
isExactReceive: false, isTradeBuy: false,
marketAmountOut: json.mo, marketAmountOut: json.mo,
availableAmountIn: json.aa, availableAmountIn: json.aa,
...baseSwapInfo, ...baseSwapInfo,
@@ -556,7 +556,7 @@ class AggregatorWS {
break; break;
case true: case true:
this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({ this.subscriptions[SubscriptionType.SWAP_SUBSCRIBE]?.[json.S]?.callback({
isExactReceive: true, isTradeBuy: true,
...baseSwapInfo, ...baseSwapInfo,
marketAmountIn: json.mi, marketAmountIn: json.mi,
availableAmountOut: json.aao, availableAmountOut: json.aao,

View File

@@ -56,7 +56,7 @@ const swapInfoSchemaByAmountIn = swapInfoSchemaBase.extend({
aa: z.number(), // available amount in aa: z.number(), // available amount in
}).transform((content) => ({ }).transform((content) => ({
...content, ...content,
er: false as const, tb: false as const, // isTradeBuy
})); }));
const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({ const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({
@@ -64,7 +64,7 @@ const swapInfoSchemaByAmountOut = swapInfoSchemaBase.extend({
aao: z.number(), // available amount out aao: z.number(), // available amount out
}).transform((content) => ({ }).transform((content) => ({
...content, ...content,
er: true as const, tb: true as const, // isTradeBuy
})); }));
const swapInfoSchema = z.union([ const swapInfoSchema = z.union([

View File

@@ -216,13 +216,13 @@ export type SwapInfoBase = {
} }
export type SwapInfoByAmountIn = SwapInfoBase & { export type SwapInfoByAmountIn = SwapInfoBase & {
isExactReceive: false isTradeBuy: false
availableAmountIn?: number | undefined availableAmountIn?: number | undefined
marketAmountOut?: number | undefined marketAmountOut?: number | undefined
} }
export type SwapInfoByAmountOut = SwapInfoBase & { export type SwapInfoByAmountOut = SwapInfoBase & {
isExactReceive: true isTradeBuy: true
marketAmountIn?: number | undefined marketAmountIn?: number | undefined
availableAmountOut?: number | undefined availableAmountOut?: number | undefined
} }