tokenURI
로서 사용됨민팅의 정의: Minting refers to the process of turning a digital file into an NFT on the Ethereum blockchain. This NFT is stored on the decentralized database making it impossible to edit, modify, or delete.
Step 1. 변수 정의 (위에서 업데이트한 .env
파일의 변수를 process.env.API_URL 형식으로 활용)
Step 2. ABI 정의 / hardhat이 이미 json파일 형식으로 ABI 생성해둠 / ABI는 smart contract와 상호작용하기 위한 interface
Step 3. 민팅 함수 정의 // nonce와 transaction에 필요한 값들 설정
Step 4. Transaction 할당
Step 5. 이미지의 메타데이터 tokenURI
를 이용해 민팅 함수 콜 (The mintNFT
function requires a tokenURI
parameter that refers to a JSON document where the metadata (image, properties, name, description,…) is stored.)
//step 1: You define your variables from .env file
require('dotenv').config();
const API_URL = process.env.API_URL;
const PUBLIC_KEY = process.env.PUBLIC_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(API_URL);
//step 2: Define our contract ABI (Application Binary Interface) & adresses
const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json");
const contractAddress = "0x099D1751c33d75297c9f331a5Cd39275ff534f96";
const nftContract = new web3.eth.Contract(contract.abi, contractAddress);
//step 3: Define the minting function
async function mintNFT(tokenURI) {
const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, 'latest'); //get latest nonce
//the transaction
const tx = {
'from': PUBLIC_KEY,
'to': contractAddress,
'nonce': nonce,
'gas': 500000,
'maxPriorityFeePerGas': 1999999987,
'data': nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI()
};
//step 4: Sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY);
const transactionReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Transaction receipt: ${JSON.stringify(transactionReceipt)}`);
}
//step 5: Call the mintNFT function
mintNFT("https://gateway.pinata.cloud/ipfs/Qmeou5f7ttU98n96mYWfYzKHV7pfRe5rcZBhYznHZCUV7M");
node scripts/mint-nft.js
튜토리얼 참고 : https://docs.alchemy.com/alchemy/tutorials/how-to-create-an-nft