faucet.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const fs = require("fs");
  2. // This file is only here to make interacting with the Dapp easier,
  3. // feel free to ignore it if you don't need it.
  4. task("faucet", "Sends ETH and tokens to an address")
  5. .addPositionalParam("receiver", "The address that will receive them")
  6. .setAction(async ({ receiver }, { ethers }) => {
  7. if (network.name === "hardhat") {
  8. console.warn(
  9. "You are running the faucet task with Hardhat network, which" +
  10. "gets automatically created and destroyed every time. Use the Hardhat" +
  11. " option '--network localhost'"
  12. );
  13. }
  14. const addressesFile =
  15. __dirname + "/../frontend/src/contracts/contract-address.json";
  16. if (!fs.existsSync(addressesFile)) {
  17. console.error("You need to deploy your contract first");
  18. return;
  19. }
  20. const addressJson = fs.readFileSync(addressesFile);
  21. const address = JSON.parse(addressJson);
  22. if ((await ethers.provider.getCode(address.Token)) === "0x") {
  23. console.error("You need to deploy your contract first");
  24. return;
  25. }
  26. const token = await ethers.getContractAt("Token", address.Token);
  27. const [sender] = await ethers.getSigners();
  28. const tx = await token.transfer(receiver, 100);
  29. await tx.wait();
  30. const tx2 = await sender.sendTransaction({
  31. to: receiver,
  32. value: ethers.constants.WeiPerEther,
  33. });
  34. await tx2.wait();
  35. console.log(`Transferred 1 ETH and 100 tokens to ${receiver}`);
  36. });