Skip to content

Instantly share code, notes, and snippets.

@rdixon22
Created January 22, 2021 00:37
Show Gist options
  • Save rdixon22/50c5d542eb7f22326a35ca34d5764528 to your computer and use it in GitHub Desktop.
Save rdixon22/50c5d542eb7f22326a35ca34d5764528 to your computer and use it in GitHub Desktop.
Just a suggestion about a way to create OpenSea-style metadata for an ArtBlocks token.
{
"external_url": "https://api.artblocks.io/generator/18000234",
"name": "MyProject by Me #234",
"image": "https://api.artblocks.io/generator/18000234.jpg",
"attributes": [{
"trait_type": "serial_num",
"value": 234,
"max_value": 500,
"display_type": "number"
},
{
"trait_type": "series",
"value": 18,
"display_type": "number"
},
{
"trait_type": "color_scheme",
"value": "Grayscale"
},
{
"trait_type": "chaos_factor",
"value": 56,
"max_value": 100
},
{
"trait_type": "tile_shape",
"value": 14,
"max_value": 81
},
{
"trait_type": "animation_speed",
"value": 12,
"max_value": 60
}]
}
// Sample code for creating OpenSea-style metadata for an ArtBlocks token.
// Replace hardcoded params with real ones from the project.
let tokenData = {"hash":"0x31fa2d12d85e8aeea04e79dc9ed6d3fd2377de7d17fe4233e8c34aab4b48f0f6","tokenId":"18000234"};
function getMetadata(tokenData)
{
let metadata = new TokenMetadata(tokenData, "MyProject by Me", "https://api.artblocks.io/generator/", 500);
//
metadata.addTrait("color_scheme", "Grayscale");
metadata.addTrait("chaos_factor", 56, 100);
metadata.addTrait("tile_shape", 14, 81);
metadata.addTrait("animation_speed", 12, 60);
return metadata.getJson();
}
class TokenMetadata
{
constructor(tokenData, projectName, uriPrefix, maxSupply = null, desc = null, bgColor = null)
{
this.tokenHash = tokenData.hash;
this.tokenId = tokenData.tokenId;
this.traits = [];
this.tokenSerial = this.tokenId % 1000000;
this.addTrait("serial_num", this.tokenSerial, maxSupply, "number");
this.tokenSeries = Math.floor(this.tokenId / 1000000);
this.addTrait("series", this.tokenSeries, null, "number");
this.projectName = projectName;
this.uriPrefix = uriPrefix;
this.desc = desc;
this.bgColor = bgColor;
}
addTrait(traitType, value, maxValue = null, displayType = null)
{
// trait_type is the name. Separate multi-word names with an underscore,
// like color_scheme, which will appear on OpenSea as "Color Scheme".
// value can be a number or a string. If it's a string it always displays in the Properties section.
let obj = {
"trait_type": traitType,
"value": value
};
// if you include max_value the attribute displays in the Levels section as a progress bar
if (maxValue != null)
{
obj["max_value"] = maxValue;
}
// set display_type = "number" and it displays in the Stats section instead, like "1 of 250"
if (displayType != null)
{
obj["display_type"] = displayType;
}
this.traits.push(obj);
}
getJson()
{
let obj = {
"external_url": this.uriPrefix + this.tokenId,
"name": this.projectName + " #" + this.tokenSerial,
"image": this.uriPrefix + "images/" + this.tokenId + ".jpg", // or whatever
}
if (this.desc != null)
{
obj["description"] = this.desc;
}
if (this.bgColor != null)
{
obj["background_color"] = this.bgColor;
}
obj["attributes"] = this.traits;
return JSON.stringify(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment