import BigNumber from 'bignumber.js' ; import BN = require ( 'bn.js' ) ; import { WAD } from './constants' ; import { Wallet , ContractTransaction , Signer } from 'ethers' ; import { HardhatRuntimeEnvironment } from 'hardhat/types' ; import { tEthereumAddress } from './types' ; import { isAddress } from 'ethers/lib/utils' ; import { isZeroAddress } from 'ethereumjs-util' ; export const toWad = ( value ) => new BigNumber ( value ) . times ( WAD ) . toFixed ( ) ; export const bnToBigNumber = ( amount ) : BigNumber => new BigNumber ( < any > amount ) ; export const stringToBigNumber = ( amount ) : BigNumber => new BigNumber ( amount ) ; export let DRE ; export const setDRE = ( _DRE ) => { DRE = _DRE ; } ; export const sleep = ( milliseconds ) => { return new Promise ( ( resolve ) => setTimeout ( resolve , milliseconds ) ) ; } ; export const createRandomAddress = ( ) => Wallet . createRandom ( ) . address ; export const evmSnapshot = async ( ) => await DRE . ethers . provider . send ( 'evm_snapshot' , [ ] ) ; export const evmRevert = async ( id ) => DRE . ethers . provider . send ( 'evm_revert' , [ id ] ) ; export const timeLatest = async ( ) => { const block = await DRE . ethers . provider . getBlock ( 'latest' ) ; return new BigNumber ( block . timestamp ) ; } ; export const advanceBlock = async ( timestamp ) => await DRE . ethers . provider . send ( 'evm_mine' , [ timestamp ] ) ; export const increaseTime = async ( secondsToIncrease ) => { await DRE . ethers . provider . send ( 'evm_increaseTime' , [ secondsToIncrease ] ) ; await DRE . ethers . provider . send ( 'evm_mine' , [ ] ) ; } ; export const advanceTimeAndBlock = async function ( forwardTime ) { const currentBlockNumber = await DRE . ethers . provider . getBlockNumber ( ) ; const currentBlock = await DRE . ethers . provider . getBlock ( currentBlockNumber ) ; if ( currentBlock === null ) { await DRE . ethers . provider . send ( 'evm_increaseTime' , [ forwardTime ] ) ; await DRE . ethers . provider . send ( 'evm_mine' , [ ] ) ; await DRE . ethers . provider . send ( 'evm_increaseTime' , [ 15 ] ) ; return ; } const currentTime = currentBlock . timestamp ; const futureTime = currentTime + forwardTime ; await DRE . ethers . provider . send ( 'evm_setNextBlockTimestamp' , [ futureTime ] ) ; await DRE . ethers . provider . send ( 'evm_mine' , [ ] ) ; } ; export const latestBlock = async ( ) => await DRE . ethers . provider . getBlockNumber ( ) ; export const advanceBlockTo = async ( target ) => { const currentBlock = await latestBlock ( ) ; const start = Date . now ( ) ; let notified ; let increase = 10 ; if ( target < currentBlock ) throw Error ( `Target block #(${ target }) is lower than current block #(${ currentBlock })` ) ; while ( ( await latestBlock ( ) ) < target ) { if ( ! notified && Date . now ( ) - start >= 5000 ) { notified = true ; console . log ( `advanceBlockTo: Advancing too many blocks is causing this test to be slow.'` ) ; } await advanceBlock ( start + increase ) ; increase += 10 ; } } ; export const waitForTx = async ( tx ) => await tx . wait ( 1 ) ; export const filterMapBy = ( raw , fn ) => Object . keys ( raw ) . filter ( fn ) . reduce < { [ key ] : any ; } > ( ( obj , key ) => { obj [ key ] = raw [ key ] ; return obj ; } , { } ) ; export const chunk = < T > ( arr , chunkSize ) : Array < Array < T > > => { return arr . reduce ( ( prevVal , currVal , currIndx , array ) => ! ( currIndx % chunkSize ) ? prevVal . concat ( [ array . slice ( currIndx , currIndx + chunkSize ) ] ) : prevVal , [ ] ) ; } ; interface DbEntry { [ network ] : { deployer : string ; address : string ; } ; } export const getImpersonatedSigner = async ( address ) : Promise < Signer > => { await DRE . network . provider . request ( { method : 'hardhat_impersonateAccount' , params : [ address ] , } ) ; return await DRE . ethers . getSigner ( address ) ; } ; export const notFalsyOrZeroAddress = ( address ) : boolean => { if ( ! address ) { return false ; } return isAddress ( address ) && ! isZeroAddress ( address ) ; } ;