Skip to content

Commit 5ac45c7

Browse files
committed
feat: update nft descriptor
1 parent a24f6a1 commit 5ac45c7

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

contracts/base/SubgraphNFTDescriptor.sol

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,79 @@
22

33
pragma solidity ^0.7.6;
44

5+
import "base64-sol/base64.sol";
6+
import "@openzeppelin/contracts/utils/Strings.sol";
7+
58
import "./ISubgraphNFTDescriptor.sol";
69

710
/// @title Describes subgraph NFT tokens via URI
811
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+
951
/// @inheritdoc ISubgraphNFTDescriptor
1052
function tokenURI(IGNS _gns, uint256 _subgraphID)
1153
external
1254
view
1355
override
1456
returns (string memory)
1557
{
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+
);
2279
}
2380
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@typescript-eslint/typescript-estree": "^4.0.0",
4444
"@urql/core": "^2.1.3",
4545
"axios": "^0.21.1",
46+
"base64-sol": "^1.1.0",
4647
"bignumber.js": "^9.0.0",
4748
"chai": "^4.3.4",
4849
"cli-table": "^0.3.6",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,11 @@ base64-js@^1.3.1:
24872487
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
24882488
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
24892489

2490+
base64-sol@^1.1.0:
2491+
version "1.1.0"
2492+
resolved "https://registry.yarnpkg.com/base64-sol/-/base64-sol-1.1.0.tgz#39c3c91a35790b6eb3b8e73114b46bacf0fc2865"
2493+
integrity sha512-mksLxtFyBcXd3LYK5UxyMj29YR//zoaeXpJaKRbnLQC0E99oCJl7VpKaJDXWAuTvnhxBU9XNYzhxSL8umsrxWg==
2494+
24902495
base@^0.11.1:
24912496
version "0.11.2"
24922497
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"

0 commit comments

Comments
 (0)