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
parent f9934675b2
commit 6560639a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 54 deletions

View File

@ -10,37 +10,23 @@
## Run
* Run the following command to generate a flattened contract file from a contract file:
```bash
yarn codegen:flatten <input-file-path> [output-dir]
```
* `input-file-path`: Input contract file path (absolute) (required). Note: Currently, relative path doesn't work.
* `output-dir`: Directory to store the flattened contract output file (default: `./out`).
Example:
```bash
yarn codegen:flatten ~/watcher-ts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol ./flattened
```
This will generate file `ERC20_flat.sol` in `./flattened`.
* Run the following command to generate schema from a contract file:
```bash
yarn codegen:gql --input-file <input-file-path> --output-file [output-file-path] --mode [eth_call | storage]
yarn codegen:gql --input-file <input-file-path> --output-file [output-file-path] --mode [eth_call | storage] --flatten [true | false]
```
* `input-file`: Input contract (must be a flattened contract) file path or an URL (required).
* `output-file`: Schema output file path (logs output using `stdout` if not provided).
* `mode`: Contract variables access mode (default: `storage`).
* `input-file`(alias: `i`): Input contract file path or an URL (required).
* `output-file`(alias: `o`): Schema output file path (logs output using `stdout` if not provided).
* `mode`(alias: `m`): Code generation mode (default: `storage`).
* `flatten`(alias: `f`): Flatten the input contract file (default: `true`).
**Note**: When passed an *URL* as `input-file`, it is assumed that it points to an already flattened contract file.
Examples:
```bash
yarn codegen:gql --input-file ./test/examples/contracts/ERC20-flat.sol --output-file ./ERC20-schema.gql --mode eth_call
yarn codegen:gql --input-file ./test/examples/contracts/ERC20.sol --output-file ./ERC20-schema.gql --mode eth_call
```
```bash
@ -55,21 +41,13 @@
yarn
```
* Flatten a contract file:
```bash
# Note: Currently, relative path for input-file-path doesn't work. Use absolute path.
yarn codegen:flatten ~/watcher-ts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol ./flattened
```
* Generate schema from the flattened contract file:
* Generate schema from a contract file:
```bash
yarn codegen:gql --input-file ./flattened/ERC20_flat.sol --output-file ./ERC20-schema.gql --mode storage
yarn codegen:gql --input-file ../../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol --output-file ./ERC20-schema.gql --mode storage
```
* Generate schema from the flattened contract file from an URL:
* Generate schema from a flattened contract file from an URL:
```bash
yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode eth_call
@ -81,3 +59,7 @@
* [ERC20 schema generation (storage mode).](https://git.io/JuhNr)
* [ERC721 schema generation (eth_call mode).](https://git.io/JuhNK)
* [ERC721 schema generation (storage mode).](https://git.io/JuhN1)
## Known Issues
* Currently, `node-fetch v2.6.2` is being used to fetch from URLs as `v3.0.0` is an [ESM-only module](https://www.npmjs.com/package/node-fetch#loading-and-configuring-the-module) and `ts-node` transpiles to import it using `require`.

View File

@ -6,8 +6,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint .",
"codegen:gql": "ts-node src/generate-schema.ts",
"codegen:flatten": "poa-solidity-flattener"
"codegen:gql": "ts-node src/generate-schema.ts"
},
"repository": {
"type": "git",
@ -20,7 +19,7 @@
},
"homepage": "https://github.com/vulcanize/watcher-ts#readme",
"dependencies": {
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git#pm-make-compatible",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
"@solidity-parser/parser": "^0.13.2",
"graphql": "^15.5.0",
"graphql-compose": "^9.0.3",

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);
};

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
});
}

View File

@ -0,0 +1,5 @@
//
// Copyright 2021 Vulcanize, Inc.
//
declare module '@poanet/solidity-flattener';

View File

@ -0,0 +1,6 @@
{
"name": "common",
"version": "0.1.0",
"license": "AGPL-3.0",
"typings": "main.d.ts"
}

View File

@ -48,7 +48,9 @@
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
"typeRoots": [
"./src/types"
], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

View File

@ -2018,9 +2018,9 @@
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.2.tgz#ff80affd6d352dbe1bbc5b4e1833c41afd6283b6"
integrity sha512-AybF1cesONZStg5kWf6ao9OlqTZuPqddvprc0ky7lrUVOjXeKpmQ2Y9FK+6ygxasb+4aic4O5pneFBfwVsRRRg==
"@poanet/solidity-flattener@https://github.com/vulcanize/solidity-flattener.git#pm-make-compatible":
"@poanet/solidity-flattener@https://github.com/vulcanize/solidity-flattener.git":
version "3.0.6"
resolved "https://github.com/vulcanize/solidity-flattener.git#2fddc0eb439b558861e16288f970b24dab7df245"
resolved "https://github.com/vulcanize/solidity-flattener.git#397f7295acff78eb039e479a85e8c3a9d78a96dd"
dependencies:
bunyan "^1.8.12"
decomment "^0.9.1"