FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
uniswap_interface_sc/apps/web/src/utils/transfer.ts at main · stringcode86/uniswap_interface_sc · GitHub
stringcode86
uniswap_interface_sc
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
uniswap_interface_sc
/
apps
/
web
/
src
/
utils
/
transfer.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (96 loc) · 3.47 KB
Breadcrumbs
uniswap_interface_sc
/
apps
/
web
/
src
/
utils
/
transfer.ts
Copy path
File metadata and controls
113 lines (96 loc) · 3.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import
{
TransactionRequest
}
from
'@ethersproject/abstract-provider'
import
type
{
Web3Provider
}
from
'@ethersproject/providers'
import
{
Currency
,
CurrencyAmount
}
from
'@uniswap/sdk-core'
import
{
useWeb3React
}
from
'@web3-react/core'
import
{
useCallback
}
from
'react'
import
ERC20_ABI
from
'uniswap/src/abis/erc20.json'
import
{
Erc20
}
from
'uniswap/src/abis/types'
import
{
UniverseChainId
}
from
'uniswap/src/types/chains'
import
{
getContract
}
from
'utilities/src/contracts/getContract'
import
{
logger
}
from
'utilities/src/logger/logger'
import
{
useAsyncData
}
from
'utilities/src/react/hooks'
interface
TransferInfo
{
provider
?:
Web3Provider
account
?:
string
chainId
?:
UniverseChainId
currencyAmount
?:
CurrencyAmount
<
Currency
>
toAddress
?:
string
}
interface
TransferCurrencyParams
{
provider
:
Web3Provider
account
:
string
chainId
:
UniverseChainId
toAddress
:
string
tokenAddress
:
string
amountInWei
:
string
}
export
function
useCreateTransferTransaction
(
transferInfo
:
TransferInfo
)
{
const
transactionFetcher
=
useCallback
(
(
)
=>
{
return
getTransferTransaction
(
transferInfo
)
}
,
[
transferInfo
]
)
return
useAsyncData
(
transactionFetcher
)
.
data
}
async
function
getTransferTransaction
(
transferInfo
:
TransferInfo
)
:
Promise
<
TransactionRequest
|
undefined
>
{
const
{
provider
,
account
,
chainId
,
currencyAmount
,
toAddress
}
=
transferInfo
if
(
!
provider
||
!
account
||
!
chainId
||
!
currencyAmount
||
!
toAddress
)
{
return
undefined
}
const
currency
=
currencyAmount
.
currency
const
params
=
{
provider
,
account
,
chainId
,
toAddress
,
tokenAddress
:
currency
.
isNative
?
''
:
currency
.
address
,
amountInWei
:
currencyAmount
.
quotient
.
toString
(
)
,
}
return
currency
.
isNative
?
getNativeTransferRequest
(
params
)
:
getTokenTransferRequest
(
params
)
}
function
getNativeTransferRequest
(
params
:
TransferCurrencyParams
)
:
TransactionRequest
{
const
{
account
,
toAddress
,
amountInWei
,
chainId
}
=
params
return
{
from
:
account
,
to
:
toAddress
,
value
:
amountInWei
,
chainId
,
}
}
async
function
getTokenTransferRequest
(
transferParams
:
TransferCurrencyParams
,
)
:
Promise
<
TransactionRequest
|
undefined
>
{
const
{
provider
,
account
,
chainId
,
toAddress
,
tokenAddress
,
amountInWei
}
=
transferParams
const
tokenContract
=
getContract
(
tokenAddress
,
ERC20_ABI
,
provider
,
account
)
as
Erc20
try
{
const
populatedTransaction
=
await
tokenContract
.
populateTransaction
.
transfer
(
toAddress
,
amountInWei
,
{
from
:
account
,
}
)
return
{
...
populatedTransaction
,
chainId
}
}
catch
(
error
)
{
logger
.
error
(
error
,
{
tags
:
{
file
:
'transfer'
,
function
:
'getTokenTransferRequest'
,
}
,
extra
:
{
transferParams
}
,
}
)
}
return
undefined
}
// TODO: https://linear.app/uniswap/issue/WEB-3495/import-useasyncdata-from-mobile
export
function
useIsSmartContractAddress
(
address
?:
string
)
:
{
loading
:
boolean
isSmartContractAddress
:
boolean
}
{
const
{
provider
}
=
useWeb3React
(
)
const
fetchIsSmartContractAddress
=
useCallback
(
async
(
)
=>
{
if
(
!
address
)
{
return
false
}
const
code
=
await
provider
?.
getCode
(
address
)
// provider.getCode(address) will return a hex string if a smart contract is deployed at that address
// returning just 0x means there's no code and it's not a smart contract
return
code
!==
'0x'
}
,
[
provider
,
address
]
)
const
{
data
,
isLoading
}
=
useAsyncData
(
fetchIsSmartContractAddress
)
return
{
isSmartContractAddress
:
!
!
data
,
loading
:
isLoading
}
}
Back
|
FazBrowse Home
|
New Git URL