Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
e31f4e54
Unverified
Commit
e31f4e54
authored
Aug 11, 2023
by
Tina
Committed by
GitHub
Aug 11, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Fetch updated nonce before a UniswapX swap (#7120)
* add method to fetch updated gouda nonce * remove console log
parent
f47e1f16
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
0 deletions
+25
-0
useUniswapXSwapCallback.ts
src/hooks/useUniswapXSwapCallback.ts
+25
-0
No files found.
src/hooks/useUniswapXSwapCallback.ts
View file @
e31f4e54
import
{
BigNumber
}
from
'
@ethersproject/bignumber
'
import
*
as
Sentry
from
'
@sentry/react
'
import
{
SwapEventName
}
from
'
@uniswap/analytics-events
'
import
{
SwapEventName
}
from
'
@uniswap/analytics-events
'
import
{
signTypedData
}
from
'
@uniswap/conedison/provider/signing
'
import
{
signTypedData
}
from
'
@uniswap/conedison/provider/signing
'
import
{
Percent
}
from
'
@uniswap/sdk-core
'
import
{
Percent
}
from
'
@uniswap/sdk-core
'
...
@@ -25,6 +27,25 @@ if (UNISWAP_API_URL === undefined) {
...
@@ -25,6 +27,25 @@ if (UNISWAP_API_URL === undefined) {
throw
new
Error
(
`UNISWAP_API_URL must be a defined environment variable`
)
throw
new
Error
(
`UNISWAP_API_URL must be a defined environment variable`
)
}
}
// getUpdatedNonce queries the UniswapX service for the most up-to-date nonce for a user.
// The `nonce` exists as part of the Swap quote response already, but if a user submits back-to-back
// swaps without refreshing the quote (and therefore uses the same nonce), then the subsequent swaps will fail.
//
async
function
getUpdatedNonce
(
swapper
:
string
,
chainId
:
number
):
Promise
<
BigNumber
|
null
>
{
try
{
const
res
=
await
fetch
(
`
${
UNISWAP_API_URL
}
/nonce?address=
${
swapper
}
&chainId=
${
chainId
}
`
)
const
{
nonce
}
=
await
res
.
json
()
return
BigNumber
.
from
(
nonce
)
}
catch
(
e
)
{
Sentry
.
withScope
(
function
(
scope
)
{
scope
.
setTag
(
'
method
'
,
'
getUpdatedNonce
'
)
scope
.
setLevel
(
'
warning
'
)
Sentry
.
captureException
(
e
)
})
return
null
}
}
export
function
useUniswapXSwapCallback
({
export
function
useUniswapXSwapCallback
({
trade
,
trade
,
allowedSlippage
,
allowedSlippage
,
...
@@ -46,6 +67,8 @@ export function useUniswapXSwapCallback({
...
@@ -46,6 +67,8 @@ export function useUniswapXSwapCallback({
const
signDutchOrder
=
async
():
Promise
<
{
signature
:
string
;
updatedOrder
:
DutchOrder
}
>
=>
{
const
signDutchOrder
=
async
():
Promise
<
{
signature
:
string
;
updatedOrder
:
DutchOrder
}
>
=>
{
try
{
try
{
const
updatedNonce
=
await
getUpdatedNonce
(
account
,
trade
.
order
.
chainId
)
const
startTime
=
Math
.
floor
(
Date
.
now
()
/
1000
)
+
DEFAULT_START_TIME_PADDING_SECONDS
const
startTime
=
Math
.
floor
(
Date
.
now
()
/
1000
)
+
DEFAULT_START_TIME_PADDING_SECONDS
setTraceData
(
'
startTime
'
,
startTime
)
setTraceData
(
'
startTime
'
,
startTime
)
...
@@ -62,6 +85,8 @@ export function useUniswapXSwapCallback({
...
@@ -62,6 +85,8 @@ export function useUniswapXSwapCallback({
.
deadline
(
deadline
)
.
deadline
(
deadline
)
.
swapper
(
account
)
.
swapper
(
account
)
.
nonFeeRecipient
(
account
)
.
nonFeeRecipient
(
account
)
// if fetching the nonce fails for any reason, default to existing nonce from the Swap quote.
.
nonce
(
updatedNonce
??
trade
.
order
.
info
.
nonce
)
.
build
()
.
build
()
const
{
domain
,
types
,
values
}
=
updatedOrder
.
permitData
()
const
{
domain
,
types
,
values
}
=
updatedOrder
.
permitData
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment