LogoLogo
  • Horizon Network
    • What is Horizon Network?
    • API Document
      • Horizon Network API
      • Quick Start
      • API Reference
        • Assets
        • Collections
        • Orders
        • Offers
        • Events
        • Statistics
        • Categories
        • Chains
        • Request an API key
    • SDKs Document
      • SDKs overview
      • NODE SDK
        • Installation
        • Initialization
        • Basic Example
          • Assets Management
          • Marketplace
        • Reference
          • Assets
          • Collections
          • Offers
          • Orders
          • Categories
          • Events
          • Statistics
          • Blockchain and Layer 2
      • JAVASCRIPT SDK
        • Overview
        • Installation
        • Quickstart
        • Reference
    • Launching NFT Collection
      • Launch A Collection
        • Overview
        • Part 1: Register as a user
        • Part 2: Deploy a smart contract
        • Part 3: Set creator earnings
        • Horizon Example Repository
      • Creating Your NFT Smart Contract
        • Overview
        • Getting Started
        • Setting a basic project
        • Minting from your new contract
        • Add your own NFT contract to Horizon Network
    • Horizon Network's Layer 2 Solution
      • Why Horizon Network?
      • Horizon Network's layer 2 Solutions
      • Basic Concepts
  • Horizon Land Metaverse
    • Feature
      • LAND in the metaverse
      • NFT Avatar
      • Horizon Center
      • Game Center
      • Shopping Center
      • Music Center
      • Play to Earn
      • Social Activities
      • Finance Center
      • Marketplace
      • Experience Mode
      • Zon Token
    • Stake Holder
      • Players
      • Creators
      • Land Owners
      • Partners
      • Investors
    • Economy
      • Revenue Stream
  • Technology
    • Blockchain
    • Product
    • System
    • Wallet
    • Security
    • Token
  • Tokenomic
  • Investor & Partner
  • Roadmap
  • Contact
  • Conclusion
  • Recommendation
  • FAQs
    • Policy
    • Terms of Use
Powered by GitBook
On this page
  1. Horizon Network
  2. Launching NFT Collection
  3. Creating Your NFT Smart Contract

Minting from your new contract

In this next part of the tutorial, we'll cover all the basics needed to mint an NFT to users wallets

Minting from new NFT contract

First, define some common helpers that we can reuse in later pieces of code. Create a new helpers.js file and add following code:

const { ethers } = require("ethers");
const { getContractAt } = require("@nomiclabs/hardhat-ethers/internal/helpers");


// Helper method for fetching environment variables from .env
function getEnvVariable(key, defaultValue) {
    if (process.env[key]) {
        return process.env[key];
    }
    if (!defaultValue) {
        throw `${key} is not defined and no default value was provided`;
    }
    return defaultValue;
}

// Helper method for fetching a connection provider to the Ethereum network
function getProvider() {
    return ethers.getDefaultProvider(getEnvVariable("NETWORK", "rinkeby"), {
        alchemy: getEnvVariable("ALCHEMY_KEY"),
    });
}

// Helper method for fetching a wallet account using an environment variable for the PK
function getAccount() {
    return new ethers.Wallet(getEnvVariable("ACCOUNT_PRIVATE_KEY"), getProvider());
}

// Helper method for fetching a contract instance at a given address
function getContract(contractName, hre) {
    const account = getAccount();
    return getContractAt(hre, contractName, getEnvVariable("NFT_CONTRACT_ADDRESS"), account);
}

module.exports = {
    getEnvVariable,
    getProvider,
    getAccount,
    getContract,
}

Open up the .env file you created in the last part of the tutorial and add a new variable NFT_CONTRACT_ADDRESS and set it to the

ALCHEMY_KEY = "alchemy-api-key"
ACCOUNT_PRIVATE_KEY = "private-key"
NETWORK="rinkeby"
NFT_CONTRACT_ADDRESS="0x61E815D04578B3E251d5Dca28b7CA99378dF66FE"

Add a new mint.js file to your scripts folder and write the task:

const { task } = require("hardhat/config");
const { getContract } = require("./helpers");

task("mint", "Mints from the NFT contract")
.addParam("address", "The address to receive a token")
.setAction(async function (taskArguments, hre) {
    const contract = await getContract("NFT", hre);
    const transactionResponse = await contract.mintTo(taskArguments.address, {
        gasLimit: 500_000,
    });
    console.log(`Transaction Hash: ${transactionResponse.hash}`);
});
/**
* @type import('hardhat/config').HardhatUserConfig
*/

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
require("./scripts/mint.js");

const { ALCHEMY_KEY, ACCOUNT_PRIVATE_KEY } = process.env;

module.exports = {
   solidity: "0.8.0",
   defaultNetwork: "rinkeby",
   networks: {
    hardhat: {},
    rinkeby: {
      url: `https://eth-rinkeby.alchemyapi.io/v2/${ALCHEMY_KEY}`,
      accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]
    },
    ethereum: {
      chainId: 1,
      url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}`,
      accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]
    },
  },
}

We are now ready to go! To mint tokens, call our new mint task:

npx hardhat mint --address 0x0dD768A47eA2395bbE5574E3252505fe1202C4A8
> npx hardhat mint --address 0xb9720BE63Ea8896956A06d2dEd491De125fD705E
========= NOTICE =========
Request-Rate Exceeded  (this message will not be repeated)

The default API keys for each service are provided as a highly-throttled,
community resource for low-traffic projects and early prototyping.

While your application will continue to function, we highly recommended
signing up for your own API keys to improve performance, increase your
request rate/limit and enable other perks, such as metrics and advanced APIs.

For more details: https://docs.ethers.io/api-keys/
==========================
Transaction Hash: 0x16ea832e486f115fb828dbec20e578a008675998256c807682d8cbdca17264d3

This transaction record shows us who called the contract function, what the outcome was (Tokens Transferred) and how much the transaction cost the caller in both gas fees and cost to transact.

PreviousSetting a basic projectNextAdd your own NFT contract to Horizon Network

Last updated 2 years ago

Hardhat allows users to in their projects that can execute common user actions using the Hardhat CLI directly. To do that, we will import the new mint.js file to our hardhat.config.js configuration so that it is picked up by Hardhat

If you take that transaction hash over to , you should see something like this:

create preconfigured tasks
Etherscan