import got from 'got' ; import { ContractAbiDefinition } from 'web3x/contract' ; function getHost ( net ) { switch ( net ) { case 'mainnet' : return 'etherscan.io' ; case 'kovan' : return 'kovan.etherscan.io' ; case 'ropsten' : return 'ropsten.etherscan.io' ; default : throw new Error ( `Unknown network ${ net }` ) ; } } function getApiHost ( net ) { switch ( net ) { case 'mainnet' : return 'api.etherscan.io' ; case 'kovan' : return 'api-kovan.etherscan.io' ; case 'ropsten' : return 'api-ropsten.etherscan.io' ; default : throw new Error ( `Unknown network ${ net }` ) ; } } async function getAbi ( net , address ) { const host = getApiHost ( net ) ; const abiUrl = `http://${ host }/api?module=contract&action=getabi&address=${ address }&format=raw` ; const response = await got ( abiUrl , { json : true } ) ; return response . body ; } async function getInitData ( net , address ) { const host = getHost ( net ) ; const response = ( await got ( `https://${ host }/address/${ address }` ) ) . body ; const initCodeMd = response . match ( /<div id='verifiedbytecode2'>([0-9a-f]+)</ ) ; if ( ! initCodeMd ) { return ; } const initCode = '0x' + initCodeMd ! [ 1 ] ; const ctorParamsMd = response . match ( /last bytes of the Contract Creation Code above.*?margin-top: 5px;'>([0-9a-f]+)</ ) ; if ( ctorParamsMd ) { const ctorParams = ctorParamsMd ! [ 1 ] ; if ( ! initCode . endsWith ( ctorParams ) ) { throw new Error ( 'Expected ctor params to be appended to end of init code.' ) ; } return initCode . slice ( 0 , - ctorParams . length ) ; } return initCode ; } export async function getFromEtherscan ( net , address ) { const abi = await getAbi ( net , address ) ; const initData = await getInitData ( net , address ) ; return { abi , initData } ; }