Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 4 #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.env
artifacts
cache
35 changes: 19 additions & 16 deletions contracts/MyNFT.sol
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
// Contract based on https://docs.openzeppelin.com/contracts/4.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma solidity ^0.8.20; // Using latest stable Solidity version

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyNFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
contract MyNFT is ERC721URIStorage, Ownable {
uint256 private _nextTokenId;

constructor() ERC721("MyNFT", "MNFT") {}
constructor()
ERC721("MyNFT", "MNFT")
Ownable(msg.sender) // Explicitly set the owner
{}

function mintNFT(address recipient, string memory tokenURI)
public
returns (uint256)
public
returns (uint256)
{
_tokenIds.increment();

uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
uint256 tokenId = _nextTokenId++;
_safeMint(recipient, tokenId); // Using safeMint instead of _mint
_setTokenURI(tokenId, tokenURI);

return tokenId;
}

return newItemId;
function getNextTokenId() public view returns (uint256) {
return _nextTokenId;
}
}
}
44 changes: 30 additions & 14 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import("@nomiclabs/hardhat-ethers");
import("@nomiclabs/hardhat-waffle");
import dotenv from "dotenv";

const argv = JSON.parse(env("npm_config_argv"));
if (argv.original !== ["hardhat", "test"]) {
require('dotenv').config();
}
import { HardhatUserConfig } from "hardhat/config";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";
import * as dotenv from "dotenv";

import("./tasks/nft");
// Load environment variables
dotenv.config();

import { HardhatUserConfig } from "hardhat/config";
// Import tasks
import "./tasks/nft";

const config: HardhatUserConfig = {
solidity: "0.8.0",
solidity: {
version: "0.8.20",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
networks: {
hardhat: {},
sepolia: {
url: `https://eth-sepolia.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts: [process.env.ETH_PRIVATE_KEY],
},
accounts: process.env.ETH_PRIVATE_KEY ? [process.env.ETH_PRIVATE_KEY] : [],
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 40000
}
};

export default config;
export default config;
21 changes: 10 additions & 11 deletions lib/contract.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Contract, ethers } from "ethers";
import { getContractAt } from "@nomiclabs/hardhat-ethers/internal/helpers";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { env } from "./env";
import { getProvider } from "./provider";
import { ethers } from 'ethers';
import { getWallet } from './wallet';
import * as MyNFT from '../artifacts/contracts/MyNFT.sol/MyNFT.json';

export function getContract(
name: string,
hre: HardhatRuntimeEnvironment
): Promise<Contract> {
const WALLET = new ethers.Wallet(env("ETH_PRIVATE_KEY"), getProvider());
return getContractAt(hre, name, env("NFT_CONTRACT_ADDRESS"), WALLET);
export async function getContract(contractAddress: string): Promise<ethers.Contract> {
const wallet = getWallet();
return new ethers.Contract(
contractAddress,
MyNFT.abi,
wallet
);
}
11 changes: 7 additions & 4 deletions lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export function env(key: string): string {
import * as dotenv from 'dotenv';
dotenv.config();

export function getEnvVar(key: string): string {
const value = process.env[key];
if (value === undefined) {
throw `${key} is undefined`;
if (!value) {
throw new Error(`Environment variable ${key} is not set`);
}
return value;
}
}
12 changes: 7 additions & 5 deletions lib/provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ethers } from "ethers";
import { ethers } from 'ethers';
import { getEnvVar } from './env';

export function getProvider(): ethers.providers.Provider {
return ethers.getDefaultProvider("ropsten", {
alchemy: process.env.ALCHEMY_API_KEY,
});
export function getProvider(): ethers.providers.AlchemyProvider {
return new ethers.providers.AlchemyProvider(
'sepolia',
getEnvVar('ALCHEMY_API_KEY')
);
}
11 changes: 7 additions & 4 deletions lib/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ethers } from "ethers";
import { env } from "./env";
import { getProvider } from "./provider";
import { ethers } from 'ethers';
import { getEnvVar } from './env';
import { getProvider } from './provider';

export function getWallet(): ethers.Wallet {
return new ethers.Wallet(env("ETH_PRIVATE_KEY"), getProvider());
return new ethers.Wallet(
getEnvVar('ETH_PRIVATE_KEY'),
getProvider()
);
}
Loading