Skip to content

Instantly share code, notes, and snippets.

@fend25
Last active July 13, 2023 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fend25/f5b7681caaf1a8a0fb6abee204bfbc82 to your computer and use it in GitHub Desktop.
Save fend25/f5b7681caaf1a8a0fb6abee204bfbc82 to your computer and use it in GitHub Desktop.
A minimal demonstration of creating an empty collection and minting an empty token on Unique using hardhat.
import { HardhatUserConfig, task } from "hardhat/config"
import "@typechain/hardhat"
import * as dotenv from "dotenv"
dotenv.config()
const config: HardhatUserConfig = {
solidity: "0.8.21",
defaultNetwork: "localhost",
networks: {
localhost: { url: "http://localhost:8545", },
opal: {
url: "https://rpc-opal.unique.network",
accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
},
},
}
export default config
import {ethers} from 'hardhat'
import {CollectionHelpersFactory, UniqueNFTFactory, parseEthersV5TxReceipt} from '@unique-nft/solidity-interfaces'
import {Address} from '@unique-nft/utils'
async function main() {
// network is already provided by hardhat
const provider = ethers.provider
console.log(`Network is:`, await provider.getNetwork())
//////////////////////////////////////////////////////
// initialize wallet and check that balance is enough
//////////////////////////////////////////////////////
const wallet = (await ethers.getSigners())[0]
if (!wallet) {
throw new Error(`wallet is not initialized, please set it up in hardhat.config.ts`)
}
const balance = parseFloat(ethers.utils.formatEther(await wallet.getBalance()))
console.log(`Wallet address is ${wallet.address}, balance is ${balance.toFixed(2)} OPL/QTZ/UNQ`)
if (balance < 3) {
throw new Error(`balance should be at least 3 OPL/QTZ/UNQ`)
}
const collectionHelpers = await CollectionHelpersFactory(wallet, ethers)
const gasLimit= 1000000
const gasPrice = await provider.getGasPrice()
//////////////////////////////////////////////////////
// create collection
//////////////////////////////////////////////////////
const collectionCreationTx = await (await collectionHelpers.createNFTCollection(
'My first collection',
'My first collection description',
'MY',
{
value: await collectionHelpers.collectionCreationFee(),
gasLimit,
gasPrice,
}
)).wait()
// yes, CollectionCreated.collectionId, but actually it returns address, not collection id
const collectionAddress = parseEthersV5TxReceipt(collectionCreationTx).events.CollectionCreated.collectionId
const collectionId = Address.collection.addressToId(collectionAddress)
console.log(`Created collection ${collectionId}, address is ${collectionAddress}`)
//////////////////////////////////////////////////////
// mint token
//////////////////////////////////////////////////////
const collection = await UniqueNFTFactory(collectionAddress, wallet, ethers)
const mintTx = await (await collection.mint(
wallet.address,
{
gasLimit,
gasPrice,
}
)).wait()
const tokenId = parseEthersV5TxReceipt(mintTx).events.Transfer.tokenId
console.log(`Minted token #${tokenId} (${collectionId}/${tokenId}) to ${wallet.address}`)
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment