-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
270 lines (245 loc) · 9.59 KB
/
index.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { useState, useContext, useEffect } from 'react'
import { toast } from 'react-toastify'
import { EuiConfirmModal } from '@elastic/eui'
import BridgeAppContext from 'context/BridgeAppContext'
import ToastMessage from '../ToastMessage'
import WalletModal from '../WalletModal'
import { useActiveWeb3React, useBridgeAddress, useBridgeContract, useNetworkInfo, useTokenBalanceCallback } from 'hooks'
import { NetworkLogo, StyledButton, TokenAmount } from './styled'
import {
CasperClient,
CasperServiceByJsonRPC,
CLPublicKey,
CLValueBuilder,
decodeBase16,
DeployUtil,
RuntimeArgs,
} from 'casper-js-sdk'
import { SafeEventEmitterProvider } from 'casper-js-sdk/dist/services/ProviderTransport'
import { contractSimpleGetter } from 'casper-js-client-helper/dist/helpers/lib'
import { toPlainString, toWei } from 'utils'
import { CasperSignerConnector } from '@dotoracle/web3-react-caspersigner-connector'
import { isAddress } from 'web3-utils'
import UnknownSVG from 'assets/images/unknown.svg'
import { NATIVE_TOKEN_ADDERSS } from '../../constants'
import Web3 from 'web3'
interface TransferButtonProps {
receipient: string
onRefresh: () => void
}
function TransferButton(props: TransferButtonProps): JSX.Element {
const { receipient, onRefresh } = props
const { selectedToken, sourceNetwork, targetNetwork, tokenAmount, setTokenAmount, ledgerAddress } =
useContext(BridgeAppContext)
const { account: web3Account, chainId: web3ChainId, library: web3Library, connector } = useActiveWeb3React()
const [isValidAddress, setIsValidAddress] = useState(false)
const account = ledgerAddress !== '' ? ledgerAddress : web3Account
const chainId = ledgerAddress !== '' ? sourceNetwork?.chainId : web3ChainId
const networkInfo = useNetworkInfo(chainId)
const library = ledgerAddress !== '' ? new Web3.providers.HttpProvider(networkInfo?.rpcURL ?? '') : web3Library
const [isLoadingBalance, setIsLoadingBalance] = useState(false)
const [tokenBalance, setTokenBalance] = useState(0)
const tokenBalanceCallback = useTokenBalanceCallback(
selectedToken ? selectedToken.address : undefined,
selectedToken ? selectedToken.decimals : undefined,
account,
library,
tokenAmount,
networkInfo,
)
const [showWalletModal, setShowWalletModal] = useState(false)
const [isLoading, setLoading] = useState(false)
const [showConfirmModal, setShowConfirmModal] = useState(false)
const bridgeAddress = useBridgeAddress(chainId)
const bridgeContract = useBridgeContract(bridgeAddress)
const originNetwork = useNetworkInfo(selectedToken?.originChainId)
const loadTokenBalance = async () => {
setIsLoadingBalance(true)
const _tokenBalance = await tokenBalanceCallback()
setTokenBalance(_tokenBalance)
setIsLoadingBalance(false)
}
useEffect(() => {
loadTokenBalance()
setIsValidAddress(isAddress(receipient))
}, [account, chainId, selectedToken, receipient])
const genRanHex = (size: number) => [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')
const onTransferERC20Token = async () => {
try {
setLoading(true)
if (account && selectedToken && connector && networkInfo && bridgeContract) {
const decimal = selectedToken ? selectedToken.decimals : 18
const value = toWei(tokenAmount, decimal)
const senderKey = CLPublicKey.fromHex(account)
const deployParams = new DeployUtil.DeployParams(senderKey, networkInfo?.key ?? 'casper-test', 1, 1800000)
const contractHash = selectedToken.address
const contractHashAsByteArray = decodeBase16(contractHash)
const id = genRanHex(64).toLowerCase()
const fee = await contractSimpleGetter(networkInfo.rpcURL, contractHash, ['swap_fee'])
let deployRes
const deploy = DeployUtil.makeDeploy(
deployParams,
DeployUtil.ExecutableDeployItem.newStoredContractByHash(
contractHashAsByteArray,
'request_bridge_back',
RuntimeArgs.fromMap({
amount: CLValueBuilder.u256(toPlainString(value)),
fee: CLValueBuilder.u256(fee),
to_chainid: CLValueBuilder.u256(targetNetwork ? targetNetwork.chainId.toString() : '42'),
receiver_address: CLValueBuilder.string(receipient),
id: CLValueBuilder.string(id),
}),
),
DeployUtil.standardPayment(400000000),
)
if (connector instanceof CasperSignerConnector) {
const json = DeployUtil.deployToJson(deploy)
const casperClient = new CasperClient(networkInfo.rpcURL)
// Sign transcation using casper-signer.
const signature = await window.casperlabsHelper.sign(json, account)
const deployObject = DeployUtil.deployFromJson(signature)
if (deployObject.val instanceof DeployUtil.Deploy) {
deployRes = await casperClient.putDeploy(deployObject.val)
}
} else {
// @ts-ignore
const { torus } = connector
const casperService = new CasperServiceByJsonRPC(torus?.provider as SafeEventEmitterProvider)
deployRes = await casperService.deploy(deploy)
}
if (deployRes) {
toast.success(
<ToastMessage
color="success"
headerText="Success!"
bodyText="Please wait a few minutes to see ERC20 token in your wallet"
/>,
{
toastId: 'onTransferToken',
},
)
loadTokenBalance()
setTokenAmount(0)
} else {
toast.error(<ToastMessage color="danger" headerText="Error!" bodyText="Invalid signature" />, {
toastId: 'onTransferToken',
})
setLoading(false)
}
onRefresh()
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// we only care if the error is something _other_ than the user rejected the tx
if (error?.code !== 4001) {
const message = `Could not transfer this token to our bridge. Please try again.`
toast.error(<ToastMessage color="danger" headerText="Error!" bodyText={message} />, {
toastId: 'onTransferToken',
})
}
console.error(error)
} finally {
setLoading(false)
}
}
const onOpenConfirmModal = () => {
setLoading(true)
if (
selectedToken &&
originNetwork &&
targetNetwork &&
selectedToken.originContractAddress == NATIVE_TOKEN_ADDERSS &&
originNetwork.chainId != targetNetwork.chainId
) {
setShowConfirmModal(true)
} else {
onTransferERC20Token()
}
}
const onCancelTransfer = () => {
setShowConfirmModal(false)
setLoading(false)
}
return (
<>
{account ? (
<>
{selectedToken ? (
<StyledButton
fill
isLoading={isLoading || isLoadingBalance}
isDisabled={tokenAmount <= 0 || tokenAmount > tokenBalance || !isValidAddress}
onClick={onOpenConfirmModal}
>
{isValidAddress
? tokenAmount > 0 && tokenAmount <= tokenBalance
? `Transfer ${selectedToken.symbol} to bridge`
: 'Insufficient balance'
: 'Invalid receipient address'}
</StyledButton>
) : (
<StyledButton fill isDisabled>
Select a token to transfer
</StyledButton>
)}
</>
) : (
<>
<StyledButton fill onClick={() => setShowWalletModal(true)}>
Unlock Wallet
</StyledButton>
{showWalletModal && <WalletModal closeModal={() => setShowWalletModal(false)} />}
</>
)}
{showConfirmModal &&
selectedToken &&
tokenAmount > 0 &&
originNetwork &&
sourceNetwork &&
targetNetwork &&
selectedToken.originContractAddress == NATIVE_TOKEN_ADDERSS &&
originNetwork.chainId != targetNetwork.chainId && (
<EuiConfirmModal
title="Note!"
onCancel={onCancelTransfer}
onConfirm={onTransferERC20Token}
cancelButtonText="No, don't do it"
confirmButtonText="Yes, do it"
>
<>
<p style={{ lineHeight: 2 }}>
You are transfering{' '}
<TokenAmount>
{tokenAmount} {selectedToken.symbol}
</TokenAmount>{' '}
from{' '}
<strong>
<NetworkLogo src={sourceNetwork.logoURI ? sourceNetwork.logoURI : UnknownSVG}></NetworkLogo>
{sourceNetwork.name}
</strong>{' '}
to{' '}
<strong>
<NetworkLogo src={targetNetwork.logoURI ? targetNetwork.logoURI : UnknownSVG}></NetworkLogo>
{targetNetwork.name}
</strong>
.
<br />
This token was deployed originally on
<strong>
<NetworkLogo src={originNetwork.logoURI ? originNetwork.logoURI : UnknownSVG}></NetworkLogo>
{originNetwork.name}
</strong>
, you will receive DotOracle Wrapped {originNetwork.nativeCurrency.symbol} on
<strong>
<NetworkLogo src={targetNetwork.logoURI ? targetNetwork.logoURI : UnknownSVG}></NetworkLogo>
{targetNetwork.name}
</strong>
. Do you want to continue?
</p>
</>
</EuiConfirmModal>
)}
</>
)
}
export default TransferButton