Last active
May 20, 2023 12:26
-
-
Save solangegueiros/bbcda078a502511f4724bd7a49b66578 to your computer and use it in GitHub Desktop.
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
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity 0.8.19; | |
// Importing OpenZeppelin contracts | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "@openzeppelin/contracts/utils/Base64.sol"; | |
// Importing Chainlink contracts | |
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
contract PriceSVG is ERC721, ERC721URIStorage { | |
// Set ETH price placeholder | |
int256 public previousEthPrice = 0; | |
// Set variables for NFT | |
string ethIndicatorUp = unicode"😀"; | |
string ethIndicatorDown = unicode"😔"; | |
string ethIndicatorFlat = unicode"😑"; | |
string public ethIndicator; | |
string[] public colors = [ | |
"ff0000", | |
"ffff00", | |
"00ff00", | |
"0000ff", | |
"00ff00", | |
"808080", | |
"ff00ff" | |
]; | |
uint8 public actualColor = 0; | |
// Create price feed | |
AggregatorV3Interface internal priceFeed; | |
constructor() ERC721("ETH Watch SVG", "ewSVG") { | |
/** | |
* Network: Mumbai | |
* Aggregator: ETH/USD | |
* Address: 0x0715A7794a1dc8e42615F059dD6e406A6594651A | |
*/ | |
priceFeed = AggregatorV3Interface( | |
0x0715A7794a1dc8e42615F059dD6e406A6594651A | |
); | |
// Mint an NFT | |
_safeMint(msg.sender, 0); | |
updateSVG(); | |
} | |
// Update the SVG | |
function updateSVG() public { | |
// Create the SVG string | |
string memory finalSVG = buildSVG(); | |
// Base64 encode the SVG | |
string memory json = Base64.encode( | |
bytes( | |
string( | |
abi.encodePacked( | |
'{"name": "ETH Watching SVG",', | |
'"description": "An Automated ETH tracking SVG",', | |
'"image": "data:image/svg+xml;base64,', | |
Base64.encode(bytes(finalSVG)), | |
'"}' | |
) | |
) | |
) | |
); | |
// Create token URI | |
string memory finalTokenURI = string( | |
abi.encodePacked("data:application/json;base64,", json) | |
); | |
// Set token URI | |
_setTokenURI(0, finalTokenURI); | |
} | |
// Build the SVG string | |
function buildSVG() internal returns (string memory) { | |
actualColor = actualColor + 1; | |
if (actualColor == colors.length) { | |
actualColor = 0; | |
} | |
string memory fillColor = string(abi.encodePacked("#", colors[actualColor])); | |
// Create SVG rectangle with random color | |
string memory headSVG = string( | |
abi.encodePacked( | |
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' width='500' height='500' preserveAspectRatio='none' viewBox='0 0 500 500'> <rect width='100%' height='100%' fill='", | |
fillColor, | |
"' />" | |
) | |
); | |
// Update emoji based on ETH price | |
string memory bodySVG = string( | |
abi.encodePacked( | |
"<text x='50%' y='50%' font-size='128' dominant-baseline='middle' text-anchor='middle'>", | |
compareETHPrice(), | |
"</text>" | |
) | |
); | |
// Close SVG | |
string memory tailSVG = "</svg>"; | |
// Concatenate SVG strings | |
string memory _finalSVG = string( | |
abi.encodePacked(headSVG, bodySVG, tailSVG) | |
); | |
return _finalSVG; | |
} | |
// Compare ETH price to previous price | |
function compareETHPrice() public returns (string memory) { | |
int256 currentEthPrice = getETHPrice(); | |
if (currentEthPrice > previousEthPrice) { | |
ethIndicator = ethIndicatorUp; | |
} else if (currentEthPrice < previousEthPrice) { | |
ethIndicator = ethIndicatorDown; | |
} else { | |
ethIndicator = ethIndicatorFlat; | |
} | |
previousEthPrice = currentEthPrice; | |
return ethIndicator; | |
} | |
function getETHPrice() public view returns (int256) { | |
(, int256 price, , , ) = priceFeed.latestRoundData(); | |
return price; | |
} | |
// The following functions is an override required by Solidity. | |
function _burn(uint256 tokenId) | |
internal | |
override(ERC721, ERC721URIStorage) | |
{ | |
super._burn(tokenId); | |
} | |
function tokenURI(uint256 tokenId) | |
public | |
view | |
override(ERC721, ERC721URIStorage) | |
returns (string memory) | |
{ | |
return super.tokenURI(tokenId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment