@@ -18,7 +18,10 @@ const {
1818 rangeSellPrice,
1919 rangeBuyPrice,
2020} = require ( "../utils/pricing" ) ;
21- const { haveSwapCapsChanged } = require ( "../utils/priceUpdate" ) ;
21+ const {
22+ haveSwapCapsChanged,
23+ resolveDexQuoteAmount,
24+ } = require ( "../utils/priceUpdate" ) ;
2225
2326const log = require ( "../utils/logger" ) ( "task:prices" ) ;
2427
@@ -133,11 +136,38 @@ const setPrices = async (options) => {
133136 if ( curve && options . armName !== "Lido" )
134137 throw new Error ( `Curve prices only available for Lido` ) ;
135138
139+ let reserves ;
140+ if ( options . amount === undefined || options . amount === null ) {
141+ if ( baseContext . version !== "multiBase" ) {
142+ throw new Error (
143+ `--amount is required when pricing a legacy ${ options . armName } ARM` ,
144+ ) ;
145+ }
146+ reserves = await baseContext . arm . getReserves ( baseAddress , {
147+ blockTag : options . blockTag ?? "latest" ,
148+ } ) ;
149+ }
150+
151+ const dexAmount = resolveDexQuoteAmount ( {
152+ amount : options . amount ,
153+ liquidityAssets : reserves ?. liquidityAssets ?? reserves ?. [ 0 ] ,
154+ baseAssetReserve : reserves ?. baseAssetReserve ?? reserves ?. [ 1 ] ,
155+ buyLiquidity : parseSwapCap ( buyAmount ) ,
156+ sellLiquidity : parseSwapCap ( sellAmount ) ,
157+ liquidityDecimals,
158+ baseDecimals,
159+ } ) ;
160+ if ( options . amount === undefined || options . amount === null ) {
161+ log (
162+ `Using ${ dexAmount } as the DEX quote amount based on available reserves and price liquidity` ,
163+ ) ;
164+ }
165+
136166 // 2.1 Get latest market prices if no midPrice is provided
137167 referencePrices = inch
138168 ? // 2.1.b Otherwise, get prices from 1Inch
139169 await get1InchPrices (
140- options . amount ,
170+ dexAmount ,
141171 assets ,
142172 inchFee ,
143173 1 ,
@@ -147,25 +177,26 @@ const setPrices = async (options) => {
147177 : kyber
148178 ? // 2.1.c Or from Kyber if specified
149179 await getKyberPrices (
150- options . amount ,
180+ dexAmount ,
151181 assets ,
152182 baseDecimals ,
153183 liquidityDecimals ,
154184 )
155185 : // 2.1.d Or from Curve if specified
156186 await getCurvePrices ( {
157187 ...options ,
188+ amount : dexAmount ,
158189 poolAddress : addresses . mainnet . CurveNgStEthPool ,
159190 } ) ;
160191
161192 // Adjust price down if a wrapped asset like sUSDe or wstETH
162193 if ( shouldAdjustWrapped ) {
163- const amountIn = parseUnits ( options . amount . toString ( ) , baseDecimals ) ;
194+ const amountIn = parseUnits ( dexAmount , baseDecimals ) ;
164195 // The legacy convertToAsset path returns 18 decimals while the adapter
165196 // converts a base decimals input to liquidity decimals
166197 const convertedAssets =
167198 config . adapter === ZeroAddress
168- ? await convertToAsset ( baseAddress , options . amount , signer )
199+ ? await convertToAsset ( baseAddress , dexAmount , signer )
169200 : await (
170201 await adapterContract ( config . adapter , signer )
171202 ) . convertToAssets ( amountIn ) ;
0 commit comments