Created
September 19, 2022 03:59
-
-
Save Montoya/3d503353d3d21869613e9a7dc366349f to your computer and use it in GitHub Desktop.
SVG to PNG from contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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