|
2 | 2 |
|
3 | 3 | pragma solidity ^0.7.6; |
4 | 4 |
|
| 5 | +import "base64-sol/base64.sol"; |
| 6 | +import "@openzeppelin/contracts/utils/Strings.sol"; |
| 7 | + |
5 | 8 | import "./ISubgraphNFTDescriptor.sol"; |
6 | 9 |
|
7 | 10 | /// @title Describes subgraph NFT tokens via URI |
8 | 11 | contract SubgraphNFTDescriptor is ISubgraphNFTDescriptor { |
| 12 | + string private constant SUBGRAPH_ENDPOINT = |
| 13 | + "https://api.thegraph.com/subgraphs/name/juanmardefago/dev-subgraph"; |
| 14 | + |
| 15 | + function generateSVGImage(uint256 _subgraphID) internal view returns (string memory) { |
| 16 | + // TODO: remove whitespaces |
| 17 | + return |
| 18 | + string( |
| 19 | + abi.encodePacked( |
| 20 | + '<svg id="svg">', |
| 21 | + '<script type="text/javascript">', |
| 22 | + ' fetch("', |
| 23 | + SUBGRAPH_ENDPOINT, |
| 24 | + '", {', |
| 25 | + ' "headers": {"accept": "*/*","content-type": "application/json"},', |
| 26 | + ' "body": "{\\"operationName\\":\\"subgraph\\", \\"variables\\":{\\"id\\":\\"', |
| 27 | + Strings.toString(_subgraphID), |
| 28 | + '\\"}, \\"query\\":\\"query subgraph($id: ID!) {subgraphs(where: { nftID: $id}) { id displayName image }}\\"}",', |
| 29 | + ' "method": "POST",', |
| 30 | + " })" |
| 31 | + " .then(res => { return res.json()})", |
| 32 | + " .then(json => {", |
| 33 | + ' var svg = document.getElementById("svg");', |
| 34 | + ' var textContainer = document.createElementNS("http://www.w3.org/2000/svg", "text");', |
| 35 | + ' textContainer.setAttributeNS(null, "x", 5);', |
| 36 | + ' textContainer.setAttributeNS(null, "y", 15);', |
| 37 | + " var txt = document.createTextNode(json?.data?.subgraphs[0]?.displayName);", |
| 38 | + " textContainer.appendChild(txt);", |
| 39 | + ' var image = document.createElementNS("http://www.w3.org/2000/svg", "image");', |
| 40 | + ' image.setAttributeNS(null, "width", 200);', |
| 41 | + ' image.setAttributeNS(null, "href", json?.data?.subgraphs[0]?.image);', |
| 42 | + " svg.appendChild(image);", |
| 43 | + " svg.appendChild(textContainer);", |
| 44 | + " })", |
| 45 | + " </script>", |
| 46 | + "</svg>" |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
9 | 51 | /// @inheritdoc ISubgraphNFTDescriptor |
10 | 52 | function tokenURI(IGNS _gns, uint256 _subgraphID) |
11 | 53 | external |
12 | 54 | view |
13 | 55 | override |
14 | 56 | returns (string memory) |
15 | 57 | { |
16 | | - // TODO: fancy implementation |
17 | | - // uint256 signal = _gns.subgraphSignal(_subgraphID); |
18 | | - // uint256 tokens = _gns.subgraphTokens(_subgraphID); |
19 | | - // id |
20 | | - // owner |
21 | | - return ""; |
| 58 | + string memory image = Base64.encode(bytes(generateSVGImage(_subgraphID))); |
| 59 | + return |
| 60 | + string( |
| 61 | + abi.encodePacked( |
| 62 | + "data:application/json;base64,", |
| 63 | + Base64.encode( |
| 64 | + bytes( |
| 65 | + abi.encodePacked( |
| 66 | + '{"name":"', |
| 67 | + "name", |
| 68 | + '", "description":"', |
| 69 | + "description" |
| 70 | + '", "image": "', |
| 71 | + "data:image/svg+xml;base64,", |
| 72 | + image, |
| 73 | + '"}' |
| 74 | + ) |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ); |
22 | 79 | } |
23 | 80 | } |
0 commit comments