mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-08-03 20:54:07 +00:00
Use flatten function and modify ResultEvent in schema. (#247)
Co-authored-by: prathamesh <prathamesh.musale0@gmail.com>
This commit is contained in:
parent
f9934675b2
commit
6560639a68
@ -10,37 +10,23 @@
|
|||||||
|
|
||||||
## Run
|
## 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:
|
* Run the following command to generate schema from a contract file:
|
||||||
|
|
||||||
```bash
|
```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).
|
* `input-file`(alias: `i`): Input contract file path or an URL (required).
|
||||||
* `output-file`: Schema output file path (logs output using `stdout` if not provided).
|
* `output-file`(alias: `o`): Schema output file path (logs output using `stdout` if not provided).
|
||||||
* `mode`: Contract variables access mode (default: `storage`).
|
* `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:
|
Examples:
|
||||||
|
|
||||||
```bash
|
```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
|
```bash
|
||||||
@ -55,21 +41,13 @@
|
|||||||
yarn
|
yarn
|
||||||
```
|
```
|
||||||
|
|
||||||
* Flatten a contract file:
|
* Generate schema from a contract file:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Note: Currently, relative path for input-file-path doesn't work. Use absolute path.
|
yarn codegen:gql --input-file ../../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol --output-file ./ERC20-schema.gql --mode storage
|
||||||
yarn codegen:flatten ~/watcher-ts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol ./flattened
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* Generate schema from a flattened contract file from an URL:
|
||||||
* Generate schema from the flattened contract file:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
yarn codegen:gql --input-file ./flattened/ERC20_flat.sol --output-file ./ERC20-schema.gql --mode storage
|
|
||||||
```
|
|
||||||
|
|
||||||
* Generate schema from the flattened contract file from an URL:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode eth_call
|
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)
|
* [ERC20 schema generation (storage mode).](https://git.io/JuhNr)
|
||||||
* [ERC721 schema generation (eth_call mode).](https://git.io/JuhNK)
|
* [ERC721 schema generation (eth_call mode).](https://git.io/JuhNK)
|
||||||
* [ERC721 schema generation (storage mode).](https://git.io/JuhN1)
|
* [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`.
|
||||||
|
@ -6,8 +6,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"codegen:gql": "ts-node src/generate-schema.ts",
|
"codegen:gql": "ts-node src/generate-schema.ts"
|
||||||
"codegen:flatten": "poa-solidity-flattener"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,7 +19,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/vulcanize/watcher-ts#readme",
|
"homepage": "https://github.com/vulcanize/watcher-ts#readme",
|
||||||
"dependencies": {
|
"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",
|
"@solidity-parser/parser": "^0.13.2",
|
||||||
"graphql": "^15.5.0",
|
"graphql": "^15.5.0",
|
||||||
"graphql-compose": "^9.0.3",
|
"graphql-compose": "^9.0.3",
|
||||||
|
@ -7,6 +7,7 @@ import fetch from 'node-fetch';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
import { hideBin } from 'yargs/helpers';
|
import { hideBin } from 'yargs/helpers';
|
||||||
|
import { flatten } from '@poanet/solidity-flattener';
|
||||||
|
|
||||||
import { parse, visit } from '@solidity-parser/parser';
|
import { parse, visit } from '@solidity-parser/parser';
|
||||||
|
|
||||||
@ -30,20 +31,31 @@ const main = async (): Promise<void> => {
|
|||||||
})
|
})
|
||||||
.option('mode', {
|
.option('mode', {
|
||||||
alias: 'm',
|
alias: 'm',
|
||||||
|
describe: 'Code generation mode.',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: MODE_STORAGE,
|
default: MODE_STORAGE,
|
||||||
choices: [MODE_ETH_CALL, MODE_STORAGE]
|
choices: [MODE_ETH_CALL, MODE_STORAGE]
|
||||||
})
|
})
|
||||||
|
.option('flatten', {
|
||||||
|
alias: 'f',
|
||||||
|
describe: 'Flatten the input contract file.',
|
||||||
|
type: 'boolean',
|
||||||
|
default: true
|
||||||
|
})
|
||||||
.argv;
|
.argv;
|
||||||
|
|
||||||
let data: string;
|
let data: string;
|
||||||
if (argv['input-file'].startsWith('http')) {
|
if (argv['input-file'].startsWith('http')) {
|
||||||
|
// Assume flattened file in case of URL.
|
||||||
const response = await fetch(argv['input-file']);
|
const response = await fetch(argv['input-file']);
|
||||||
data = await response.text();
|
data = await response.text();
|
||||||
} else {
|
} 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);
|
const ast = parse(data);
|
||||||
|
|
||||||
// Filter out library nodes.
|
// 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);
|
visitor.exportSchema(outStream);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -166,27 +166,45 @@ export class Schema {
|
|||||||
* Adds types 'ResultEvent' and 'WatchedEvent' to the schema.
|
* Adds types 'ResultEvent' and 'WatchedEvent' to the schema.
|
||||||
*/
|
*/
|
||||||
_addEventsRelatedTypes (): void {
|
_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.
|
// Create the ResultEvent type.
|
||||||
const resultEventName = 'ResultEvent';
|
const resultEventName = 'ResultEvent';
|
||||||
this._composer.createObjectTC({
|
this._composer.createObjectTC({
|
||||||
name: resultEventName,
|
name: resultEventName,
|
||||||
fields: {
|
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,
|
event: () => this._composer.getUTC('Event').NonNull,
|
||||||
proof: () => this._composer.getOTC('Proof')
|
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 {
|
_addEventSubscription (): void {
|
||||||
// Add a subscription to the schema composer.
|
// Add a subscription to the schema composer.
|
||||||
this._composer.Subscription.addFields({
|
this._composer.Subscription.addFields({
|
||||||
onEvent: () => this._composer.getOTC('WatchedEvent').NonNull
|
onEvent: () => this._composer.getOTC('ResultEvent').NonNull
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
packages/codegen/src/types/common/main.d.ts
vendored
Normal file
5
packages/codegen/src/types/common/main.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
// Copyright 2021 Vulcanize, Inc.
|
||||||
|
//
|
||||||
|
|
||||||
|
declare module '@poanet/solidity-flattener';
|
6
packages/codegen/src/types/common/package.json
Normal file
6
packages/codegen/src/types/common/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "common",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"license": "AGPL-3.0",
|
||||||
|
"typings": "main.d.ts"
|
||||||
|
}
|
@ -48,7 +48,9 @@
|
|||||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
// "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'. */
|
// "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. */
|
// "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. */
|
// "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. */
|
// "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'. */
|
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
|
@ -2018,9 +2018,9 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.2.tgz#ff80affd6d352dbe1bbc5b4e1833c41afd6283b6"
|
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.2.tgz#ff80affd6d352dbe1bbc5b4e1833c41afd6283b6"
|
||||||
integrity sha512-AybF1cesONZStg5kWf6ao9OlqTZuPqddvprc0ky7lrUVOjXeKpmQ2Y9FK+6ygxasb+4aic4O5pneFBfwVsRRRg==
|
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"
|
version "3.0.6"
|
||||||
resolved "https://github.com/vulcanize/solidity-flattener.git#2fddc0eb439b558861e16288f970b24dab7df245"
|
resolved "https://github.com/vulcanize/solidity-flattener.git#397f7295acff78eb039e479a85e8c3a9d78a96dd"
|
||||||
dependencies:
|
dependencies:
|
||||||
bunyan "^1.8.12"
|
bunyan "^1.8.12"
|
||||||
decomment "^0.9.1"
|
decomment "^0.9.1"
|
||||||
|
Loading…
Reference in New Issue
Block a user