To initialize an instance of the ZebecBulkClient class, you need to provide a signer or provider and optionally specify contract addresses for BulkTransfer and Core. Here's an example of how to initialize the class:
import { ethers } from"ethers";import { ZebecBulkClient } from"@zebec-protocol/zebec-bnb-sdk";// Initialize with a signer (e.g., MetaMask)constsigner=newethers.Wallet("your-private-key");constbulkClient=newZebecBulkClient(signer);
Initialize Bulk Instant
constbulkStreamName="YourBulkStreamName";constnow=Math.floor(Date.now() /1000);constamounts= ["1","2","3"];consttokenAddress="0xTokenAddress";constreceivers= [receiver1.address,receiver2.address,receiver3.address];conststartTimes= [BigNumber.from(now +100)]; // An array of start times. Length must be equal to recurring FrequencyconstrecurringFrequency=1;constoverrides= {}; // Optional overridesconstbulkInstantTree=awaitbulkClient.getBulkInstantTransferRoot( tokenAddress, amounts, receivers );consttxnReceipt=awaitbulkClient.initBlulkInstantTransfer( bulkStreamName,bulkInstantTree.root, startTimes, recurringFrequency );console.log(txnReceipt);
initBlulkInstantTransfer method initializes a bulk stream with a bulk tree root, start times, and other parameters.
getBulkInstantTransferRoot function is designed to calculate and retrieve the Merkle tree root for a bulk instant transfer. This function is used to create a Merkle tree structure that represents a set of instant token transfers that will occur simultaneously.
Update Bulk Transfer
constbulkName="Updated Bulk";constbulkIndex=awaitreceiver1Client.getLatestBulkCount(sender.address).toNumber(); // bulk index to updateconstamounts= ["5","7","9"];consttokenAddress="0xNewTokenAddress";constreceivers= [receiver1.address,receiver2.address,receiver3.address];conststartTimes= [BigNumber.from(now +600),BigNumber.from(now +900)]; // An array of start times. Length must be equal to recurring FrequencyconstrecurringFrequency=2;constoverrides= {}; // Optional overridesbulkInstantTree =awaitbulkClient.updateBulkInstantTransferRoot( tokenAddress, amounts, receivers, withdrawBulkCount);consttxnReceipt=awaitbulkClient.updateBulkTransfer( bulkName, bulkIndex, merkleRoot, startTimes, recurringFrequency, overrides);console.log(txnReceipt);
updateBulkInstantTransferRoot function is used to update an existing bulk instant transfer by recalculating the Merkle tree root based on new transfer data.
updateBulkTransfer method updates a bulk transfer with new information such as a merkle root and start times.
Withdraw Bulk Instant Transfer By Receiver
constamount="1";consttokenAddress="0xTokenAddress";constsender="0xSenderAddress";constreceiver="0xReceiverAddress";constproofs= ["0xProof1","0xProof2"]; // Array of merkle proofsconstzebecWalletTransfer=false;constoverrides= {}; // Optional overridesconstbulkIndex=awaitreceiver1Client.getLatestBulkCount(sender.address).toNumber(); // get the latest bulk indexconsttxnReceipt=awaitbulkClient.withdrawBulkInstantTransfer( bulkIndex, amount, tokenAddress, sender, receiver, proofs, zebecWalletTransfer, overrides);console.log(txnReceipt);
This method allows a user to withdraw funds from a bulk instant transfer using merkle proofs.
Note: The purpose of the getLatestBulkCount function is to determine the number of bulk transfers that a user has created within the Zebec protocol. Bulk transfers are collections of individual token streams, and each bulk transfer has a unique index or identifier.
The calculateLatestBulkBytes function in the ZebecBulkClient class is used to calculate the byte representation of the latest bulk transfer associated with a specific user's address. This function allows you to obtain the byte representation of the most recent bulk transfer.
Get Bulk Transfer Root
constbulkBytes="0xabcdef123456..."; // Replace with the actual bulk transfer identifier// Retrieve the Merkle root of the specified bulk transferconstbulkTransferRoot=awaitbulkClient.getBulkTransferRoot(bulkTransferBytes);console.log(`Merkle Root of Bulk Transfer: ${bulkTransferRoot}`)
The getBulkTransferRoot function is used to retrieve the Merkle root associated with a specific bulk transfer.
Verify Bulk Transfer
// Verify the bulk instant transferconstverified=awaitbulkClient.verifyBulkInstantTransfer( index, amount, tokenAddress, sender, receiver, proofs);if (verified) {console.log("Bulk Instant Transfer is valid and verified.");} else {console.log("Bulk Instant Transfer is not valid.");}
The purpose of the verifyBulkInstantTransfer function is to perform verification checks on a bulk instant transfer to ensure that it matches the expected properties and has not been manipulated or tampered with.