lock.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { task } from "hardhat/config";
  2. import type { TaskArguments } from "hardhat/types";
  3. function distance(past: number, future: number): string {
  4. // get total seconds between the times
  5. let delta = future - past;
  6. // calculate (and subtract) whole days
  7. const days = Math.floor(delta / 86400);
  8. delta -= days * 86400;
  9. // calculate (and subtract) whole hours
  10. const hours = Math.floor(delta / 3600) % 24;
  11. delta -= hours * 3600;
  12. // calculate (and subtract) whole minutes
  13. const minutes = Math.floor(delta / 60) % 60;
  14. delta -= minutes * 60;
  15. // what's left is seconds
  16. const seconds = delta % 60; // in theory the modulus is not required
  17. return `${days} day(s), ${hours} hour(s), ${minutes} minute(s) and ${seconds} second(s)`;
  18. }
  19. task("task:withdraw", "Calls the withdraw function of Lock Contract")
  20. .addOptionalParam("address", "Optionally specify the Lock address to withdraw")
  21. .addParam("account", "Specify which account [0, 9]")
  22. .setAction(async function (taskArguments: TaskArguments, hre) {
  23. const { ethers, deployments } = hre;
  24. const Lock = taskArguments.address ? { address: taskArguments.address } : await deployments.get("Lock");
  25. const signers = await ethers.getSigners();
  26. console.log(taskArguments.address);
  27. const lock = await ethers.getContractAt("Lock", Lock.address);
  28. const initialBalance = await ethers.provider.getBalance(Lock.address);
  29. await lock.connect(signers[taskArguments.account]).withdraw();
  30. const finalBalance = await ethers.provider.getBalance(Lock.address);
  31. console.log("Contract balance before withdraw", ethers.formatEther(initialBalance));
  32. console.log("Contract balance after withdraw", ethers.formatEther(finalBalance));
  33. console.log("Lock Withdraw Success");
  34. });
  35. task("task:deployLock", "Deploys Lock Contract")
  36. .addParam("unlock", "When to unlock funds in seconds (i.e currentTime + --unlock seconds)")
  37. .addParam("value", "How much ether you intend locking (in ether not wei i.e 0.1)")
  38. .setAction(async function (taskArguments: TaskArguments, { ethers }) {
  39. const NOW_IN_SECONDS = Math.round(Date.now() / 1000);
  40. const signers = await ethers.getSigners();
  41. const lockedAmount = ethers.parseEther(taskArguments.value);
  42. const unlockTime = NOW_IN_SECONDS + parseInt(taskArguments.unlock);
  43. const lockFactory = await ethers.getContractFactory("Lock");
  44. console.log(`Deploying Lock and locking ${taskArguments.value} ETH for ${distance(NOW_IN_SECONDS, unlockTime)}`);
  45. const lock = await lockFactory.connect(signers[0]).deploy(unlockTime, { value: lockedAmount });
  46. await lock.waitForDeployment();
  47. console.log("Lock deployed to: ", await lock.getAddress());
  48. });