-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapproveToken.tsx
86 lines (78 loc) · 2.2 KB
/
approveToken.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { CHAINS_BY_ID } from '@/constants/chains'
import { txErrorHandler } from './txErrorHandler'
import toast from 'react-hot-toast'
import ExplorerToastLink from '@components/ExplorerToastLink'
import { type Address, zeroAddress } from 'viem'
import { approveErc20Token } from '@/actions/approveErc20Token'
import { segmentAnalyticsEvent } from '@/contexts/SegmentAnalyticsProvider'
import { TranslatedText } from '@/components/ui/TranslatedText'
export const approveToken = async (
address: string,
chainId: number,
tokenAddress: string,
amount?: bigint
) => {
const currentChainName = CHAINS_BY_ID[chainId].name
let pendingPopup: any
let successPopup: any
const msg = (
<TranslatedText namespace="Pools.Other" id="Requesting approval" />
)
pendingPopup = toast(msg, {
id: 'approve-in-progress-popup',
duration: Infinity,
})
try {
segmentAnalyticsEvent(
`[Approval] initiates approval`,
{
chainId,
tokenAddress,
amount,
},
true
)
const approveTx = await approveErc20Token({
spender: address as Address,
chainId,
amount,
tokenAddress: tokenAddress as Address,
})
if (approveTx?.status === 'success') {
toast.dismiss(pendingPopup)
segmentAnalyticsEvent(`[Approval] successfully approves token`, {
chainId,
tokenAddress,
amount,
})
const successToastContent = (
<div>
<div>
<TranslatedText namespace="Pools" id="Successfully approved on" />{' '}
{currentChainName}
</div>
<ExplorerToastLink
transactionHash={approveTx?.transactionHash ?? zeroAddress}
chainId={chainId}
/>
</div>
)
successPopup = toast.success(successToastContent, {
id: 'approve-success-popup',
duration: 10000,
})
}
return approveTx?.transactionHash
} catch (error) {
segmentAnalyticsEvent(`[Approval] approval fails`, {
chainId,
tokenAddress,
amount,
errorCode: error.code,
})
toast.dismiss(pendingPopup)
console.log(`Transaction failed with error: ${error}`)
txErrorHandler(error)
throw error
}
}