Skip to content

Commit

Permalink
Share constants for filenames in deploy and faucet scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyBernier committed Nov 9, 2022
1 parent 13bd712 commit 897cfc1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Then, on a new terminal, go to the repository's root folder and run this to
deploy your contract:

```sh
npx hardhat run scripts/deploy.js --network localhost
npx hardhat run scripts/deploy.ts --network localhost
```

Finally, we can run the frontend with:
Expand Down
20 changes: 12 additions & 8 deletions scripts/deploy.js → scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// This is a script for deploying your contracts. You can adapt it to deploy
// yours, or create new ones.

const path = require("path");
import fs from "fs";
import path from "path";

const {
CONTRACTS_DIRECTORY,
CONTRACT_OUTPUT_FILENAME,
TOKEN_JSON_FILE,
} = require("../constants");

async function main() {
// This is just a convenience check
Expand Down Expand Up @@ -33,22 +40,19 @@ async function main() {
}

function saveFrontendFiles(token) {
const fs = require("fs");
const contractsDir = path.join(__dirname, "..", "frontend", "src", "contracts");

if (!fs.existsSync(contractsDir)) {
fs.mkdirSync(contractsDir);
if (!fs.existsSync(CONTRACTS_DIRECTORY)) {
fs.mkdirSync(CONTRACTS_DIRECTORY);
}

fs.writeFileSync(
path.join(contractsDir, "contract-address.json"),
path.join(CONTRACTS_DIRECTORY, CONTRACT_OUTPUT_FILENAME),
JSON.stringify({ Token: token.address }, undefined, 2)
);

const TokenArtifact = artifacts.readArtifactSync("Token");

fs.writeFileSync(
path.join(contractsDir, "Token.json"),
path.join(CONTRACTS_DIRECTORY, TOKEN_JSON_FILE),
JSON.stringify(TokenArtifact, null, 2)
);
}
Expand Down
13 changes: 9 additions & 4 deletions tasks/faucet.js → tasks/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const fs = require("fs");
import fs from "fs";
import path from "path";

const {
CONTRACTS_DIRECTORY,
CONTRACT_OUTPUT_FILENAME,
} = require("../constants");

const addressesFile = path.join(CONTRACTS_DIRECTORY, CONTRACT_OUTPUT_FILENAME);

// This file is only here to make interacting with the Dapp easier,
// feel free to ignore it if you don't need it.
Expand All @@ -14,9 +22,6 @@ task("faucet", "Sends ETH and tokens to an address")
);
}

const addressesFile =
__dirname + "/../frontend/src/contracts/contract-address.json";

if (!fs.existsSync(addressesFile)) {
console.error("You need to deploy your contract first");
return;
Expand Down

0 comments on commit 897cfc1

Please sign in to comment.