From 71e23f85ab1aacbb5ac4d6019269b5d88e77d668 Mon Sep 17 00:00:00 2001 From: Pierre Bertet Date: Tue, 19 Mar 2024 09:35:08 +0000 Subject: [PATCH] Add a deployment script for app development purposes (#83) - Hardhat: add ACCOUNTS_BALANCE to change the default accounts ETH balance. - Hardhat: add a script to deploy contracts for app development purposes. --- contracts/hardhat.config.js | 14 ++++-- contracts/hardhatAccountsList2k.js | 2 +- contracts/utils/deploymentDev.js | 75 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 contracts/utils/deploymentDev.js diff --git a/contracts/hardhat.config.js b/contracts/hardhat.config.js index eb6c8f4d..c0e182a4 100644 --- a/contracts/hardhat.config.js +++ b/contracts/hardhat.config.js @@ -3,8 +3,16 @@ require("@nomicfoundation/hardhat-toolbox"); require("@nomiclabs/hardhat-truffle5"); require("solidity-coverage"); -const accounts = require("./hardhatAccountsList2k.js"); -const accountsList = accounts.accountsList; +function accounts() { + const { accountsList } = require("./hardhatAccountsList2k.js"); + const balanceStr = process.env.ACCOUNTS_BALANCE?.trim(); + return balanceStr + ? accountsList.map((account) => ({ + ...account, + balance: String(BigInt(balanceStr) * 10n ** 18n), + })) + : accountsList; +} /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { @@ -23,7 +31,7 @@ module.exports = { }, networks: { hardhat: { - accounts: accountsList, + accounts: accounts(), gas: 10000000, blockGasLimit: 15000000, gasPrice: 20000000000, diff --git a/contracts/hardhatAccountsList2k.js b/contracts/hardhatAccountsList2k.js index d4e4e2b2..f6911b91 100644 --- a/contracts/hardhatAccountsList2k.js +++ b/contracts/hardhatAccountsList2k.js @@ -1,4 +1,4 @@ -const _1e36Str = "1000000000000000000000000000000000000" +const _1e36Str = String(10n ** 36n) const accountsList = diff --git a/contracts/utils/deploymentDev.js b/contracts/utils/deploymentDev.js new file mode 100644 index 00000000..c462f07f --- /dev/null +++ b/contracts/utils/deploymentDev.js @@ -0,0 +1,75 @@ +const hre = require("hardhat"); +const { deployLiquityCoreHardhat, connectCoreContracts } = require("./deploymentHelpers.js"); + +// Deploy Liquity contracts for development purposes +async function main() { + const contracts = await deployLiquityCoreHardhat(); + await connectCoreContracts(contracts); + + const accounts = await hre.ethers.getSigners(); + + // list of [debt, coll] tuples + const trovesParams = [ + [1_800n, 20n], + [2_800n, 32n], + [4_000n, 30n], + [6_000n, 65n], + [5_000n, 50n], + [2_400n, 37n], + ]; + + // open troves + await Promise.all(trovesParams.map(async ([debt, coll], index) => { + const account = await accounts[index].getAddress(); + return contracts.borrowerOperations.openTrove( + String(100n * 10n ** 16n), // 100% + String(debt * 10n ** 18n), + account, + account, + String(5n * 10n ** 16n), // 5% + { + from: account, + value: String(coll * 10n ** 18n), + }, + ); + })); + + let namesWidth = 0; + const contractsLog = []; + for (const contract of Object.values(contracts)) { + const { contractName } = contract.constructor; + contractsLog.push([contractName, contract.address]); + namesWidth = Math.max(namesWidth, contractName.length); + } + + console.log(""); + console.log("Core contracts deployed."); + console.log(""); + console.log( + contractsLog + .map(([name, address]) => `${name.padEnd(namesWidth)} ${address}`) + .join("\n"), + ); + console.log(""); + + console.log("Frontend vars (put this in frontend/*.env):"); + console.log(""); + console.log( + contractsLog + .map(([name, address]) => envVarContract(name, address)) + .join("\n"), + ); + console.log(""); +} + +function envVarContract(name, address) { + const envVarEnd = name.replace(/[A-Z]/g, (s) => "_" + s); + return `NEXT_PUBLIC_CONTRACT${envVarEnd.toUpperCase()}=${address}`; +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + });