This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
DynamicSvgNft.sol
122 lines (105 loc) · 4.49 KB
/
DynamicSvgNft.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
// The new-version of "@openzeppelin/contracts": "^5.0.1", already has Base64.sol
// You can import it like as shown just below...
// import "@openzeppelin/contracts/utils/Base64.sol"; // 👈 comment in this import
// instead of installing "base64-sol": "^1.1.0",
import "base64-sol/base64.sol"; // 👈 comment out this import
import "hardhat/console.sol";
error ERC721Metadata__URI_QueryFor_NonExistentToken();
contract DynamicSvgNft is ERC721, Ownable {
uint256 private s_tokenCounter;
string private s_lowImageURI;
string private s_highImageURI;
mapping(uint256 => int256) private s_tokenIdToHighValues;
AggregatorV3Interface internal immutable i_priceFeed;
event CreatedNFT(uint256 indexed tokenId, int256 highValue);
constructor(
address priceFeedAddress,
string memory lowSvg,
string memory highSvg
) ERC721("Dynamic SVG NFT", "DSN") {
s_tokenCounter = 0;
i_priceFeed = AggregatorV3Interface(priceFeedAddress);
// setLowSVG(lowSvg);
// setHighSVG(highSvg);
s_lowImageURI = svgToImageURI(lowSvg);
s_highImageURI = svgToImageURI(highSvg);
}
// function setLowURI(string memory svgLowURI) public onlyOwner {
// s_lowImageURI = svgLowURI;
// }
// function setHighURI(string memory svgHighURI) public onlyOwner {
// s_highImageURI = svgHighURI;
// }
// function setLowSVG(string memory svgLowRaw) public onlyOwner {
// string memory svgURI = svgToImageURI(svgLowRaw);
// setLowURI(svgURI);
// }
// function setHighSVG(string memory svgHighRaw) public onlyOwner {
// string memory svgURI = svgToImageURI(svgHighRaw);
// setHighURI(svgURI);
// }
function mintNft(int256 highValue) public {
uint256 newTokenId = s_tokenCounter;
s_tokenIdToHighValues[newTokenId] = highValue;
_safeMint(msg.sender, newTokenId);
s_tokenCounter = s_tokenCounter + 1;
emit CreatedNFT(newTokenId, highValue);
}
// You could also just upload the raw SVG and let solildity convert it!
function svgToImageURI(string memory svg) public pure returns (string memory) {
// example:
// '<svg width="500" height="500" viewBox="0 0 285 350" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill="black" d="M150,0,L75,200,L225,200,Z"></path></svg>'
// would return ""
string memory baseURL = "data:image/svg+xml;base64,";
string memory svgBase64Encoded = Base64.encode(bytes(string(abi.encodePacked(svg))));
return string(abi.encodePacked(baseURL, svgBase64Encoded));
}
function _baseURI() internal pure override returns (string memory) {
return "data:application/json;base64,";
}
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
if (!_exists(tokenId)) {
revert ERC721Metadata__URI_QueryFor_NonExistentToken();
}
(, int256 price, , , ) = i_priceFeed.latestRoundData();
string memory imageURI = s_lowImageURI;
if (price >= s_tokenIdToHighValues[tokenId]) {
imageURI = s_highImageURI;
}
return
string(
abi.encodePacked(
_baseURI(),
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"',
name(), // You can add whatever name here
'", "description":"An NFT that changes based on the Chainlink Feed", ',
'"attributes": [{"trait_type": "coolness", "value": 100}], "image":"',
imageURI,
'"}'
)
)
)
)
);
}
function getLowSVG() public view returns (string memory) {
return s_lowImageURI;
}
function getHighSVG() public view returns (string memory) {
return s_highImageURI;
}
function getPriceFeed() public view returns (AggregatorV3Interface) {
return i_priceFeed;
}
function getTokenCounter() public view returns (uint256) {
return s_tokenCounter;
}
}