import { ethers , waffle } from "hardhat" ; import { ParamType } from "ethers/lib/utils" ; import GnosisSafeArtifact from "@gnosis.pm/safe-contracts/build/contracts/GnosisSafe.json" ; import GnosisProxyFactoryArtifact from "@gnosis.pm/safe-contracts/build/contracts/GnosisSafeProxyFactory.json" ; import { Event , Signer } from "ethers" ; const { deployContract } = waffle ; import { User } from "./types" ; import { Address0 } from "./constants" ; export const mineBlockAtTimestamp = async ( timestamp ) : Promise < any > => { await ethers . provider . send ( "evm_mine" , [ timestamp ] ) ; } ; export const getCurrentBlockTimestamp = async ( ) : Promise < number > => { const currentBlock = await ethers . provider ?. getBlock ( "latest" ) ; const currentTimestamp = currentBlock ?. timestamp ?? 0 ; return currentTimestamp ; } ; export const getTransactionEta = async ( timelockDelay ) : Promise < number > => { const currentTimestamp = await getCurrentBlockTimestamp ( ) ; const transactionEta = currentTimestamp + timelockDelay + 1 ; return transactionEta ; } ; export const getExpectedContractAddress = async ( deployer ) : Promise < string > => { const adminAddressTransactionCount = await deployer . signer . getTransactionCount ( ) ; const expectedContractAddress = ethers . utils . getContractAddress ( { from : deployer . address , nonce : adminAddressTransactionCount + 1 , } ) ; return expectedContractAddress ; } ; export const encodeParameters = ( types , values ) : string => { const abi = new ethers . utils . AbiCoder ( ) ; return abi . encode ( types , values ) ; } ; export const generateMultisigWallet = async ( signers , treshold , deployer ) : Promise < any > => { const gnosisSafeMasterCopy = await deployContract ( deployer . signer , GnosisSafeArtifact , [ ] ) ; const gnosisSafeInterface = new ethers . utils . Interface ( GnosisSafeArtifact . abi ) ; const gnosisSafeData = gnosisSafeInterface . encodeFunctionData ( "setup" , [ signers , treshold , Address0 , "0x" , Address0 , Address0 , 0 , Address0 , ] ) ; const gnosisProxyFactory = await deployContract ( deployer . signer , GnosisProxyFactoryArtifact , [ ] ) ; try { const proxyCreationTx = await gnosisProxyFactory . createProxy ( gnosisSafeMasterCopy . address , gnosisSafeData ) ; const proxyCreationTxReceipt = await proxyCreationTx . wait ( ) ; const gnosisSafe = await getGnosisSafeContractFromTxEvent ( proxyCreationTxReceipt . events , "ProxyCreation" , gnosisProxyFactory . address , "proxy" , deployer . signer ) ; return gnosisSafe ; } catch ( e ) { console . log ( "error" , e ) ; } } ; async function getGnosisSafeContractFromTxEvent ( events , eventName , contractAddress , paramName , signer ) { const event = events . find ( ( eventItem ) => eventItem . event === eventName && eventItem . address === contractAddress ) ; const eventArguments = event ?. args ; const param = eventArguments ? eventArguments [ paramName ] : "" ; if ( param ) { const gnosisInterface = new ethers . utils . Interface ( GnosisSafeArtifact . abi ) ; const contract = new ethers . Contract ( param , gnosisInterface , signer ) ; return contract ; } else { return param ; } } export const getRandomNum = ( ) => { const randomNum = Math . floor ( Math . random ( ) * 10 ) ; if ( randomNum === 0 ) { return 1 ; } return randomNum ; } ; export const parseEvent = ( events , eventSignature ) => { if ( Array . isArray ( events ) ) { const event = events . find ( ( e ) => { return e . eventSignature === eventSignature ; } ) ; return event || null ; } return null ; } ;