Skip to content

Commit 1dcf0e2

Browse files
author
Yash Agrawal
committed
feat: use single bytes instead of structs
1 parent ca19ca3 commit 1dcf0e2

File tree

2 files changed

+26
-89
lines changed

2 files changed

+26
-89
lines changed

contracts/SBTToken.sol

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,17 @@ contract SBTToken is ISBTToken, ERC721EnumerableUpgradeable {
1717
/// @dev Account with proxy adming rights.
1818
address private _proxyAdmin;
1919

20-
/// @dev Holds the generic metadata and attribute information of a SBT token.
21-
mapping(uint256 => SBTData) private _sbtdata;
20+
/// @dev Holds the encoded metadata of a SBT token.
21+
mapping(uint256 => bytes) private _sbtdata;
2222

2323
modifier onlyMinter() {
2424
require(_minter == _msgSender(), "Illegal access");
2525
_;
2626
}
2727

28-
function _setTokenURI(
29-
uint256 tokenId,
30-
string memory name,
31-
string memory description,
32-
StringAttribute[] memory stringAttributes,
33-
NumberAttribute[] memory numberAttributes,
34-
string memory tokenUriImage
35-
) private {
36-
bytes memory stringAttributesEncoded = abi.encode(stringAttributes);
37-
bytes memory numberAttributesEncoded = abi.encode(numberAttributes);
38-
SBTData memory sbtData = SBTData({
39-
name: name,
40-
image: tokenUriImage,
41-
description: description,
42-
stringAttributesEncoded: stringAttributesEncoded,
43-
numberAttributesEncoded: numberAttributesEncoded
44-
});
45-
_sbtdata[tokenId] = sbtData;
46-
emit SetSBTTokenURI(tokenId, abi.encode(sbtData, tokenUriImage));
28+
function _setTokenURI(uint256 tokenId, bytes memory metadata) private {
29+
_sbtdata[tokenId] = metadata;
30+
emit SetSBTTokenURI(tokenId, metadata);
4731
}
4832

4933
function _beforeTokenTransfer(
@@ -80,57 +64,34 @@ contract SBTToken is ISBTToken, ERC721EnumerableUpgradeable {
8064

8165
function setTokenURI(
8266
uint256 tokenId,
83-
string memory name,
84-
string memory description,
85-
StringAttribute[] memory stringAttributes,
86-
NumberAttribute[] memory numberAttributes,
87-
string memory tokenUriImage
67+
bytes memory metadata
8868
) external override onlyMinter {
8969
require(tokenId < currentIndex(), "Token not found");
90-
_setTokenURI(
91-
tokenId,
92-
name,
93-
description,
94-
stringAttributes,
95-
numberAttributes,
96-
tokenUriImage
97-
);
70+
_setTokenURI(tokenId, metadata);
9871
}
9972

10073
function mint(
10174
address to,
102-
string memory name,
103-
string memory description,
104-
StringAttribute[] memory stringAttributes,
105-
NumberAttribute[] memory numberAttributes,
106-
string memory tokenUriImage
75+
bytes memory metadata
10776
) external override onlyMinter returns (uint256 tokenId_) {
10877
uint256 currentId = currentIndex();
10978
_mint(to, currentId);
11079
emit Minted(currentId, to);
111-
_setTokenURI(
112-
currentId,
113-
name,
114-
description,
115-
stringAttributes,
116-
numberAttributes,
117-
tokenUriImage
118-
);
80+
_setTokenURI(currentId, metadata);
11981
return currentId;
12082
}
12183

12284
function _tokenURI(uint256 tokenId) private view returns (string memory) {
123-
SBTData memory sbtData = _sbtdata[tokenId];
124-
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-
);
85+
(
86+
string memory name,
87+
string memory description,
88+
string memory tokenUriImage,
89+
StringAttribute[] memory stringAttributes,
90+
NumberAttribute[] memory numberAttributes
91+
) = abi.decode(
92+
_sbtdata[tokenId],
93+
(string, string, string, StringAttribute[], NumberAttribute[])
94+
);
13495

13596
bool isStringDataPresent = false;
13697
string memory sbtAttributes = "";
@@ -201,13 +162,13 @@ contract SBTToken is ISBTToken, ERC721EnumerableUpgradeable {
201162
.encodePacked(
202163
// solhint-disable-next-line quotes
203164
'{"name":"',
204-
sbtData.name,
165+
name,
205166
// solhint-disable-next-line quotes
206167
'", "description":"',
207-
sbtData.description,
168+
description,
208169
// solhint-disable-next-line quotes
209170
'", "image": "',
210-
sbtData.image,
171+
tokenUriImage,
211172
// solhint-disable-next-line quotes
212173
'", "attributes":',
213174
sbtAttributes,

contracts/interfaces/ISBTToken.sol

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ interface ISBTToken {
1515
uint256 value;
1616
}
1717

18-
/// @dev Data strucutre to represent all generic metadata of the SBT.
19-
struct SBTData {
20-
string name;
21-
string image;
22-
string description;
23-
bytes stringAttributesEncoded;
24-
bytes numberAttributesEncoded;
25-
}
26-
2718
/*
2819
* @dev The event fired when a token is minted.
2920
* @param tokenId The ID of the created SBT.
@@ -48,35 +39,20 @@ interface ISBTToken {
4839
* @dev Creates the new staking position for the caller.
4940
* Mint must be called by the minter address.
5041
* @param _owner The address of the owner of the new SBT.
51-
* @param metadata The tokenURI data of the new SBT.
52-
* @param tokenUriImage The link for the image of the SBT.
42+
* @param metadata The encoded metadata of the SBT.
5343
*/
5444
function mint(
5545
address _owner,
56-
string memory name,
57-
string memory description,
58-
StringAttribute[] memory stringAttributes,
59-
NumberAttribute[] memory numberAttributes,
60-
string memory tokenURIImage
46+
bytes memory metadata
6147
) external returns (uint256);
6248

6349
/*
6450
* @dev Sets the token URI image for a token.
6551
* @notice must be called by the minter address.
6652
* @param _tokenId The token for which we are setting the image uri.
67-
* @param metadata The generic metadata of the SBT (e.g name, description).
68-
* @param stringAttributes The string attributes that form the SBT.
69-
* @param numericAttributes The numeric attributes that form the SBT.
70-
* @param tokenUriImage The link for the image of the SBT.
53+
* @param bytes The encoded metadata of the NFT.
7154
*/
72-
function setTokenURI(
73-
uint256 _tokenId,
74-
string memory name,
75-
string memory description,
76-
StringAttribute[] memory stringAttributes,
77-
NumberAttribute[] memory numberAttributes,
78-
string memory tokenURIImage
79-
) external;
55+
function setTokenURI(uint256 _tokenId, bytes memory metadata) external;
8056

8157
/*
8258
* @dev Get the NFT metadata in encoded format.

0 commit comments

Comments
 (0)