forked from solangegueiros/chainlink-bootcamp-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossChainPriceNFT.sol
168 lines (146 loc) · 5.56 KB
/
CrossChainPriceNFT.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
// Deploy this contract on Sepolia
// Importing OpenZeppelin contracts
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/[email protected]/utils/Counters.sol";
import "@openzeppelin/[email protected]/utils/Base64.sol";
// Importing Chainlink contracts
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract CrossChainPriceNFT is ERC721, ERC721URIStorage {
using Counters for Counters.Counter;
using Strings for uint256;
Counters.Counter public tokenIdCounter;
// Create price feed
AggregatorV3Interface internal priceFeed;
uint256 public lastPrice = 0;
string priceIndicatorUp = unicode"😀";
string priceIndicatorDown = unicode"😔";
string priceIndicatorFlat = unicode"😑";
string public priceIndicator;
struct ChainStruct {
uint64 code;
string name;
string color;
}
mapping (uint256 => ChainStruct) chain;
//https://docs.chain.link/ccip/supported-networks/testnet
constructor() ERC721("CrossChain Price", "CCPrice") {
chain[0] = ChainStruct ({
code: 16015286601757825753,
name: "Sepolia",
color: "#0000ff" //blue
});
chain[1] = ChainStruct ({
code: 14767482510784806043,
name: "Fuji",
color: "#ff0000" //red
});
chain[2] = ChainStruct ({
code: 12532609583862916517,
name: "Mumbai",
color: "#4b006e" //purple
});
// https://docs.chain.link/data-feeds/price-feeds/addresses
priceFeed = AggregatorV3Interface(
// Sepolia BTC/USD
0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
);
// Mint an NFT
mint(msg.sender);
}
function mint(address to) public {
// Mint from Sepolia network = chain[0]
mintFrom(to, 0);
}
function mintFrom(address to, uint256 sourceId) public {
// sourceId 0 Sepolia, 1 Fuji, 2 Mumbai
uint256 tokenId = tokenIdCounter.current();
_safeMint(to, tokenId);
updateMetaData(tokenId, sourceId);
tokenIdCounter.increment();
}
// Update MetaData
function updateMetaData(uint256 tokenId, uint256 sourceId) public {
// Create the SVG string
string memory finalSVG = buildSVG(sourceId);
// Base64 encode the SVG
string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "Cross-chain Price SVG",',
'"description": "SVG NFTs in different chains",',
'"image": "data:image/svg+xml;base64,',
Base64.encode(bytes(finalSVG)), '",',
'"attributes": [',
'{"trait_type": "source",',
'"value": "', chain[sourceId].name ,'"},',
'{"trait_type": "price",',
'"value": "', lastPrice.toString() ,'"}',
']}'
)
)
)
);
// Create token URI
string memory finalTokenURI = string(
abi.encodePacked("data:application/json;base64,", json)
);
// Set token URI
_setTokenURI(tokenId, finalTokenURI);
}
// Build the SVG string
function buildSVG(uint256 sourceId) internal returns (string memory) {
// 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='",
chain[sourceId].color,
"' />"
)
);
// Update emoji based on price
string memory bodySVG = string(
abi.encodePacked(
"<text x='50%' y='50%' font-size='128' dominant-baseline='middle' text-anchor='middle'>",
comparePrice(),
"</text>"
)
);
// Close SVG
string memory tailSVG = "</svg>";
// Concatenate SVG strings
string memory _finalSVG = string(
abi.encodePacked(headSVG, bodySVG, tailSVG)
);
return _finalSVG;
}
// Compare new price to previous price
function comparePrice() public returns (string memory) {
uint256 currentPrice = getChainlinkDataFeedLatestAnswer();
if (currentPrice > lastPrice) {
priceIndicator = priceIndicatorUp;
} else if (currentPrice < lastPrice) {
priceIndicator = priceIndicatorDown;
} else {
priceIndicator = priceIndicatorFlat;
}
lastPrice = currentPrice;
return priceIndicator;
}
function getChainlinkDataFeedLatestAnswer() public view returns (uint256) {
(, int256 price, , , ) = priceFeed.latestRoundData();
return uint256(price);
}
function tokenURI(uint256 tokenId)
public view override(ERC721, ERC721URIStorage) returns (string memory)
{
return super.tokenURI(tokenId);
}
// The following function is an override required by Solidity.
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage)
{
super._burn(tokenId);
}
}