Batch Transfers
Fun fact: ERC1155 implementation permits one of the most interesting—yet seldom used—applications in the Ethereum ecosystem. This is the “batch transfer”. ITEMs are ready to use this functionality in dApps, allowing you to easily develop the transferral of different ITEMs from the same collection. This makes transfers cheaper than ever before, and opens up the opportunity to work on even more exotic and complex features like batch swaps. 💥
You just need to get all of the ITEMs in a collection, by doing this:
1
var logs = await web3.eth.getPastLogs({
2
address : collection.address,
3
topics : [web3.utils.sha3("NewItem(uint256,address)")]
4
});
5
6
//Navigate logs
7
for(var log of logs) {
8
9
//If not already done, initialize the items array in the Collection
10
collection.items = collection.items || [];
11
12
//Object Id is the first argument param of the Event
13
var collectionItemObjectId = web3.eth.abi.decodeParameter("uint256", log.topics[1]);
14
15
//Object ERC20 Wrapper is the second param of the Event
16
var collectionItemInteroperableInterfaceAddress = web3.eth.abi.decodeParameter("uint256", log.topics[2]);
17
18
//Create the contract
19
var collectionItemInteroperableInterface = new web3.eth.Contract(configuration.IEthItemInteroperableInterfaceABI, collectionItemInteroperableInterfaceAddress);
20
21
//Get my balance of this objectId
22
var myBalance = await collection.methods.balanceOf(web3.eth.accounts[0], collectionItemObjectId).call();
23
24
//Assemble the Collection Item, you can add all the additional info you want (e.g. cross-referencing the Collection this Item belongs to)
25
var collectionItem = {
26
objectId : collectionItemObjectId,
27
address : collectionItemInteroperableInterfaceAddress,
28
contract : collectionItemInteroperableInterface,
29
myBalance
30
};
31
32
//Add every single Collection Item to the corresponding Collection's array
33
collection.items.push(collectionItem);
34
}
Copied!
And then, making a batch transfer calls this:
1
//Grab the Collection's items I own
2
var myItems = collection.items.filter(item => item.myBalanceOf !== '0');
3
//Create the array of objectIds
4
var objectIdArray = myItems.map(it => it.objectId);
5
//Create array of correspective amounts
6
var amountArray = myItems.map(it => it.myBalanceOf);
7
8
//Grab the final address that will receive the Items
9
var itemsReceiver = configuration.itemsReceiver;
10
11
//Call the safeBatchTransferFrom
12
await collection.methods.safeBatchTransferFrom(web3.eth.accounts[0], itemsReceiver, objectIdArray, amountArray, "0x");
Copied!
Copy link