Explore how to generate a pseudo-random SZNoun, fully on-chain, with just two contract calls.
The traits of SZNouns are determined by a Seed . To generate an on-chain image of a SZNoun, we will first generate a seed using the NounSeeder contract. Once we have our seed, we will then generate our SZNoun image using the NounsDescriptor contract.
The NounsSeeder contract is in charge of generating SZNoun seeds. The contract has a generateSeed function that accepts two arguments: nounId and INounsDescriptor .
nounId can be any valid uint256.INounsDescriptor is the address of the NounDescriptor contract.function generateSeed(uint256 nounId, INounsDescriptor descriptor) external view returns (Seed memory);
The Seed is a list of part ids that determine the SZNoun traits. It can be randomly generated using the NounSeeder contract or specified directly if you know which parts you want. Pseudo-random generation happens using the pending block's blockhash and passed in nounId.
A successful call will return a Seed with random traits:
struct Seed {
uint48 background;
uint48 body;
uint48 accessory;
uint48 head;
uint48 glasses;
}
Using your favorite web3 library, call the NounSeeder contract passing an arbitrary nounId and the NounDescriptor address as arguments.
const nounId = 1234;
const nounDescriptor = "0x11fb55d9580cdbfb83de3510ff5ba74309800ad1";
**const seed = await nounSeederContract.generateSeed(nounId, nounDescriptor);**
<aside>
💡 To make sure we get different Seed on every call, be sure to change the nounId on e every call as the blockhash will remain the same while the block is being mined (~15s).
</aside>
The NounsDescriptor contract is used to store/render SZNoun artwork. We will use it to generate an .svg image from our Seed.
function generateSVGImage(INounsSeeder.Seed memory seed) external view returns (string memory);
Using our Seed from step one, we can call the function as follows:
const nounId = 1234;
const nounDescriptor = "0x11fb55d9580cdbfb83de3510ff5ba74309800ad1";
const seed = await nounSeederContract.generateSeed(nounId, nounDescriptor);
**const svg = await nounsDescriptorContract.generateSVGImage(seed)**
A successful response will return a base64 encoded string representing the .svg image of the SZNoun determined by the Seed: