1
1
const immutable = require ( 'immutable' )
2
+ const fs = require ( 'fs' )
3
+ const yaml = require ( 'yaml' )
4
+ const request = require ( 'sync-request' )
5
+ const Web3EthAbi = require ( 'web3-eth-abi' ) ;
2
6
3
7
const tsCodegen = require ( '../../../codegen/typescript' )
4
8
const typesCodegen = require ( '../../../codegen/types' )
5
9
const util = require ( '../../../codegen/util' )
6
10
11
+ const doFixtureCodegen = fs . existsSync ( './fixtures.yaml' ) ;
12
+
7
13
module . exports = class AbiCodeGenerator {
8
14
constructor ( abi ) {
9
15
this . abi = abi
10
16
}
11
17
12
18
generateModuleImports ( ) {
13
- return [
19
+ let imports = [
14
20
tsCodegen . moduleImports (
15
21
[
16
22
// Ethereum integration
@@ -27,8 +33,21 @@ module.exports = class AbiCodeGenerator {
27
33
'BigInt' ,
28
34
] ,
29
35
'@graphprotocol/graph-ts' ,
30
- ) ,
36
+ )
31
37
]
38
+
39
+ if ( doFixtureCodegen ) {
40
+ imports . push (
41
+ tsCodegen . moduleImports (
42
+ [
43
+ 'newMockEvent' ,
44
+ ] ,
45
+ 'matchstick-as/assembly/index' ,
46
+ )
47
+ )
48
+ }
49
+
50
+ return imports
32
51
}
33
52
34
53
generateTypes ( ) {
@@ -182,6 +201,7 @@ module.exports = class AbiCodeGenerator {
182
201
setName : ( input , name ) => input . set ( 'name' , name ) ,
183
202
} )
184
203
204
+ let namesAndTypes = [ ]
185
205
inputs . forEach ( ( input , index ) => {
186
206
// Generate getters and classes for event params
187
207
let paramObject = this . _generateInputOrOutput (
@@ -192,6 +212,11 @@ module.exports = class AbiCodeGenerator {
192
212
`parameters` ,
193
213
)
194
214
paramsClass . addMethod ( paramObject . getter )
215
+ let ethType = typesCodegen . ethereumTypeForAsc ( paramObject . getter . returnType )
216
+ if ( typeof ethType === typeof { } && ( ethType . test ( "int256" ) || ethType . test ( "uint256" ) ) ) {
217
+ ethType = "int32"
218
+ }
219
+ namesAndTypes . push ( { name : paramObject . getter . name . slice ( 4 ) , type : ethType } )
195
220
tupleClasses . push ( ...paramObject . classes )
196
221
} )
197
222
@@ -208,6 +233,51 @@ module.exports = class AbiCodeGenerator {
208
233
`return new ${ paramsClassName } (this)` ,
209
234
) ,
210
235
)
236
+
237
+ // Fixture generation
238
+ if ( doFixtureCodegen ) {
239
+ const args = yaml . parse ( fs . readFileSync ( './fixtures.yaml' , 'utf8' ) )
240
+ const blockNumber = args [ 'blockNumber' ]
241
+ const contractAddr = args [ 'contractAddr' ]
242
+ const topic0 = args [ 'topic0' ]
243
+ const apiKey = args [ 'apiKey' ]
244
+ const url = `https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=${ blockNumber } &toBlock=${ blockNumber } &address=${ contractAddr } &${ topic0 } =topic0&apikey=${ apiKey } ` ;
245
+
246
+ let resp = request ( "GET" , url )
247
+ let body = JSON . parse ( resp . getBody ( "utf8" ) )
248
+ if ( body . status === '0' ) {
249
+ throw new Error ( body . result )
250
+ }
251
+
252
+ let res = Web3EthAbi . decodeLog (
253
+ namesAndTypes ,
254
+ body . result [ 0 ] . data ,
255
+ [ ]
256
+ ) ;
257
+
258
+ let stmnts = ""
259
+ for ( let i = 0 ; i < namesAndTypes . length ; i ++ ) {
260
+ let code = '"' + res [ i ] + '"'
261
+ if ( namesAndTypes [ i ] . type . toString ( ) == "address" ) {
262
+ code = `Address.fromString(${ code } )`
263
+ }
264
+ stmnts = stmnts . concat ( `event.parameters.push(new ethereum.EventParam(\"${ namesAndTypes [ i ] . name } \", ${ typesCodegen . ethereumFromAsc ( code , namesAndTypes [ i ] . type ) } ));` , `\n` )
265
+ }
266
+
267
+ klass . addMethod (
268
+ tsCodegen . staticMethod (
269
+ `mock${ eventClassName } ` ,
270
+ [ ] ,
271
+ tsCodegen . namedType ( eventClassName ) ,
272
+ `
273
+ let event = changetype<${ eventClassName } >(newMockEvent());
274
+ ${ stmnts }
275
+ return event;
276
+ ` ,
277
+ )
278
+ )
279
+ }
280
+
211
281
return [ klass , paramsClass , ...tupleClasses ]
212
282
} )
213
283
0 commit comments