Create and Transfer an NFT using a Solidity Contract
Summary
Besides creating NFTs using Hedera SDK, you can use a Solidity Contract to create, mint, and transfer NFTs by calling contract functions directly. These are the contracts you will need to import into your working directory provided by Hedera that you can find in the contracts folder here:
HederaTokenService.sol
HederaResponseCodes.sol
IHederaTokenService.sol
ExpiryHelper.sol
FeeHelper.sol
KeyHelper.sol
Prerequisites
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.
Get a Hedera testnet account.
Set up your environment here.
If you are interested in creating, minting, and transferring NFTs using Hedera SDKs you can find the example here.
In this example, you will set gas for smart contract transactions multiple times. If you don't have enough gas you will receive anINSUFFICIENT_GAS response. If you set the value too high you will be refunded a maximum of 20% of the amount that was set for the transaction.
1. Create an “NFT Creator” Smart Contract
You can find an NFTCreator Solidity contract sample below with the contract bytecode obtained by compiling the solidity contract using Remix IDE. If you are not familiar with Solidity, you can take a look at the docs here.
The following contract is composed of three functions:
createNftmintNfttransferNft
The important thing to know is that the NFT created in this example will have the contract itself as Treasury Account, Supply Key, and Auto-renew account. There’s NO admin key for the NFT or the contract.
Store your contract on Hedera using ContractCreateFlow(). This single call performs FileCreateTransaction(),FileAppendTransaction(), and ContractCreateTransaction() for you. See the difference here.
3. Execute the Contract to Create an NFT
The parameters you need to specify for this contract call are Name, Symbol, Memo, Maximum Supply, and Expiration. Setting an expiration date in seconds is required because entities on Hedera will need to pay a “rent” to persist. In this case, the contract entity will pay all NFT auto-renewal fees.
4. Execute the Contract to Mint a New NFT
After the token ID is created, you mint each NFT under that ID using the mintNft function. For the minting, you must specify the token ID as a Solidity address and the NFT metadata.
Both the NFT image and metadata live in the InterPlanetary File System (IPFS), which provides decentralized storage. The file metadata.json contains the metadata for the NFT. An IPFS URI pointing to the metadata file is used during minting of a new NFT. Notice that the metadata file contains a URI pointing to the NFT image.
5. Execute the Contract to Transfer the NFT
The NFT is minted to the contract address because the contract is the treasury for the token. Now transfer the NFT to another account or contract address. In this example, you will transfer the NFT to Alice. For the transfer, you must specify the token address and NFT serial number.
The transferNft function in the Solidity contract contains a call to an associateToken function that will automatically associate Alice to the token ID. This association transaction must be signed using Alice's private key. After signing, Alice will receive the NFT.
Code Check ✅
Last updated