micro-onchain-metadata-utils
v0.1.1
Published
On-chain JSON builder in solidity
Readme
µ on-chain metadata utils
Features:
- on-chain base-64 encoding for any content type
- on-chain json builder (supports arrays, nested content).
- re-exports openzeppelin strings support
this is the spiritual successor of https://github.com/ourzora/nft-editions/blob/main/contracts/IPublicSharedMetadata.sol the public shared metadata renderer libraries within nft-editions.
This project may be exported and deployed on-chain as a registry, but for now is being offered in library form.
API:
MetadataBuilder: Main class for metadata methodsgenerateJSON(MetadataBuilder.JSONItem[] items): Takes a list of items and makes a key value json object from them. Objects with empty values will be removed.generateEncodedJSON(MetadataBuilder.JSONItem[] items): Same as above but returns a data-uri instead of the raw JSON string.generateSVG(string contents, string viewBox, string width, string height): Generates a SVG tag and content for an SVG image.generateEncodedSVG(string contents, string viewBox, string width, string height): Same as above but generates a data-uri instead of the raw SVG.generateJSONArray(MetadataBuilder.JSONItem[] itemsArray): Takes an array of items and using thevalueandquoteflags generates a JSON array. Ignores thekeyvalue of the object and emptyvalueswill be removed from the array generated.encodeURI(string contentType, string content): Encodes the given content string as a base-64 data-uri with the given contentType.
MetadataMIMETypes: List of common metadata mime type constantsmimeJSON = "application/json"mimeSVG = "image/svg+xml"mimeTextPlain = "text/plain"
MetadataJSONKeys: List of common NFT Metadata JSON keyskeyName = "name"keyDescription = "description"keyImage = "image"keyAnimationURL = "animation_url"keyAttributes = "attributes"keyProperties = "properties";
- All methods are views returning strings unless otherwise noted.
import {MetadataBuilder} from "micro-onchain-metadata-utils/MetadataBuilder.sol";
import {MetadataJSONKeys} from "micro-onchain-metadata-utils/MetadataJSONKeys.sol";
contract NFTObject {
function contractURI() external view returns (string memory) {
// Build JSON object
MetadataBuilder.JSONItem[] memory items = new MetadataBuilder.JSONItem[](2);
items[0].key = MetadataJSONKeys.keyName;
items[0].value = "Contract Name";
items[0].quote = true;
items[1].key = MetadataJSONKeys.keyImage;
// Build Embedded SVG
items[1].value = MetadataBuilder.generateEncodedSVG({
contents: "<rect width='10' height='10' style='fill: red' />",
viewBox: '0 0 10 10',
width: '10',
height: '10',
});
items[1].quote = true;
// Build JSON Array
MetadataBuilder.JSONItem[] memory randomNumbersArray = MetadataBuilder.JSONItem[](2);
properties[0].value = '2';
properties[0].quote = false;
properties[1].value = '42';
properties[1].quote = false;
items[2].key = 'randomNumbers';
items[2].quote = false;
items[2].value = MetadataBuilder.generateJSON(properties);
return MetadataBuilder.generateEncodedJSON(items);
}
}