deploy.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This is a script for deploying your contracts. You can adapt it to deploy
  2. // yours, or create new ones.
  3. const path = require("path");
  4. async function main() {
  5. // This is just a convenience check
  6. if (network.name === "hardhat") {
  7. console.warn(
  8. "You are trying to deploy a contract to the Hardhat Network, which" +
  9. "gets automatically created and destroyed every time. Use the Hardhat" +
  10. " option '--network localhost'"
  11. );
  12. }
  13. // ethers is available in the global scope
  14. const [deployer] = await ethers.getSigners();
  15. console.log(
  16. "Deploying the contracts with the account:",
  17. await deployer.getAddress()
  18. );
  19. console.log("Account balance:", (await deployer.getBalance()).toString());
  20. const Token = await ethers.getContractFactory("Token");
  21. const token = await Token.deploy();
  22. await token.deployed();
  23. console.log("Token address:", token.address);
  24. // We also save the contract's artifacts and address in the frontend directory
  25. saveFrontendFiles(token);
  26. }
  27. function saveFrontendFiles(token) {
  28. const fs = require("fs");
  29. const contractsDir = path.join(__dirname, "..", "frontend", "src", "contracts");
  30. if (!fs.existsSync(contractsDir)) {
  31. fs.mkdirSync(contractsDir);
  32. }
  33. fs.writeFileSync(
  34. path.join(contractsDir, "contract-address.json"),
  35. JSON.stringify({ Token: token.address }, undefined, 2)
  36. );
  37. const TokenArtifact = artifacts.readArtifactSync("Token");
  38. fs.writeFileSync(
  39. path.join(contractsDir, "Token.json"),
  40. JSON.stringify(TokenArtifact, null, 2)
  41. );
  42. }
  43. main()
  44. .then(() => process.exit(0))
  45. .catch((error) => {
  46. console.error(error);
  47. process.exit(1);
  48. });