Transfer Token to Users
To transfer tokens to users using the ERC20 API, you can use the transferToken
method. This method allows you to send tokens to a specific user.
Prerequisites
Authenticate the client using the edeXa SDK. For authentication details, see the authentication method.
Ensure that users are enrolled and have valid usernames. You can enroll users using the enrollUser method.
Mint tokens using the Mint Tokens method before attempting to transfer them.
Sample Code Snippet
Here is a JavaScript code snippet demonstrating the use of the tranferToken
method:
// SDK.js
import { ERC20, Network } from 'edeXa-sdk';
async function transferToken() {
const erc20 = new ERC20({
...settings,
authorization: `Bearer ${token}`,
});
const settings = {
network: Network.SANDBOX, // or Network.MAINNET for the mainnet network
};
const dataToTransferToken = {
to: 'Enter the username of the user to whom you want to transfer the tokens',
value: 'The amount of tokens to transfer',
chaincode: 'Name of the smart contract', // Optional
channel: 'Name of the channel', // Optional
};
try {
const transferTokenResponse = await erc20.transferToken(
dataToTransferToken
);
// Handle success
} catch (error) {
// Handle error
}
}
Important Notes
- Authentication Token: Replace the
token
in theauthorization
header with your actual authentication token. - Asynchronous Execution: The
transferToken
function is anasync
function, usingawait
for theerc20.transferToken
method's promise to resolve. The transfer details will be logged on successful resolution. Errors are caught and logged withconsole.error
.
Request Parameters
The dataToTransferToken
object contains the following parameters for the transferToken
method:
Parameter | Required | Type | Description |
---|---|---|---|
to | Yes | String | The username of the user to whom you want to transfer the tokens |
value | Yes | String | The amount of tokens to transfer |
chaincode | Optional | String | The name of the smart contract or chaincode managing the tokens |
channel | Optional | String | The name of the channel in the blockchain network |
- If
chaincode
andchannel
are not provided, the default chaincode and channel will be used.
Response Format
The response for a successful token transfer includes the following attributes:
Key | Type | Description |
---|---|---|
to | string | The recipient's username |
from | string | The sender's username |
updatedBalance | number | The updated balance of the sender |