safeTransferFrom
The safeTransferFrom
method for the ERC-1155 token standard manages the secure transfer of a specified amount of a token with a given ID from one Ethereum address to another. This method ensures a safe transaction by checking if the receiving address implements the necessary ERC-1155 token receiver interface. The method is encapsulated within the ERC1155Instance
object, which is available through the appropriate method of the EdexaClient
or the associated library.
Method Signature
async safeTransferFrom(
from: string,
to: string,
id: string,
amount: string,
data: string = "0x",
signer: WalletSigner
): Promise<TransactionResponse>
Parameters
Parameter | Type | Description |
---|---|---|
from | string | The Ethereum address from which the tokens will be transferred. |
to | string | The Ethereum address designated to receive the tokens. |
id | string | The specific ID of the ERC-1155 token involved in the transfer. |
amount | string | The quantity of the token to be transferred for the provided ID. |
data | string | Optional additional data or bytes to accompany the transfer; defaults to "0x". |
signer | WalletSigner | The wallet signer instance responsible for signing the transaction. |
Returns
- A
Promise
resolving to aTransactionResponse
, which provides details about the secure transfer transaction.
Usage Example
First, instantiate the desired ERC-1155 token contract:
const erc1155 = edexaClient.getERC1155Instance("your ERC-1155 contract address here");
Then, initiate the safeTransferFrom
method with the relevant parameters:
const senderAddress = "0x1a2b3C45d6E7f8b9E0ed5678Df1234567A89BcDe";
const recipientAddress = "0x2c360D20cE6b3D8b466511eF093C9177c3817B94";
const tokenId = "specific-token-id";
const transferAmount = "50";
const dataBytes = "0x"; // Optional data bytes
const signer = edexaClient.createWalletSigner("your private key");
const transferTransaction = await erc1155.safeTransferFrom(senderAddress, recipientAddress, tokenId, transferAmount, dataBytes, signer);
console.log(`Transfer Transaction Hash: ${transferTransaction.hash}`);
Notes
The
safeTransferFrom
method adds an additional layer of security by confirming that the recipient address is capable of receiving ERC-1155 tokens. This helps avoid unintentional loss of tokens.Always validate the Ethereum addresses, token ID, and other parameters prior to initiating a transfer to ensure accurate and secure token movement.