Skip to main content

Fetching Token Owner

Determining the ownership of a specific token is a common requirement in applications that deal with Non-Fungible Tokens (NFTs). The edeXa ERC721 SDK simplifies this task with its getOwner method. This guide provides an example of how to retrieve the owner of a token.


Overview of the getOwner Method

The getOwner method is designed to fetch the owner of a particular token, based on its unique token ID. This functionality is crucial for tracking ownership and managing transfers of NFTs.


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 getOwner method:

import { ERC721, Network } from 'edeXa-sdk';
async function getOwner() {
const settings = {
network: Network.SANDBOX, // Or Network.MAINNET for production
};
const erc721 = new ERC721({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your actual authentication token
});

const dataToGetOwner = {
tokenId: 'Enter the tokenId to find the owner', // Required token ID
chaincode: 'Optional smart contract name', // Managing tokens' smart contract name
channel: 'Optional blockchain channel name', // Blockchain channel name
};

try {
const getOwnerResponse = await erc721.getOwner(dataToGetOwner);
// Handle success
} catch (error) {
// Handle error
}
}

Request Parameters

ParameterRequiredTypeDescription
tokenIdYesNumberThe unique identifier of the token whose owner you want to find.
chaincodeNoStringName of the smart contract managing the tokens (optional).
channelNoStringName of the blockchain channel (optional).

Response Format

When the getOwner method successfully retrieves the token's owner, it returns a response containing the owner's identifier.

AttributeTypeDescription
ownerStringThe username or identifier of the owner of the specified token.