Skip to content

Commit

Permalink
fix: deep link by swapping sooner after user interaction (Uniswap#534)
Browse files Browse the repository at this point in the history
* build: [email protected]

* fix: use conedison/sendTransaction when able

* build: upgrade conedison again

* test: conedison import

* fix: nits
  • Loading branch information
zzmp authored Mar 6, 2023
1 parent 7220284 commit 17ac9d3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module.exports = {

// Jest does not always resolve src/test (probably because of babel's TypeScript transpilation):
'^test/*': '<rootDir>/src/test',

'@uniswap/conedison/format': '@uniswap/conedison/dist/format.js',
'@uniswap/conedison/provider': '@uniswap/conedison/dist/provider.js',
'@uniswap/conedison/provider': '@uniswap/conedison/dist/provider/index.js',
},
setupFiles: ['<rootDir>/test/setup.ts'],
setupFilesAfterEnv: ['<rootDir>/test/setup-jest.ts'],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@fontsource/inter": "^4.5.1",
"@popperjs/core": "^2.4.4",
"@reduxjs/toolkit": "^1.6.1",
"@uniswap/conedison": "^1.3.0",
"@uniswap/conedison": "^1.5.1",
"@uniswap/permit2-sdk": "^1.2.0",
"@uniswap/redux-multicall": "^1.1.8",
"@uniswap/router-sdk": "^1.3.0",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ export const BETTER_TRADE_LESS_HOPS_THRESHOLD = new Percent(JSBI.BigInt(50), BIP
export const ZERO_PERCENT = new Percent('0')
export const TWO_PERCENT = new Percent(JSBI.BigInt(200), BIPS_BASE)
export const ONE_HUNDRED_PERCENT = new Percent('1')

// gas margin to ensure successful transactions
export const TX_GAS_MARGIN = 0.2
2 changes: 1 addition & 1 deletion src/hooks/usePermitAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { signTypedData } from '@uniswap/conedison/provider'
import { signTypedData } from '@uniswap/conedison/provider/signing'
import { AllowanceTransfer, MaxAllowanceTransferAmount, PERMIT2_ADDRESS, PermitSingle } from '@uniswap/permit2-sdk'
import { CurrencyAmount, Token } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
Expand Down
13 changes: 3 additions & 10 deletions src/hooks/useUniversalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BigNumber } from '@ethersproject/bignumber'
import { TransactionRequest, TransactionResponse } from '@ethersproject/providers'
import { t } from '@lingui/macro'
import { sendTransaction } from '@uniswap/conedison/provider/index'
import { Percent } from '@uniswap/sdk-core'
import { SwapRouter, UNIVERSAL_ROUTER_ADDRESS } from '@uniswap/universal-router-sdk'
import { FeeOptions, toHex } from '@uniswap/v3-sdk'
import { useWeb3React } from '@web3-react/core'
import { ErrorCode } from 'constants/eip1193'
import { TX_GAS_MARGIN } from 'constants/misc'
import { SwapError } from 'errors'
import { useCallback } from 'react'
import { InterfaceTrade } from 'state/routing/types'
import { calculateGasMargin } from 'utils/calculateGasMargin'
import isZero from 'utils/isZero'
import { getReason, swapErrorToUserReadableMessage } from 'utils/swapErrorToUserReadableMessage'

Expand Down Expand Up @@ -68,15 +69,7 @@ export function useUniversalRouterSwapCallback(trade: InterfaceTrade | undefined
...(value && !isZero(value) ? { value: toHex(value) } : {}),
}

let gasEstimate: BigNumber
try {
gasEstimate = await provider.estimateGas(tx)
} catch (gasError) {
console.warn(gasError)
throw new SwapError({ header: t`Swap Error`, message: t`Your swap is expected to fail` })
}
const gasLimit = calculateGasMargin(gasEstimate)
response = await provider.getSigner().sendTransaction({ ...tx, gasLimit })
response = await sendTransaction(provider, tx, TX_GAS_MARGIN)
} catch (swapError) {
if (didUserReject(swapError)) {
return null
Expand Down
2 changes: 1 addition & 1 deletion test/setup-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.mock('@uniswap/conedison/format', () => ({
const MOCK_TYPED_DATA_SIG =
'0x1befd08fcc4085dc484346d69fd15659616522454a33e66e7b0f6917379ab888236304ebed307813208bf004da04d998dcd15a8f83241d033e4040adc4b0b5311b'

jest.mock('@uniswap/conedison/provider', () => ({
jest.mock('@uniswap/conedison/provider/signing', () => ({
signTypedData: () => Promise.resolve(MOCK_TYPED_DATA_SIG),
}))

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3518,10 +3518,10 @@
resolved "https://registry.yarnpkg.com/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz#aa58042711d6e3275dd37dc597e5d31e8c290a44"
integrity sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==

"@uniswap/conedison@^1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@uniswap/conedison/-/conedison-1.3.0.tgz#998aca2bad27f0780a05b40e4512acfcadfece79"
integrity sha512-zpZ52svBJ2btwl09mLOw7HlBxFDuYAjAZXLAR7WQZJeRgjD1yD2QuI3v7JliXvHzJh3ePYH6820EMp7xQbdAGQ==
"@uniswap/conedison@^1.5.1":
version "1.5.1"
resolved "https://registry.yarnpkg.com/@uniswap/conedison/-/conedison-1.5.1.tgz#91527cc9928ce0187f30a5eb4abb705b8f0cd013"
integrity sha512-VJqUW4l54QVj5a4vAzAlWWd193iCcT8HMugFPB28S2Uqhs2elAg/RDQmiPOf9TOFB635MdBlD0S6xUuqo7FB4A==

"@uniswap/default-token-list@^2.0.0":
version "2.2.0"
Expand Down

0 comments on commit 17ac9d3

Please sign in to comment.