1
1
const immutable = require ( 'immutable' )
2
+ const fs = require ( 'fs' )
3
+ const yaml = require ( 'yaml' )
4
+ const request = require ( 'sync-request' )
5
+ const Web3 = require ( 'web3' )
6
+ const web3 = new Web3 ( null )
2
7
3
8
const tsCodegen = require ( '../../../codegen/typescript' )
4
9
const typesCodegen = require ( '../../../codegen/types' )
@@ -28,6 +33,12 @@ module.exports = class AbiCodeGenerator {
28
33
] ,
29
34
'@graphprotocol/graph-ts' ,
30
35
) ,
36
+ tsCodegen . moduleImports (
37
+ [
38
+ 'newMockEvent' ,
39
+ ] ,
40
+ 'matchstick-as/assembly/index' ,
41
+ ) ,
31
42
]
32
43
}
33
44
@@ -182,6 +193,7 @@ module.exports = class AbiCodeGenerator {
182
193
setName : ( input , name ) => input . set ( 'name' , name ) ,
183
194
} )
184
195
196
+ let namesAndTypes = [ ]
185
197
inputs . forEach ( ( input , index ) => {
186
198
// Generate getters and classes for event params
187
199
let paramObject = this . _generateInputOrOutput (
@@ -192,6 +204,11 @@ module.exports = class AbiCodeGenerator {
192
204
`parameters` ,
193
205
)
194
206
paramsClass . addMethod ( paramObject . getter )
207
+ let ethType = typesCodegen . ethereumTypeForAsc ( paramObject . getter . returnType )
208
+ if ( typeof ethType === typeof { } && ( ethType . test ( "int256" ) || ethType . test ( "uint256" ) ) ) {
209
+ ethType = "int32"
210
+ }
211
+ namesAndTypes . push ( { name : paramObject . getter . name . slice ( 4 ) , type : ethType } )
195
212
tupleClasses . push ( ...paramObject . classes )
196
213
} )
197
214
@@ -208,6 +225,54 @@ module.exports = class AbiCodeGenerator {
208
225
`return new ${ paramsClassName } (this)` ,
209
226
) ,
210
227
)
228
+
229
+ // Fixture generation
230
+ try {
231
+ const args = yaml . parse ( fs . readFileSync ( './fixtures.yaml' , 'utf8' ) )
232
+ const blockNumber = args [ 'blockNumber' ]
233
+ const contractAddr = args [ 'contractAddr' ]
234
+ const topic0 = args [ 'topic0' ]
235
+ const apiKey = args [ 'apiKey' ]
236
+ const url = `https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=${ blockNumber } &toBlock=${ blockNumber } &address=${ contractAddr } &${ topic0 } =topic0&apikey=${ apiKey } ` ;
237
+
238
+ let resp = request ( "GET" , url )
239
+ let body = JSON . parse ( resp . getBody ( "utf8" ) )
240
+ if ( body . status === '0' ) {
241
+ throw new Error ( body . result )
242
+ }
243
+
244
+ let res = web3 . eth . abi . decodeLog (
245
+ namesAndTypes ,
246
+ body . result [ 0 ] . data ,
247
+ [ ]
248
+ ) ;
249
+
250
+ let stmnts = ""
251
+ for ( let i = 0 ; i < namesAndTypes . length ; i ++ ) {
252
+ let code = '"' + res [ i ] + '"'
253
+ if ( namesAndTypes [ i ] . type . toString ( ) == "address" ) {
254
+ code = `Address.fromString(${ code } )`
255
+ }
256
+ stmnts = stmnts . concat ( `event.parameters.push(new ethereum.EventParam(\"${ namesAndTypes [ i ] . name } \", ${ typesCodegen . ethereumFromAsc ( code , namesAndTypes [ i ] . type ) } ));` , `\n` )
257
+ }
258
+
259
+ klass . addMethod (
260
+ tsCodegen . staticMethod (
261
+ `mock${ eventClassName } ` ,
262
+ [ ] ,
263
+ tsCodegen . namedType ( eventClassName ) ,
264
+ `
265
+ let event = changetype<${ eventClassName } >(newMockEvent());
266
+ ${ stmnts }
267
+ return event;
268
+ ` ,
269
+ )
270
+ )
271
+ } catch ( e ) {
272
+ // no fixtures
273
+ console . log ( e )
274
+ }
275
+
211
276
return [ klass , paramsClass , ...tupleClasses ]
212
277
} )
213
278
0 commit comments