diff --git a/projects/sdk-wells/src/lib/Well.ts b/projects/sdk-wells/src/lib/Well.ts index db4a4a3ac9..4932a3195f 100644 --- a/projects/sdk-wells/src/lib/Well.ts +++ b/projects/sdk-wells/src/lib/Well.ts @@ -456,7 +456,7 @@ export class Well { } /** - * Estimage gas for `swapTo()` + * Estimate gas for `swapTo()` * @param fromToken The token to swap from * @param toToken The token to swap to * @param maxAmountIn The maximum amount of `fromToken` to spend @@ -465,7 +465,7 @@ export class Well { * @param deadline The transaction deadline in seconds (defaults to MAX_UINT256) * @return Estimated gas needed */ - async swapToEstimageGas( + async swapToGasEstimate( fromToken: Token, toToken: Token, maxAmountIn: TokenValue, diff --git a/projects/sdk-wells/src/lib/swap/Quote.ts b/projects/sdk-wells/src/lib/swap/Quote.ts index dd16000df0..f4463c98a0 100644 --- a/projects/sdk-wells/src/lib/swap/Quote.ts +++ b/projects/sdk-wells/src/lib/swap/Quote.ts @@ -17,6 +17,8 @@ export type QuotePrepareResult = { export type QuoteResult = { amount: TokenValue; + estimate: TokenValue; + gas: TokenValue; doSwap: () => Promise; doApproval?: () => Promise; }; @@ -92,11 +94,13 @@ export class Quote { let prevQuote: TokenValue = amount; let prevQuoteWSlippage: TokenValue = amount; + let prevQuoteGasEstimate: TokenValue = TokenValue.ZERO; for (const step of steps) { // console.log("Quote Step:", step.fromToken.symbol, " -> ", step.toToken.symbol); - const { quote, quoteWithSlippage } = await step.quote(isMultiReverse ? prevQuoteWSlippage : prevQuote, direction, slippage); + const { quote, quoteWithSlippage, quoteGasEstimate } = await step.quote(isMultiReverse ? prevQuoteWSlippage : prevQuote, direction, slippage); prevQuote = quote; prevQuoteWSlippage = quoteWithSlippage; + prevQuoteGasEstimate = prevQuoteGasEstimate.add(quoteGasEstimate) } this.fullQuote = prevQuote; @@ -104,6 +108,8 @@ export class Quote { return { amount: prevQuote, + estimate: prevQuoteWSlippage, + gas: prevQuoteGasEstimate, doApproval, doSwap }; diff --git a/projects/sdk-wells/src/lib/swap/SwapStep.ts b/projects/sdk-wells/src/lib/swap/SwapStep.ts index 97105f36b0..3cd09a116a 100644 --- a/projects/sdk-wells/src/lib/swap/SwapStep.ts +++ b/projects/sdk-wells/src/lib/swap/SwapStep.ts @@ -37,6 +37,8 @@ export class SwapStep { quoteResult: TokenValue | undefined; // The resulting quote after slippage applied quoteResultWithSlippage: TokenValue | undefined; + // The resulting quote's gas estimate + quoteGasEstimate: TokenValue | undefined; slippage: number; constructor(well: Well, fromToken: Token, toToken: Token) { @@ -53,14 +55,24 @@ export class SwapStep { if (this.direction == Direction.FORWARD) { this.quoteResult = await this.well.swapFromQuote(this.fromToken, this.toToken, amount); this.quoteResultWithSlippage = this.quoteResult.subSlippage(slippage); + try { + this.quoteGasEstimate = await this.well.swapFromGasEstimate(this.fromToken, this.toToken, amount, this.quoteResultWithSlippage); + } catch (e) { + this.quoteGasEstimate = TokenValue.ZERO; + } } else { this.quoteResult = await this.well.swapToQuote(this.fromToken, this.toToken, amount); this.quoteResultWithSlippage = this.quoteResult.addSlippage(slippage); + try { + this.quoteGasEstimate = await this.well.swapToGasEstimate(this.fromToken, this.toToken, amount, this.quoteResultWithSlippage); + } catch (e) { + this.quoteGasEstimate = TokenValue.ZERO; + } } this.hasQuoted = true; - return { quote: this.quoteResult, quoteWithSlippage: this.quoteResultWithSlippage }; + return { quote: this.quoteResult, quoteWithSlippage: this.quoteResultWithSlippage, quoteGasEstimate: this.quoteGasEstimate }; } swapSingle(amount: TokenValue, amountWithSlippage: TokenValue, recipient: string, deadline: number): SwapFromOp | SwapToOp {