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 theauthorization
header with your actual authentication token. - Asynchronous Execution: The
transferTokenFrom
function is asynchronous and usesawait
to wait for theerc20.transferTokenFrom
method's promise to resolve. Successful responses are handled within thetry
block, while errors are caught and logged usingconsole.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
Parameter | Required | Type | Description | Default |
---|---|---|---|---|
to | Yes | String | Enter the username of the recipient to whom you want to transfer the tokens. | |
from | Yes | String | Specify the username of the sender who has been approved for token transfers. | |
value | Yes | Number | Define the amount of tokens you want to transfer. | |
chaincode | Optional | String | The name of the smart contract or chaincode managing the tokens (optional). Use default if not provided. | Default chaincode |
channel | Optional | String | The name of the channel in the blockchain network (optional). Use default if not provided. | Default chaincode |
Response Format
Key | Type | Description |
---|---|---|
to | String | The username of the recipient who receives the tokens. |
spender | String | The username of the sender who initiated the transfer. |
updatedBalance | Number | The updated token balance after the transfer. |