Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Last active July 21, 2021 23:43
Show Gist options
  • Save ClayShentrup/28f373d9289f1ee0861116d667aa1ddd to your computer and use it in GitHub Desktop.
Save ClayShentrup/28f373d9289f1ee0861116d667aa1ddd to your computer and use it in GitHub Desktop.
import {ethers} from 'ethers';
const contractArtifact = require("../artifacts/contracts/MyNFT.sol/MyNFT.json");
export async function mintNft(
tokenURI: string,
env: NodeJS.ProcessEnv,
provider: ethers.providers.Provider,
contractAddress: string,
) {
const publicKey = env.PUBLIC_KEY!;
const privateKey = env.PRIVATE_KEY!;
const wallet = new ethers.Wallet(privateKey, provider);
await wallet.getBalance().then((gp) => console.log('gp', gp))
const nftContract = new ethers.Contract(contractAddress, contractArtifact.abi, wallet);
await nftContract.mintNFT(publicKey, tokenURI, { gasLimit: 500_000})
.then((result: any) => console.log(`result`, result))
}
import { mintNft } from "../lib/mint-nft"
import {ethers, waffle} from "hardhat";
import { Contract } from "@ethersproject/contracts"
describe("MyNFT", function () {
let signerAddress: string;
const tokenURI = "http://example.com";
let env: NodeJS.ProcessEnv;
let deployedContract: Contract;
beforeEach(async () => {
const MyNft = await ethers.getContractFactory("MyNFT");
const hardhatMyNft = await MyNft.deploy();
const [wallet] = waffle.provider.getWallets()
const connected = hardhatMyNft.connect(wallet)
deployedContract = await hardhatMyNft.deployed();
env = {
PUBLIC_KEY: wallet.address,
PRIVATE_KEY: wallet.privateKey
}
})
it("works", async function () {
mintNft(tokenURI, env, ethers.provider, deployedContract.address);
});
});
const provider = ethers.getDefaultProvider("ropsten", {
alchemy: '8-bL_8THTfHufs-0RcDbsDLyOoM1epiC', // This is fake; use yours, at the end of your Alchemy API URI.
});
// These are fake values for example. Set them in your actual environment.
process.env = {
PUBLIC_KEY: "0xFBDE00762b7157e766560547F749351659483A64",
PRIVATE_KEY: "0x2c204284da092c30b0b24c13b08ed8e096a09571f6030ccc81a5f93246be17c9",
}
const deployedContractAddress = "0x81c587EB0fE773404c66b5f5542c1d267C470eED";
mintNft("http://example.com/your-endpoint", process.env, provider, deployedContractAddress);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment