|
| 1 | +const hre = require("hardhat"); |
| 2 | +const fs = require('fs'); |
| 3 | +const chalk = require('chalk'); |
| 4 | + |
| 5 | +// Constants |
| 6 | +const seshAddress = "0x6C9D6d6FB69927e3CED37374d820121c7c5b77e1"; |
| 7 | +// Load this jsonFilePath based on what is output when running deploy-investor-vesting.js script. Its default |
| 8 | +// output path is: `deployments/vesting-${networkName}-${Date.now()}.json` |
| 9 | +const jsonFilePath = "./deployments/vesting-sepolia-1747608242040.json"; |
| 10 | + |
| 11 | +async function main() { |
| 12 | + const [deployer] = await hre.ethers.getSigners(); |
| 13 | + console.log("Transferring tokens with account:", chalk.yellow(deployer.address)); |
| 14 | + |
| 15 | + const networkName = hre.network.name; |
| 16 | + console.log("Network:", chalk.cyan(networkName)); |
| 17 | + console.log("SESH token address:", chalk.yellow(seshAddress)); |
| 18 | + console.log("JSON file:", chalk.yellow(jsonFilePath)); |
| 19 | + |
| 20 | + if (!fs.existsSync(jsonFilePath)) { |
| 21 | + console.error(chalk.red(`Error: JSON file not found at ${jsonFilePath}`)); |
| 22 | + process.exit(1); |
| 23 | + } |
| 24 | + |
| 25 | + if (!hre.ethers.isAddress(seshAddress)) { |
| 26 | + console.error(chalk.red(`Error: Invalid SESH token address: ${seshAddress}`)); |
| 27 | + process.exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + const fileContent = fs.readFileSync(jsonFilePath, 'utf8'); |
| 31 | + let deployments; |
| 32 | + |
| 33 | + try { |
| 34 | + deployments = JSON.parse(fileContent); |
| 35 | + } catch (error) { |
| 36 | + console.error(chalk.red(`Error parsing JSON file: ${error.message}`)); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + |
| 40 | + if (!deployments || !Array.isArray(deployments['contracts']) || deployments['contracts'].length === 0) { |
| 41 | + console.error(chalk.red("Error: JSON file is empty or invalid")); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + const seshContract = await hre.ethers.getContractAt("SESH", seshAddress); |
| 46 | + |
| 47 | + const deployerBalance = await seshContract.balanceOf(deployer.address); |
| 48 | + const totalRequired = deployments['contracts'].reduce((sum, deployment) => { |
| 49 | + return sum + hre.ethers.parseUnits(deployment.amount, 9); // Assuming 9 decimals for SESH |
| 50 | + }, 0n); |
| 51 | + |
| 52 | + console.log("Your balance:", chalk.yellow(hre.ethers.formatUnits(deployerBalance, 9)), "SESH"); |
| 53 | + console.log("Total required:", chalk.yellow(hre.ethers.formatUnits(totalRequired, 9)), "SESH"); |
| 54 | + |
| 55 | + if (deployerBalance < totalRequired) { |
| 56 | + console.error(chalk.red("Error: Insufficient SESH balance for transfers")); |
| 57 | + console.error(`You have ${hre.ethers.formatUnits(deployerBalance, 9)} SESH, but need ${hre.ethers.formatUnits(totalRequired, 9)} SESH`); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(chalk.yellow("Starting transfers...\n")); |
| 62 | + |
| 63 | + let successful = 0; |
| 64 | + let failed = 0; |
| 65 | + |
| 66 | + for (const deployment of deployments['contracts']) { |
| 67 | + try { |
| 68 | + if (!hre.ethers.isAddress(deployment['vestingAddress'])) { |
| 69 | + throw new Error(`Invalid vesting contract address: ${deployment['vestingAddress']}`); |
| 70 | + } |
| 71 | + |
| 72 | + const amount = hre.ethers.parseUnits(deployment['amount'], 9); // Assuming 9 decimals for SESH |
| 73 | + console.log(chalk.cyan(`Transferring ${deployment['amount']} SESH to ${deployment['vestingAddress']}...`)); |
| 74 | + |
| 75 | + const transferTx = await seshContract.transfer(deployment['vestingAddress'], amount); |
| 76 | + await transferTx.wait(); |
| 77 | + |
| 78 | + console.log(chalk.green("✓ Transfer successful! Tx hash:"), transferTx.hash); |
| 79 | + successful++; |
| 80 | + } catch (error) { |
| 81 | + console.error(chalk.red(`Error transferring to ${deployment['vestingAddress']}:`), error.message); |
| 82 | + failed++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + console.log(chalk.cyan("\nTransfer Summary:")); |
| 87 | + console.log("Total contracts:", chalk.yellow(deployments['contracts'].length)); |
| 88 | + console.log("Successful:", chalk.green(successful)); |
| 89 | + console.log("Failed:", failed > 0 ? chalk.red(failed) : chalk.green(failed)); |
| 90 | +} |
| 91 | + |
| 92 | +main() |
| 93 | + .then(() => process.exit(0)) |
| 94 | + .catch((error) => { |
| 95 | + console.error(chalk.red("Unhandled error:")); |
| 96 | + console.error(error); |
| 97 | + process.exit(1); |
| 98 | + }); |
0 commit comments