Skip to main content

safeTransferFrom

The safeTransferFrom method pertains to the ERC-721 token standard (often associated with non-fungible tokens or NFTs). This method allows for the secure transfer of a specific NFT from one Ethereum address to another. The "safe" in its name implies that this method also checks to ensure that the receiving address is capable of handling ERC-721 tokens, adding an extra layer of protection against accidental loss of tokens. The method is part of the ERC721Instance object, typically accessible through client libraries such as EdexaClient.

Method Signature

async safeTransferFrom(from: string, to: string, tokenId: string, signer: WalletSigner): Promise<TransactionResponse>

Parameters

ParameterTypeDescription
fromstringThe Ethereum address from which the non-fungible token (NFT) is being sent. This should be the current owner of the specified token.
tostringThe Ethereum address where the NFT will be received. This method will ensure that this address is compatible with ERC-721 tokens.
tokenIdstringA unique identifier corresponding to the ERC-721 token that is to be transferred. Each NFT has a distinct tokenId that differentiates it from other tokens within the same contract.
signerWalletSignerThe entity or mechanism responsible for signing and authorizing the transaction. This signer is often generated from various methods such as a software wallet, a hardware wallet, or other private key management mechanisms, ensuring transaction security and authenticity.

Returns

  • A Promise that resolves to a TransactionResponse object. This object provides details about the transaction, such as its status, transaction hash, and more.

Usage Example

First, instantiate the desired ERC-721 token contract:

const erc721 = edexaClient.getERC721Instance("your ERC-721 contract address here");

Then, use the safeTransferFrom method to securely transfer a specific NFT:

const senderAddress = "0xSenderEthereumAddress";
const recipientAddress = "0xRecipientEthereumAddress";
const tokenId = "12345"; // Example token ID
const signer = edexaClient.createWalletSigner("your private key");

const transactionResponse = await erc721.safeTransferFrom(senderAddress, recipientAddress, tokenId, signer);
console.log(`Transaction Hash: ${transactionResponse.hash}`);

Notes

  • Ensure that the from address actually owns the token with the specified tokenId before initiating the transfer.

  • The safeTransferFrom method will fail if the to address is a contract that does not implement the required ERC-721 token receiver interface.

  • As always, handle private keys with extreme caution. Ensure the signer is managed securely and avoid exposing private keys in unsecured environments.