From 95e80e26dcd276fd6d0d97107b95625a6d845cf1 Mon Sep 17 00:00:00 2001 From: Hadrien Charlanes Date: Mon, 21 Jun 2021 10:23:59 +0200 Subject: [PATCH] feat: added helper to setHardhatBalance --- helpers/misc-utils.ts | 12 +++++++++++- package.json | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/helpers/misc-utils.ts b/helpers/misc-utils.ts index 0315dfe0..0e3e2fec 100644 --- a/helpers/misc-utils.ts +++ b/helpers/misc-utils.ts @@ -3,7 +3,7 @@ import BN = require('bn.js'); import low from 'lowdb'; import FileSync from 'lowdb/adapters/FileSync'; import { WAD } from './constants'; -import { Wallet, ContractTransaction } from 'ethers'; +import { Wallet, ContractTransaction, BigNumberish } from 'ethers'; import { HardhatRuntimeEnvironment } from 'hardhat/types'; import { BuidlerRuntimeEnvironment } from '@nomiclabs/buidler/types'; import { tEthereumAddress } from './types'; @@ -46,6 +46,16 @@ export const increaseTime = async (secondsToIncrease: number) => { await DRE.ethers.provider.send('evm_mine', []); }; +export const setHardhatBalance = async (addresses: string[], balances: BigNumberish[]) => { + for (const [index, address] of addresses.entries()) { + const hexBalance = DRE.ethers.BigNumber.from(balances[index]).toHexString(); + // ethers big number .toHexString adds a 0x0 that hardhat can't remove. + // from 0x0Deb12312 to 0xDeb123112 + const hexBalanceWithoutZero = hexBalance.slice(0, 2) + hexBalance.slice(3); + await DRE.ethers.provider.send('hardhat_setBalance', [address, hexBalanceWithoutZero]); + } +}; + // Workaround for time travel tests bug: https://github.com/Tonyhaenn/hh-time-travel/blob/0161d993065a0b7585ec5a043af2eb4b654498b8/test/test.js#L12 export const advanceTimeAndBlock = async function (forwardTime: number) { const currentBlockNumber = await DRE.ethers.provider.getBlockNumber(); diff --git a/package.json b/package.json index b9debafc..59d24bee 100644 --- a/package.json +++ b/package.json @@ -120,7 +120,7 @@ "ethereumjs-util": "7.0.2", "ethers": "^5.0.19", "globby": "^11.0.1", - "hardhat": "^2.2.0", + "hardhat": "^2.4.0", "hardhat-gas-reporter": "^1.0.0", "hardhat-typechain": "^0.3.3", "husky": "^4.2.5",