-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpage.tsx
140 lines (129 loc) · 3.73 KB
/
page.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
'use client';
import styles from './page.module.css';
import { useWallet } from '@aptos-labs/wallet-adapter-react';
import { useEffect, useState } from 'react';
import { createEntryPayload } from '@thalalabs/surf';
import { useSubmitTransaction, useWalletClient } from '@thalalabs/surf/hooks';
import { COIN_ABI } from '../abi/coin';
export default function Home() {
const {
isIdle,
reset,
isLoading: submitIsLoading,
error: submitError,
submitTransaction,
data: submitResult,
} = useSubmitTransaction();
const { client } = useWalletClient();
const wallet = useWallet();
useEffect(() => {
if (!wallet.connected) {
wallet.connect(wallet.wallets?.find((w) => w.name === 'Petra')?.name!);
}
}, []);
const [balanceMessage, setBalanceMessage] = useState();
const onRefresh = () => {
setBalanceMessage(undefined);
fetch('/balance').then(async (response) => {
const data = await response.json();
setBalanceMessage(data.message);
});
};
useEffect(() => {
onRefresh();
}, []);
const [result, setResult] = useState('');
const onSubmitForUseSubmitTransaction = async () => {
try {
const payload = createEntryPayload(COIN_ABI, {
function: 'transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
functionArguments: ['0x1', BigInt(1)],
});
await submitTransaction(payload);
onRefresh();
} catch (e) {
console.error('error', e);
}
};
const onSubmitForUseWalletClient = async () => {
try {
if (!client) return;
const result = await client.useABI(COIN_ABI).transfer({
arguments: ['0x1', BigInt(1)],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
setResult(result.hash);
onRefresh();
} catch (e) {
console.error('error', e);
}
};
return (
<main className={styles.main}>
<div className={styles.description}>
<p>
Get started by editing
<code className={styles.code}>src/app/page.tsx</code>
</p>
</div>
{
<div
style={{
fontSize: '20px',
}}
className={styles.center}
>
<div>{balanceMessage ?? 'loading balance of 0x1'}</div>
<div>
{`Wallet status: ${
wallet.connected ? 'connected' : 'disconnected'
}`}
</div>
{isIdle && (
<button
style={{
padding: '10px',
margin: '50px',
fontSize: '20px',
}}
onClick={onSubmitForUseSubmitTransaction}
>
Submit transaction: transfer 1 coin to 0x1 on testnet <br />{' '}
(useSubmitTransaction)
</button>
)}
{!isIdle && (
<button
style={{
padding: '10px',
margin: '50px',
fontSize: '20px',
}}
onClick={reset}
>
Reset <br /> (useSubmitTransaction)
</button>
)}
<button
style={{
padding: '10px',
margin: '50px',
fontSize: '20px',
}}
onClick={onSubmitForUseWalletClient}
>
Submit transaction: transfer 1 coin to 0x1 on testnet <br />{' '}
(useWalletClient)
</button>
{submitIsLoading && <div>running</div>}
{submitResult && <div>{`Success: ${submitResult.hash}`}</div>}
{submitError && <div>{`Failed: ${submitError}`}</div>}
{result && (
<div>{`Submission with useWalletClient Success: ${result}`}</div>
)}
</div>
}
</main>
);
}