首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行ERC-1155合同

执行ERC-1155合同
EN

Ethereum用户
提问于 2021-01-27 15:57:24
回答 2查看 1.2K关注 0票数 2

我想实现一个ERC-1155令牌来代表视频的所有权。创建这样一个合同相对容易:

代码语言:javascript
复制
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");
    }
}

但是,我不知道如何动态和动态地创建这样的ERC-1155令牌。具体来说,我如何使用10000令牌添加Video3?

代码语言:javascript
复制
(_mint(msg.sender, Video4, 10000, "IPFS hash");

但是,如何在合同MyVideos已经创建之后添加这个新令牌?

我真的很感谢你能帮我的忙。

非常感谢!

EN

回答 2

Ethereum用户

回答已采纳

发布于 2021-01-27 16:35:13

您需要为_mint函数编写一个公共包装函数。因为这个函数是internal,所以只能在契约本身的上下文中调用它。

类似于:

代码语言:javascript
复制
function addNewVideo(uint VideoN, uint ntokens, bytes memory IPFS_hash) public {
    _mint(msg.sender, VideoN, ntokens, IPFS_hash);
}
票数 2
EN

Ethereum用户

发布于 2021-01-27 16:47:06

这是使用“工厂”方法完成的。你有一个工厂合同,它为你做这件事。请看一看uniswap v2合同工厂:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/92823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档