Skip to main content

Batch Token Transfer

The batchTokenTransfer method enables the efficient transfer of multiple tokens to a single user or multiple users in a single operation. This functionality is essential for applications that need to distribute tokens to users en masse, such as rewards distribution, airdrops, or bulk transfers.


Overview of the batchTokenTransfer Method

Transferring tokens is a foundational operation in token-based blockchain applications. The batchTokenTransfer method enhances this operation by allowing developers to transfer various amounts of different tokens to users in one transaction, simplifying the process and saving transaction fees.


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 batchTokenTransfer method:

import { ERC1155, Network } from 'edeXa-sdk';

async function batchTokenTransfer() {
// Configuration settings for the ERC1155 instance
const settings = {
network: Network.SANDBOX, // Or Network.MAINNET for production
};

const erc1155 = new ERC1155({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your actual token
});

// Data required for the batch token transfer
const dataToBatchTokenTransfer = {
to: 'usernameOfRecipient', // Username of the recipient
tokenId: ['tokenId1', 'tokenId2'], // Array of token IDs to transfer
value: [100, 200], // Array of amounts for each token
chaincode: 'optionalChaincodeName', // Optional chaincode name
channel: 'optionalChannelName', // Optional channel name
};

try {
// Perform the batch token transfer
const transferTokenResponse = await erc1155.batchTokenTransfer(
dataToBatchTokenTransfer
);
// Handle success
} catch (error) {
// Handle error
}
}

Request Parameters

The table below details the parameters required for executing a batch token transfer:

ParameterTypeRequiredDescription
toStringYesThe username of the user to whom you want to transfer the tokens.
tokenIdArrayYesAn array of token IDs for the tokens you want to transfer.
valueArrayYesAn array of respective amounts of tokens to transfer.
chaincodeStringNoThe name of the smart contract or chaincode managing the tokens.
channelStringNoThe name of the blockchain channel where the transfer occurs.

Response Format

Upon successfully transferring the tokens, the batchTokenTransfer method returns a response object containing:

KeyTypeDescription
toStringThe username of the recipient.
fromStringThe username of the sender (the authenticated user).