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",
"version": "0.20.88-rc6",
"version": "0.20.88-rc7",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -19,7 +19,7 @@ export type GetSwapInfoParams = {
poolOnly?: boolean
}
walletAddress?: string
isExactReceive?: boolean
isTradeBuy?: boolean
}
export default async function getSwapInfo({
@@ -31,7 +31,7 @@ export default async function getSwapInfo({
aggregator,
options,
walletAddress,
isExactReceive = false,
isTradeBuy = false,
}: GetSwapInfoParams) {
if (amount === '') throw new Error('Amount 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
? 'pools'
: undefined,
isExactReceive,
isTradeBuy,
);
const { exchanges: swapExchanges } = swapInfo;

View File

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

View File

@@ -50,7 +50,7 @@ export default async function swapMarket({
signer,
unit,
options,
isExactReceive = false,
isTradeBuy = false,
}: SwapMarketParams): Promise<Swap> {
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
? 'pools'
: undefined,
isExactReceive,
isTradeBuy,
);
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 (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}`);
}
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}`);
}
@@ -202,7 +202,7 @@ export default async function swapMarket({
.multipliedBy(new BigNumber(1).plus(percent))
.toString();
const amountSpend = swapInfo?.isExactReceive ? amountInWithSlippage : swapInfo.amountIn;
const amountSpend = swapInfo?.isTradeBuy ? amountInWithSlippage : swapInfo.amountIn;
balanceGuard.registerRequirement({
reason: 'Amount spend',
@@ -215,7 +215,7 @@ export default async function swapMarket({
sources: getAvailableSources('amount', assetInAddress, 'pool'),
});
const amountReceive = swapInfo?.isExactReceive ? amountOutWithSlippage : swapInfo.amountOut;
const amountReceive = swapInfo?.isTradeBuy ? amountOutWithSlippage : swapInfo.amountOut;
const amountSpendBlockchainParam = normalizeNumber(
amountSpend,
INTERNAL_PROTOCOL_PRECISION,

View File

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

View File

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

View File

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

View File

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

View File

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