1
1
import blockies from "ethereum-blockies-base64"
2
2
import type { Address } from "viem"
3
- import { sepolia } from "viem/chains"
3
+ import { hardhat } from "viem/chains"
4
4
import { createConfig , getPublicClient , http } from "@wagmi/core"
5
5
6
6
import Torch from "@/data/Torch.json"
7
7
8
8
const TORCH_CONTRACT_ADDRESS = Torch . address as Address
9
9
const TORCH_ABI = Torch . abi
10
+ const TORCH_BLOCK_NUMBER = Torch . blockNumber
10
11
11
12
export const config = createConfig ( {
12
- chains : [ sepolia ] ,
13
+ chains : [ hardhat ] ,
13
14
transports : {
14
- [ sepolia . id ] : http (
15
- `https://eth-sepolia.g.alchemy.com/v2/${ process . env . ALCHEMY_API_KEY } `
16
- ) ,
15
+ // [sepolia.id]: http(
16
+ // `https://eth-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
17
+ // ),
18
+ [ hardhat . id ] : http ( "http://127.0.0.1:8545" ) ,
17
19
} ,
18
20
} )
19
21
@@ -41,82 +43,31 @@ export type TorchHolderEvent = TorchHolder & {
41
43
event : TransferEvent
42
44
}
43
45
44
- const mockLogs = [
45
- {
46
- eventName : "Transfer" ,
47
- args : {
48
- from : "0x0000000000000000000000000000000000000000" ,
49
- to : "0x0e972f52C49e353Dc88C9f7F8e200c1cFE0d27b7" ,
50
- tokenId : BigInt ( 1 ) ,
51
- } ,
52
- address : "0xbcb60ff26412d7a27dde9b61f0655a207eae80ed" ,
53
- blockHash :
54
- "0x204c90926c265abbe78d321cbfa2e97a2185aa3804a46cab3960fe15652a90de" ,
55
- blockNumber : BigInt ( 8605647 ) ,
56
- data : "0x" ,
57
- logIndex : 121 ,
58
- removed : false ,
59
- topics : [
60
- "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ,
61
- "0x0000000000000000000000000000000000000000000000000000000000000000" ,
62
- "0x0000000000000000000000000e972f52c49e353dc88c9f7f8e200c1cfe0d27b7" ,
63
- "0x0000000000000000000000000000000000000000000000000000000000000001" ,
64
- ] ,
65
- transactionHash :
66
- "0xb549dc9540ec1c1146a5b9b7c6f9d62a207db8d1ebeb049687a74c53239fe323" ,
67
- transactionIndex : 87 ,
68
- } ,
69
- {
70
- eventName : "Transfer" ,
71
- args : {
72
- from : "0x0e972f52C49e353Dc88C9f7F8e200c1cFE0d27b7" ,
73
- to : "0x7bc34Ec96a2da5FbC3c0cA0530d989821241516D" ,
74
- tokenId : BigInt ( 1 ) ,
75
- } ,
76
- address : "0xbcb60ff26412d7a27dde9b61f0655a207eae80ed" ,
77
- blockHash :
78
- "0xfc9c50c6a7180a425e92d1774a88ee58dacb92a2a4554a013ec72ef14acfbe98" ,
79
- blockNumber : BigInt ( 8610141 ) ,
80
- data : "0x" ,
81
- logIndex : 30 ,
82
- removed : false ,
83
- topics : [
84
- "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ,
85
- "0x0000000000000000000000000e972f52c49e353dc88c9f7f8e200c1cfe0d27b7" ,
86
- "0x0000000000000000000000007bc34ec96a2da5fbc3c0ca0530d989821241516d" ,
87
- "0x0000000000000000000000000000000000000000000000000000000000000001" ,
88
- ] ,
89
- transactionHash :
90
- "0xac1ff7552b6844dfcd46424d7ed6a5bd32316cccd88bc280721f0ece96501219" ,
91
- transactionIndex : 19 ,
92
- } ,
93
- ]
94
-
95
46
export const getTransferEvents = async ( ) => {
96
47
const publicClient = getPublicClient ( config )
97
48
49
+ // Get the current block number to ensure consistent results
50
+ const currentBlock = await publicClient . getBlockNumber ( )
51
+
98
52
// Get Transfer events from the contract
99
53
// ERC721 Transfer event signature: Transfer(address indexed from, address indexed to, uint256 indexed tokenId)
100
- // const logs = await publicClient.getLogs({
101
- // address: TORCH_CONTRACT_ADDRESS,
102
- // event: {
103
- // type: "event",
104
- // name: "Transfer",
105
- // inputs: [
106
- // { name: "from", type: "address", indexed: true },
107
- // { name: "to", type: "address", indexed: true },
108
- // { name: "tokenId", type: "uint256", indexed: true },
109
- // ],
110
- // },
111
- // args: {
112
- // tokenId: BigInt(1), // Torch NFT token ID is always 1
113
- // },
114
- // fromBlock: "earliest",
115
- // toBlock: "latest",
116
- // })
117
-
118
- // TODO: Remove mock logs
119
- const logs = mockLogs
54
+ const logs = await publicClient . getLogs ( {
55
+ address : TORCH_CONTRACT_ADDRESS ,
56
+ event : {
57
+ type : "event" ,
58
+ name : "Transfer" ,
59
+ inputs : [
60
+ { name : "from" , type : "address" , indexed : true } ,
61
+ { name : "to" , type : "address" , indexed : true } ,
62
+ { name : "tokenId" , type : "uint256" , indexed : true } ,
63
+ ] ,
64
+ } ,
65
+ args : {
66
+ tokenId : BigInt ( 1 ) , // Torch NFT token ID is always 1
67
+ } ,
68
+ fromBlock : BigInt ( TORCH_BLOCK_NUMBER ) || "earliest" ,
69
+ toBlock : currentBlock ,
70
+ } )
120
71
121
72
// Process logs and get timestamps
122
73
const transferEvents : TransferEvent [ ] = [ ]
@@ -150,8 +101,32 @@ const getHolderEvents = async (
150
101
) => {
151
102
return transferEvents . map < TorchHolderEvent > ( ( event ) => {
152
103
const holderMetadata = torchHolderMap [ event . to . toLowerCase ( ) ]
104
+
105
+ // If the torch was transferred to the zero address (burned), create a special holder entry
106
+ if ( event . to === "0x0000000000000000000000000000000000000000" ) {
107
+ return {
108
+ address : event . to ,
109
+ name : "Burned" ,
110
+ role : "Torch has been burned" ,
111
+ twitter : "" ,
112
+ event,
113
+ }
114
+ }
115
+
116
+ // If we have metadata for this holder, use it
117
+ if ( holderMetadata ) {
118
+ return {
119
+ ...holderMetadata ,
120
+ event,
121
+ }
122
+ }
123
+
124
+ // If no metadata found, create a fallback entry
153
125
return {
154
- ...holderMetadata ,
126
+ address : event . to ,
127
+ name : `Unknown Holder (${ formatAddress ( event . to ) } )` ,
128
+ role : "Previous torch holder" ,
129
+ twitter : "" ,
155
130
event,
156
131
}
157
132
} )
@@ -166,16 +141,20 @@ export const getHolders = async (
166
141
transferEvents
167
142
)
168
143
169
- return torchHoldersEvents . map ( ( event ) => {
170
- return {
171
- ...event ,
172
- }
173
- } )
144
+ return torchHoldersEvents
174
145
}
175
146
176
147
export const getCurrentHolderAddress = async ( ) => {
177
148
const publicClient = getPublicClient ( config )
178
149
150
+ // First check if the torch is burned
151
+ const isBurned = await isTorchBurned ( )
152
+
153
+ if ( isBurned ) {
154
+ return "0x0000000000000000000000000000000000000000" as Address
155
+ }
156
+
157
+ // If not burned, get the current holder
179
158
const currentHolderAddress = ( await publicClient . readContract ( {
180
159
address : TORCH_CONTRACT_ADDRESS ,
181
160
abi : TORCH_ABI ,
@@ -185,6 +164,18 @@ export const getCurrentHolderAddress = async () => {
185
164
return currentHolderAddress
186
165
}
187
166
167
+ export const isTorchBurned = async ( ) => {
168
+ const publicClient = getPublicClient ( config )
169
+
170
+ const isBurned = ( await publicClient . readContract ( {
171
+ address : TORCH_CONTRACT_ADDRESS ,
172
+ abi : TORCH_ABI ,
173
+ functionName : "isBurned" ,
174
+ } ) ) as boolean
175
+
176
+ return isBurned
177
+ }
178
+
188
179
export const getBlockieImage = ( address : Address ) => {
189
180
return blockies ( address )
190
181
}
0 commit comments