Transferring Tokens to Users
This guide explains how to use the tokenTransfer
method to transfer tokens to users within your blockchain application using the ERC1155 API. The method allows for the secure and efficient transfer of tokens between accounts.
Overview of the tokenTransfer
Method
Token transfers are a fundamental operation in token-based blockchain applications, enabling the movement of tokens from one account to another. The tokenTransfer
method simplifies this process, ensuring tokens are transferred correctly and securely.
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's an example demonstrating how to use the tokenTransfer
method:
import { ERC1155, Network } from 'edeXa-sdk';
async function transferToken() {
// 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 dataToTransferToken = {
to: 'usernameOfRecipient', // Username of the recipient
tokenId: 'uniqueTokenId', // Token ID of the token to transfer
value: 100, // Amount of tokens to transfer
chaincode: 'optionalChaincodeName', // Optional chaincode name
channel: 'optionalChannelName', // Optional blockchain channel
};
try {
// Execute the token transfer
const transferTokenResponse = await erc1155.tokenTransfer(
dataToTransferToken
);
// Handle success
} catch (error) {
// Handle error
}
}
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
to | String | Yes | The username of the user to whom you want to transfer the tokens. |
tokenId | String | Yes | The ID of the token you wish to transfer. |
value | Number | Yes | The amount of tokens to transfer. |
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 token transfer, the tokenTransfer
method returns a response object containing:
Key | Type | Description |
---|---|---|
to | String | The username of the recipient. |
from | String | The username of the sender (the authenticated user). |