11const 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' ) ;
26
37const tsCodegen = require ( '../../../codegen/typescript' )
48const typesCodegen = require ( '../../../codegen/types' )
59const util = require ( '../../../codegen/util' )
610
11+ const doFixtureCodegen = fs . existsSync ( './fixtures.yaml' ) ;
12+
713module . exports = class AbiCodeGenerator {
814 constructor ( abi ) {
915 this . abi = abi
1016 }
1117
1218 generateModuleImports ( ) {
13- return [
19+ let imports = [
1420 tsCodegen . moduleImports (
1521 [
1622 // Ethereum integration
@@ -27,8 +33,21 @@ module.exports = class AbiCodeGenerator {
2733 'BigInt' ,
2834 ] ,
2935 '@graphprotocol/graph-ts' ,
30- ) ,
36+ )
3137 ]
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
3251 }
3352
3453 generateTypes ( ) {
@@ -182,6 +201,7 @@ module.exports = class AbiCodeGenerator {
182201 setName : ( input , name ) => input . set ( 'name' , name ) ,
183202 } )
184203
204+ let namesAndTypes = [ ]
185205 inputs . forEach ( ( input , index ) => {
186206 // Generate getters and classes for event params
187207 let paramObject = this . _generateInputOrOutput (
@@ -192,6 +212,11 @@ module.exports = class AbiCodeGenerator {
192212 `parameters` ,
193213 )
194214 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 } )
195220 tupleClasses . push ( ...paramObject . classes )
196221 } )
197222
@@ -208,6 +233,51 @@ module.exports = class AbiCodeGenerator {
208233 `return new ${ paramsClassName } (this)` ,
209234 ) ,
210235 )
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+
211281 return [ klass , paramsClass , ...tupleClasses ]
212282 } )
213283
0 commit comments