-
Notifications
You must be signed in to change notification settings - Fork 7
/
WagmiWrites.tsx
57 lines (51 loc) · 1.37 KB
/
WagmiWrites.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
import { WagmiMintExample } from '../../contracts/WagmiMintExample.sol'
import { getRandomInt } from '../utils/getRandomInt'
import {
Address,
useAccount,
useChainId,
useContractRead,
useContractWrite,
useWaitForTransaction,
} from 'wagmi'
export const WagmiWrites = () => {
const chainId = useChainId()
const { address, isConnected } = useAccount()
const { data, refetch } = useContractRead({
/**
* Spreading in a method will spread abi, address and args
* Hover over balanceOf and click go-to-definition should take you to the method definition in solidity if compiling from solidity
*/
...WagmiMintExample.read({ chainId }).balanceOf(address as Address),
enabled: isConnected,
})
const { writeAsync: writeMint, data: mintData } = useContractWrite({
/**
* Not calling the function will return abi and address without args
* This is useful for when you want to lazily call the function like in case of useContractWrite
*/
...WagmiMintExample.write({ chainId }).mint,
})
useWaitForTransaction({
hash: mintData?.hash,
onSuccess: (receipt) => {
console.log('minted', receipt)
refetch()
},
})
return (
<div>
<div>
<div>balance: {data?.toString()}</div>
</div>
<button
type='button'
onClick={() =>
writeMint(WagmiMintExample.write({ chainId }).mint(BigInt(getRandomInt())))
}
>
Mint
</button>
</div>
)
}