1
1
import React , { useEffect , useState } from "react" ;
2
- import { Client , getContract } from "viem" ;
3
- import { useAccount , useWaitForTransactionReceipt , useWriteContract } from "wagmi" ;
4
- import { getClient } from "@wagmi/core" ;
2
+ import { getContract } from "viem" ;
3
+ import { useAccount } from "wagmi" ;
5
4
import { ethers } from "ethers" ;
6
5
7
6
import { LarsKristoHellheads__factory } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads__factory" ;
8
- import { useEvmWalletSelectorContext } from "../wallet-selector/useEvmWalletSelectorContext" ;
9
7
import currency from "providers/currency" ;
10
8
import { ZeroXAddress } from "../wallet-selector/EvmWalletSelectorContext.types" ;
9
+ import evm from "providers/evm" ;
11
10
12
11
import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext" ;
13
12
import {
@@ -36,82 +35,117 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
36
35
isPending : false ,
37
36
isConfirmed : false ,
38
37
} ,
38
+ getTokenPrice : {
39
+ isLoading : false ,
40
+ } ,
39
41
} ) ;
40
42
41
- const { address : connectedAccountAddress , chainId } = useAccount ( ) ;
42
- const { wagmiConfig } = useEvmWalletSelectorContext ( ) ;
43
- const { data : hash , error : writeContractError , writeContract, isPending } = useWriteContract ( ) ;
44
- const {
45
- isLoading : isConfirming ,
46
- isSuccess : isConfirmed ,
47
- data,
48
- } = useWaitForTransactionReceipt ( {
49
- hash,
50
- } ) ;
51
-
52
- useEffect ( ( ) => {
53
- setActions ( ( prev ) => ( {
54
- ...prev ,
55
- buyToken : {
56
- isPending : isPending || isConfirming ,
57
- isConfirmed,
58
- transactionHash : data ?. transactionHash ,
59
- } ,
60
- } ) ) ;
61
- } , [ isPending , isConfirming , isConfirmed , data ] ) ;
43
+ const { address : connectedAccountAddress } = useAccount ( ) ;
44
+ const { publicClient } = evm ;
62
45
63
- if ( writeContractError ) {
64
- console . error ( writeContractError ) ;
65
- }
46
+ const getContractInstance = ( ) =>
47
+ getContract ( {
48
+ address : contractAddress as ZeroXAddress ,
49
+ abi : LarsKristoHellheads__factory . abi ,
50
+ client : publicClient ,
51
+ } ) ;
66
52
67
- const client = getClient ( wagmiConfig , { chainId } ) as Client ;
53
+ const connectedAccountIsOwner = ( ) => owner === connectedAccountAddress ;
68
54
69
55
const buyToken = async ( tokenId : number ) => {
70
56
try {
71
- await writeContract ( {
72
- address : contractAddress as ZeroXAddress ,
73
- abi : LarsKristoHellheads__factory . abi ,
74
- functionName : "buyToken" as "buyToken" ,
75
- args : [ BigInt ( tokenId ) ] ,
57
+ setActions ( ( prev ) => ( {
58
+ ...prev ,
59
+ buyToken : {
60
+ isPending : true ,
61
+ isConfirmed : false ,
62
+ } ,
63
+ } ) ) ;
64
+
65
+ const contract = getContractInstance ( ) ;
66
+
67
+ const hash = await contract . write . buyToken ( [ BigInt ( tokenId ) ] , {
76
68
account : connectedAccountAddress as ZeroXAddress ,
77
69
value : tokenPrice ?. rawValue ! ,
70
+ chain : undefined ,
78
71
} ) ;
72
+
73
+ console . log ( { hash } ) ;
74
+
75
+ const receipt = await publicClient . waitForTransactionReceipt ( { hash } ) ;
76
+
77
+ console . log ( { receipt } ) ;
78
+
79
+ setActions ( ( prev ) => ( {
80
+ ...prev ,
81
+ buyToken : {
82
+ isPending : false ,
83
+ isConfirmed : true ,
84
+ transactionHash : receipt . transactionHash ,
85
+ } ,
86
+ } ) ) ;
79
87
} catch ( error ) {
80
88
console . error ( error ) ;
81
89
}
82
90
} ;
83
91
84
- const getTokenPrice = async ( tokenId : number ) => {
92
+ const getTokenPrice = async ( tokenId : number , options ?: { excludeExchangeRate ?: boolean } ) => {
85
93
try {
86
- const contract = getContract ( {
87
- address : contractAddress as ZeroXAddress ,
88
- abi : LarsKristoHellheads__factory . abi ,
89
- client,
90
- } ) ;
94
+ setActions ( ( prev ) => ( {
95
+ ...prev ,
96
+ getTokenPrice : {
97
+ isLoading : true ,
98
+ } ,
99
+ } ) ) ;
100
+
101
+ const contract = getContractInstance ( ) ;
91
102
92
103
const rawValue = await contract . read . getTokenPrice ( [ BigInt ( tokenId ) ] ) ;
93
104
const formattedValue = ethers . formatEther ( rawValue ) ;
94
- const exchangeRate = await currency . getCoinCurrentPrice ( "ethereum" , "usd" ) ;
95
- const exchangeRateFormatted = currency . formatFiatCurrency ( Number ( formattedValue ) * exchangeRate ) ;
96
105
97
- setTokenPrice ( {
106
+ const values : TokenPrice = {
98
107
rawValue,
99
108
formattedValue,
100
- exchangeRate,
101
- exchangeRateFormatted,
102
- } ) ;
109
+ } ;
110
+
111
+ if ( ! options ?. excludeExchangeRate ) {
112
+ const exchangeRate = await currency . getCoinCurrentPrice ( "ethereum" , "usd" ) ;
113
+ const exchangeRateFormatted = currency . formatFiatCurrency ( Number ( formattedValue ) * exchangeRate ) ;
114
+ values . exchangeRate = exchangeRate ;
115
+ values . exchangeRateFormatted = exchangeRateFormatted ;
116
+ }
117
+
118
+ setTokenPrice ( values ) ;
119
+
120
+ setActions ( ( prev ) => ( {
121
+ ...prev ,
122
+ getTokenPrice : {
123
+ isLoading : false ,
124
+ } ,
125
+ } ) ) ;
126
+
127
+ return values ;
103
128
} catch ( error ) {
104
129
console . error ( error ) ;
130
+ setTokenPrice ( undefined ) ;
105
131
}
132
+
133
+ setActions ( ( prev ) => ( {
134
+ ...prev ,
135
+ getTokenPrice : {
136
+ isLoading : false ,
137
+ } ,
138
+ } ) ) ;
139
+
140
+ return {
141
+ rawValue : BigInt ( 0 ) ,
142
+ formattedValue : "0.00" ,
143
+ } ;
106
144
} ;
107
145
108
146
const royaltyInfo = async ( tokenId : number ) => {
109
147
try {
110
- const contract = getContract ( {
111
- address : contractAddress as ZeroXAddress ,
112
- abi : LarsKristoHellheads__factory . abi ,
113
- client,
114
- } ) ;
148
+ const contract = getContractInstance ( ) ;
115
149
116
150
const [ , rawValue ] = await contract . read . royaltyInfo ( [ BigInt ( tokenId ) , tokenPrice ! . rawValue ] ) ;
117
151
const percentage = Number ( ethers . formatEther ( rawValue ) ) / Number ( ethers . formatEther ( tokenPrice ! . rawValue ) ) ;
@@ -128,11 +162,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
128
162
129
163
const ownerOf = async ( tokenId : number ) => {
130
164
try {
131
- const contract = getContract ( {
132
- address : contractAddress as ZeroXAddress ,
133
- abi : LarsKristoHellheads__factory . abi ,
134
- client,
135
- } ) ;
165
+ const contract = getContractInstance ( ) ;
136
166
137
167
const result = await contract . read . ownerOf ( [ BigInt ( tokenId ) ] ) ;
138
168
@@ -154,7 +184,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
154
184
const contract = getContract ( {
155
185
address : address as ZeroXAddress ,
156
186
abi : LarsKristoHellheads__factory . abi ,
157
- client,
187
+ client : publicClient ,
158
188
} ) ;
159
189
160
190
const [ name , symbol ] = await Promise . all ( [ contract . read . name ( ) , contract . read . symbol ( ) ] ) ;
@@ -199,6 +229,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
199
229
tokenPrice,
200
230
royaltyInfo,
201
231
royalty,
232
+ connectedAccountIsOwner,
202
233
} ;
203
234
204
235
return < LarskristoHellheadsContext . Provider value = { props } > { children } </ LarskristoHellheadsContext . Provider > ;
0 commit comments