-
I can't manage to convert (in the end concat) a
But in the end it returns: Or maybe my question is: How to convert a unicode-encoded bytes to string? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 5 replies
-
Got help in discord: _artworkId: uint256 = _tokenId % 6
_artworkString: bytes32 = convert(_artworkId + 48, bytes32)
_artworkStringNoPad: Bytes[1] = slice( _artworkString, 31, 1) # otherwise it's prefixed with 31x "\u0000"
return concat(
b'https://cloudflare-ipfs.com/ipfs/QmZXXXCp/',
_artworkStringNoPad
) |
Beta Was this translation helpful? Give feedback.
-
I ended up switching to Solidity... return string(abi.encodePacked(
super.tokenURI(tokenId % 6),
'.json'
)) |
Beta Was this translation helpful? Give feedback.
-
I have the same question, did you manage to find a way with vyper? |
Beta Was this translation helpful? Give feedback.
-
I believe this should work: baseURI: public(String[56]) # e.g. ipfs://zb2rhe5P4gXftAwvA4eXQ5HJwsER2owDyS9sKaQRRVQPn93bA
# ...
@pure
@internal
def _digitToString(digit: uint256) -> String[1]:
assert digit < 10 # only works with digits 0-9
digit_bytes32: bytes32 = convert(digit + 48, bytes32) # ASCII `0` is 0x30 (48 in decimal)
digit_bytes1: Bytes[1] = slice(digit_bytes32, 31, 1) # Remove padding bytes
return convert(digit_bytes1, String[1])
@view
@internal
def _tokenIdToString(tokenId: uint256) -> String[4]:
# NOTE: Only handles up to 4 digits, e.g. tokenId in [0, 9999]
digit1: uint256 = tokenId % 10
digit2: uint256 = (tokenId % 100) / 10
digit3: uint256 = (tokenId % 1000) / 100
digit4: uint256 = tokenId / 1000
return concat(
self._digitToString(digit1),
self._digitToString(digit2),
self._digitToString(digit3),
self._digitToString(digit4),
)
@view
@external
def tokenURI(tokenId: uint256) -> String[66]:
return concat(self.baseURI, "/", self._tokenIdToString(tokenId), ".json") |
Beta Was this translation helpful? Give feedback.
-
As of v0.3.6, we have |
Beta Was this translation helpful? Give feedback.
As of v0.3.6, we have
uint2str
!