approve
The approve
method, in the context of the ERC-721 token standard, permits the owner of a specific non-fungible token (NFT) to approve another Ethereum address to transfer said NFT on their behalf. Each NFT can have only one approved address at any given time. This method belongs to the ERC721Instance
object, obtainable from the appropriate client library (like EdexaClient
or its equivalent).
Method Signature
async approve(userAddress: string, tokenId: string, signer: WalletSigner): Promise<TransactionResponse>
Parameters
Parameter | Type | Description |
---|---|---|
userAddress | string | The Ethereum address to which you're granting permission to transfer the specific ERC-721 token. |
tokenId | string | The unique identifier of the ERC-721 token for which you're setting the approved address. |
signer | WalletSigner | The wallet signer instance used for signing the transaction. This should be associated with the owner of the specified token. |
Returns
- A
Promise
that resolves to aTransactionResponse
object. This object provides transaction details like its status, transaction hash, and others.
Usage Example
First, you'd instantiate your desired ERC-721 token contract:
const erc721 = edexaClient.getERC721Instance("your ERC-721 contract address here");
Then, utilize the approve
method to set an approval:
const userAddressToApprove = "0xExampleUserAddressHere";
const tokenIdToApprove = "12345"; // Example token ID
const signer = edexaClient.createWalletSigner("your private key");
const transactionResponse = await erc721.approve(userAddressToApprove, tokenIdToApprove, signer);
console.log(`Transaction Hash: ${transactionResponse.hash}`);
Notes
Only the current owner of an ERC-721 token can set approvals for it.
Setting a new approved address will override any previous approval for that specific token.
Always handle your private keys with care. The
signer
should be created and used securely, avoiding exposure in client-side or insecure environments.