2019-05-15 01:25:46 +03:00
"use strict" ;
import { BlockTag , TransactionRequest , TransactionResponse } from "@ethersproject/abstract-provider" ;
2020-11-23 01:43:32 +03:00
import { hexlify , hexValue , isHexString } from "@ethersproject/bytes" ;
2020-05-04 00:32:16 +03:00
import { Network , Networkish } from "@ethersproject/networks" ;
2019-08-01 23:13:35 +03:00
import { deepCopy , defineReadOnly } from "@ethersproject/properties" ;
2021-04-14 22:04:24 +03:00
import { accessListify } from "@ethersproject/transactions" ;
2020-10-23 02:46:52 +03:00
import { ConnectionInfo , fetchJson } from "@ethersproject/web" ;
2019-05-15 01:25:46 +03:00
2020-07-14 09:26:45 +03:00
import { showThrottleMessage } from "./formatter" ;
2019-08-02 01:04:06 +03:00
import { Logger } from "@ethersproject/logger" ;
import { version } from "./_version" ;
const logger = new Logger ( version ) ;
2019-05-15 01:25:46 +03:00
import { BaseProvider } from "./base-provider" ;
// The transaction has already been sanitized by the calls in Provider
2020-10-23 02:46:52 +03:00
function getTransactionPostData ( transaction : TransactionRequest ) : Record < string , string > {
const result : Record < string , string > = { } ;
2019-05-15 01:25:46 +03:00
for ( let key in transaction ) {
if ( ( < any > transaction ) [ key ] == null ) { continue ; }
2021-03-26 23:16:56 +03:00
let value = ( < any > transaction ) [ key ] ;
2020-10-23 02:46:52 +03:00
// Quantity-types require no leading zero, unless 0
2021-03-26 23:16:56 +03:00
if ( ( < any > { type : true , gasLimit : true , gasPrice : true , nonce : true , value : true } ) [ key ] ) {
value = hexValue ( hexlify ( value ) ) ;
} else if ( key === "accessList" ) {
2021-04-14 22:04:24 +03:00
const sets = accessListify ( value ) ;
value = '[' + sets . map ( ( set ) = > {
return ` {address:" ${ set . address } ",storageKeys:[" ${ set . storageKeys . join ( '","' ) } "]} ` ;
} ) . join ( "," ) + "]" ;
2021-03-26 23:16:56 +03:00
} else {
value = hexlify ( value ) ;
2019-05-15 01:25:46 +03:00
}
2020-10-23 02:46:52 +03:00
result [ key ] = value ;
2019-05-15 01:25:46 +03:00
}
2020-10-23 02:46:52 +03:00
return result ;
2019-05-15 01:25:46 +03:00
}
function getResult ( result : { status? : number , message? : string , result? : any } ) : any {
// getLogs, getHistory have weird success responses
if ( result . status == 0 && ( result . message === "No records found" || result . message === "No transactions found" ) ) {
return result . result ;
}
if ( result . status != 1 || result . message != "OK" ) {
2019-11-01 17:51:08 +03:00
const error : any = new Error ( "invalid response" ) ;
2019-05-15 01:25:46 +03:00
error . result = JSON . stringify ( result ) ;
2020-07-14 09:26:45 +03:00
if ( ( result . result || "" ) . toLowerCase ( ) . indexOf ( "rate limit" ) >= 0 ) {
error . throttleRetry = true ;
}
2019-05-15 01:25:46 +03:00
throw error ;
}
return result . result ;
}
function getJsonResult ( result : { jsonrpc : string , result? : any , error ? : { code? : number , data? : any , message? : string } } ) : any {
2020-07-14 09:26:45 +03:00
// This response indicates we are being throttled
if ( result && ( < any > result ) . status == 0 && ( < any > result ) . message == "NOTOK" && ( result . result || "" ) . toLowerCase ( ) . indexOf ( "rate limit" ) >= 0 ) {
const error : any = new Error ( "throttled response" ) ;
error . result = JSON . stringify ( result ) ;
error . throttleRetry = true ;
throw error ;
}
2019-05-15 01:25:46 +03:00
if ( result . jsonrpc != "2.0" ) {
// @TODO: not any
2019-11-01 17:51:08 +03:00
const error : any = new Error ( "invalid response" ) ;
2019-05-15 01:25:46 +03:00
error . result = JSON . stringify ( result ) ;
throw error ;
}
if ( result . error ) {
// @TODO: not any
2019-11-01 17:51:08 +03:00
const error : any = new Error ( result . error . message || "unknown error" ) ;
2019-05-15 01:25:46 +03:00
if ( result . error . code ) { error . code = result . error . code ; }
if ( result . error . data ) { error . data = result . error . data ; }
throw error ;
}
return result . result ;
}
// The blockTag was normalized as a string by the Provider pre-perform operations
function checkLogTag ( blockTag : string ) : number | "latest" {
if ( blockTag === "pending" ) { throw new Error ( "pending not supported" ) ; }
if ( blockTag === "latest" ) { return blockTag ; }
return parseInt ( blockTag . substring ( 2 ) , 16 ) ;
}
2020-02-18 02:14:02 +03:00
const defaultApiKey = "9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB" ;
2020-11-23 01:43:32 +03:00
function checkError ( method : string , error : any , transaction : any ) : any {
// Undo the "convenience" some nodes are attempting to prevent backwards
// incompatibility; maybe for v6 consider forwarding reverts as errors
if ( method === "call" && error . code === Logger . errors . SERVER_ERROR ) {
const e = error . error ;
if ( e && e . message . match ( "reverted" ) && isHexString ( e . data ) ) {
return e . data ;
}
}
2020-09-16 09:19:28 +03:00
// Get the message from any nested error structure
2020-09-11 08:10:15 +03:00
let message = error . message ;
2020-09-16 09:19:28 +03:00
if ( error . code === Logger . errors . SERVER_ERROR ) {
if ( error . error && typeof ( error . error . message ) === "string" ) {
message = error . error . message ;
} else if ( typeof ( error . body ) === "string" ) {
message = error . body ;
} else if ( typeof ( error . responseText ) === "string" ) {
message = error . responseText ;
}
}
message = ( message || "" ) . toLowerCase ( ) ;
// "Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 21464000000000 and got: 0"
if ( message . match ( /insufficient funds/ ) ) {
logger . throwError ( "insufficient funds for intrinsic transaction cost" , Logger . errors . INSUFFICIENT_FUNDS , {
error , method , transaction
} ) ;
}
// "Transaction with the same hash was already imported."
if ( message . match ( /same hash was already imported|transaction nonce is too low/ ) ) {
logger . throwError ( "nonce has already been used" , Logger . errors . NONCE_EXPIRED , {
error , method , transaction
} ) ;
}
// "Transaction gas price is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce."
if ( message . match ( /another transaction with same nonce/ ) ) {
logger . throwError ( "replacement fee too low" , Logger . errors . REPLACEMENT_UNDERPRICED , {
error , method , transaction
} ) ;
2020-09-11 08:10:15 +03:00
}
if ( message . match ( /execution failed due to an exception/ ) ) {
logger . throwError ( "cannot estimate gas; transaction may fail or may require manual gas limit" , Logger . errors . UNPREDICTABLE_GAS_LIMIT , {
2020-09-16 09:19:28 +03:00
error , method , transaction
2020-09-11 08:10:15 +03:00
} ) ;
}
throw error ;
}
2019-05-15 01:25:46 +03:00
export class EtherscanProvider extends BaseProvider {
readonly baseUrl : string ;
readonly apiKey : string ;
2020-07-14 09:26:45 +03:00
2019-05-15 01:25:46 +03:00
constructor ( network? : Networkish , apiKey? : string ) {
2019-08-02 01:04:06 +03:00
logger . checkNew ( new . target , EtherscanProvider ) ;
2019-05-15 01:25:46 +03:00
super ( network ) ;
let name = "invalid" ;
if ( this . network ) { name = this . network . name ; }
let baseUrl = null ;
switch ( name ) {
case "homestead" :
baseUrl = "https://api.etherscan.io" ;
break ;
case "ropsten" :
baseUrl = "https://api-ropsten.etherscan.io" ;
break ;
case "rinkeby" :
baseUrl = "https://api-rinkeby.etherscan.io" ;
break ;
case "kovan" :
baseUrl = "https://api-kovan.etherscan.io" ;
break ;
case "goerli" :
baseUrl = "https://api-goerli.etherscan.io" ;
break ;
default :
throw new Error ( "unsupported network" ) ;
}
defineReadOnly ( this , "baseUrl" , baseUrl ) ;
2020-02-18 02:14:02 +03:00
defineReadOnly ( this , "apiKey" , apiKey || defaultApiKey ) ;
2019-05-15 01:25:46 +03:00
}
2020-05-04 00:32:16 +03:00
async detectNetwork ( ) : Promise < Network > {
return this . network ;
}
2019-05-15 01:25:46 +03:00
2019-11-01 17:51:08 +03:00
async perform ( method : string , params : any ) : Promise < any > {
2020-10-23 02:46:52 +03:00
let url = this . baseUrl + "/api" ;
2019-05-15 01:25:46 +03:00
let apiKey = "" ;
if ( this . apiKey ) { apiKey += "&apikey=" + this . apiKey ; }
2020-10-23 02:46:52 +03:00
const get = async ( url : string , payload : Record < string , string > , procFunc ? : ( value : any ) = > any ) : Promise < any > = > {
2019-08-01 23:13:35 +03:00
this . emit ( "debug" , {
action : "request" ,
request : url ,
provider : this
} ) ;
2020-07-14 09:26:45 +03:00
2020-10-23 02:46:52 +03:00
const connection : ConnectionInfo = {
2020-07-14 09:26:45 +03:00
url : url ,
2020-07-16 12:29:33 +03:00
throttleSlotInterval : 1000 ,
2020-07-14 09:26:45 +03:00
throttleCallback : ( attempt : number , url : string ) = > {
2020-10-08 00:28:18 +03:00
if ( this . isCommunityResource ( ) ) {
2020-07-14 09:26:45 +03:00
showThrottleMessage ( ) ;
}
return Promise . resolve ( true ) ;
}
} ;
2020-10-23 02:46:52 +03:00
let payloadStr : string = null ;
if ( payload ) {
connection . headers = { "content-type" : "application/x-www-form-urlencoded; charset=UTF-8" } ;
payloadStr = Object . keys ( payload ) . map ( ( key ) = > {
return ` ${ key } = ${ payload [ key ] } `
} ) . join ( "&" ) ;
}
const result = await fetchJson ( connection , payloadStr , procFunc || getJsonResult ) ;
2019-11-01 17:51:08 +03:00
this . emit ( "debug" , {
action : "response" ,
request : url ,
response : deepCopy ( result ) ,
provider : this
2019-05-15 01:25:46 +03:00
} ) ;
2019-11-01 17:51:08 +03:00
return result ;
2019-05-15 01:25:46 +03:00
} ;
switch ( method ) {
case "getBlockNumber" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_blockNumber" + apiKey ;
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "getGasPrice" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_gasPrice" + apiKey ;
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "getBalance" :
// Returns base-10 result
2020-10-23 02:46:52 +03:00
url += "?module=account&action=balance&address=" + params . address ;
2019-05-15 01:25:46 +03:00
url += "&tag=" + params . blockTag + apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null , getResult ) ;
2019-05-15 01:25:46 +03:00
case "getTransactionCount" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getTransactionCount&address=" + params . address ;
2019-05-15 01:25:46 +03:00
url += "&tag=" + params . blockTag + apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "getCode" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getCode&address=" + params . address ;
2019-05-15 01:25:46 +03:00
url += "&tag=" + params . blockTag + apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "getStorageAt" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getStorageAt&address=" + params . address ;
2019-05-15 01:25:46 +03:00
url += "&position=" + params . position ;
url += "&tag=" + params . blockTag + apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "sendTransaction" :
2020-10-23 02:46:52 +03:00
return get ( url , {
module : "proxy" ,
action : "eth_sendRawTransaction" ,
hex : params.signedTransaction ,
apikey : this.apiKey
} ) . catch ( ( error ) = > {
2020-09-16 09:19:28 +03:00
return checkError ( "sendTransaction" , error , params . signedTransaction ) ;
2019-05-15 01:25:46 +03:00
} ) ;
case "getBlock" :
if ( params . blockTag ) {
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getBlockByNumber&tag=" + params . blockTag ;
2019-05-15 01:25:46 +03:00
if ( params . includeTransactions ) {
url += "&boolean=true" ;
} else {
url += "&boolean=false" ;
}
url += apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
}
2020-04-22 09:42:25 +03:00
throw new Error ( "getBlock by blockHash not implemented" ) ;
2019-05-15 01:25:46 +03:00
case "getTransaction" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getTransactionByHash&txhash=" + params . transactionHash ;
2019-05-15 01:25:46 +03:00
url += apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "getTransactionReceipt" :
2020-10-23 02:46:52 +03:00
url += "?module=proxy&action=eth_getTransactionReceipt&txhash=" + params . transactionHash ;
2019-05-15 01:25:46 +03:00
url += apiKey ;
2020-10-23 02:46:52 +03:00
return get ( url , null ) ;
2019-05-15 01:25:46 +03:00
case "call" : {
if ( params . blockTag !== "latest" ) {
throw new Error ( "EtherscanProvider does not support blockTag for call" ) ;
}
2020-10-23 02:46:52 +03:00
const postData = getTransactionPostData ( params . transaction ) ;
postData . module = "proxy" ;
postData . action = "eth_call" ;
postData . apikey = this . apiKey ;
2020-09-11 08:10:15 +03:00
try {
2020-10-23 02:46:52 +03:00
return await get ( url , postData ) ;
2020-09-11 08:10:15 +03:00
} catch ( error ) {
2020-09-16 09:19:28 +03:00
return checkError ( "call" , error , params . transaction ) ;
2020-09-11 08:10:15 +03:00
}
2019-05-15 01:25:46 +03:00
}
case "estimateGas" : {
2020-10-23 02:46:52 +03:00
const postData = getTransactionPostData ( params . transaction ) ;
postData . module = "proxy" ;
postData . action = "eth_estimateGas" ;
postData . apikey = this . apiKey ;
2020-09-11 08:10:15 +03:00
try {
2020-10-23 02:46:52 +03:00
return await get ( url , postData ) ;
2020-09-11 08:10:15 +03:00
} catch ( error ) {
2020-09-16 09:19:28 +03:00
return checkError ( "estimateGas" , error , params . transaction ) ;
2020-09-11 08:10:15 +03:00
}
2019-05-15 01:25:46 +03:00
}
2019-11-01 17:51:08 +03:00
case "getLogs" : {
2020-10-23 02:46:52 +03:00
url += "?module=logs&action=getLogs" ;
2019-05-15 01:25:46 +03:00
2019-11-01 17:51:08 +03:00
if ( params . filter . fromBlock ) {
url += "&fromBlock=" + checkLogTag ( params . filter . fromBlock ) ;
}
2019-05-15 01:25:46 +03:00
2019-11-01 17:51:08 +03:00
if ( params . filter . toBlock ) {
url += "&toBlock=" + checkLogTag ( params . filter . toBlock ) ;
}
if ( params . filter . address ) {
url += "&address=" + params . filter . address ;
}
// @TODO: We can handle slightly more complicated logs using the logs API
if ( params . filter . topics && params . filter . topics . length > 0 ) {
if ( params . filter . topics . length > 1 ) {
logger . throwError ( "unsupported topic count" , Logger . errors . UNSUPPORTED_OPERATION , { topics : params.filter.topics } ) ;
2019-05-15 01:25:46 +03:00
}
2019-11-01 17:51:08 +03:00
if ( params . filter . topics . length === 1 ) {
const topic0 = params . filter . topics [ 0 ] ;
2019-05-15 01:25:46 +03:00
if ( typeof ( topic0 ) !== "string" || topic0 . length !== 66 ) {
2019-11-01 17:51:08 +03:00
logger . throwError ( "unsupported topic format" , Logger . errors . UNSUPPORTED_OPERATION , { topic0 : topic0 } ) ;
2019-05-15 01:25:46 +03:00
}
url += "&topic0=" + topic0 ;
}
}
url += apiKey ;
2020-10-23 02:46:52 +03:00
const logs : Array < any > = await get ( url , null , getResult ) ;
2019-11-01 17:51:08 +03:00
// Cache txHash => blockHash
2020-10-23 04:03:51 +03:00
let blocks : { [ tag : string ] : string } = { } ;
2019-11-01 17:51:08 +03:00
// Add any missing blockHash to the logs
for ( let i = 0 ; i < logs . length ; i ++ ) {
const log = logs [ i ] ;
if ( log . blockHash != null ) { continue ; }
2020-10-23 04:03:51 +03:00
if ( blocks [ log . blockNumber ] == null ) {
const block = await this . getBlock ( log . blockNumber ) ;
if ( block ) {
blocks [ log . blockNumber ] = block . hash ;
2019-11-01 17:51:08 +03:00
}
}
2020-10-23 04:03:51 +03:00
log . blockHash = blocks [ log . blockNumber ] ;
2019-11-01 17:51:08 +03:00
}
return logs ;
}
2019-05-15 01:25:46 +03:00
case "getEtherPrice" :
2019-11-01 17:51:08 +03:00
if ( this . network . name !== "homestead" ) { return 0.0 ; }
2020-10-23 02:46:52 +03:00
url += "?module=stats&action=ethprice" ;
2019-05-15 01:25:46 +03:00
url += apiKey ;
2020-10-23 02:46:52 +03:00
return parseFloat ( ( await get ( url , null , getResult ) ) . ethusd ) ;
2019-05-15 01:25:46 +03:00
default :
break ;
}
return super . perform ( method , params ) ;
}
// @TODO: Allow startBlock and endBlock to be Promises
getHistory ( addressOrName : string | Promise < string > , startBlock? : BlockTag , endBlock? : BlockTag ) : Promise < Array < TransactionResponse > > {
let url = this . baseUrl ;
let apiKey = "" ;
if ( this . apiKey ) { apiKey += "&apikey=" + this . apiKey ; }
if ( startBlock == null ) { startBlock = 0 ; }
if ( endBlock == null ) { endBlock = 99999999 ; }
return this . resolveName ( addressOrName ) . then ( ( address ) = > {
url += "/api?module=account&action=txlist&address=" + address ;
url += "&startblock=" + startBlock ;
url += "&endblock=" + endBlock ;
url += "&sort=asc" + apiKey ;
2019-08-01 23:13:35 +03:00
this . emit ( "debug" , {
action : "request" ,
request : url ,
provider : this
} ) ;
2020-07-14 09:26:45 +03:00
const connection = {
url : url ,
2020-07-16 12:29:33 +03:00
throttleSlotInterval : 1000 ,
2020-07-14 09:26:45 +03:00
throttleCallback : ( attempt : number , url : string ) = > {
if ( this . apiKey === defaultApiKey ) {
showThrottleMessage ( ) ;
}
return Promise . resolve ( true ) ;
}
}
return fetchJson ( connection , null , getResult ) . then ( ( result : Array < any > ) = > {
2019-05-15 01:25:46 +03:00
this . emit ( "debug" , {
2019-08-01 23:13:35 +03:00
action : "response" ,
2019-05-15 01:25:46 +03:00
request : url ,
2019-08-01 23:13:35 +03:00
response : deepCopy ( result ) ,
2019-05-15 01:25:46 +03:00
provider : this
} ) ;
2019-08-01 23:13:35 +03:00
2019-05-15 01:25:46 +03:00
let output : Array < TransactionResponse > = [ ] ;
result . forEach ( ( tx ) = > {
[ "contractAddress" , "to" ] . forEach ( function ( key ) {
if ( tx [ key ] == "" ) { delete tx [ key ] ; }
} ) ;
if ( tx . creates == null && tx . contractAddress != null ) {
tx . creates = tx . contractAddress ;
}
let item = this . formatter . transactionResponse ( tx ) ;
if ( tx . timeStamp ) { item . timestamp = parseInt ( tx . timeStamp ) ; }
output . push ( item ) ;
} ) ;
return output ;
} ) ;
} ) ;
}
2020-10-08 00:28:18 +03:00
isCommunityResource ( ) : boolean {
return ( this . apiKey === defaultApiKey ) ;
}
2019-05-15 01:25:46 +03:00
}