From 8578d8f98056c56bee62898a1388268aa50e6b0a Mon Sep 17 00:00:00 2001
From: chefjackson <116779127+chefjackson@users.noreply.github.com>
Date: Fri, 20 Dec 2024 22:45:57 +0800
Subject: [PATCH] fix: Fail to adjust gas cost (#11078)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on fixing error handling when adjusting token output by
gas cost in the `offChainQuoteProvider.ts` file of the `smart-router`
package.

### Detailed summary
- Updated the destructuring of `route` to include `output` and `input`.
- Introduced `quoteCurrency` to determine the correct currency based on
the wrapped amounts.
- Changed the `quote` assignment to use `finalQuote` created from
`CurrencyAmount.fromRawAmount`.
- Adjusted the `quote` and `quoteAdjustedForGas` properties in the
`routesWithQuote` push to use `finalQuote`.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
---
 .changeset/popular-bananas-breathe.md                  |  5 +++++
 .../evm/v3-router/providers/offChainQuoteProvider.ts   | 10 ++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
 create mode 100644 .changeset/popular-bananas-breathe.md

diff --git a/.changeset/popular-bananas-breathe.md b/.changeset/popular-bananas-breathe.md
new file mode 100644
index 0000000000000..ad788a860e20c
--- /dev/null
+++ b/.changeset/popular-bananas-breathe.md
@@ -0,0 +1,5 @@
+---
+'@pancakeswap/smart-router': patch
+---
+
+Fix error throws when adjusting token output by gas cost
diff --git a/packages/smart-router/evm/v3-router/providers/offChainQuoteProvider.ts b/packages/smart-router/evm/v3-router/providers/offChainQuoteProvider.ts
index 0fb47f7aaeef2..59a6d2c851334 100644
--- a/packages/smart-router/evm/v3-router/providers/offChainQuoteProvider.ts
+++ b/packages/smart-router/evm/v3-router/providers/offChainQuoteProvider.ts
@@ -45,7 +45,8 @@ export function createOffChainQuoteProvider(): QuoteProvider {
       const routesWithQuote: RouteWithQuote[] = []
       for (const route of routes) {
         try {
-          const { pools, amount } = route
+          const { pools, amount, output, input } = route
+          const quoteCurrency = amount.currency.wrapped.equals(input.wrapped) ? output : input
           let quote = amount
           const initializedTickCrossedList = Array(pools.length).fill(0)
           let quoteSuccess = true
@@ -75,17 +76,18 @@ export function createOffChainQuoteProvider(): QuoteProvider {
             continue
           }
 
+          const finalQuote = CurrencyAmount.fromRawAmount(quoteCurrency, quote.quotient)
           const { gasEstimate, gasCostInUSD, gasCostInToken } = gasModel.estimateGasCost(
             {
               ...route,
-              quote,
+              quote: finalQuote,
             },
             { initializedTickCrossedList },
           )
           routesWithQuote.push({
             ...route,
-            quote,
-            quoteAdjustedForGas: adjustQuoteForGas(quote, gasCostInToken),
+            quote: finalQuote,
+            quoteAdjustedForGas: adjustQuoteForGas(finalQuote, gasCostInToken),
             gasEstimate,
             gasCostInUSD,
             gasCostInToken,