-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuyBoxNft.tsx
113 lines (105 loc) · 3.22 KB
/
buyBoxNft.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
import React from 'react';
import { usePrepareContractWrite, useContractWrite, useWaitForTransaction } from 'wagmi';
import { boxSalesContractConfig } from '@/src/generated';
import Image from 'next/image';
const etherToWei = (ether: string) => {
const weiString = (parseFloat(ether) * 10 ** 18).toString();
return BigInt(weiString);
};
const weiToEther = (wei: BigInt) => {
return (Number(wei) / 10 ** 18).toString();
};
const BuyBox: React.FC = () => {
const boxTypes = [
{ type: 'Bronze', args: [0] as const, value: etherToWei('0.01') },
{ type: 'Silver', args: [1] as const, value: etherToWei('0.05') },
{ type: 'Gold', args: [2] as const, value: etherToWei('0.1') },
];
const boxes = boxTypes.map((box) => {
const {
config,
error: prepareError,
isError: isPrepareError,
} = usePrepareContractWrite({
address: boxSalesContractConfig.address[80001],
abi: boxSalesContractConfig.abi,
functionName: 'buyBox',
args: box.args,
value: box.value,
});
const {
write,
data,
isError,
error,
isLoading: metamaskLoading,
} = useContractWrite({
...config,
onError: (error) => {
console.log(`Error buying ${box.type} box:`, error);
},
});
const { isLoading: transactionLoading, isSuccess } = useWaitForTransaction({
hash: data?.hash,
});
return {
...box,
write,
data,
metamaskLoading,
transactionLoading,
isSuccess,
error,
prepareError,
isPrepareError,
isError,
};
});
return (
<div className="space-y-2">
<div className="grid grid-cols-3 gap-3 justify-center items-center">
{boxes.map((box) => (
<div
key={box.type}
className="py-5 flex flex-col items-center gap-3 justify-between px-5"
>
<Image
src={`/img/boxes/${box.type}_box.png`.toLowerCase()}
alt={box.type}
width={116}
height={116}
/>
<span>{`${weiToEther(box.value)} MATIC`}</span>
<button disabled={!box.write} onClick={box.write}>
{box.transactionLoading ? `Buying ${box.type} Box...` : `Buy ${box.type} Box`}
</button>
<div className="flex h-16">
{box.metamaskLoading && <div>Opening Metamask</div>}
{box.isSuccess && (
<div>
<p className="text-lime-400">Success!</p>
<p>
<a
href={`https://mumbai.polygonscan.com/tx/${box.data?.hash}`}
target="_blank"
rel="noopener noreferrer"
className="text-blue-400"
>
Click to see
</a>
</p>
</div>
)}
<div className="text-red-500">
{(box.isPrepareError || box.isError) && (
<div>Error: {(box.prepareError || box.error)?.message.split('.')[0] + '.'}</div>
)}
</div>
</div>
</div>
))}
</div>
</div>
);
};
export default React.memo(BuyBox);