import { Wallet , ethers , ContractFactory , Signer } from 'ethers' ; import { deployContract } from 'ethereum-waffle' ; import fs from 'fs' ; import readline from 'readline' ; export const txnArgs = { gasLimit : 2000000 , gasPrice : 16000000000 } ; export const getContractJSON = ( contractName ) => require ( `../build/${ contractName }.json` ) ; const confirmDeploy = async ( contractName ) => { const rl = readline . createInterface ( { input : process . stdin , output : process . stdout , } ) ; return new Promise < boolean > ( resolve => { rl . question ( `Deploy ${ contractName }? (Y/n) ` , ( answer ) => { rl . close ( ) ; if ( [ 'n' , 'no' ] . includes ( answer . toLowerCase ( ) ) ) { resolve ( false ) ; } else { resolve ( true ) ; } } ) ; } ) ; } ; export const setupDeployer = ( wallet ) => async ( contractName , ... args ) => { const contractJson = getContractJSON ( contractName ) ; if ( await confirmDeploy ( contractName ) ) { const contract = await deployContract ( wallet , contractJson , args , txnArgs ) ; console . log ( `${ contractName } address: ${ contract . address }` ) ; return contract ; } else { process . exit ( 0 ) ; } } ; export const deployBehindCustomProxy = ( proxyName ) => async ( wallet , contractName , ... args ) => { const deploy = setupDeployer ( wallet ) ; const implementation = await deploy ( contractName , ... args ) ; const proxy = await deploy ( proxyName ) ; const contract = implementation . attach ( proxy . address ) ; console . log ( `deployed ${ contractName }Proxy at: ` , contract . address ) ; return [ implementation , proxy , contract ] ; } ; export const deployBehindProxy = deployBehindCustomProxy ( 'OwnedUpgradeabilityProxy' ) ; export const deployBehindTimeProxy = deployBehindCustomProxy ( 'TimeOwnedUpgradeabilityProxy' ) ; export const getContract = ( wallet ) => ( contractName , contractAddress ) => { const contractJson = getContractJSON ( contractName ) ; return new ethers . Contract ( contractAddress , contractJson . abi , wallet ) ; } ; export const validatePrivateKey = ( subject ) => { if ( ! ( /^0x[0-9-a-fA-F]{64}$/ . test ( subject ) ) ) throw new Error ( 'Pass proper private key' ) ; } ; export const validateAddress = ( subject ) => { try { ethers . utils . getAddress ( subject ) ; } catch ( e ) { throw new Error ( 'Pass proper deploy helper address' ) ; } } ; export type Newable < T > = { new ( ... args ) : T ; } ; export const setupDeploy = ( wallet ) => async < T extends ContractFactory > ( Factory , ... args ) : Promise < ReturnType < T [ 'deploy' ] > > => { const contract = await new Factory ( wallet ) . deploy ( ... args ) ; await contract . deployed ( ) ; return contract ; } ; export type Deployer = ReturnType < typeof setupDeploy > ; export const saveDeployResult = ( fileName ) => async ( result ) => { console . log ( 'saving results...' ) ; if ( ! fs . existsSync ( './scripts/deploy' ) ) { fs . mkdirSync ( './scripts/deploy' ) ; } fs . writeFileSync ( `./scripts/deploy/${ fileName }.json` , JSON . stringify ( result , null , 2 ) ) ; } ; export { ask } from './ask' ; export { toTrustToken } from './toTrustToken' ;