-
Notifications
You must be signed in to change notification settings - Fork 23
/
TicketRenderer.sol
48 lines (44 loc) · 1.96 KB
/
TicketRenderer.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import '@openzeppelin/contracts/utils/Base64.sol';
import '@openzeppelin/contracts/utils/Strings.sol';
/// @title TicketRenderer
/// @author RabbitHole.gg
/// @dev This contract is used to render on-chain data for RabbitHole tickets (aka an 1155 Reward)
contract TicketRenderer {
using Strings for uint256;
/// @dev generates the tokenURI for a given 1155 token ID
/// @param tokenId_ The token id to generate the URI for
/// @return encoded JSON following the generic OpenSea metadata standard
function generateTokenURI(
uint tokenId_
) public pure returns (string memory) {
bytes memory dataURI = abi.encodePacked(
'{',
'"name": "RabbitHole Tickets #',
tokenId_.toString(),
'",',
'"description": "A reward for completing quests within RabbitHole, with unk(no)wn utility",',
'"image": "',
generateSVG(tokenId_),
'"',
'}'
);
return string(abi.encodePacked('data:application/json;base64,', Base64.encode(dataURI)));
}
/// @dev generates the on-chain SVG for an 1155 token ID
/// @param tokenId_ The token id to generate the svg for
/// @return encoded JSON for an SVG image
function generateSVG(uint tokenId_) public pure returns (string memory) {
bytes memory svg = abi.encodePacked(
'<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350">',
'<style>.base { fill: white; font-family: serif; font-size: 14px; }</style>',
'<rect width="100%" height="100%" fill="black" />',
'<text x="50%" y="40%" class="base" dominant-baseline="middle" text-anchor="middle">RabbitHole Tickets #',
tokenId_.toString(),
'</text>',
'</svg>'
);
return string(abi.encodePacked('data:image/svg+xml;base64,', Base64.encode(svg)));
}
}