Skip to content

Commit f794f27

Browse files
committed
add metadata generation
1 parent e4881ba commit f794f27

3 files changed

Lines changed: 109 additions & 45 deletions

File tree

components/Introspection.tsx

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
'use client';
2-
import React, {Suspense, useEffect, useState} from "react";
2+
import React, { useEffect, useRef, useState} from "react";
33
import {useControls} from "leva";
4-
import {Canvas} from "@react-three/fiber";
5-
import {Bounds, OrbitControls} from "@react-three/drei";
6-
import {Vector3} from "three";
7-
import {MainScene} from "@/components/MainScene";
8-
import {Effects} from "@/components/Effects";
94
import {parseDataset} from "@/utils/parse-dataset";
105
import {BlockDataset} from "@/types/block-dataset";
116
import {getTraitConfiguration} from "@/utils/trait-configuration";
@@ -14,11 +9,17 @@ import {PaletteName} from "@/types/palette-name";
149
import {patterns} from "@/constants/patterns";
1510
import {Theme} from "@/types/theme";
1611

12+
type Attribute = {
13+
trait_type: string;
14+
value: string;
15+
};
16+
1717
export function Introspection() {
1818
const [parsedBlock, setParsedBlock] = useState<BlockDataset | undefined>(undefined);
1919
const startBlock = 236425343;
2020
const endBlock = 236435967;
2121
const [blockId, setBlockId] = useState(startBlock);
22+
const [metadatum, setMetadatum] = useState<{ [key: number]: Attribute[] }>({});
2223

2324
const controls = useControls({
2425
blockId: {
@@ -40,6 +41,55 @@ export function Introspection() {
4041
render: false,
4142
});
4243
const traitConfiguration = parsedBlock ? getTraitConfiguration(parsedBlock) : undefined;
44+
const nbMintAttempt = parsedBlock?.processedTransactions.filter(t => t.isMintAttempt).length || 0;
45+
const nbError = parsedBlock?.processedTransactions.filter(t => t.isError).length || 0;
46+
const nbTransaction = parsedBlock?.processedTransactions.length || 0;
47+
48+
const attributes: Attribute[] = [
49+
{
50+
"trait_type": "Block",
51+
"value": parsedBlock?.meta.blockSlot.toString() || ""
52+
},
53+
{
54+
"trait_type": "Mint Attempts",
55+
"value": nbMintAttempt.toString() || ""
56+
},
57+
{
58+
"trait_type": "Failed Transactions",
59+
"value": nbError.toString() || ""
60+
},
61+
{
62+
"trait_type": "Transactions",
63+
"value": nbTransaction.toString() || ""
64+
},
65+
{
66+
"trait_type": "Introspection Theme",
67+
"value": traitConfiguration?.theme || ""
68+
},
69+
{
70+
"trait_type": "Introspection Palette",
71+
"value": traitConfiguration?.palette || ""
72+
},
73+
{
74+
"trait_type": "Introspection Pattern",
75+
"value": traitConfiguration?.pattern || ""
76+
},
77+
];
78+
79+
if (parsedBlock?.meta?.blockSlot && traitConfiguration && !metadatum[parsedBlock.meta.blockSlot]) {
80+
setMetadatum(prev => ({
81+
...prev,
82+
[parsedBlock.meta.blockSlot]: attributes
83+
}))
84+
}
85+
86+
if (blockId % 100 === 0 || blockId === endBlock) {
87+
console.log(Object.keys(metadatum).length);
88+
}
89+
90+
if (blockId === endBlock) {
91+
console.log(metadatum);
92+
}
4393

4494
if (traitConfiguration) {
4595
traitConfiguration.palette = controls.ovrPalette ? controls.palette : traitConfiguration.palette;
@@ -76,47 +126,15 @@ export function Introspection() {
76126
setBlockId(controls.blockId)
77127
}, [controls.blockId]);
78128

129+
useEffect(() => {
130+
if (controls.render) {
131+
tryIncrementId();
132+
}
133+
}, [metadatum, controls.render]);
134+
79135
return (
80136
<div className='w-[4096px] h-[4096px]'>
81-
<Canvas
82-
gl={{ preserveDrawingBuffer: true, antialias: true }}
83-
dpr={[1, 2]}
84-
orthographic
85-
>
86-
<color attach="background" args={[isDarkMode ? '#1c1c1c' : '#ffffff']}/>
87-
<hemisphereLight
88-
position={new Vector3(-100,1000,200)}
89-
intensity={4}
90-
color={'#ffffff'}
91-
groundColor={'#8c8c8c'}
92-
/>
93-
<Effects is4k={true}/>
94-
<Suspense fallback={null}>
95-
{((parsedBlock && traitConfiguration) && (
96-
<Bounds maxDuration={0} fit clip observe margin={1.1}>
97-
<MainScene
98-
traitConfiguration={traitConfiguration}
99-
beautiful={true}
100-
position={new Vector3()}
101-
parsedBlock={parsedBlock}
102-
increaseId={tryIncrementId}
103-
render={controls.render}
104-
/>
105-
</Bounds>
106-
))}
107-
</Suspense>
108-
<OrbitControls
109-
enableRotate={false}
110-
enableZoom={false}
111-
enablePan={false}
112-
enableDamping={false}
113-
makeDefault
114-
minDistance={0}
115-
maxDistance={Infinity}
116-
minPolarAngle={Math.PI / 4.5}
117-
maxPolarAngle={Math.PI / 4.5}
118-
/>
119-
</Canvas>
137+
120138
</div>
121139
);
122140
}

metadata.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Metadata generation
2+
3+
1. Start the project
4+
2. Open the app and the developer tool in the console tab
5+
3. Click render
6+
4. Wait until all the metadata are generated
7+
5. Copy the last object logged containing the metadata into a metadata.json file
8+
6. Start the scripts/generate-metadata.js

scripts/generate-metadata.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const outputDir = '../metadata/';
5+
6+
if (!fs.existsSync(outputDir)) {
7+
fs.mkdirSync(outputDir, { recursive: true });
8+
}
9+
10+
// Function to read the input JSON file and process its content
11+
function processInputFile(inputFile) {
12+
fs.readFile(inputFile, 'utf8', (err, data) => {
13+
if (err) {
14+
console.error('Error reading the input file:', err);
15+
return;
16+
}
17+
18+
try {
19+
const obj = JSON.parse(data);
20+
21+
// Iterate over each key in the object
22+
for (const key in obj) {
23+
if (obj.hasOwnProperty(key)) {
24+
const filePath = path.join(outputDir, `${key}.json`);
25+
const content = JSON.stringify(obj[key], null, 4);
26+
27+
// Write to a file named as the key
28+
fs.writeFileSync(filePath, content);
29+
}
30+
}
31+
} catch (parseError) {
32+
console.error('Error parsing the input JSON:', parseError);
33+
}
34+
});
35+
}
36+
37+
// Replace 'path/to/input.json' with the actual path of your input JSON file
38+
processInputFile('../metadata.json');

0 commit comments

Comments
 (0)