|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { isAddress, Signer, ZeroAddress } from "ethers"; |
| 3 | +import { task, types } from "hardhat/config"; |
| 4 | +import * as z from "zod"; |
| 5 | + |
| 6 | +import fs from "fs"; |
| 7 | + |
| 8 | +import type { IDistributeArgs } from "../helpers/constants"; |
| 9 | + |
| 10 | +const addressSchema = z.string().refine((value) => isAddress(value) && value !== ZeroAddress, { |
| 11 | + message: "Invalid Address", |
| 12 | +}); |
| 13 | + |
| 14 | +const projectsSchema = z.array( |
| 15 | + z.object({ |
| 16 | + recipientIndex: z.coerce.number(), |
| 17 | + payoutAddress: addressSchema, |
| 18 | + }), |
| 19 | +); |
| 20 | + |
| 21 | +const amountsSchema = z.array(z.coerce.bigint()); |
| 22 | + |
| 23 | +// erc20,0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,receiver,amount |
| 24 | + |
| 25 | +/** |
| 26 | + * Distribute unallocated amounts among the specified projects |
| 27 | + */ |
| 28 | +task("distribute", "Command to distribute unallocated amounts among the specified projects") |
| 29 | + .addParam("projectsFile", "The file with projects", undefined, types.string) |
| 30 | + .addParam("amountsFile", "The file with amounts in wei to transfer", undefined, types.string) |
| 31 | + .addOptionalParam("tokenAddress", "ERC-20 token address", "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", types.string) |
| 32 | + .addOptionalParam("startIndex", "The index of the recipient to start sending transactions from", 0, types.int) |
| 33 | + .addFlag("dryRun", "Run distribute command on fork and impersonate WETH account") |
| 34 | + .setAction(async ({ projectsFile, amountsFile, tokenAddress, startIndex, dryRun }: IDistributeArgs, hre) => { |
| 35 | + const address = addressSchema.parse(tokenAddress); |
| 36 | + |
| 37 | + const projects = await fs.promises |
| 38 | + .readFile(projectsFile, "utf8") |
| 39 | + .then((result) => projectsSchema.parse(JSON.parse(result))); |
| 40 | + |
| 41 | + const amounts = await fs.promises |
| 42 | + .readFile(amountsFile, "utf8") |
| 43 | + .then((result) => amountsSchema.parse(JSON.parse(result))); |
| 44 | + |
| 45 | + if (projects.length !== amounts.length) { |
| 46 | + throw new Error("Projects and amounts arrays must be the same length"); |
| 47 | + } |
| 48 | + |
| 49 | + const data = amounts.map((amount, index) => ({ ...projects[index], amount })); |
| 50 | + |
| 51 | + let signer: Signer; |
| 52 | + |
| 53 | + if (dryRun) { |
| 54 | + await hre.network.provider.request({ |
| 55 | + method: "hardhat_impersonateAccount", |
| 56 | + params: [tokenAddress], |
| 57 | + }); |
| 58 | + |
| 59 | + signer = await hre.ethers.getSigner(tokenAddress); |
| 60 | + } else { |
| 61 | + [signer] = await hre.ethers.getSigners(); |
| 62 | + } |
| 63 | + |
| 64 | + console.log(`Using signer: ${await signer.getAddress()}`); |
| 65 | + |
| 66 | + const contract = await hre.ethers.getContractAt("IERC20", address, signer); |
| 67 | + const startSignerBalance = await contract.balanceOf(signer); |
| 68 | + const startBalance = await signer.provider!.getBalance(signer); |
| 69 | + const totalAmount = data.reduce((acc, x) => acc + x.amount, 0n); |
| 70 | + |
| 71 | + console.log(`Total allocated: ${totalAmount.toString()} wei`); |
| 72 | + console.log(`Signer balance: ${startBalance.toString()} wei`); |
| 73 | + console.log(`Signer token balance: ${startSignerBalance.toString()} wei`); |
| 74 | + |
| 75 | + if (startSignerBalance <= totalAmount) { |
| 76 | + throw new Error("Not enough balance"); |
| 77 | + } |
| 78 | + |
| 79 | + // eslint-disable-next-line @typescript-eslint/prefer-for-of |
| 80 | + for (let index = startIndex; index < data.length; index += 1) { |
| 81 | + const { payoutAddress, amount } = data[index]; |
| 82 | + |
| 83 | + console.log(`Recipeint ${index}: transferring ${amount} wei to ${payoutAddress}`); |
| 84 | + |
| 85 | + // eslint-disable-next-line no-await-in-loop |
| 86 | + const receipt = await contract.transfer(payoutAddress, amount).then((tx) => tx.wait()); |
| 87 | + |
| 88 | + console.log(`Sent ${amount} wei to ${payoutAddress} (recipient index ${index}) (tx: ${receipt?.hash})\n`); |
| 89 | + } |
| 90 | + |
| 91 | + const endSignerBalance = await contract.balanceOf(signer); |
| 92 | + const endBalance = await signer.provider!.getBalance(signer); |
| 93 | + |
| 94 | + const totalTransferred = startSignerBalance - endSignerBalance; |
| 95 | + |
| 96 | + if (totalAmount !== totalTransferred) { |
| 97 | + console.warn( |
| 98 | + `Transferred amount (${totalTransferred} wei) doesn't match with total allocated amount (${totalAmount} wei)`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + console.log(`Total transferred: ${totalTransferred.toString()} wei`); |
| 103 | + console.log(`Signer balance: ${endBalance.toString()} wei`); |
| 104 | + console.log(`Signer token balance: ${endSignerBalance.toString()} wei`); |
| 105 | + console.log(`Total transaction cost: ${(startBalance - endBalance).toString()} wei`); |
| 106 | + }); |
0 commit comments