Transfer Token on behalf of the Token Operator
Facilitating the transfer of tokens from one user to another, particularly on behalf of the original token owner, is a crucial feature of the ERC721 standard. The tokenTransferFrom
method in the edeXa ERC721 SDK enables this operation by allowing tokens to be transferred with prior approval. This document details how to use the tokenTransferFrom
method effectively.
Overview of the tokenTransferFrom
Method
The tokenTransferFrom
method is designed for situations where a token needs to be transferred from one user to another, not directly by the token owner but by an approved operator or the sender on behalf of the owner.
Prerequisites
- Installation of the edeXa ERC721 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 is a JavaScript code snippet demonstrating the use of the tokenTransferFrom
method:
import { ERC721, Network } from 'edeXa-sdk';
async function tokenTransferFrom() {
const settings = {
network: Network.SANDBOX, // Or Network.MAINNET for production
authorization: `Bearer ${token}`, // Replace `${token}` with your actual authentication token
};
const erc721 = new ERC721(settings);
const dataToTokenTransferFrom = {
to: 'Enter the username of the recipient', // Required
from: 'Enter the username of the sender', // Required
tokenId: 'The tokenId of the token to transfer', // Required
chaincode: 'Optional smart contract name', // Managing tokens' smart contract
channel: 'Optional blockchain channel name', // Blockchain channel
};
try {
const tokenTransferFromResponse = await erc721.tokenTransferFrom(
dataToTokenTransferFrom
);
// Handle success
} catch (error) {
// Handle error
}
}
Request Parameters
Parameter | Required | Type | Description |
---|---|---|---|
to | Yes | String | The username of the recipient to whom the tokens are being transferred. |
from | Yes | String | The username of the sender who is approved for token transfers. |
tokenId | Yes | Number | The unique identifier of the token to transfer. |
chaincode | No | String | Name of the smart contract managing the tokens (optional). |
channel | No | String | Name of the blockchain channel (optional). |
Response Format
Upon successful execution, the tokenTransferFrom
method returns a response containing details about the transfer operation.
Attribute | Type | Description |
---|---|---|
spender | String | The username of the user who initiated the transfer. |
to | String | The username of the user who received the token. |
tokenId | Number | The unique identifier of the token that was transferred. |