is exact receive

This commit is contained in:
Kirill Litvinov
2024-05-22 22:09:11 +03:00
parent 4c63f330b8
commit 32836a2bde
9 changed files with 106 additions and 20 deletions

View File

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

View File

@@ -34,6 +34,7 @@ export type SwapLimitParams = {
route?: 'aggregator' | 'pool'
}
}
isExactReceive?: boolean
}
type AggregatorOrder = {
@@ -65,6 +66,7 @@ export default async function swapLimit({
signer,
unit,
options,
isExactReceive = 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');
@@ -143,6 +145,7 @@ export default async function swapLimit({
options?.poolOnly !== undefined && options.poolOnly
? 'pools'
: undefined,
isExactReceive,
);
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo;
@@ -151,7 +154,11 @@ export default async function swapLimit({
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
if (amountBN.lt(swapInfo.minAmountIn)) {
if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) {
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
}
if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) {
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
}
@@ -193,7 +200,9 @@ export default async function swapLimit({
options?.logger?.(`Safe price is ${swapInfo.orderInfo.safePrice} ${quoteAssetName}`);
// BTEMP — better than or equal market price
const priceIsBTEMP = priceBN.lte(swapInfo.orderInfo.safePrice);
const priceIsBTEMP = isExactReceive
? priceBN.gte(swapInfo.orderInfo.safePrice)
: priceBN.lte(swapInfo.orderInfo.safePrice);
options?.logger?.(`Your price ${priceBN.toString()} is ${priceIsBTEMP ? 'better than or equal' : 'worse than'} market price ${swapInfo.orderInfo.safePrice}`);
@@ -237,7 +246,9 @@ export default async function swapLimit({
if (factoryAddress !== undefined) options?.logger?.(`Factory address is ${factoryAddress}. Exchange is ${firstSwapExchange}`);
}
const amountSpend = swapInfo.amountIn;
const amountSpend = !(swapInfo?.isExactReceive)
? swapInfo.amountIn
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
balanceGuard.registerRequirement({
reason: 'Amount spend',
@@ -250,7 +261,9 @@ export default async function swapLimit({
sources: getAvailableSources('amount', assetInAddress, 'pool'),
});
const amountReceive = new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
const amountReceive = swapInfo?.isExactReceive
? swapInfo.amountOut
: new BigNumber(swapInfo.orderInfo.amount).multipliedBy(swapInfo.orderInfo.safePrice)
const amountSpendBlockchainParam = normalizeNumber(
amountSpend,
INTERNAL_PROTOCOL_PRECISION,

View File

@@ -37,7 +37,6 @@ type PoolSwap = {
export type Swap = AggregatorOrder | PoolSwap;
const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory: string }): singleSwap is SingleSwap => {
return isValidFactory(singleSwap.factory);
}
@@ -51,6 +50,7 @@ export default async function swapMarket({
signer,
unit,
options,
isExactReceive = false,
}: SwapMarketParams): Promise<Swap> {
if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!');
@@ -131,6 +131,7 @@ export default async function swapMarket({
options?.poolOnly !== undefined && options.poolOnly
? 'pools'
: undefined,
isExactReceive,
);
const { exchanges: swapExchanges, exchangeContractPath } = swapInfo;
@@ -139,7 +140,11 @@ export default async function swapMarket({
if (swapExchanges.length > 0) options?.logger?.(`Swap exchanges: ${swapExchanges.join(', ')}`);
if (amountBN.lt(swapInfo.minAmountIn)) {
if (swapInfo?.isExactReceive && amountBN.lt(swapInfo.minAmountOut)) {
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
}
if (!(swapInfo?.isExactReceive) && amountBN.lt(swapInfo.minAmountIn)) {
throw new Error(`Amount is too low. Min amountIn is ${swapInfo.minAmountIn} ${assetIn}`);
}
@@ -193,8 +198,11 @@ 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.amountIn;
const amountSpend = swapInfo?.isExactReceive ? amountInWithSlippage : swapInfo.amountIn;
balanceGuard.registerRequirement({
reason: 'Amount spend',
@@ -207,13 +215,14 @@ export default async function swapMarket({
sources: getAvailableSources('amount', assetInAddress, 'pool'),
});
const amountReceive = swapInfo?.isExactReceive ? amountOutWithSlippage : swapInfo.amountOut;
const amountSpendBlockchainParam = normalizeNumber(
amountSpend,
INTERNAL_PROTOCOL_PRECISION,
BigNumber.ROUND_CEIL,
);
const amountReceiveBlockchainParam = normalizeNumber(
amountOutWithSlippage,
amountReceive,
INTERNAL_PROTOCOL_PRECISION,
BigNumber.ROUND_FLOOR,
);