This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
YourCollectible.sol
135 lines (119 loc) · 4.44 KB
/
YourCollectible.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
pragma solidity >=0.6.0 <0.7.0;
//SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import 'base64-sol/base64.sol';
import './HexStrings.sol';
import './ToColor.sol';
//learn more: https://docs.openzeppelin.com/contracts/3.x/erc721
// GET LISTED ON OPENSEA: https://testnets.opensea.io/get-listed/step-two
contract YourCollectible is ERC721, Ownable {
using Strings for uint256;
using HexStrings for uint160;
using ToColor for bytes3;
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() public ERC721("dynamicSVG", "dSVG") {
// RELEASE THE LOOGIES!
}
mapping (uint256 => string) public svgData;
mapping (uint256 => string) public attributes;
function mintItem()
public
returns (uint256)
{
_tokenIds.increment();
uint256 id = _tokenIds.current();
_mint(msg.sender, id);
return id;
}
function edit(uint256 id, string memory data) public {
require(_isApprovedOrOwner(_msgSender(), id), "ERC721: edit caller is not owner nor approved");
svgData[id]=data;
}
function tokenURI(uint256 id) public view override returns (string memory) {
require(_exists(id), "not exist");
string memory name = string(abi.encodePacked('dSVG #',id.toString()));
string memory description = string(abi.encodePacked('This a dynamicSVG!'));
string memory image = Base64.encode(bytes(generateSVGofTokenById(id)));
return
string(
abi.encodePacked(
'data:application/json;base64,',
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"',
name,
'", "description":"',
description,
'", "external_url":"https://dyanmicsvgexample4georgios.surge.sh/',
id.toString(),
'", "attributes": [',
attributes[id],
'], "owner":"',
(uint160(ownerOf(id))).toHexString(20),
'", "image": "',
'data:image/svg+xml;base64,',
image,
'"}'
)
)
)
)
);
}
function generateSVGofTokenById(uint256 id) internal view returns (string memory) {
string memory svg = string(abi.encodePacked(
'<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">',
renderTokenById(id),
'</svg>'
));
return svg;
}
// Visibility is `public` to enable it being called by other contracts for composition.
function renderTokenById(uint256 id) public view returns (string memory) {
/*string memory render = string(abi.encodePacked(
'<g id="eye1">',
'<ellipse stroke-width="3" ry="29.5" rx="29.5" id="svg_1" cy="154.5" cx="181.5" stroke="#000" fill="#fff"/>',
'<ellipse ry="3.5" rx="2.5" id="svg_3" cy="154.5" cx="173.5" stroke-width="3" stroke="#000" fill="#000000"/>',
'</g>',
'<g id="head">',
'<ellipse fill="#',
color[id].toColor(),
'" stroke-width="3" cx="204.5" cy="211.80065" id="svg_5" rx="',
chubbiness[id].toString(),
'" ry="51.80065" stroke="#000"/>',
'</g>',
'<g id="eye2">',
'<ellipse stroke-width="3" ry="29.5" rx="29.5" id="svg_2" cy="168.5" cx="209.5" stroke="#000" fill="#fff"/>',
'<ellipse ry="3.5" rx="3" id="svg_4" cy="169.5" cx="208" stroke-width="3" fill="#000000" stroke="#000"/>',
'</g>'
));
*/
return svgData[id];
}
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
}