safeMint
The safeMint
method is particular to the ERC-721 token standard, which represents non-fungible tokens (NFTs) on the Ethereum blockchain. This method allows the minting of a new NFT and assigns it to a specified Ethereum address. The "safe" in safeMint
ensures that the destination address can securely receive NFTs, checking if the recipient address has implemented the required functions to accept ERC-721 tokens.
This method is part of the ERC721Instance
object, which can be obtained via an appropriate method from a client library, such as EdexaClient
.
Method Signature
async safeMint(recipient: string, tokenURI: string, signer: WalletSigner): Promise<TransactionResponse>
Parameters
Parameter | Type | Description |
---|---|---|
recipient | string | The Ethereum address designated to receive the newly minted non-fungible token (NFT). |
tokenURI | string | A unique resource identifier string pointing to the metadata of the token, usually referencing a JSON file that describes the properties and attributes of the NFT. |
signer | WalletSigner | The signer instance responsible for initiating and signing the transaction. The signer can be derived from various sources, such as software wallets, hardware wallets, or private key storage solutions. |
Returns
- A
Promise
resolving to aTransactionResponse
object, which provides details about the minting transaction, including 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 safeMint
method to mint a new NFT:
const recipientAddress = "0xExampleRecipientAddressHere";
const metadataURI = "https://example.com/token/12345.json"; // An example token metadata URI
const signer = edexaClient.createWalletSigner("your private key");
const transactionResponse = await erc721.safeMint(recipientAddress, metadataURI, signer);
console.log(`Minting Transaction Hash: ${transactionResponse.hash}`);
Notes
Ensure the minting address (associated with the signer) has the necessary permissions or roles to mint tokens, as defined in the ERC-721 contract.
The
tokenURI
usually points to metadata about the NFT, such as its name, image, description, and other attributes. Ensure it's accessible and correctly formatted.The
safeMint
method checks if the recipient address can handle ERC-721 tokens. If not, the minting process will fail to ensure the tokens aren't sent to contracts or addresses that can't manage them.