-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
121 lines (114 loc) · 3.79 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
import { client } from '@/client';
import { getRelayFeeBySimulate } from '@/utils/simulate';
import { ISimulateGasFee } from '@bnb-chain/greenfield-js-sdk';
import { useWallets } from '@privy-io/react-auth';
import { useState } from 'react';
import { parseEther } from 'viem';
import { useAccount } from 'wagmi';
export const Withdraw = () => {
const { address } = useAccount();
const [transferoutInfo, setTransferoutInfo] = useState({
to: '0x0000000000000000000000000000000000000001',
amount: '0.001',
gasLimit: '210000',
});
const [simulateInfo, setSimulateInfo] = useState<ISimulateGasFee | null>(null);
const [transferOutRelayFee, setTransferOutRelayFee] = useState('');
const { wallets } = useWallets();
const wallet = wallets[0];
return (
<div>
<h2>Withdraw</h2>
to :
<input
value={transferoutInfo.to}
placeholder="0x00000000000000000"
onChange={(e) => {
setTransferoutInfo({ ...transferoutInfo, to: e.target.value });
}}
/>
<br />
amount:
<input
value={transferoutInfo.amount}
placeholder="amount"
onChange={(e) => {
setTransferoutInfo({ ...transferoutInfo, amount: e.target.value });
}}
/>
<br />
gas limit:
<input
value={transferoutInfo.gasLimit}
placeholder="gas limit"
onChange={(e) => {
setTransferoutInfo({ ...transferoutInfo, gasLimit: e.target.value });
}}
/>
<br />
<button
onClick={async () => {
if (!address) return;
const transferOutPayload = {
from: address,
to: transferoutInfo.to,
amount: {
amount: parseEther(`${Number(transferoutInfo.amount)}`).toString(),
denom: 'BNB',
},
};
console.log('transferOutPayload', transferOutPayload);
const transferOutTx = await client.crosschain.transferOut(transferOutPayload);
const simulateGasFee = await transferOutTx.simulate({
denom: 'BNB',
});
const relayFeeInfo = await client.crosschain.getParams();
console.log('transferout simuluate relayFee', relayFeeInfo);
console.log('transferout simulate gasFee', simulateGasFee);
const relayFee = relayFeeInfo.params
? getRelayFeeBySimulate(
relayFeeInfo.params.bscTransferOutAckRelayerFee,
relayFeeInfo.params.bscTransferOutRelayerFee,
)
: '0';
setTransferOutRelayFee(relayFee.toString());
setSimulateInfo(simulateGasFee);
const provider = await wallet.getEthereumProvider();
const accounts = await provider.request({
method: 'eth_requestAccounts',
});
const res = await transferOutTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateGasFee.gasLimit),
gasPrice: simulateGasFee.gasPrice,
payer: address,
granter: '',
signTypedDataCallback: async (addr: string, message: string) => {
console.log('message', message);
return await provider?.request({
method: 'eth_signTypedData_v4',
params: [addr, message],
});
}
}).then(res => {
console.log('transferOutTx res', res);
return res;
}).catch(e => {
console.log('transferOutTx error', e);
return e;
});
if (res.code === 0) {
alert('broadcast success');
}
}}
>
broadcast with simulate
</button>
<br />
relay fee: {transferOutRelayFee}
<br />
gas fee: {simulateInfo?.gasFee}
<br />
</div>
);
};