@@ -3,7 +3,6 @@ import { NetworksRegistry } from '@pinax/graph-networks-registry';
33import debugFactory from '../debug.js' ;
44import fetch from '../fetch.js' ;
55import ABI from '../protocols/ethereum/abi.js' ;
6- import { withSpinner } from './spinner.js' ;
76
87const logger = debugFactory ( 'graph-cli:contract-service' ) ;
98
@@ -72,108 +71,84 @@ export class ContractService {
7271
7372 async getABI ( ABICtor : typeof ABI , networkId : string , address : string ) {
7473 const urls = this . getEtherscanUrls ( networkId ) ;
75-
7674 let errors : string [ ] = [ ] ;
77- return await withSpinner (
78- `Fetching ABI from contract API` ,
79- `Failed to fetch ABI from contract API` ,
80- `Warnings while fetching ABI from contract API` ,
81- async ( ) => {
82- if ( ! urls . length ) {
83- throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
84- }
85- for ( const url of urls ) {
86- try {
87- const json = await this . fetchFromEtherscan (
88- `${ url } ?module=contract&action=getabi&address=${ address } ` ,
89- ) ;
90-
91- if ( json ) {
92- return new ABICtor ( 'Contract' , undefined , immutable . fromJS ( JSON . parse ( json . result ) ) ) ;
93- }
94- throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
95- } catch ( error ) {
96- logger ( `Failed to fetch from ${ url } : ${ error } ` ) ;
97- errors . push ( `${ error } ` ) ;
98- }
75+ if ( ! urls . length ) {
76+ throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
77+ }
78+ for ( const url of urls ) {
79+ try {
80+ const json = await this . fetchFromEtherscan (
81+ `${ url } ?module=contract&action=getabi&address=${ address } ` ,
82+ ) ;
83+
84+ if ( json ) {
85+ return new ABICtor ( 'Contract' , undefined , immutable . fromJS ( JSON . parse ( json . result ) ) ) ;
9986 }
87+ throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
88+ } catch ( error ) {
89+ logger ( `Failed to fetch from ${ url } : ${ error } ` ) ;
90+ errors . push ( `${ error } ` ) ;
91+ }
92+ }
10093
101- throw new Error ( errors ?. [ 0 ] ) ;
102- } ,
103- ) ;
94+ throw new Error ( errors ?. [ 0 ] ) ;
10495 }
10596
10697 async getStartBlock ( networkId : string , address : string ) : Promise < string > {
10798 const urls = this . getEtherscanUrls ( networkId ) ;
99+ if ( ! urls . length ) {
100+ throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
101+ }
102+ for ( const url of urls ) {
103+ try {
104+ const json = await this . fetchFromEtherscan (
105+ `${ url } ?module=contract&action=getcontractcreation&contractaddresses=${ address } ` ,
106+ ) ;
108107
109- return await withSpinner (
110- `Fetching start block` ,
111- `Failed to fetch start block` ,
112- `Warnings while fetching deploy contract transaction from contract API` ,
113- async ( ) => {
114- if ( ! urls . length ) {
115- throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
116- }
117- for ( const url of urls ) {
118- try {
119- const json = await this . fetchFromEtherscan (
120- `${ url } ?module=contract&action=getcontractcreation&contractaddresses=${ address } ` ,
121- ) ;
122-
123- if ( json ?. result ?. length ) {
124- if ( json . result [ 0 ] ?. blockNumber ) {
125- return json . result [ 0 ] . blockNumber ;
126- }
127- const txHash = json . result [ 0 ] . txHash ;
128- const tx = await this . fetchTransactionByHash ( networkId , txHash ) ;
129- if ( ! tx ?. blockNumber ) {
130- throw new Error ( `no blockNumber: ${ JSON . stringify ( tx ) } ` ) ;
131- }
132- return Number ( tx . blockNumber ) . toString ( ) ;
133- }
134- throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
135- } catch ( error ) {
136- logger ( `Failed to fetch start block from ${ url } : ${ error } ` ) ;
108+ if ( json ?. result ?. length ) {
109+ if ( json . result [ 0 ] ?. blockNumber ) {
110+ return json . result [ 0 ] . blockNumber ;
111+ }
112+ const txHash = json . result [ 0 ] . txHash ;
113+ const tx = await this . fetchTransactionByHash ( networkId , txHash ) ;
114+ if ( ! tx ?. blockNumber ) {
115+ throw new Error ( `no blockNumber: ${ JSON . stringify ( tx ) } ` ) ;
137116 }
117+ return Number ( tx . blockNumber ) . toString ( ) ;
138118 }
119+ throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
120+ } catch ( error ) {
121+ logger ( `Failed to fetch start block from ${ url } : ${ error } ` ) ;
122+ }
123+ }
139124
140- throw new Error ( `Failed to fetch deploy contract transaction for ${ address } ` ) ;
141- } ,
142- ) ;
125+ throw new Error ( `Failed to fetch deploy contract transaction for ${ address } ` ) ;
143126 }
144127
145128 async getContractName ( networkId : string , address : string ) : Promise < string > {
146129 const urls = this . getEtherscanUrls ( networkId ) ;
147-
148- return await withSpinner (
149- `Fetching contract name` ,
150- `Failed to fetch contract name` ,
151- `Warnings while fetching contract name from contract API` ,
152- async ( ) => {
153- if ( ! urls . length ) {
154- throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
155- }
156- for ( const url of urls ) {
157- try {
158- const json = await this . fetchFromEtherscan (
159- `${ url } ?module=contract&action=getsourcecode&address=${ address } ` ,
160- ) ;
161-
162- if ( json ) {
163- const { ContractName } = json . result [ 0 ] ;
164- if ( ContractName !== '' ) {
165- return ContractName ;
166- }
167- }
168- throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
169- } catch ( error ) {
170- logger ( `Failed to fetch from ${ url } : ${ error } ` ) ;
130+ if ( ! urls . length ) {
131+ throw new Error ( `No contract API available for ${ networkId } in the registry` ) ;
132+ }
133+ for ( const url of urls ) {
134+ try {
135+ const json = await this . fetchFromEtherscan (
136+ `${ url } ?module=contract&action=getsourcecode&address=${ address } ` ,
137+ ) ;
138+
139+ if ( json ) {
140+ const { ContractName } = json . result [ 0 ] ;
141+ if ( ContractName !== '' ) {
142+ return ContractName ;
171143 }
172144 }
145+ throw new Error ( `no result: ${ JSON . stringify ( json ) } ` ) ;
146+ } catch ( error ) {
147+ logger ( `Failed to fetch from ${ url } : ${ error } ` ) ;
148+ }
149+ }
173150
174- throw new Error ( `Failed to fetch contract name for ${ address } ` ) ;
175- } ,
176- ) ;
151+ throw new Error ( `Failed to fetch contract name for ${ address } ` ) ;
177152 }
178153
179154 private async fetchTransactionByHash ( networkId : string , txHash : string ) {
0 commit comments