Skip to content

Commit 7b84e0c

Browse files
author
Yash Agrawal
committed
feat: update logic of tokenURI
1 parent 343eba4 commit 7b84e0c

File tree

1 file changed

+49
-75
lines changed

1 file changed

+49
-75
lines changed

contracts/SBTToken.sol

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -121,89 +121,63 @@ contract SBTToken is ISBTToken, ERC721EnumerableUpgradeable {
121121

122122
function _tokenURI(uint256 tokenId) private view returns (string memory) {
123123
SBTData memory sbtData = _sbtdata[tokenId];
124-
(
125-
bool stringDataPresent,
126-
StringAttribute[] memory stringAttributes
127-
) = sbtData.stringAttributesEncoded.length > 0
128-
? (
129-
true,
130-
abi.decode(
131-
sbtData.stringAttributesEncoded,
132-
(StringAttribute[])
133-
)
134-
)
135-
: (false, new StringAttribute[](0));
136-
(
137-
bool numberDataPresent,
138-
NumberAttribute[] memory numberAttributes
139-
) = sbtData.numberAttributesEncoded.length > 0
140-
? (
141-
true,
142-
abi.decode(
143-
sbtData.numberAttributesEncoded,
144-
(NumberAttribute[])
145-
)
146-
)
147-
: (false, new NumberAttribute[](0));
148124

125+
StringAttribute[] memory stringAttributes = abi.decode(
126+
sbtData.stringAttributesEncoded,
127+
(StringAttribute[])
128+
);
129+
130+
NumberAttribute[] memory numberAttributes = abi.decode(
131+
sbtData.numberAttributesEncoded,
132+
(NumberAttribute[])
133+
);
134+
135+
bool isStringDataPresent = false;
149136
string memory sbtAttributes = "";
150137

151-
if (stringDataPresent) {
152-
for (uint256 i = 0; i < stringAttributes.length; i++) {
153-
string memory attributeInString = string(
154-
abi.encodePacked(
155-
'{"trait_type": "',
156-
stringAttributes[i].trait_type,
157-
'",',
158-
' "value": "',
159-
stringAttributes[i].value,
160-
'"}'
161-
)
162-
);
138+
for (uint256 i = 0; i < stringAttributes.length; 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+
);
163149

164-
if (i == 0) {
165-
sbtAttributes = attributeInString;
166-
} else {
167-
sbtAttributes = string(
168-
abi.encodePacked(sbtAttributes, ",", attributeInString)
169-
);
170-
}
150+
if (i == 0) {
151+
isStringDataPresent = true;
152+
sbtAttributes = attributeInString;
153+
} else {
154+
sbtAttributes = string(
155+
abi.encodePacked(sbtAttributes, ",", attributeInString)
156+
);
171157
}
172158
}
173159

174-
if (numberDataPresent) {
175-
for (uint256 i = 0; i < numberAttributes.length; i++) {
176-
string memory attributeInString = string(
177-
abi.encodePacked(
178-
'{"trait_type": "',
179-
numberAttributes[i].trait_type,
180-
'",',
181-
' "display_type": "',
182-
numberAttributes[i].display_type,
183-
'",',
184-
' "value": "',
185-
numberAttributes[i].value.toString(),
186-
'"}'
187-
)
188-
);
160+
for (uint256 i = 0; i < numberAttributes.length; i++) {
161+
string memory attributeInString = string(
162+
abi.encodePacked(
163+
"{'trait_type': '",
164+
numberAttributes[i].trait_type,
165+
"',",
166+
" 'display_type': '",
167+
numberAttributes[i].display_type,
168+
"',",
169+
" 'value': '",
170+
numberAttributes[i].value.toString(),
171+
"'}"
172+
)
173+
);
189174

190-
if (i == 0) {
191-
if (stringDataPresent) {
192-
sbtAttributes = string(
193-
abi.encodePacked(
194-
sbtAttributes,
195-
",",
196-
attributeInString
197-
)
198-
);
199-
} else {
200-
sbtAttributes = attributeInString;
201-
}
202-
} else {
203-
sbtAttributes = string(
204-
abi.encodePacked(sbtAttributes, ",", attributeInString)
205-
);
206-
}
175+
if (i == 0 && !isStringDataPresent) {
176+
sbtAttributes = attributeInString;
177+
} else {
178+
sbtAttributes = string(
179+
abi.encodePacked(sbtAttributes, ",", attributeInString)
180+
);
207181
}
208182
}
209183

0 commit comments

Comments
 (0)