Skip to main content

Setting Token Approval

This guide provides detailed instructions on how to set approval for token access using the ERC1155 API, a key feature for managing token permissions within your blockchain application. By following this guide, developers can allow users to grant permission to third parties, enabling them to spend tokens on their behalf.


Overview of the approveTokenAccess Method

The approveTokenAccess method is crucial for applications that require token spending by an entity other than the token owner. This functionality is similar to the ERC-20 approve method but tailored for the ERC1155 standard, which supports fungible and non-fungible token types.


Prerequisites

  • Installed the edeXa-sdk in your project.
  • An authentication token obtained through the SDK's authentication method. This token is necessary to authorize your requests.
  • Identified the spender's username and the amount of tokens you wish to approve for spending.

Sample Code Snippet

The following example demonstrates how to use the approveTokenAccess method to set a token spending approval:

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

async function approveTokenAccess() {
// Configuration settings for the ERC1155 instance
const settings = {
network: Network.SANDBOX, // Use Network.MAINNET for production environments
};

// Initialize the ERC1155 instance with authorization token and settings
const erc1155 = new ERC1155({
...settings,
authorization: `Bearer ${token}`, // Ensure to replace `${token}` with your actual token
});

// Data required to set token spending approval
const dataToSetApproval = {
spender: 'usernameOfSpender', // Username of the user to grant spending approval
value: 100, // Amount of tokens to approve for spending
chaincode: 'optionalChaincodeName', // Name of the smart contract (optional)
channel: 'optionalChannelName', // Name of the blockchain channel (optional)
};

try {
// Attempt to set token spending approval
const setOperatorResponse = await erc1155.approveTokenAccess(
dataToSetApproval
);
// Handle success
} catch (error) {
// Handle error
}
}

Request Parameters

Below is a table that outlines the parameters required to set token spending approval through the approveTokenAccess method. Each parameter is listed with its type, whether it is required or optional, and a description of its purpose.

ParameterRequiredTypeDescription
spenderYesStringThe username of the user whom you wish to grant approval for token access. This should match the username obtained through user enrollment or account creation.
valueYesNumberThe amount of tokens the spender is allowed to use. This should be a positive integer or decimal number, depending on the token's divisibility.
chaincodeNoStringSpecifies the name of the smart contract managing the tokens. This is useful when interacting with specific contracts within your application.
channelNoStringIndicates the blockchain network channel where the transaction will be processed. This is relevant for applications that operate across multiple channels.

Response Format

Upon successful execution, the approveTokenAccess method returns a response object containing:

KeyTypeDescription
toStringThe username of the spender.
allowanceAmountNumberThe amount of tokens approved for spending.