Batch Token Transfer to Multiple Users
This guide outlines the process for transferring multiple tokens to multiple users in a single operation using the ERC1155 API. The tokenTransferMultiUsers
method facilitates this process, enhancing the efficiency of token distributions.
Overview of the tokenTransferMultiUsers
Method
Batch token transfers are crucial for applications that require distributing tokens to multiple users simultaneously, such as rewards, airdrops, or initial token distributions. This method streamlines the process, reducing the complexity and cost associated with individual transfer transactions.
Prerequisites
- Installed the
edeXa-sdk
in your project. - An authentication token obtained through the SDK's authentication method. This token is necessary to authorize your requests.
Sample Code Snippet
Here is a JavaScript code snippet demonstrating the use of the tokenTransferMultiUsers
method:
import { ERC1155, Network } from 'edeXa-sdk';
async function tokenTransferToMultiUser() {
// Configuration settings for the ERC1155 instance
const settings = {
network: Network.SANDBOX, // Use Network.MAINNET for production environments
};
const erc1155 = new ERC1155({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your actual token
});
// Data required for the token transfer
const dataToTokenTransfer = {
to: ['username1', 'username2'], // Usernames of the recipients
tokenId: ['tokenId1', 'tokenId2'], // Token IDs to transfer
value: [100, 200], // Amounts of each token to transfer
chaincode: 'optionalChaincodeName', // Optional chaincode name
channel: 'optionalChannelName', // Optional blockchain channel
};
try {
// Execute the batch token transfer
const transferTokenResponse = await erc1155.tokenTransferMultiUsers(
dataToTokenTransfer
);
// Handle success
} catch (error) {
// Handle error
}
}
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
to | Array | Yes | An array containing the usernames of the users to receive tokens. |
tokenId | Array | Yes | An array of token IDs for the tokens being transferred. |
value | Array | Yes | An array of amounts for each token being transferred. |
chaincode | String | No | The name of the smart contract or chaincode managing the tokens. |
channel | String | No | The name of the blockchain channel where the transfer occurs. |
Response Format
Upon successfully executing the batch token transfer, the tokenTransferMultiUsers
method returns a response object containing:
Key | Type | Description |
---|---|---|
to | Array | An array of usernames of the users who received tokens. |
from | String | The username of the user who initiated the transfer. |