@@ -22,7 +22,6 @@ import type { SingleSwap } from '../../types.js';
2222import { must , safeGet } from '../../utils/safeGetters.js' ;
2323
2424export type SwapLimitParams = {
25- type : 'exactSpend' | 'exactReceive'
2625 assetIn : string
2726 assetOut : string
2827 price : BigNumber . Value
@@ -39,6 +38,7 @@ export type SwapLimitParams = {
3938 route ?: 'aggregator' | 'pool'
4039 }
4140 }
41+ isTradeBuy ?: boolean
4242}
4343
4444type AggregatorOrder = {
@@ -62,7 +62,6 @@ const isValidSingleSwap = (singleSwap: Omit<SingleSwap, 'factory'> & { factory:
6262}
6363
6464export default async function swapLimit ( {
65- type,
6665 assetIn,
6766 assetOut,
6867 price,
@@ -71,6 +70,7 @@ export default async function swapLimit({
7170 signer,
7271 unit,
7372 options,
73+ isTradeBuy = false ,
7474} : SwapLimitParams ) : Promise < Swap > {
7575 if ( options ?. developer ) options . logger ?.( 'YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!' ) ;
7676 if ( amount === '' ) throw new Error ( 'Amount can not be empty' ) ;
@@ -142,14 +142,14 @@ export default async function swapLimit({
142142 ) ;
143143
144144 const swapInfo = await simpleFetch ( aggregator . getSwapInfo ) (
145- type ,
146145 assetIn ,
147146 assetOut ,
148147 amountBN . toString ( ) ,
149148 options ?. instantSettlement ,
150149 options ?. poolOnly !== undefined && options . poolOnly
151150 ? 'pools'
152151 : undefined ,
152+ isTradeBuy ,
153153 ) ;
154154
155155 const { exchanges : swapExchanges , exchangeContractPath } = swapInfo ;
@@ -158,11 +158,11 @@ export default async function swapLimit({
158158
159159 if ( swapExchanges . length > 0 ) options ?. logger ?.( `Swap exchanges: ${ swapExchanges . join ( ', ' ) } ` ) ;
160160
161- if ( swapInfo . type === 'exactReceive' && amountBN . lt ( swapInfo . minAmountOut ) ) {
161+ if ( swapInfo ?. isTradeBuy && amountBN . lt ( swapInfo . minAmountOut ) ) {
162162 throw new Error ( `Amount is too low. Min amountOut is ${ swapInfo . minAmountOut } ${ assetOut } ` ) ;
163163 }
164164
165- if ( swapInfo . type === 'exactSpend' && amountBN . lt ( swapInfo . minAmountIn ) ) {
165+ if ( ! ( swapInfo ?. isTradeBuy ) && amountBN . lt ( swapInfo . minAmountIn ) ) {
166166 throw new Error ( `Amount is too low. Min amountIn is ${ swapInfo . minAmountIn } ${ assetIn } ` ) ;
167167 }
168168
@@ -204,9 +204,9 @@ export default async function swapLimit({
204204
205205 options ?. logger ?.( `Safe price is ${ swapInfo . orderInfo . safePrice } ${ quoteAssetName } ` ) ;
206206 // BTEMP — better than or equal market price
207- const priceIsBTEMP = type === 'exactSpend'
208- ? priceBN . lte ( swapInfo . orderInfo . safePrice )
209- : priceBN . gte ( swapInfo . orderInfo . safePrice ) ;
207+ const priceIsBTEMP = isTradeBuy
208+ ? priceBN . gte ( swapInfo . orderInfo . safePrice )
209+ : priceBN . lte ( swapInfo . orderInfo . safePrice ) ;
210210
211211 options ?. logger ?.( `Your price ${ priceBN . toString ( ) } is ${ priceIsBTEMP ? 'better than or equal' : 'worse than' } market price ${ swapInfo . orderInfo . safePrice } ` ) ;
212212
@@ -250,7 +250,7 @@ export default async function swapLimit({
250250 if ( factoryAddress !== undefined ) options ?. logger ?.( `Factory address is ${ factoryAddress } . Exchange is ${ firstSwapExchange } ` ) ;
251251 }
252252
253- const amountSpend = swapInfo . type === 'exactSpend'
253+ const amountSpend = ! ( swapInfo ?. isTradeBuy )
254254 ? swapInfo . amountIn
255255 : new BigNumber ( swapInfo . orderInfo . amount ) . multipliedBy ( swapInfo . orderInfo . safePrice )
256256
@@ -265,7 +265,7 @@ export default async function swapLimit({
265265 sources : getAvailableSources ( 'amount' , assetInAddress , 'pool' ) ,
266266 } ) ;
267267
268- const amountReceive = swapInfo . type === 'exactReceive'
268+ const amountReceive = swapInfo ?. isTradeBuy
269269 ? swapInfo . amountOut
270270 : new BigNumber ( swapInfo . orderInfo . amount ) . multipliedBy ( swapInfo . orderInfo . safePrice )
271271 const amountSpendBlockchainParam = normalizeNumber (
0 commit comments