@@ -2,32 +2,36 @@ import React, { useEffect, useState } from "react";
2
2
import { getContract } from "viem" ;
3
3
import { useAccount } from "wagmi" ;
4
4
import { ethers } from "ethers" ;
5
+ import axios from "axios" ;
5
6
6
7
import { LarsKristoHellheads__factory } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads__factory" ;
7
8
import currency from "providers/currency" ;
8
9
import { ZeroXAddress } from "../wallet-selector/EvmWalletSelectorContext.types" ;
9
10
import evm from "providers/evm" ;
10
11
11
- import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext " ;
12
+ import { ERC721Context } from "./ERC721Context " ;
12
13
import {
13
- LarskristoHellheadsContextActions ,
14
- LarskristoHellheadsContextControllerProps ,
15
- LarskristoHellheadsContextType ,
16
- LarskristoHellheadsContractValues ,
14
+ ERC721ContextActions ,
15
+ ERC721ContextControllerProps ,
16
+ ERC721ContextType ,
17
+ ERC721ContractValues ,
18
+ ItemMetadata ,
17
19
Royalty ,
18
20
TokenPrice ,
19
- } from "./LarskristoHellheadsContext .types" ;
20
-
21
- const SEPOLIA_TESTNET_ADDRESS = "0x2abbf9c29606b4c752944942aa9952ac2cdf552b" ;
22
- const ETHEREUM_MAINNET_ADDRESS = "0x5D003EBE7348d6D3aC1a397619ED2016711d7615" ;
23
-
24
- export const LarskristoHellheadsContextController = ( { children } : LarskristoHellheadsContextControllerProps ) => {
25
- const [ contractAddress , setContractAddress ] = useState < string | undefined > ( ) ;
26
- const [ contractValues , setContractValues ] = useState < LarskristoHellheadsContractValues > ( ) ;
21
+ } from "./ERC721Context .types" ;
22
+
23
+ export const ERC721ContextController = ( {
24
+ children ,
25
+ tokenId ,
26
+ address : contractAddress ,
27
+ } : ERC721ContextControllerProps ) => {
28
+ const [ contractValues , setContractValues ] = useState < ERC721ContractValues > ( ) ;
27
29
const [ owner , setOwner ] = useState < `0x${string } `> ( ) ;
28
30
const [ tokenPrice , setTokenPrice ] = useState < TokenPrice | undefined > ( ) ;
29
31
const [ royalty , setRoyaltyInfo ] = useState < Royalty | undefined > ( ) ;
30
- const [ actions , setActions ] = useState < LarskristoHellheadsContextActions > ( {
32
+ const [ tokenUri , setTokenUri ] = useState < string | undefined > ( ) ;
33
+ const [ tokenMetadata , setTokenMetadata ] = useState < ItemMetadata | undefined > ( ) ;
34
+ const [ actions , setActions ] = useState < ERC721ContextActions > ( {
31
35
fetchContractValues : {
32
36
isLoading : false ,
33
37
} ,
@@ -52,7 +56,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
52
56
53
57
const connectedAccountIsOwner = ( ) => owner === connectedAccountAddress ;
54
58
55
- const buyToken = async ( tokenId : number ) => {
59
+ const buyToken = async ( ) => {
56
60
try {
57
61
setActions ( ( prev ) => ( {
58
62
...prev ,
@@ -89,7 +93,53 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
89
93
}
90
94
} ;
91
95
92
- const getTokenPrice = async ( tokenId : number , options ?: { excludeExchangeRate ?: boolean } ) => {
96
+ const getTokenMetadata = async ( ) => {
97
+ try {
98
+ const result = await axios . get ( tokenUri ! , {
99
+ headers : {
100
+ "Content-Type" : "application/json" ,
101
+ } ,
102
+ } ) ;
103
+
104
+ setTokenMetadata ( {
105
+ ...result . data ,
106
+ id : tokenId ,
107
+ } ) ;
108
+ } catch ( error ) {
109
+ console . error ( error ) ;
110
+ }
111
+ } ;
112
+
113
+ const getTokenUri = async ( ) => {
114
+ try {
115
+ const contract = getContractInstance ( ) ;
116
+
117
+ const result = await contract . read . tokenURI ( [ BigInt ( tokenId ) ] ) ;
118
+
119
+ setTokenUri ( result ) ;
120
+ } catch ( error ) {
121
+ console . error ( error ) ;
122
+ }
123
+ } ;
124
+
125
+ const royaltyInfo = async ( ) => {
126
+ try {
127
+ const contract = getContractInstance ( ) ;
128
+
129
+ const [ , rawValue ] = await contract . read . royaltyInfo ( [ BigInt ( tokenId ) , tokenPrice ! . rawValue ] ) ;
130
+ const percentage = Number ( ethers . formatEther ( rawValue ) ) / Number ( ethers . formatEther ( tokenPrice ! . rawValue ) ) ;
131
+
132
+ setRoyaltyInfo ( {
133
+ rawValue,
134
+ percentage,
135
+ percentageFormatted : `${ ( percentage * 100 ) . toFixed ( 2 ) } %` ,
136
+ } ) ;
137
+ } catch ( error ) {
138
+ console . error ( error ) ;
139
+ }
140
+ } ;
141
+
142
+ const getTokenPrice = async ( options ?: { excludeExchangeRate ?: boolean } ) => {
93
143
try {
94
144
setActions ( ( prev ) => ( {
95
145
...prev ,
@@ -143,24 +193,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
143
193
} ;
144
194
} ;
145
195
146
- const royaltyInfo = async ( tokenId : number ) => {
147
- try {
148
- const contract = getContractInstance ( ) ;
149
-
150
- const [ , rawValue ] = await contract . read . royaltyInfo ( [ BigInt ( tokenId ) , tokenPrice ! . rawValue ] ) ;
151
- const percentage = Number ( ethers . formatEther ( rawValue ) ) / Number ( ethers . formatEther ( tokenPrice ! . rawValue ) ) ;
152
-
153
- setRoyaltyInfo ( {
154
- rawValue,
155
- percentage,
156
- percentageFormatted : `${ ( percentage * 100 ) . toFixed ( 2 ) } %` ,
157
- } ) ;
158
- } catch ( error ) {
159
- console . error ( error ) ;
160
- }
161
- } ;
162
-
163
- const ownerOf = async ( tokenId : number ) => {
196
+ const ownerOf = async ( ) => {
164
197
try {
165
198
const contract = getContractInstance ( ) ;
166
199
@@ -172,7 +205,7 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
172
205
}
173
206
} ;
174
207
175
- const fetchContractValues = async ( address : string ) => {
208
+ const fetchContractValues = async ( ) => {
176
209
setActions ( ( prev ) => ( {
177
210
...prev ,
178
211
fetchContractValues : {
@@ -181,15 +214,11 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
181
214
} ) ) ;
182
215
183
216
try {
184
- const contract = getContract ( {
185
- address : address as ZeroXAddress ,
186
- abi : LarsKristoHellheads__factory . abi ,
187
- client : publicClient ,
188
- } ) ;
217
+ const contract = getContractInstance ( ) ;
189
218
190
219
const [ name , symbol ] = await Promise . all ( [ contract . read . name ( ) , contract . read . symbol ( ) ] ) ;
191
220
192
- const values : LarskristoHellheadsContractValues = {
221
+ const values : ERC721ContractValues = {
193
222
name,
194
223
symbol,
195
224
} ;
@@ -208,16 +237,21 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
208
237
} ;
209
238
210
239
useEffect ( ( ) => {
211
- const address =
212
- process . env . NEXT_PUBLIC_DEFAULT_NETWORK_ENV === "testnet" ? SEPOLIA_TESTNET_ADDRESS : ETHEREUM_MAINNET_ADDRESS ;
213
- setContractAddress ( address ) ;
214
-
215
240
( async ( ) => {
216
- await fetchContractValues ( address ) ;
241
+ await getTokenUri ( ) ;
242
+ await fetchContractValues ( ) ;
217
243
} ) ( ) ;
218
244
} , [ ] ) ;
219
245
220
- const props : LarskristoHellheadsContextType = {
246
+ useEffect ( ( ) => {
247
+ if ( ! tokenUri ) return ;
248
+
249
+ ( async ( ) => {
250
+ await getTokenMetadata ( ) ;
251
+ } ) ( ) ;
252
+ } , [ tokenUri ] ) ;
253
+
254
+ const props : ERC721ContextType = {
221
255
fetchContractValues,
222
256
contractValues,
223
257
actions,
@@ -230,7 +264,10 @@ export const LarskristoHellheadsContextController = ({ children }: LarskristoHel
230
264
royaltyInfo,
231
265
royalty,
232
266
connectedAccountIsOwner,
267
+ getTokenUri,
268
+ getTokenMetadata,
269
+ tokenMetadata,
233
270
} ;
234
271
235
- return < LarskristoHellheadsContext . Provider value = { props } > { children } </ LarskristoHellheadsContext . Provider > ;
272
+ return < ERC721Context . Provider value = { props } > { children } </ ERC721Context . Provider > ;
236
273
} ;
0 commit comments