我想实现一个ERC-1155令牌来代表视频的所有权。创建这样一个合同相对容易:
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?
(_mint(msg.sender, Video4, 10000, "IPFS hash");但是,如何在合同MyVideos已经创建之后添加这个新令牌?
我真的很感谢你能帮我的忙。
非常感谢!
发布于 2021-01-27 16:35:13
您需要为_mint函数编写一个公共包装函数。因为这个函数是internal,所以只能在契约本身的上下文中调用它。
类似于:
function addNewVideo(uint VideoN, uint ntokens, bytes memory IPFS_hash) public {
_mint(msg.sender, VideoN, ntokens, IPFS_hash);
}发布于 2021-01-27 16:47:06
这是使用“工厂”方法完成的。你有一个工厂合同,它为你做这件事。请看一看uniswap v2合同工厂:
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
https://ethereum.stackexchange.com/questions/92823
复制相似问题