24
loading...
This website collects cookies to deliver better user experience
cd
into the directory we wish to work with and then run the following commands:mkdir mini-buymeacoffee-be
cd mini-buymeacoffee-be
npm init -y
npm install --save-dev hardhat
npx hardhat
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
npx hardhat test
sample-test.js
from the test folder. Delete sample-script.js
from the scripts directory as well. After that, go to contracts and delete Greeter.sol
. The folders themselves should not be deleted!
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract CoffeePortal {
constructor() payable {
console.log("Yo! Smart Contract");
}
}
scripts
folder, create a new file called run.js,
and update it with the following code snippet:const main = async () => {
// This will actually compile our contract and generate the necessary files we need to work with our contract under the artifacts directory.
const coffeeContractFactory = await hre.ethers.getContractFactory('CoffeePortal');
const coffeeContract = await coffeeContractFactory.deploy();
await coffeeContract.deployed(); // We'll wait until our contract is officially deployed to our local blockchain! Our constructor runs when we actually deploy.
console.log("Coffee Contract deployed to:", coffeeContract.address);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
};
runMain();
npx hardhat run scripts/run.js
scripts
folder, create a file called deploy.js
. Here's the code for it. It looks super similar to run.js.const main = async () => {
const [deployer] = await hre.ethers.getSigners();
const accountBalance = await deployer.getBalance();
console.log("Deploying contracts with account: ", deployer.address);
console.log("Account balance: ", accountBalance.toString());
const Token = await hre.ethers.getContractFactory("CoffeePortal");
const portal = await Token.deploy();
await portal.deployed();
console.log("CoffeePortal address: ", portal.address);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};
runMain();
npx hardhat run scripts/deploy.js --network localhost
contracts/CoffeePortal.sol
scripts/run.js
scripts/deploy.js
Name | Link | Amount | Time |
---|---|---|---|
MyCrypto | https://app.mycrypto.com/faucet | 0.01 | None |
Buildspace | https://buildspace-faucet.vercel.app/ | 0.025 | 1d |
Ethily | https://ethily.io/rinkeby-faucet/ | 0.2 | 1w |
Official Rinkeby | https://faucet.rinkeby.io/ | 3 / 7.5 / 18.75 | 8h / 1d / 3d |
require("@nomiclabs/hardhat-waffle");
require("dotenv").config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.8.4",
networks: {
rinkeby: {
url: process.env.STAGING_ALCHEMY_KEY,
accounts: [process.env.PRIVATE_KEY],
},
},
};
.env
file, as well as the import at the top of require("dotenv").config()
, which implies we'll need to install the dotenv package and also create a .env
file using the command below:npm install -D dotenv
touch .env
STAGING_ALCHEMY_KEY= // Add the key we copied from the Alchemy dashboard here
PRIVATE_KEY= // Add your account private key here
npx hardhat run scripts/deploy.js --network rinkeby
npx create-next-app -e with-tailwindcss
command to scaffold a new project in a directory of our choice.cd <project name>
npm install ethers react-toastify
npm run dev
http://localhost:3000
pages
.utils
, then using the following command to create a file called CoffeePortal.json
inside the utils
folder.mkdir utils
touch CoffeePortal.json
CoffeePortal.json
file. What's the best way to get it? artifacts/contracts/coffeePortal.json
and copy the entire content inside it, as well as our contract address, which was displayed in our terminal when we deployed our contract to the blockchain.CoffeePortal.json
file with what we copied and also update index.js as shown below:// ...
import Head from "next/head";
// Import abi
import abi from "../utils/CoffeePortal.json";
export default function Home() {
/**
* Create a variable here that holds the contract address after you deploy!
*/
const contractAddress = ""; // Add contract address here
// ...
return (
<div className="flex flex-col items-center justify-center min-h-screen py-2">
[...]
</div>
);
}
http://localhost:3000