Skip to main content

Transfer Tokens on Behalf of the Token Operator

Transferring tokens from one user to another, on behalf of the original token owner, is a crucial operation made possible by the ERC20 API. This operation is simplified with the transferTokenFrom method. Here's how it works:


Prerequisites

  • Authenticate the client using the edeXa SDK. For authentication details, see the authentication method.

  • Ensure that the sender has been approved for token transfers by setting an operator. The operator is a user who has been granted permission to transfer tokens on behalf of another user. This should be done using the setOperator method.


Sample Code Snippet

Here is a JavaScript code snippet demonstrating the use of the transferTokenFrom method:

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

async function transferTokenFrom() {
const erc20 = new ERC20({
...settings,
authorization: `Bearer ${token}`,
});

const settings = {
network: Network.SANDBOX, // or Network.MAINNET for the mainnet network
};

const dataToTransferTokenFrom = {
to: 'Enter the username of the recipient to whom you want to transfer the tokens',
from: 'Enter the username of the sender who has been approved for token transfers',
value: 'The amount of tokens to transfer',
chaincode:
'The name of the smart contract or chaincode managing the tokens (optional)',
channel: 'The name of the channel in the blockchain network (optional)',
};

try {
const transferTokenFromResponse = await erc20.transferTokenFrom(
dataToTransferTokenFrom
);
// 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 transferTokenFrom function is asynchronous and uses await to wait for the erc20.transferTokenFrom method's promise to resolve. Successful responses are handled within the try block, while errors are caught and logged using console.error.

Before using the transferTokenFrom method, make sure you've set an operator for the sender and approved some allowance for that particular sender. This ensures that you can accurately transfer tokens on behalf of the original token owner.


Request Parameters

ParameterRequiredTypeDescriptionDefault
toYesStringEnter the username of the recipient to whom you want to transfer the tokens.
fromYesStringSpecify the username of the sender who has been approved for token transfers.
valueYesNumberDefine the amount of tokens you want to transfer.
chaincodeOptionalStringThe name of the smart contract or chaincode managing the tokens (optional). Use default if not provided.Default chaincode
channelOptionalStringThe name of the channel in the blockchain network (optional). Use default if not provided.Default chaincode

Response Format

KeyTypeDescription
toStringThe username of the recipient who receives the tokens.
spenderStringThe username of the sender who initiated the transfer.
updatedBalanceNumberThe updated token balance after the transfer.