5

I need to convert a uint256 to String in vyper, I notice that theres something similar on Solidity (Taken from OpenSea's docs):

/**
   * @dev Returns an URI for a given token ID
   */
  function tokenURI(uint256 _tokenId) public view returns (string) {
    return Strings.strConcat(
        baseTokenURI(),
        Strings.uint2str(_tokenId)
    );
  }

There's a method called "Strings.uint2str()", is there something equivalent in Vyper?

Alex
  • 139
  • 6
  • The Solidity code is a using an external library named `Strings` and its function `uint2str()` - not a native function... Based on the context specified in the OpenSea docs page, it seems like they use the OpenZeppelin [Strings](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.2.0/contracts/utils/Strings.sol) library that might have had the `uint2str()` function in earlier versions, but now there's the `toString()` function to convert an `uint256` value to a `string`. – Petr Hejda Dec 12 '21 at 02:26
  • The bad news is that the toString() function is an implementation for Solidity right? not Vyper. – Alex Dec 12 '21 at 02:41
  • That's correct, it's in Solidity - not Vyper. My comment was simply aiming to highlight the fact that it's a custom function, not a native one. – Petr Hejda Dec 12 '21 at 03:36
  • I am not a Vyper expert. But you can use abi encode and decode then uint256 that variable. It should work because everything is byte you know :) – Erim Varış Aug 28 '22 at 13:01

1 Answers1

1

the vyper git repo has an examples folder, one of which is for tokens which contains and ERC-721 contract implementation in vyper

@view
@external
def tokenURI(tokenId: uint256) -> String[132]:
    return concat(self.baseURL, uint2str(tokenId))

in the vyper docs

uint2str(value: unsigned integer)→ String Returns an unsigned integer’s string representation.

value: Unsigned integer to convert.

Returns the string representation of value.

Liam O'Toole
  • 167
  • 3
  • 13