Use flatten function and modify ResultEvent in schema. (#247)

Co-authored-by: prathamesh <prathamesh.musale0@gmail.com>
This commit is contained in:
Ashwin Phatak
2021-09-17 17:10:08 +05:30
committed by GitHub
co-authored by prathamesh
parent f9934675b2
commit 6560639a68
8 changed files with 80 additions and 54 deletions
+16 -2
View File
@@ -7,6 +7,7 @@ import fetch from 'node-fetch';
import path from 'path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { flatten } from '@poanet/solidity-flattener';
import { parse, visit } from '@solidity-parser/parser';
@@ -30,20 +31,31 @@ const main = async (): Promise<void> => {
})
.option('mode', {
alias: 'm',
describe: 'Code generation mode.',
type: 'string',
default: MODE_STORAGE,
choices: [MODE_ETH_CALL, MODE_STORAGE]
})
.option('flatten', {
alias: 'f',
describe: 'Flatten the input contract file.',
type: 'boolean',
default: true
})
.argv;
let data: string;
if (argv['input-file'].startsWith('http')) {
// Assume flattened file in case of URL.
const response = await fetch(argv['input-file']);
data = await response.text();
} else {
data = readFileSync(path.resolve(argv['input-file'])).toString();
data = argv.flatten
? await flatten(path.resolve(argv['input-file']))
: readFileSync(path.resolve(argv['input-file'])).toString();
}
// Get the abstract syntax tree for the flattened contract.
const ast = parse(data);
// Filter out library nodes.
@@ -63,7 +75,9 @@ const main = async (): Promise<void> => {
});
}
const outStream = argv['output-file'] ? createWriteStream(path.resolve(argv['output-file'])) : process.stdout;
const outStream = argv['output-file']
? createWriteStream(path.resolve(argv['output-file']))
: process.stdout;
visitor.exportSchema(outStream);
};
+31 -13
View File
@@ -166,27 +166,45 @@ export class Schema {
* Adds types 'ResultEvent' and 'WatchedEvent' to the schema.
*/
_addEventsRelatedTypes (): void {
// Create Ethereum types.
// Create the Block type.
const blockName = 'Block';
this._composer.createObjectTC({
name: blockName,
fields: {
hash: 'String!',
number: 'Int!',
timestamp: 'Int!',
parentHash: 'String!'
}
});
// Create the Transaction type.
const transactionName = 'Transaction';
this._composer.createObjectTC({
name: transactionName,
fields: {
hash: 'String!',
index: 'Int!',
from: 'String!',
to: 'String!'
}
});
// Create the ResultEvent type.
const resultEventName = 'ResultEvent';
this._composer.createObjectTC({
name: resultEventName,
fields: {
// Get type composer object for Event union from the schema composer.
// Get type composer object for 'blockName' type from the schema composer.
block: () => this._composer.getOTC(blockName).NonNull,
tx: () => this._composer.getOTC(transactionName).NonNull,
contract: 'String!',
eventIndex: 'Int!',
event: () => this._composer.getUTC('Event').NonNull,
proof: () => this._composer.getOTC('Proof')
}
});
// Create the WatchedEvent type.
const watchedEventName = 'WatchedEvent';
this._composer.createObjectTC({
name: watchedEventName,
fields: {
blockHash: 'String!',
contractAddress: 'String!',
event: () => this._composer.getOTC(resultEventName).NonNull
}
});
}
/**
@@ -211,7 +229,7 @@ export class Schema {
_addEventSubscription (): void {
// Add a subscription to the schema composer.
this._composer.Subscription.addFields({
onEvent: () => this._composer.getOTC('WatchedEvent').NonNull
onEvent: () => this._composer.getOTC('ResultEvent').NonNull
});
}
+5
View File
@@ -0,0 +1,5 @@
//
// Copyright 2021 Vulcanize, Inc.
//
declare module '@poanet/solidity-flattener';
@@ -0,0 +1,6 @@
{
"name": "common",
"version": "0.1.0",
"license": "AGPL-3.0",
"typings": "main.d.ts"
}