You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently there are no tests for these functions in the test file for the DynamicSvgNft contract. I did some research and came up with a couple of functions to create the image and token URIs off-chain using ethers@^5.5.4. These are the functions:
createImageUri function:
constcreateImageUri=(svg)=>{constbaseImageUri="data:image/svg+xml;base64,"constsolidityEncodePacked=ethers.utils.solidityPack(["string"],[svg])// equivalent to `bytes(string(abi.encodePack(svg)))`constbase64Encode=ethers.utils.base64.encode(solidityEncodePacked)// equivalent to `Base64.encode(bytes(string(abi.encodePacked(svg))))`// equivalent to `abi.encodePacked(baseURL, svgBase64Encoded)`constbytesImageUri=ethers.utils.solidityPack(["string","string"],[baseImageUri,base64Encode])conststringImageUri=ethers.utils.toUtf8String(bytesImageUri)// equivalent to typecasting the result to string as shown in the contract: `string(abi.encodePacked(baseURL, svgBase64Encoded))`returnstringImageUri}
createTokenUri function:
constcreateTokenUri=(name,imageUri)=>{constbaseTokenUri="data:application/json;base64,"constsolidityEncodePacked=ethers.utils.solidityPack(["string","string","string","string","string","string"],['{"name":"',name,'", "description":"An NFT that changes based on the Chainlink Feed", ','"attributes": [{"trait_type": "coolness", "value": 100}], "image":"',imageUri,'"}',])constbase64Encode=ethers.utils.base64.encode(solidityEncodePacked)constbytesTokenUri=ethers.utils.solidityPack(["string","string"],[baseTokenUri,base64Encode])conststringTokenUri=ethers.utils.toUtf8String(bytesTokenUri)returnstringTokenUri}
Possible implementation:
To avoid polluting the test file I would create a createUris.js file inside the utils folder with the functions in it and import them from there.
At the beginning of the test suite I would read both svg files:
Finally in any test case I would call the functions and pass the arguments, example:
// ...consttokenURI=awaitdynamicSvgNft.tokenURI(0)// after minting NFTconsthighImageUri=createImageUri(highSvg)// create image URIconsthighTokenUri=createTokenUri("Dynamic SVG NFT",highImageUri)// create token URI by passing the image URIassert.equal(tokenURI,highTokenUri)// assert
Hopefully the comments aren't too complex to understand and this post might serve as a reference for future readers that could be interested in testing these functions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently there are no tests for these functions in the test file for the
DynamicSvgNft
contract. I did some research and came up with a couple of functions to create the image and token URIs off-chain usingethers@^5.5.4
. These are the functions:createImageUri
function:createTokenUri
function:Possible implementation:
To avoid polluting the test file I would create a
createUris.js
file inside theutils
folder with the functions in it and import them from there.At the beginning of the test suite I would read both svg files:
Finally in any test case I would call the functions and pass the arguments, example:
Hopefully the comments aren't too complex to understand and this post might serve as a reference for future readers that could be interested in testing these functions.
Beta Was this translation helpful? Give feedback.
All reactions