@@ -18,6 +18,8 @@ import EthereumABI from '../protocols/ethereum/abi.js';
18
18
import Protocol from '../protocols/index.js' ;
19
19
import Subgraph from '../subgraph.js' ;
20
20
21
+ const DEFAULT_CONTRACT_NAME = 'Contract' ;
22
+
21
23
export default class AddCommand extends Command {
22
24
static description = 'Adds a new datasource to a subgraph.' ;
23
25
@@ -36,14 +38,14 @@ export default class AddCommand extends Command {
36
38
char : 'h' ,
37
39
} ) ,
38
40
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. ' ,
40
42
} ) ,
41
43
'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' ,
43
46
} ) ,
44
47
'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' ,
47
49
} ) ,
48
50
'merge-entities' : Flags . boolean ( {
49
51
summary : 'Whether to merge entities with the same name.' ,
@@ -79,17 +81,8 @@ export default class AddCommand extends Command {
79
81
const registry = await loadRegistry ( ) ;
80
82
const contractService = new ContractService ( registry ) ;
81
83
82
- let startBlock = startBlockFlag ;
83
- let contractName = contractNameFlag ;
84
-
85
- const entities = getEntities ( manifest ) ;
86
- const contractNames = getContractNames ( manifest ) ;
87
- if ( contractNames . includes ( contractName ) ) {
88
- this . error (
89
- `Datasource or template with name ${ contractName } already exists, please choose a different name.` ,
90
- { exit : 1 } ,
91
- ) ;
92
- }
84
+ let startBlock = startBlockFlag ? parseInt ( startBlockFlag ) . toString ( ) : startBlockFlag ;
85
+ let contractName = contractNameFlag || DEFAULT_CONTRACT_NAME ;
93
86
94
87
let ethabi = null ;
95
88
if ( abi ) {
@@ -103,44 +96,36 @@ export default class AddCommand extends Command {
103
96
'Fetching ABI from contract API...' ,
104
97
'Failed to fetch ABI' ,
105
98
'Warning fetching ABI' ,
106
- ( ) => contractService ? .getABI ( EthereumABI , network , address ) ,
99
+ ( ) => contractService . getABI ( EthereumABI , network , address ) ,
107
100
) ,
108
101
) ;
102
+ if ( ! ethabi ) throw Error ;
109
103
} catch ( error ) {
110
104
// we cannot ask user to do prompt in test environment
111
105
if ( process . env . NODE_ENV !== 'test' ) {
112
- const { abi : abiFromFile } = await prompt . ask < { abi : EthereumABI } > ( [
106
+ const { abi : abiFile } = await prompt . ask < { abi : string } > ( [
113
107
{
114
108
type : 'input' ,
115
109
name : 'abi' ,
116
110
message : 'ABI file (path)' ,
117
- initial : ethabi ,
118
111
validate : async ( value : string ) => {
119
112
try {
120
113
EthereumABI . load ( contractName , value ) ;
121
114
return true ;
122
115
} catch ( e ) {
123
- this . error ( e . message ) ;
124
- }
125
- } ,
126
- result : async ( value : string ) => {
127
- try {
128
- return EthereumABI . load ( contractName , value ) ;
129
- } catch ( e ) {
130
- return e . message ;
116
+ return `Failed to load ABI from ${ value } : ${ e . message } ` ;
131
117
}
132
118
} ,
133
119
} ,
134
120
] ) ;
135
- ethabi = abiFromFile ;
121
+ ethabi = EthereumABI . load ( contractName , abiFile ) ;
136
122
}
137
123
}
138
124
}
139
125
140
126
try {
141
127
if ( isLocalHost ) throw Error ; // Triggers user prompting without waiting for Etherscan lookup to fail
142
-
143
- startBlock ||= Number ( await contractService ?. getStartBlock ( network , address ) ) . toString ( ) ;
128
+ startBlock ||= Number ( await contractService . getStartBlock ( network , address ) ) . toString ( ) ;
144
129
} catch ( error ) {
145
130
// we cannot ask user to do prompt in test environment
146
131
if ( process . env . NODE_ENV !== 'test' ) {
@@ -163,8 +148,10 @@ export default class AddCommand extends Command {
163
148
164
149
try {
165
150
if ( isLocalHost ) throw Error ; // Triggers user prompting without waiting for Etherscan lookup to fail
166
-
167
- contractName = ( await contractService ?. getContractName ( network , address ) ) ?? '' ;
151
+ if ( contractName === DEFAULT_CONTRACT_NAME ) {
152
+ contractName =
153
+ ( await contractService . getContractName ( network , address ) ) ?? DEFAULT_CONTRACT_NAME ;
154
+ }
168
155
} catch ( error ) {
169
156
// not asking user to do prompt in test environment
170
157
if ( process . env . NODE_ENV !== 'test' ) {
@@ -184,6 +171,15 @@ export default class AddCommand extends Command {
184
171
}
185
172
}
186
173
174
+ const entities = getEntities ( manifest ) ;
175
+ const contractNames = getContractNames ( manifest ) ;
176
+ if ( contractNames . includes ( contractName ) ) {
177
+ this . error (
178
+ `Datasource or template with name ${ contractName } already exists, please choose a different name.` ,
179
+ { exit : 1 } ,
180
+ ) ;
181
+ }
182
+
187
183
await writeABI ( ethabi , contractName ) ;
188
184
189
185
const { collisionEntities, onlyCollisions, abiData } = updateEventNamesOnCollision (
@@ -246,9 +242,14 @@ export default class AddCommand extends Command {
246
242
} ) ;
247
243
}
248
244
249
- await withSpinner ( 'Running codegen' , 'Failed to run codegen' , 'Warning during codegen' , ( ) =>
250
- system . run ( yarn ? 'yarn codegen' : 'npm run codegen' ) ,
245
+ await withSpinner (
246
+ 'Running codegen' ,
247
+ 'Failed to run codegen' ,
248
+ 'Warning during codegen' ,
249
+ async ( ) => await system . run ( yarn ? 'yarn codegen' : 'npm run codegen' ) ,
251
250
) ;
251
+
252
+ this . exit ( 0 ) ;
252
253
}
253
254
}
254
255
0 commit comments