Basics
Building a collection consists of specifying its name, symbol, extension address, metadata link, and whether or not to use decimals (see relevant section).
Each collection is characterized by these elements.
1
//Let's create and unload the metadata on IPFS
2
var IPFSHttpClient = require('ipfs-http-client');
3
4
var ipfs = new IPFSHttpClient();
5
6
var metadata = {
7
licence_url : 'https://my.awesome_licence.com',
8
image : 'https://ipfs.io/ipfs/xxxx',
9
description : "My Item is the best on earth!"
10
}
11
12
var fileToUpload = [new Blob([JSON.stringify(metadata, null, 4)], { type: "application/json" })];
13
14
var metadataLink = "ipfs://ipfs/";
15
for await (var upload of ipfs.add(fileToUpload)) {
16
metadataLink += upload.path;
17
break;
18
}
19
20
//Name and symbol are mandatory
21
var name = "MyAwesomeToken";
22
var symbol = "MAT";
23
24
//means that the ERC1155 side has 18 decimals, 1 if false
25
var hasDecimals = true;
26
27
//Set the Collection host. It will have the power to mint new Items, mint new tokens for an existing Item and to change Collection and Item Uris
28
var extensionAddress = web3.eth.accounts[0];
29
30
//An extension can also be a Smart Contract. If so, it is possible to specify an optional payload that the brand-new Collection will use to init the extension
31
var extensionPayload = "0x";
32
33
//The Native Collection creation is done by calling the Orchestrator which, internally, calls the active Factory.
34
//To make the structure fully upgradable, the Orchestrator will pass to the Factory an abi-encoded payload containing the parameters and the function to call the Native Collection model.
35
36
//Let's grab the first 8 bytes of the Native Model init(...) method signature
37
var initPayload = web3.utils.sha3("init(string,string,bool,string,address,bytes)");
38
initPayload = initPayload.substring(0, 10);
39
40
//Let's build the data payload
41
var initPayloadData = web3.eth.abi.encodeParameters([
42
"string",
43
"string",
44
"bool",
45
"string",
46
"address",
47
"bytes"
48
], [
49
name,
50
symbol,
51
hasDecimals,
52
metadataLink,
53
extensionAddress,
54
extensionPayload
55
]);
56
57
//Now fuse all together
58
initPayload += initPayloadData.substring(2);
59
60
//Take the current block number, useful for search later
61
var blockNumber = web3.eth.getBlockNumber();
62
63
await ethItemOrchestrator.methods.createNative(initPayload).send();
64
65
//Unfortunately, it is not actually possible to grab return types of Smart Contract from web3, so we need to search for the NatvieCreated event in the blockchain to retrieve the new item
66
var logs = await web3.eth.getPastLogs({
67
address : await orchestrator.methods.factory().call(),
68
topics : [web3.utils.sha3("NewNativeCreated(address,uint256,address,address)")],
69
fromBlock : blockNumber,
70
toBlock : 'latest'
71
});
72
73
var collectionAddress = web3.eth.abi.decodeParameter("address", logs[0].topics[log.topics.length - 1]);
74
75
var collection = {
76
address : collectionAddress,
77
category : "Native",
78
contract : new web3.eth.Contract(configuration.NativeABI, collectionAddress)
79
};
80
81
//Let's change the URI
82
metadata.licence_url = "https://another.uri.licence.com";
83
84
fileToUpload = [new Blob([JSON.stringify(metadata, null, 4)], { type: "application/json" })];
85
86
metadataLink = "ipfs://ipfs/";
87
for await (var upload of ipfs.add(fileToUpload)) {
88
metadataLink += upload.path;
89
break;
90
}
91
92
await collection.contract.methods.setUri(metadataLink).send();
93
94
//It is also possible to totally lose the host property of a Collection. THIS IS A IRREVERSIBLE FUNCTION
95
collection.contract.methods.releaseExtension().send();
96
97
//From now on, it will not be possible to mint new Items or change the URIs
Copied!
Copy link