import { BigNumber , Bytes , ContractFactory , Signer , providers } from "ethers" ; import { ethers , network } from "hardhat" ; import { Artifact } from "hardhat/types" ; import { BytesLike } from "@ethersproject/bytes" ; import { Contract } from "@ethersproject/contracts" ; import { DeploymentsExtension } from "hardhat-deploy/dist/types" ; import { ERC20 } from "../build/typechain/ERC20" ; import { Swap } from "../build/typechain/Swap" ; import merkleTreeDataTest from "../test/exampleMerkleTree.json" ; export const MAX_UINT256 = ethers . constants . MaxUint256 ; export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" ; export enum TIME { SECONDS = 1 , DAYS = 86400 , WEEKS = 604800 } export const BIG_NUMBER_1E18 = BigNumber . from ( 10 ) . pow ( 18 ) ; export const BIG_NUMBER_ZERO = BigNumber . from ( 0 ) ; export const CHAIN_ID = { MAINNET : "1" , ROPSTEN : "3" , KOVAN : "42" , HARDHAT : "31337" , } ; export function isMainnet ( networkId ) { return networkId == CHAIN_ID . MAINNET ; } export function isTestNetwork ( networkId ) { return ( networkId == CHAIN_ID . HARDHAT || networkId == CHAIN_ID . ROPSTEN || networkId == CHAIN_ID . KOVAN ) ; } export function linkBytecode ( artifact , libraries ) { let bytecode = artifact . bytecode ; for ( const [ , fileReferences ] of Object . entries ( artifact . linkReferences ) ) { for ( const [ libName , fixups ] of Object . entries ( fileReferences ) ) { const addr = libraries [ libName ] ; if ( addr === undefined ) { continue ; } for ( const fixup of fixups ) { bytecode = bytecode . substr ( 0 , 2 + fixup . start * 2 ) + addr . substr ( 2 ) + bytecode . substr ( 2 + ( fixup . start + fixup . length ) * 2 ) ; } } } return bytecode ; } export async function deployContractWithLibraries ( signer , artifact , libraries , args ? ) { const swapFactory = ( await ethers . getContractFactory ( artifact . abi , linkBytecode ( artifact , libraries ) , signer ) ) as ContractFactory ; if ( args ) { return swapFactory . deploy ( ... args ) ; } else { return swapFactory . deploy ( ) ; } } export function getTestMerkleRoot ( ) { return merkleTreeDataTest . merkleRoot ; } export function getTestMerkleAllowedAccounts ( ) { return merkleTreeDataTest . allowedAccounts ; } export function getTestMerkleProof ( address ) { const ALLOWED_ACCOUNTS = getTestMerkleAllowedAccounts ( ) ; if ( address in ALLOWED_ACCOUNTS ) { return ALLOWED_ACCOUNTS [ address ] . proof ; } return [ ] ; } export async function getDeployedContractByName ( deployments , name ) { const deployment = await deployments . get ( name ) ; return ethers . getContractAt ( deployment . abi , deployment . address ) ; } export async function getPoolBalances ( swap , numOfTokens ) { const balances = [ ] ; for ( let i = 0 ; i < numOfTokens ; i ++ ) { balances . push ( await swap . getTokenBalance ( i ) ) ; } return balances ; } export async function getUserTokenBalances ( address , tokens ) { const balanceArray = [ ] ; if ( address instanceof Signer ) { address = await address . getAddress ( ) ; } for ( const token of tokens ) { balanceArray . push ( await token . balanceOf ( address ) ) ; } return balanceArray ; } export async function getUserTokenBalance ( address , token ) { if ( address instanceof Signer ) { address = await address . getAddress ( ) ; } return token . balanceOf ( address ) ; } export async function forceAdvanceOneBlock ( timestamp ? ) { const params = timestamp ? [ timestamp ] : [ ] ; return ethers . provider . send ( "evm_mine" , params ) ; } export async function setTimestamp ( timestamp ) { return forceAdvanceOneBlock ( timestamp ) ; } export async function increaseTimestamp ( timestampDelta ) { await ethers . provider . send ( "evm_increaseTime" , [ timestampDelta ] ) ; return forceAdvanceOneBlock ( ) ; } export async function setNextTimestamp ( timestamp ) { const chainId = ( await ethers . provider . getNetwork ( ) ) . chainId ; switch ( chainId ) { case 31337 : return ethers . provider . send ( "evm_setNextBlockTimestamp" , [ timestamp ] ) ; case 1337 : default : return setTimestamp ( timestamp ) ; } } export async function getCurrentBlockTimestamp ( ) { const block = await ethers . provider . getBlock ( "latest" ) ; return block . timestamp ; } export async function impersonateAccount ( address ) { await network . provider . request ( { method : "hardhat_impersonateAccount" , params : [ address ] , } ) ; return ethers . provider . getSigner ( address ) ; } export async function asyncForEach < T > ( array , callback ) { for ( let index = 0 ; index < array . length ; index ++ ) { await callback ( array [ index ] , index ) ; } }