2

Developing an ERC1155 contract, how to generate an address for a new token?

I need to create an API function like function createToken() returns (address). How?

Should I just use an incrementing-by-one value starting from 1?

0

At first interface of eip1155 has no alike you offer method. And we need to use some interface or contract instance to create new, but by this protocol must to be possible add any contract. (univeral interface not exist)

how to generate an address for a new token?

Let see, all contract get the address when they was issued. And if you do it via another contract it will be create as internal transaction (not official ethereum nodes detects only).

I need to create an API function like function createToken() returns (address). How?

maybe do function like this addToken(address _token) public hasRights() returns (id);. Deploy contract via web3, then in callback call your method: (instContract) => instERC1155.addToken(instContract.options.address) .send({ gas: 0x, gasPrice: 0x, from: 0x })

Should I just use an incrementing-by-one value starting from 1?

Yes you could use Safe Storage pattern + mapping (id => address) as hash table.

0

I need to create an API function like function createToken() returns (address).

You should return a uint256 (not an address) because tokens in ERC1155 are represented by ids: these tokens are not contracts that have an address.

Should I just use an incrementing-by-one value starting from 1?

Yes, that's what the reference implementation of ERC1155 does. (The first _id is 1 and just gets incremented by 1.)

// Creates a new token type and assings _initialSupply to minter
function create(uint256 _initialSupply, string calldata _uri) external returns(uint256 _id) {

    _id = ++nonce;
    creators[_id] = msg.sender;
    balances[_id][msg.sender] = _initialSupply;

    // Transfer event with mint semantic
    emit TransferSingle(msg.sender, address(0x0), msg.sender, _id, _initialSupply);

    if (bytes(_uri).length > 0)
        emit URI(_uri, _id);
}

The reference implementation is among the listed ERC1155 references: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1155.md#references

  • Creating a new contract for every token is against the spirit of ERC1155 and is inefficient. Just remove your answer, it's bad. – porton Oct 20 at 21:10
  • 1
    @porton The question was unclear: you wanted a function that returns an address. I read ERC1155 further and I think the answer is what you're looking for now. I can help clean up our comments if this is satisfied. – eth Oct 23 at 1:54

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.