Skip to content

Commit 172907b

Browse files
authored
bump gas estimate by 20% when swapping on uniswap with ledger. For USDT / USDC swaps use pool that takes 0.01% (#849)
1 parent 81fe0a9 commit 172907b

2 files changed

Lines changed: 63 additions & 11 deletions

File tree

brownie/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ TODO...
55

66
## Usage
77

8+
Set etherscan env variable token
9+
export ETHERSCAN_TOKEN={TOKEN_VALUE}
10+
811
Start console and connect to forked hardhat:
912
`brownie console --network hardhat`
1013

dapp/src/hooks/useCurrencySwapper.js

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const useCurrencySwapper = ({
4646
const allowances = useStoreState(AccountStore, (s) => s.allowances)
4747
const balances = useStoreState(AccountStore, (s) => s.balances)
4848
const account = useStoreState(AccountStore, (s) => s.address)
49+
const connectorName = useStoreState(AccountStore, (s) => s.connectorName)
4950
const swapEstimations = useStoreState(ContractStore, (s) => s.swapEstimations)
5051
const swapsLoaded = swapEstimations && typeof swapEstimations === 'object'
5152
const selectedSwap = useStoreState(ContractStore, (s) => s.selectedSwap)
@@ -279,6 +280,11 @@ const useCurrencySwapper = ({
279280
let path
280281

281282
if (selectedCoin === 'dai') {
283+
/* Uniswap now supports 0.01% fees on stablecoin pools
284+
* TODO: can't get the 0.01% pool to work for DAI even though it is obviously available:
285+
*
286+
* - https://info.uniswap.org/#/pools/0x3416cf6c708da44db2624d63ea0aaef7113527c6 -> 0.01%
287+
*/
282288
if (isMintMode) {
283289
path = _encodeUniswapPath(
284290
[daiContract.address, usdtContract.address, ousdContract.address],
@@ -291,15 +297,19 @@ const useCurrencySwapper = ({
291297
)
292298
}
293299
} else if (selectedCoin === 'usdc') {
300+
/* Uniswap now supports 0.01% fees on stablecoin pools
301+
*
302+
* - https://info.uniswap.org/#/pools/0x5777d92f208679db4b9778590fa3cab3ac9e2168 -> 0.01%
303+
*/
294304
if (isMintMode) {
295305
path = _encodeUniswapPath(
296306
[usdcContract.address, usdtContract.address, ousdContract.address],
297-
[500, 500]
307+
[100, 500]
298308
)
299309
} else {
300310
path = _encodeUniswapPath(
301311
[ousdContract.address, usdtContract.address, usdcContract.address],
302-
[500, 500]
312+
[500, 100]
303313
)
304314
}
305315
} else {
@@ -359,11 +369,33 @@ const useCurrencySwapper = ({
359369

360370
const _swapUniswap = async (swapAmount, minSwapAmount, isGasEstimate) => {
361371
const isMintMode = swapMode === 'mint'
372+
373+
const increaseGasLimit = (gasLimit, percentageIncrease = 20) => {
374+
return Math.round(
375+
(parseInt(gasLimit.toString()) * (100 + percentageIncrease)) / 100
376+
)
377+
}
378+
379+
const swapWithIncreaseGasLimitOption = async (
380+
runEstimateFunction,
381+
runSwapFunction
382+
) => {
383+
if (isGasEstimate) {
384+
return await runEstimateFunction()
385+
} else {
386+
const txParams = {}
387+
388+
if (connectorName === 'Ledger') {
389+
txParams.gasLimit = increaseGasLimit(await runEstimateFunction())
390+
console.log('Ledger wallet detected. Increasing gasLimit by 20%')
391+
}
392+
393+
return await runSwapFunction(txParams)
394+
}
395+
}
396+
362397
if (selectedCoin === 'usdt') {
363-
return await (isGasEstimate
364-
? uniV3SwapRouter.estimateGas
365-
: uniV3SwapRouter
366-
).exactInputSingle([
398+
const singleCoinParams = [
367399
isMintMode ? usdtContract.address : ousdContract.address,
368400
isMintMode ? ousdContract.address : usdtContract.address,
369401
500, // pre-defined Factory fee for stablecoins
@@ -372,7 +404,20 @@ const useCurrencySwapper = ({
372404
swapAmount, // amountIn
373405
minSwapAmount, // amountOutMinimum
374406
0, // sqrtPriceLimitX96
375-
])
407+
]
408+
409+
const runUsdtGasEstimate = () =>
410+
uniV3SwapRouter.estimateGas.exactInputSingle(singleCoinParams)
411+
412+
return await swapWithIncreaseGasLimitOption(
413+
runUsdtGasEstimate,
414+
async (txParams) => {
415+
return await uniV3SwapRouter.exactInputSingle(
416+
singleCoinParams,
417+
txParams
418+
)
419+
}
420+
)
376421
}
377422

378423
const path = _encodePath()
@@ -388,10 +433,14 @@ const useCurrencySwapper = ({
388433
uniV3SwapRouter.interface.encodeFunctionData('exactInput', [params]),
389434
]
390435

391-
return await (isGasEstimate
392-
? uniV3SwapRouter.estimateGas
393-
: uniV3SwapRouter
394-
).exactInput(params)
436+
const runGasEstimate = () => uniV3SwapRouter.estimateGas.exactInput(params)
437+
438+
return await swapWithIncreaseGasLimitOption(
439+
runGasEstimate,
440+
async (txParams) => {
441+
return await uniV3SwapRouter.exactInput(params, txParams)
442+
}
443+
)
395444
}
396445

397446
const swapUniswapGasEstimate = async (swapAmount, minSwapAmount) => {

0 commit comments

Comments
 (0)