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
ffe2bd31
Unverified
Commit
ffe2bd31
authored
Jan 24, 2022
by
Zach Pomerantz
Committed by
GitHub
Jan 24, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: track swap approvals (#3183)
* fix: track swap approvals * fix: type ambiguous return value
parent
cee4b8c7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
20 deletions
+27
-20
useApproveCallback.ts
src/hooks/useApproveCallback.ts
+17
-18
useApproval.ts
src/lib/hooks/useApproval.ts
+10
-2
No files found.
src/hooks/useApproveCallback.ts
View file @
ffe2bd31
import
{
TransactionResponse
}
from
'
@ethersproject/providers
'
import
{
Trade
}
from
'
@uniswap/router-sdk
'
import
{
Trade
}
from
'
@uniswap/router-sdk
'
import
{
Currency
,
CurrencyAmount
,
Percent
,
TradeType
}
from
'
@uniswap/sdk-core
'
import
{
Currency
,
CurrencyAmount
,
Percent
,
TradeType
}
from
'
@uniswap/sdk-core
'
import
{
Trade
as
V2Trade
}
from
'
@uniswap/v2-sdk
'
import
{
Trade
as
V2Trade
}
from
'
@uniswap/v2-sdk
'
...
@@ -6,31 +5,30 @@ import { Trade as V3Trade } from '@uniswap/v3-sdk'
...
@@ -6,31 +5,30 @@ import { Trade as V3Trade } from '@uniswap/v3-sdk'
import
useSwapApproval
,
{
useSwapApprovalOptimizedTrade
}
from
'
lib/hooks/swap/useSwapApproval
'
import
useSwapApproval
,
{
useSwapApprovalOptimizedTrade
}
from
'
lib/hooks/swap/useSwapApproval
'
import
{
ApprovalState
,
useApproval
}
from
'
lib/hooks/useApproval
'
import
{
ApprovalState
,
useApproval
}
from
'
lib/hooks/useApproval
'
import
{
useCallback
}
from
'
react
'
import
{
useCallback
}
from
'
react
'
import
invariant
from
'
tiny-invariant
'
import
{
TransactionType
}
from
'
../state/transactions/actions
'
import
{
TransactionType
}
from
'
../state/transactions/actions
'
import
{
useHasPendingApproval
,
useTransactionAdder
}
from
'
../state/transactions/hooks
'
import
{
useHasPendingApproval
,
useTransactionAdder
}
from
'
../state/transactions/hooks
'
export
{
ApprovalState
}
from
'
lib/hooks/useApproval
'
export
{
ApprovalState
}
from
'
lib/hooks/useApproval
'
function
useGetAndTrackApproval
(
getApproval
:
ReturnType
<
typeof
useApproval
>
[
1
])
{
const
addTransaction
=
useTransactionAdder
()
return
useCallback
(()
=>
{
return
getApproval
().
then
((
pending
)
=>
{
if
(
pending
)
{
const
{
response
,
tokenAddress
,
spenderAddress
:
spender
}
=
pending
addTransaction
(
response
,
{
type
:
TransactionType
.
APPROVAL
,
tokenAddress
,
spender
})
}
})
},
[
addTransaction
,
getApproval
])
}
// returns a variable indicating the state of the approval and a function which approves if necessary or early returns
// returns a variable indicating the state of the approval and a function which approves if necessary or early returns
export
function
useApproveCallback
(
export
function
useApproveCallback
(
amountToApprove
?:
CurrencyAmount
<
Currency
>
,
amountToApprove
?:
CurrencyAmount
<
Currency
>
,
spender
?:
string
spender
?:
string
):
[
ApprovalState
,
()
=>
Promise
<
void
>
]
{
):
[
ApprovalState
,
()
=>
Promise
<
void
>
]
{
const
token
=
amountToApprove
?.
currency
?.
isToken
?
amountToApprove
.
currency
:
undefined
const
[
approval
,
getApproval
]
=
useApproval
(
amountToApprove
,
spender
,
useHasPendingApproval
)
const
addTransaction
=
useTransactionAdder
()
return
[
approval
,
useGetAndTrackApproval
(
getApproval
)]
const
[
approval
,
approvalCallback
]
=
useApproval
(
amountToApprove
,
spender
,
useHasPendingApproval
)
const
approveCallback
=
useCallback
(()
=>
{
return
approvalCallback
().
then
((
response
?:
TransactionResponse
)
=>
{
if
(
response
)
{
invariant
(
token
&&
spender
)
addTransaction
(
response
,
{
type
:
TransactionType
.
APPROVAL
,
tokenAddress
:
token
.
address
,
spender
})
}
})
},
[
approvalCallback
,
token
,
spender
,
addTransaction
])
return
[
approval
,
approveCallback
]
}
}
export
function
useApprovalOptimizedTrade
(
export
function
useApprovalOptimizedTrade
(
...
@@ -47,6 +45,7 @@ export function useApproveCallbackFromTrade(
...
@@ -47,6 +45,7 @@ export function useApproveCallbackFromTrade(
|
Trade
<
Currency
,
Currency
,
TradeType
>
|
Trade
<
Currency
,
Currency
,
TradeType
>
|
undefined
,
|
undefined
,
allowedSlippage
:
Percent
allowedSlippage
:
Percent
)
{
):
[
ApprovalState
,
()
=>
Promise
<
void
>
]
{
return
useSwapApproval
(
trade
,
allowedSlippage
,
useHasPendingApproval
)
const
[
approval
,
getApproval
]
=
useSwapApproval
(
trade
,
allowedSlippage
,
useHasPendingApproval
)
return
[
approval
,
useGetAndTrackApproval
(
getApproval
)]
}
}
src/lib/hooks/useApproval.ts
View file @
ffe2bd31
...
@@ -44,7 +44,10 @@ export function useApproval(
...
@@ -44,7 +44,10 @@ export function useApproval(
amountToApprove
:
CurrencyAmount
<
Currency
>
|
undefined
,
amountToApprove
:
CurrencyAmount
<
Currency
>
|
undefined
,
spender
:
string
|
undefined
,
spender
:
string
|
undefined
,
useIsPendingApproval
:
(
token
?:
Token
,
spender
?:
string
)
=>
boolean
useIsPendingApproval
:
(
token
?:
Token
,
spender
?:
string
)
=>
boolean
):
[
ApprovalState
,
()
=>
Promise
<
TransactionResponse
|
undefined
>
]
{
):
[
ApprovalState
,
()
=>
Promise
<
{
response
:
TransactionResponse
;
tokenAddress
:
string
;
spenderAddress
:
string
}
|
undefined
>
]
{
const
{
chainId
}
=
useActiveWeb3React
()
const
{
chainId
}
=
useActiveWeb3React
()
const
token
=
amountToApprove
?.
currency
?.
isToken
?
amountToApprove
.
currency
:
undefined
const
token
=
amountToApprove
?.
currency
?.
isToken
?
amountToApprove
.
currency
:
undefined
...
@@ -53,7 +56,7 @@ export function useApproval(
...
@@ -53,7 +56,7 @@ export function useApproval(
const
tokenContract
=
useTokenContract
(
token
?.
address
)
const
tokenContract
=
useTokenContract
(
token
?.
address
)
const
approve
=
useCallback
(
async
()
:
Promise
<
TransactionResponse
|
undefined
>
=>
{
const
approve
=
useCallback
(
async
()
=>
{
function
logFailure
(
error
:
Error
|
string
):
undefined
{
function
logFailure
(
error
:
Error
|
string
):
undefined
{
console
.
warn
(
`
${
token
?.
symbol
||
'
Token
'
}
approval
failed
:
`, error)
console
.
warn
(
`
${
token
?.
symbol
||
'
Token
'
}
approval
failed
:
`, error)
return
return
...
@@ -85,6 +88,11 @@ export function useApproval(
...
@@ -85,6 +88,11 @@ export function useApproval(
.approve(spender, useExact ? amountToApprove.quotient.toString() : MaxUint256, {
.approve(spender, useExact ? amountToApprove.quotient.toString() : MaxUint256, {
gasLimit: calculateGasMargin(estimatedGas),
gasLimit: calculateGasMargin(estimatedGas),
})
})
.then((response) => ({
response,
tokenAddress: token.address,
spenderAddress: spender,
}))
.catch((error: Error) => {
.catch((error: Error) => {
logFailure(error)
logFailure(error)
throw error
throw error
...
...
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