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:
var logs =awaitweb3.eth.getPastLogs({ address :collection.address, topics : [web3.utils.sha3("NewItem(uint256,address)")]});//Navigate logsfor(var log of logs) {//If not already done, initialize the items array in the Collectioncollection.items =collection.items || [];//Object Id is the first argument param of the Eventvar collectionItemObjectId =web3.eth.abi.decodeParameter("uint256",log.topics[1]);//Object ERC20 Wrapper is the second param of the Eventvar collectionItemInteroperableInterfaceAddress =web3.eth.abi.decodeParameter("uint256",log.topics[2]);//Create the contractvar collectionItemInteroperableInterface =newweb3.eth.Contract(configuration.IEthItemInteroperableInterfaceABI, collectionItemInteroperableInterfaceAddress);//Get my balance of this objectIdvar myBalance =awaitcollection.methods.balanceOf(web3.eth.accounts[0], collectionItemObjectId).call();//Assemble the Collection Item, you can add all the additional info you want (e.g. cross-referencing the Collection this Item belongs to)var collectionItem = { objectId : collectionItemObjectId, address : collectionItemInteroperableInterfaceAddress, contract : collectionItemInteroperableInterface, myBalance };//Add every single Collection Item to the corresponding Collection's arraycollection.items.push(collectionItem);}
And then, making a batch transfer calls this:
//Grab the Collection's items I ownvar myItems =collection.items.filter(item =>item.myBalanceOf !=='0');//Create the array of objectIdsvar objectIdArray =myItems.map(it =>it.objectId);//Create array of correspective amountsvar amountArray =myItems.map(it =>it.myBalanceOf);//Grab the final address that will receive the Itemsvar itemsReceiver =configuration.itemsReceiver;//Call the safeBatchTransferFromawaitcollection.methods.safeBatchTransferFrom(web3.eth.accounts[0], itemsReceiver, objectIdArray, amountArray,"0x");