Skip to content

Commit 6753e5b

Browse files
author
Yash Agrawal
committed
feat: add _tokenURI function
1 parent 8eb5786 commit 6753e5b

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

contracts/SBTToken.sol

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,113 @@ contract SBTToken is ISBTToken, ERC721EnumerableUpgradeable {
124124
}
125125

126126
function _tokenURI(uint256 tokenId) private view returns (string memory) {
127-
return string(bytes(abi.encode(_sbtdata[tokenId])));
127+
SBTData memory sbtData = _sbtdata[tokenId];
128+
(bool stringDataPresent, StringAttribute[] memory stringAttributes) = sbtData.stringAttributesEncoded.length > 0
129+
? (true, abi.decode(sbtData.stringAttributesEncoded, (StringAttribute[])))
130+
: (false, new StringAttribute[](0));
131+
(bool numberDataPresent, NumberAttribute[] memory numberAttributes) = sbtData.numberAttributesEncoded.length > 0
132+
? (true, abi.decode(sbtData.numberAttributesEncoded, (NumberAttribute[])))
133+
: (false, new NumberAttribute[](0));
134+
135+
string memory sbtAttributes = "";
136+
137+
if (stringDataPresent) {
138+
for (uint256 i = 0; i <= stringAttributes.length - 1; i++) {
139+
string memory attributeInString = string(
140+
abi.encodePacked(
141+
'{"trait_type": "',
142+
stringAttributes[i].trait_type,
143+
'",',
144+
' "value": "',
145+
stringAttributes[i].value,
146+
'"}'
147+
)
148+
);
149+
150+
if (i == 0) {
151+
sbtAttributes = attributeInString;
152+
} else {
153+
sbtAttributes = string(
154+
abi.encodePacked(
155+
sbtAttributes,
156+
",",
157+
attributeInString
158+
)
159+
);
160+
}
161+
}
162+
}
163+
164+
if (numberDataPresent) {
165+
for (uint256 i = 0; i < numberAttributes.length; i++) {
166+
string memory attributeInString = string(
167+
abi.encodePacked(
168+
'{"trait_type": "',
169+
numberAttributes[i].trait_type,
170+
'",',
171+
' "display_type": "',
172+
numberAttributes[i].display_type,
173+
'",',
174+
' "value": "',
175+
numberAttributes[i].value.toString(),
176+
'"}'
177+
)
178+
);
179+
180+
if (i == 0) {
181+
if (stringDataPresent) {
182+
sbtAttributes = string(
183+
abi.encodePacked(
184+
",",
185+
attributeInString
186+
)
187+
);
188+
} else {
189+
sbtAttributes = attributeInString;
190+
}
191+
} else {
192+
sbtAttributes = string(
193+
abi.encodePacked(
194+
sbtAttributes,
195+
",",
196+
attributeInString
197+
)
198+
);
199+
}
200+
}
201+
}
202+
203+
sbtAttributes = string(
204+
abi.encodePacked(
205+
"[",
206+
sbtAttributes,
207+
"]"
208+
)
209+
);
210+
211+
return
212+
string(
213+
abi.encodePacked(
214+
"data:application/json;base64,",
215+
abi
216+
.encodePacked(
217+
// solhint-disable-next-line quotes
218+
'{"name":"',
219+
sbtData.name,
220+
// solhint-disable-next-line quotes
221+
'", "description":"',
222+
sbtData.description,
223+
// solhint-disable-next-line quotes
224+
'", "image": "',
225+
sbtData.image,
226+
// solhint-disable-next-line quotes
227+
'", "attributes":',
228+
sbtAttributes,
229+
"}"
230+
)
231+
.encode()
232+
)
233+
);
128234
}
129235

130236
function tokenURI(

0 commit comments

Comments
 (0)