1

I want to implement an ERC-1155 token to represent ownership of videos. Creating such a contract is relatively easy:

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract MyVideos is ERC1155 {
    uint256 public constant Video1 = 0;
    uint256 public constant Video2 = 1;
    uint256 public constant Video3 = 2;

    constructor() public ERC1155("https://video/api/item/{id}.json") {
        _mint(msg.sender, Video1, 100, "IPFS hash");
        _mint(msg.sender, Video2, 1000, "IPFS hash");
        _mint(msg.sender, Video3, 1, "IPFS hash");
    }
}

However, what I have no idea how to do is to create such ERC-1155 tokens dynamically and on the fly. Specifically, how would I add Video3 with 10000 tokens

(_mint(msg.sender, Video4, 10000, "IPFS hash");

But how do I add this new token after the contract MyVideos is already created?

I would really appreciate any help you can give me.

Many thanks!

1
  • Is the video stored off-chain at a URL?
    – user610620
    Feb 28 at 13:20
2

You need to write a public wrapper function for the _mint function. Since this function is internal, it can only be called from within the context of the contract itself.

Something like:

function addNewVideo(uint VideoN, uint ntokens, bytes memory IPFS_hash) public {
    _mint(msg.sender, VideoN, ntokens, IPFS_hash);
}
1
  • Thanks! One follow-up question. If VideoN has 100 tokens, each token is atomic and cannot be broken into fractional parts. Is that correct? Or is that a setting? Thanks! Jan 27 at 22:03
1

this is done using "Factory" approach. You have a factory contract and it does this for you. Take a look at uniswap v2 contract factory:

function createPair(address tokenA, address tokenB) external returns (address pair) {
        require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
        (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
        require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
        require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
        bytes memory bytecode = type(UniswapV2Pair).creationCode;
        bytes32 salt = keccak256(abi.encodePacked(token0, token1));
        assembly {
            pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        IUniswapV2Pair(pair).initialize(token0, token1);
        getPair[token0][token1] = pair;
        getPair[token1][token0] = pair; // populate mapping in the reverse direction
        allPairs.push(pair);
        emit PairCreated(token0, token1, pair, allPairs.length);
    }

https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Factory.sol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.