ReasonJun

Solidity : abi.encodePacked(), super.uri() 본문

Blockchain/Solidity

Solidity : abi.encodePacked(), super.uri()

ReasonJun 2023. 10. 22. 22:06
728x90

The abi.encodePacked() function is a built-in Solidity function that is used to encode multiple values into a single byte array. The function takes any number of arguments and concatenates them into a single byte array.

 

The abi.encodePacked() function is typically used to encode data that is going to be passed to a contract function. For example, the following code would encode the string "Hello, world!" into a byte array:

bytes memory data = abi.encodePacked("Hello, world!");

The data variable would now contain the byte array [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21].

 

The abi.encodePacked() function can also be used to encode multiple values into a single byte array. For example, the following code would encode the values 1, 2, and 3 into a single byte array:

bytes memory data = abi.encodePacked(1, 2, 3);

The data variable would now contain the byte array [0x01, 0x02, 0x03].

The abi.encodePacked() function is a useful function for encoding data that is going to be passed to a contract function. The function can be used to encode any type of data, including strings, integers, and booleans.

function uri(uint256 tokenId) public view virtual override returns (string memory) {
        string memory tokenURI = _tokenURIs[tokenId];

        // If token URI is set, concatenate base URI and tokenURI (via abi.encodePacked).
        return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_baseURI, tokenURI)) : super.uri(tokenId);
    }

The code string(abi.encodePacked(_baseURI, tokenURI)) is used to cast the byte array returned by the abi.encodePacked() function to a string. The abi.encodePacked() function takes two arguments, _baseURI and tokenURI, and concatenates them into a single byte array. The string() function then casts the byte array to a string.

The resulting string is the token URI for the token. The token URI is a JSON document that contains information about the token, such as its name, description, and image.

 

The token URI is used by OpenSea and other NFT marketplaces to display information about the token.

In this case, the _baseURI is the base URI for all tokens. The base URI is a string that is used to generate the token URI for a token. For example, if the base URI is https://example.com/tokens/, then the token URI for token ID 1 would be https://example.com/tokens/1.

 

The tokenURI is the token URI for a specific token. The token URI can be set using the setURI() function.

For example, if the tokenURI for token ID 1 is { "name": "My Token", "description": "This is my token.", "image": "<https://example.com/tokens/1.jpg>" }, then the resulting string would be:

"https://example.com/tokens/{ "name": "My Token", "description": "This is my token.", "image": "https://example.com/tokens/1.jpg" }"

 

super.uri()

In the context of the MyERC1155 contract, the super.uri(tokenId) function is used to generate the token URI for a token if a token URI has not been set for the token. The MyERC1155 contract has a setURI() function that can be used to set a token URI for a specific token.

 

If a token URI has been set for a token, then the MyERC1155 contract will return the token URI that was set. Otherwise, the MyERC1155 contract will call the super.uri(tokenId) function to generate the token URI.

 

The super.uri(tokenId) function is a useful function for ensuring that all tokens have a token URI. The token URI is used by OpenSea and other NFT marketplaces to display information about the token.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : 0.8.20 PUSH0 ( invalid opcode )  (0) 2023.10.23
Ethereum : ERC1155  (0) 2023.10.22
Openzeppelin : ERC20PresetMinterPauser  (0) 2023.10.22
Openzeppelin : ERC721PresetMinterPauserAutoId  (0) 2023.10.22
Solidity : sol2uml  (0) 2023.10.20
Comments