Skip to content

Commit

Permalink
deploy smart contract to local chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Smith committed Dec 13, 2024
1 parent 472bdb3 commit 212a73c
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 22 deletions.
135 changes: 135 additions & 0 deletions contract/eip721/contract.json

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions contract/eip721/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { ethers } = require("ethers");
const fs = require('fs');
const path = require('path');

const privateKey = '0x0000000000000000000000000000000000000000000000000000000000000001';
const keyOfContractAddress = 'NEXT_PUBLIC_CONTRACT_ADDRESS';

async function main(): Promise<void> {
const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545");
const signer = new ethers.Wallet(privateKey, provider);

const artifactPath = path.resolve(__dirname, "./contract.json");
const artifact = JSON.parse(fs.readFileSync(artifactPath, "utf-8"));
const { abi, bytecode } = artifact;

const factory = new ethers.ContractFactory(abi, bytecode, signer);
const contract = await factory.deploy();

await contract.waitForDeployment();

const contractAddress = await contract.getAddress();
console.log("Contract deployed at:", contractAddress);

const envFilePath = path.resolve(__dirname, "../../.env.local");
let envContent = "";
if (fs.existsSync(envFilePath)) {
envContent = fs.readFileSync(envFilePath, "utf-8");
}

const updatedContent = envContent
.split("\n")
.filter((line) => !line.startsWith(keyOfContractAddress))
.join("\n");

const finalEnvContent = updatedContent.trim() + `\n${keyOfContractAddress}=${contractAddress}\n`;

fs.writeFileSync(envFilePath, finalEnvContent, "utf-8");
console.log(`Updated .env.local with ${keyOfContractAddress}:`, contractAddress);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"lint": "next lint",
"locks:remove": "rm -f yarn.lock",
"locks:create": "generate-lockfile --lockfile ../../yarn.lock --package package.json --write yarn.lock --force",
"locks": "npm run locks:remove && npm run locks:create"
"locks": "npm run locks:remove && npm run locks:create",
"run-ganache": "bash scripts/run-ganache.sh",
"deploy-contract": "ts-node contract/eip721/deploy.ts"
},
"resolutions": {
"react": "18.2.0",
Expand Down Expand Up @@ -49,7 +51,7 @@
"devDependencies": {
"@tanstack/react-query-devtools": "4.29.1",
"@types/long": "4.0.0",
"@types/node": "^20.14.6",
"@types/node": "^22.10.1",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint": "8.28.0",
Expand Down
1 change: 0 additions & 1 deletion pages/ethers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function Index() {
}
const provider = new ethers.BrowserProvider(ethereum)
const wallet = await provider.getSigner()
// wallet.signTypedData()
const tx = await wallet.sendTransaction({
to: addr0,
value: '1'
Expand Down
4 changes: 2 additions & 2 deletions pages/ethers/private-key.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useEffect, useState } from "react";
import abi from './abi.json'

export default function Index() {
const verifyingContract = '0xf67a42D581eB7d83135De8Dfe2fCccE58e5259bc'
const verifyingContract = process.env.NEXT_PUBLIC_CONTRACT_ADDRESS! // '0xf67a42D581eB7d83135De8Dfe2fCccE58e5259bc' is on bsc testnet
const addr0 = '0x0000000000000000000000000000000000000000'
const privateKey = process.env.NEXT_PUBLIC_PRIVATE_KEY
const privateKey = process.env.NEXT_PUBLIC_PRIVATE_KEY_DEV
const prc = process.env.NEXT_PUBLIC_BSC_TESTNET_RPC

const [balance, setBalance] = useState('--')
Expand Down
5 changes: 5 additions & 0 deletions scripts/run-ganache.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

npx ganache \
--account="0x0000000000000000000000000000000000000000000000000000000000000001,1000000000000000000" \
--account="0x0000000000000000000000000000000000000000000000000000000000000002,1000000000000000000"
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@/*": ["*"]
},
"typeRoots": ["node_modules/@types"],
"types": ["long"]
"types": ["long", "node"]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
23 changes: 7 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3883,7 +3883,7 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.34.tgz#10964ba0dee6ac4cd462e2795b6bebd407303433"
integrity sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==

"@types/node@*", "@types/node@>=13.7.0":
"@types/node@*", "@types/node@>=13.7.0", "@types/node@^22.10.1":
version "22.10.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.1.tgz#41ffeee127b8975a05f8c4f83fb89bcb2987d766"
integrity sha512-qKgsUwfHZV2WCWLAnVP1JqnpE6Im6h3Y0+fYgMTasNQ7V++CBX5OT1as0g0f+OyubbFqhf6XVNIsmN4IIhEgGQ==
Expand All @@ -3902,13 +3902,6 @@
dependencies:
undici-types "~6.19.2"

"@types/node@^20.14.6":
version "20.17.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.17.9.tgz#5f141d4b7ee125cdee5faefe28de095398865bab"
integrity sha512-0JOXkRyLanfGPE2QRCwgxhzlBAvaRdCNMcvbd7jFfpmD4eEXll7LRwy5ymJmyeZqk7Nh7eD2LeUyQ68BbndmXw==
dependencies:
undici-types "~6.19.2"

"@types/[email protected]":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@types/parse-package-name/-/parse-package-name-0.1.0.tgz#a4e54e3eef677d8b9d931b54b94ed77e8ae52a4f"
Expand All @@ -3932,11 +3925,9 @@
"@types/react" "*"

"@types/react-dom@^18.3.0":
version "18.3.1"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.3.1.tgz#1e4654c08a9cdcfb6594c780ac59b55aad42fe07"
integrity sha512-qW1Mfv8taImTthu4KoXgDfLuk4bydU6Q/TkADnDWWHwi4NX4BR+LWfTp2sVmTqRrsHvyDDTelgelxJ+SsejKKQ==
dependencies:
"@types/react" "*"
version "18.3.5"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.3.5.tgz#45f9f87398c5dcea085b715c58ddcf1faf65f716"
integrity sha512-P4t6saawp+b/dFrUr2cvkVsfvPguwsxtH6dNIYRllMsefqFzkZk5UIjzyDOv5g1dXIPdG4Sp1yCR4Z6RCUsG/Q==

"@types/react@*", "@types/[email protected]":
version "18.0.25"
Expand All @@ -3948,9 +3939,9 @@
csstype "^3.0.2"

"@types/react@^18.3.3":
version "18.3.12"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.12.tgz#99419f182ccd69151813b7ee24b792fe08774f60"
integrity sha512-D2wOSq/d6Agt28q7rSI3jhU7G6aiuzljDGZ2hTZHIkrTLUI+AF3WMeKkEZ9nN2fkBAlcktT6vcZjDFiIhMYEQw==
version "18.3.16"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.16.tgz#5326789125fac98b718d586ad157442ceb44ff28"
integrity sha512-oh8AMIC4Y2ciKufU8hnKgs+ufgbA/dhPTACaZPM86AbwX9QwnFtSoPWEeRUj8fge+v6kFt78BXcDhAU1SrrAsw==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"
Expand Down

0 comments on commit 212a73c

Please sign in to comment.