Skip to main content

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 the authorization header with your actual authentication token.
  • Asynchronous Execution: The transferToken function is an async function, using await for the erc20.transferToken method's promise to resolve. The transfer details will be logged on successful resolution. Errors are caught and logged with console.error.

Request Parameters

The dataToTransferToken object contains the following parameters for the transferToken method:

ParameterRequiredTypeDescription
toYesStringThe username of the user to whom you want to transfer the tokens
valueYesStringThe amount of tokens to transfer
chaincodeOptionalStringThe name of the smart contract or chaincode managing the tokens
channelOptionalStringThe name of the channel in the blockchain network
  • If chaincode and channel are not provided, the default chaincode and channel will be used.

Response Format

The response for a successful token transfer includes the following attributes:

KeyTypeDescription
tostringThe recipient's username
fromstringThe sender's username
updatedBalancenumberThe updated balance of the sender