Skip to content

Commit 9869a9f

Browse files
authored
fix(swap): if swap fails in FoT router but not in regular router, throw an error (#875)
this is an alternative solution to hard coding forced FoT router tokens
1 parent 631f29d commit 9869a9f

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/hooks/useSwapCallback.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ function getSwapType(tokens: { [field in Field]?: Token }, isExactIn: boolean, c
4040
}
4141
}
4242

43-
// list of checksummed addresses that are forced to go through the FoT methods
44-
const FORCED_FOT_TOKENS = ['0xF0FAC7104aAC544e4a7CE1A55ADF2B5a25c65bD1']
45-
4643
// returns a function that will execute a swap, if the parameters are all valid
4744
// and the user has approved the slippage adjusted input amount for the trade
4845
export function useSwapCallback(
@@ -84,7 +81,6 @@ export function useSwapCallback(
8481
const routerContract: Contract = getRouterContract(chainId, library, account)
8582

8683
const path = trade.route.path.map(t => t.address)
87-
const isForcedFOT: boolean = path.some(tokenAddress => FORCED_FOT_TOKENS.indexOf(tokenAddress) !== -1)
8884

8985
const deadlineFromNow: number = Math.ceil(Date.now() / 1000) + deadline
9086

@@ -100,9 +96,7 @@ export function useSwapCallback(
10096
value: BigNumber | null = null
10197
switch (swapType) {
10298
case SwapType.EXACT_TOKENS_FOR_TOKENS:
103-
methodNames = isForcedFOT
104-
? ['swapExactTokensForTokensSupportingFeeOnTransferTokens']
105-
: ['swapExactTokensForTokens', 'swapExactTokensForTokensSupportingFeeOnTransferTokens']
99+
methodNames = ['swapExactTokensForTokens', 'swapExactTokensForTokensSupportingFeeOnTransferTokens']
106100
args = [
107101
slippageAdjustedInput.raw.toString(),
108102
slippageAdjustedOutput.raw.toString(),
@@ -122,9 +116,7 @@ export function useSwapCallback(
122116
]
123117
break
124118
case SwapType.EXACT_ETH_FOR_TOKENS:
125-
methodNames = isForcedFOT
126-
? ['swapExactETHForTokensSupportingFeeOnTransferTokens']
127-
: ['swapExactETHForTokens', 'swapExactETHForTokensSupportingFeeOnTransferTokens']
119+
methodNames = ['swapExactETHForTokens', 'swapExactETHForTokensSupportingFeeOnTransferTokens']
128120
args = [slippageAdjustedOutput.raw.toString(), path, recipient, deadlineFromNow]
129121
value = BigNumber.from(slippageAdjustedInput.raw.toString())
130122
break
@@ -139,9 +131,7 @@ export function useSwapCallback(
139131
]
140132
break
141133
case SwapType.EXACT_TOKENS_FOR_ETH:
142-
methodNames = isForcedFOT
143-
? ['swapExactTokensForETHSupportingFeeOnTransferTokens']
144-
: ['swapExactTokensForETH', 'swapExactTokensForETHSupportingFeeOnTransferTokens']
134+
methodNames = ['swapExactTokensForETH', 'swapExactTokensForETHSupportingFeeOnTransferTokens']
145135
args = [
146136
slippageAdjustedInput.raw.toString(),
147137
slippageAdjustedOutput.raw.toString(),
@@ -157,16 +147,29 @@ export function useSwapCallback(
157147
break
158148
}
159149

160-
const safeGasEstimates = await Promise.all(
150+
const safeGasEstimates: (BigNumber | undefined)[] = await Promise.all(
161151
methodNames.map(methodName =>
162152
routerContract.estimateGas[methodName](...args, value ? { value } : {})
163153
.then(calculateGasMargin)
164154
.catch(error => {
165155
console.error(`estimateGas failed for ${methodName}`, error)
156+
return undefined
166157
})
167158
)
168159
)
169160

161+
// we expect failures from left to right, so throw if we see failures
162+
// from right to left
163+
for (let i = 0; i < safeGasEstimates.length - 1; i++) {
164+
// if the FoT method fails, but the regular method does not, we should not
165+
// use the regular method. this probably means something is wrong with the fot token.
166+
if (BigNumber.isBigNumber(safeGasEstimates[i]) && !BigNumber.isBigNumber(safeGasEstimates[i + 1])) {
167+
throw new Error(
168+
'An error occurred. Please try raising your slippage. If that does not work, contact support.'
169+
)
170+
}
171+
}
172+
170173
const indexOfSuccessfulEstimation = safeGasEstimates.findIndex(safeGasEstimate =>
171174
BigNumber.isBigNumber(safeGasEstimate)
172175
)

src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import ThemeProvider, { FixedGlobalStyle, ThemedGlobalStyle } from './theme'
1818
const Web3ProviderNetwork = createWeb3ReactRoot(NetworkContextName)
1919

2020
function getLibrary(provider: any): Web3Provider {
21-
return new Web3Provider(provider)
21+
const library = new Web3Provider(provider)
22+
library.pollingInterval = 15000
23+
return library
2224
}
2325

2426
const GOOGLE_ANALYTICS_ID: string | undefined = process.env.REACT_APP_GOOGLE_ANALYTICS_ID

0 commit comments

Comments
 (0)