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.
Parameter | Required | Type | Description |
---|---|---|---|
spender | Yes | String | The 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. |
value | Yes | Number | The amount of tokens the spender is allowed to use. This should be a positive integer or decimal number, depending on the token's divisibility. |
chaincode | No | String | Specifies the name of the smart contract managing the tokens. This is useful when interacting with specific contracts within your application. |
channel | No | String | Indicates 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:
Key | Type | Description |
---|---|---|
to | String | The username of the spender. |
allowanceAmount | Number | The amount of tokens approved for spending. |