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:

var logs = await web3.eth.getPastLogs({
    address : collection.address,
    topics : [web3.utils.sha3("NewItem(uint256,address)")]
});

//Navigate logs
for(var log of logs) {

    //If not already done, initialize the items array in the Collection
    collection.items = collection.items || [];

    //Object Id is the first argument param of the Event
    var collectionItemObjectId = web3.eth.abi.decodeParameter("uint256", log.topics[1]);

    //Object ERC20 Wrapper is the second param of the Event
    var collectionItemInteroperableInterfaceAddress = web3.eth.abi.decodeParameter("uint256", log.topics[2]);

    //Create the contract
    var collectionItemInteroperableInterface = new web3.eth.Contract(configuration.IEthItemInteroperableInterfaceABI, collectionItemInteroperableInterfaceAddress);

    //Get my balance of this objectId
    var myBalance = await collection.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 array
    collection.items.push(collectionItem);
}      

And then, making a batch transfer calls this:

//Grab the Collection's items I own
var myItems = collection.items.filter(item => item.myBalanceOf !== '0');
//Create the array of objectIds
var objectIdArray = myItems.map(it => it.objectId);
//Create array of correspective amounts
var amountArray = myItems.map(it => it.myBalanceOf);

//Grab the final address that will receive the Items
var itemsReceiver = configuration.itemsReceiver;

//Call the safeBatchTransferFrom
await collection.methods.safeBatchTransferFrom(web3.eth.accounts[0], itemsReceiver, objectIdArray, amountArray, "0x");                   

Last updated