@@ -18,6 +18,8 @@ import EthereumABI from '../protocols/ethereum/abi.js';
1818import Protocol from '../protocols/index.js' ;
1919import Subgraph from '../subgraph.js' ;
2020
21+ const DEFAULT_CONTRACT_NAME = 'Contract' ;
22+
2123export default class AddCommand extends Command {
2224 static description = 'Adds a new datasource to a subgraph.' ;
2325
@@ -36,14 +38,14 @@ export default class AddCommand extends Command {
3638 char : 'h' ,
3739 } ) ,
3840 abi : Flags . string ( {
39- summary : 'Path to the contract ABI.' ,
41+ summary : 'Path to the contract ABI. If not provided, will be fetched from contract API. ' ,
4042 } ) ,
4143 'start-block' : Flags . string ( {
42- summary : 'The block number to start indexing events from.' ,
44+ summary :
45+ 'The block number to start indexing events from. If not provided, will be fetched from contract API' ,
4346 } ) ,
4447 'contract-name' : Flags . string ( {
45- summary : 'Name of the contract.' ,
46- default : 'Contract' ,
48+ summary : 'Name of the contract. If not provided, will be fetched from contract API' ,
4749 } ) ,
4850 'merge-entities' : Flags . boolean ( {
4951 summary : 'Whether to merge entities with the same name.' ,
@@ -80,7 +82,7 @@ export default class AddCommand extends Command {
8082 const contractService = new ContractService ( registry ) ;
8183
8284 let startBlock = startBlockFlag ? parseInt ( startBlockFlag ) . toString ( ) : startBlockFlag ;
83- let contractName = contractNameFlag ;
85+ let contractName = contractNameFlag || DEFAULT_CONTRACT_NAME ;
8486
8587 let ethabi = null ;
8688 if ( abi ) {
@@ -94,43 +96,36 @@ export default class AddCommand extends Command {
9496 'Fetching ABI from contract API...' ,
9597 'Failed to fetch ABI' ,
9698 'Warning fetching ABI' ,
97- ( ) => contractService ? .getABI ( EthereumABI , network , address ) ,
99+ ( ) => contractService . getABI ( EthereumABI , network , address ) ,
98100 ) ,
99101 ) ;
102+ if ( ! ethabi ) throw Error ;
100103 } catch ( error ) {
101104 // we cannot ask user to do prompt in test environment
102105 if ( process . env . NODE_ENV !== 'test' ) {
103- const { abi : abiFromFile } = await prompt . ask < { abi : EthereumABI } > ( [
106+ const { abi : abiFile } = await prompt . ask < { abi : string } > ( [
104107 {
105108 type : 'input' ,
106109 name : 'abi' ,
107110 message : 'ABI file (path)' ,
108- initial : ethabi ,
109111 validate : async ( value : string ) => {
110112 try {
111113 EthereumABI . load ( contractName , value ) ;
112114 return true ;
113115 } catch ( e ) {
114- this . error ( e . message ) ;
115- }
116- } ,
117- result : async ( value : string ) => {
118- try {
119- return EthereumABI . load ( contractName , value ) ;
120- } catch ( e ) {
121- return e . message ;
116+ return `Failed to load ABI from ${ value } : ${ e . message } ` ;
122117 }
123118 } ,
124119 } ,
125120 ] ) ;
126- ethabi = abiFromFile ;
121+ ethabi = EthereumABI . load ( contractName , abiFile ) ;
127122 }
128123 }
129124 }
130125
131126 try {
132127 if ( isLocalHost ) throw Error ; // Triggers user prompting without waiting for Etherscan lookup to fail
133- startBlock ||= Number ( await contractService ? .getStartBlock ( network , address ) ) . toString ( ) ;
128+ startBlock ||= Number ( await contractService . getStartBlock ( network , address ) ) . toString ( ) ;
134129 } catch ( error ) {
135130 // we cannot ask user to do prompt in test environment
136131 if ( process . env . NODE_ENV !== 'test' ) {
@@ -153,7 +148,10 @@ export default class AddCommand extends Command {
153148
154149 try {
155150 if ( isLocalHost ) throw Error ; // Triggers user prompting without waiting for Etherscan lookup to fail
156- contractName = ( await contractService ?. getContractName ( network , address ) ) ?? '' ;
151+ if ( contractName === DEFAULT_CONTRACT_NAME ) {
152+ contractName =
153+ ( await contractService . getContractName ( network , address ) ) ?? DEFAULT_CONTRACT_NAME ;
154+ }
157155 } catch ( error ) {
158156 // not asking user to do prompt in test environment
159157 if ( process . env . NODE_ENV !== 'test' ) {
0 commit comments