Skip to content

Instantly share code, notes, and snippets.

@Montoya
Created September 19, 2022 03:59
Show Gist options
  • Save Montoya/3d503353d3d21869613e9a7dc366349f to your computer and use it in GitHub Desktop.
Save Montoya/3d503353d3d21869613e9a7dc366349f to your computer and use it in GitHub Desktop.
SVG to PNG from contract
const Web3 = require('web3');
const { convert } = require('convert-svg-to-png');
const fs = require('fs');
/* this assumes you have:
- an Infura account
- a smart contract with a read method "tokenSVG" that takes one argument "tokenID" and returns the SVG content as a string
*/
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/'+YOUR_API_KEY));
const abi = YOUR_ABI;
const address = YOUR_CONTRACT_ADDRESS;
const contract = new web3.eth.Contract(abi, address)
async function getSVG(id) {
let result = await contract.methods.tokenSVG(id).call();
let png = await convert(result, {scale:2}); // output PNGs are twice as big as base SVG
fs.writeFile('png/'+id+'.png', png, err => {
if (err) {
console.error(err);
}
else {
console.log("Wrote "+id+" to file");
}
});
}
for(var i=0;i<...;i++) {
getSVG(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment