-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deploy smart contract to local chain
- Loading branch information
Eason Smith
committed
Dec 13, 2024
1 parent
472bdb3
commit 212a73c
Showing
8 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|