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

@@ -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,