# Add support for ERC721 token URI concatenation of base token URI and tokenID

**URL:** <https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566>\
**Category:** General\
**Created:** [April 23, 2019, 10:45am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566 "2019-04-23T10:45:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [April 23, 2019, 10:45am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/1 "2019-04-23T10:45:41Z")

</div>

Current ERC721 minting with token URI requires specifying the URI per token.

To reduce gas cost, the OpenSea example uses concatenation of a base token URI and the tokenID. ([https://github.com/ProjectOpenSea/opensea-creatures/blob/master/contracts/TradeableERC721Token.sol](https://github.com/ProjectOpenSea/opensea-creatures/blob/master/contracts/TradeableERC721Token.sol))

I used this concatenation on BlockHorses. [https://github.com/blockhorses/BlockHorses/blob/master/contracts/BlockHorses.sol](https://github.com/blockhorses/BlockHorses/blob/master/contracts/BlockHorses.sol)  
[https://github.com/blockhorses/BlockHorses/blob/master/contracts/Strings.sol](https://github.com/blockhorses/BlockHorses/blob/master/contracts/Strings.sol)

```auto
  function tokenURI(uint256 tokenId) external view returns (string memory) {
    require(_exists(tokenId));
    return Strings.Concatenate(
      baseTokenURI(),
      Strings.UintToString(tokenId)
    );
  }

```

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [April 23, 2019, 2:41pm UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/2 "2019-04-23T14:41:53Z")

</div>

This is a fantastic idea! Thanks @abcoathup!

Potential API for this: an `ERC721` extension `ERC721DefaultURI` with a base URI parameter, which overrides `ERC721Metadata`'s `tokenURI()` with the concatenation. What I don’t like about this is that `ERC721Metadata` also has `name` and `symbol` and this gets into the unintuitive nastiness of multiple inheritance. We can avoid that by making the default URI extension only provide the URI, and not name and symbol, but then if people do combine it with the metadata extension the behavior would depend on the order of inheritance and that is nasty. I think this can be fixed by adding a common ancestor in the hierarchy, which sounds reasonable to me.

---

<div class="post-metadata">

**Author:** ![martriay](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/martriay/32/20_2.png) [@martriay](https://forum.openzeppelin.com/u/martriay)\
**Post date:** [April 23, 2019, 2:54pm UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/3 "2019-04-23T14:54:42Z")

</div>

Moving this from #support to #openzeppelin 🙂

---

<div class="post-metadata">

**Author:** ![nventuro](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/nventuro/32/8_2.png) [@nventuro](https://forum.openzeppelin.com/u/nventuro)\
**Post date:** [April 23, 2019, 10:23pm UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/4 "2019-04-23T22:23:45Z")

</div>

Ohhh, _nice_. This would also mean finally developing some sort of string management library. @abcoathup what do you think about opening an initial PR with this `String.concat` library, so we can get started?

@frangio Hmm I see what you mean, a common ancestor with the URI field may be the way to go, but it introduces a lot of artificial complexity. We should experiment a bit with this.

As a side note, our revert reasons share a prefix: I wonder if there are gas gains to be had by generating the revert reason string on the fly, as opposed to having repeated copies of the prefix stored in the blockchain. My intuition is that the `Strings` code would offset any gains, but I’ve been wrong before.

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [April 25, 2019, 9:48am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/5 "2019-04-25T09:48:26Z")

</div>

Will have a go on the weekend

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [May 10, 2019, 8:46am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/6 "2019-05-10T08:46:10Z")

</div>

@nventuro I have made an initial PR with the Strings library.

> <https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1746>

---

<div class="post-metadata">

**Author:** ![nventuro](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/nventuro/32/8_2.png) [@nventuro](https://forum.openzeppelin.com/u/nventuro)\
**Post date:** [May 24, 2019, 6:54pm UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/7 "2019-05-24T18:54:40Z")

</div>

@abcoathup given @frangio's comment in the PR (quoting here)

> Ohh that is very cool! I personally would consider removing `concatenate` altogether then. `abi.encodePacked` is superior in that it can receive a variable number of arguments.

I think you can start working on a PR for this using `abi.encodePacked` 🙂

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [May 27, 2019, 11:44pm UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/8 "2019-05-27T23:44:36Z")

</div>

Very excited that my first PR has been merged into OpenZeppelin!!  
It is only small, but great to be contributing.

Definitely learnt more about testing and naming as part of this experience.

> <https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1746>

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 28, 2019, 5:51am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/9 "2019-10-28T05:51:43Z")

</div>

There is now a PR for this:

> <https://github.com/OpenZeppelin/openzeppelin-contracts/pull/1970>

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [March 3, 2020, 5:31am UTC](https://forum.openzeppelin.com/t/add-support-for-erc721-token-uri-concatenation-of-base-token-uri-and-tokenid/566/10 "2020-03-03T05:31:32Z")

</div>

The current implementation of ERC721Metadata.sol in OpenZeppelin Contracts 3.0 Beta doesn’t support token URI concatenation of base token URI and tokenID. ([https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1745](https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1745))

In a mint function, a token URI would need to be set.

> <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0-beta.0/contracts/token/ERC721/ERC721Metadata.sol#L72>

Instead of returning an empty string, we could return the baseURI concatenated with the tokenId.

```auto
        return string(abi.encodePacked(_baseURI, Strings.fromUint256(tokenId)));

```

Alternatively, `tokenURI` could be made `virtual` to allow it to be overriden.
