npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pouns-sdk

v1.0.2

Published

SDK for working with Nouns on-chain NFTs on Polygon (Pouns)

Downloads

4

Readme

Nouns on Polygon (Pouns) NFTs SDK

Generate pixel-art images with data stored on Polygon blockchain.

Objectives

Nouns is a generative non-fungible token project on the Ethereum blockchain. Nouns are 32x32 pixel characters based on people, places, and things. There is a rich eco-system around this project - REST API services, derivative art etc. All of the images are in the public domain.

Making changes to this data however is too expensive (Ethereum blockchain fees) or impossible (contract ownership), so I deployed all the art-related contracts (DAO and sells related contracts excluded) to the Polygon testnet (Mumbai) and mainnet. You can think about this as a Read-Only storage, decentrilized, faster and more secure than IPFS.

The generation of the images is by read-only transactions to the blockchain, so it does not cost any fees and does not require Ethereum wallet as Metamask to be connected. This however require direct RPC connection to the blockchain, so you will need some RPC node provider. By default Alchemy is used, but you can change it your project. Good candidate for example is GetBlock.

Current library capsulates and isolates all the complexity of blockchain access. Simple methods for generating SVG images are provided.

Used libraries and services

Installation

npm install pouns-sdk

Development

git clone https://github.com/zh/pouns-sdk.git
cd pouns-sdk
npm install

Testing

There are some basic tests implemented. The testing does not require connection to Internet.

npm test

Provided methods

  • getContracts(chain, rpcURL) - create JSON config file to access on-chain contracts. chain='polygon|mumbai', default rpcURL pointing to Alchemy node.
  • seedPoun(contracts) - generate valid seed - a 5-numbers tuple, describing the character parts - head, glasses etc.
  • generatePoun(contracts, seed) - generate SVG image from a given seed. returns base64 encoded data.
  • getPoun(chain, seed) - high-level function, combining the seed and the image generation steps. Using default contracts. Seed argument is optional.

Usage

Generating pixel-art images is separated in two steps:

  • generate seed - a tuple of 5 numbers, representing different image parts.
  • generate SVG image from the seed

These steps can be executed in sequence, for example, when generating a random number. If the seed is already saved (for example in NFT), only the second step can be executed, based on the existing seed.

For more imformation see Nouns project documentation.

Generate seed

the example is for node.js (CLI usage):

const { getContracts, seedPoun } = require('pouns-sdk')

const createPoun = async () => {
  const contracts = getContracts()
  const seed = await seedPoun(contracts)
  ...
}
createPoun()

Generate SVG image

The example is for using in React.js component:

import { getContracts, seedPoun, generatePoun } from 'pouns-sdk'
...
const [currentImg, setCurrentImg] = useState('')

const createPoun = async () => {
  const contracts = getContracts();
  const seed = await seedPoun(contracts);
  const svg = await generatePoun(contracts, seed);
  setCurrentImg(Buffer.from(svg, 'base64').toString());
};
...

and later to display the image:

<div style={{ display: 'grid', gridTemplateColumns: '1fr' }}>
  <div dangerouslySetInnerHTML={{ __html: currentImg }}></div>
</div>

Combining the two steps

The example is for using in React.js component:

import { getPoun } from 'pouns-sdk'
...
const [currentImg, setCurrentImg] = useState('')

const createPoun = async () => {
  const svg = await getPoun();
  setCurrentImg(Buffer.from(svg, 'base64').toString());
};
...

Using in URLs

Most of NFTs specifing the media as an URL to the image, video etc. file. Pouns images can be represented as an URL with their seed: pouns://[0,18,89,117,5]. This URL can be used for image generation as follows:

if (token.url.includes('pouns://')) {
  const seed = token.url.substring(8);
  const imgData = await getPoun(seed);
  ....
}

Change generated SVG image size

The image size is hardcoded in the SVG image data - 320x320 px. But because the format of the data is always the same, you can change the image size like this:

const imgData = await getPoun(seed); // imgData is a string
const newSize = 160;
const fixedData = imgData.replace(
  '<svg width="320" height="320"',
  `<svg width="${newSize}" height="${newSize}"`
);

Using 'Mumbai' testnet

Most of the images data is also deployed to Mumbai Polygon testnet. In order to generate images, based on that data, specify testnet, when getting contracts.

const contracts = getContracts('mumbai')

Using different RPC node

If the default Alchemy RPC connection is too slow or not working, you can use for example GetBlock RPC node:

const rpcURL = 'https://matic.getblock.io/.../testnet/'
const contracts = getContracts('mumbai', rpcURL)

Future plans

  • REST interface for even easier image generation
  • Ready React.js components - NFTs etc. to generate and display the images