Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Add tslint + prettier, and upgrade dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raúl Kripalani committed May 21, 2018
1 parent 3d1cabe commit 19875ba
Show file tree
Hide file tree
Showing 10 changed files with 1,676 additions and 751 deletions.
8 changes: 8 additions & 0 deletions .prettierrc
@@ -0,0 +1,8 @@
{
"printWidth": 120,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"proseWrap": "always"
}
48 changes: 36 additions & 12 deletions package.json
@@ -1,36 +1,60 @@
{
"name": "ethql",
"version": "0.0.1",
"description": "A GraphQL interface to Ethereum",
"author": "PegaSys – ConsenSys",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/ConsenSys/ethql"
},
"main": "index.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"clean": "$(npm bin)/rimraf dist",
"start": "$(npm bin)/dotenv -- node dist/index.js",
"dev": "$(npm bin)/nodemon -e ts -x ts-node src/index.ts",
"debug": "$(npm bin)/dotenv -- nodemon -e ts -x ts-node --inspect src/index.ts",
"build": "$(npm bin)/rimraf dist && tsc"
"build": "npm run clean && tsc && npm run tslint",
"lint":
"tslint --fix -t stylish -c tslint.json -p tsconfig.json && prettier --parser typescript --write src/**/*.ts",
"precommit": "lint-staged"
},
"lint-staged": {
"*.{ts}": [
"tslint --fix -t stylish -c tslint.json -p tsconfig.json",
"prettier --parser typescript --write",
"git add"
],
"*.{js,css,scss,html,json,graphql,md}": ["prettier --write", "git add"]
},
"version": "1.0.0",
"description": "A GraphQL interface to Ethereum",
"main": "index.js",
"author": "Raúl Kripalani <raul.kripalani@gmail.com>",
"license": "Apache-2.0",
"dependencies": {
"@types/lodash": "^4.14.104",
"abi-decoder": "^1.1.0",
"express": "^4.16.2",
"express-graphql": "^0.6.11",
"graphql": "^0.13.1",
"lodash": "^4.17.5",
"web3": "^1.0.0-beta.30"
"web3": "^1.0.0-beta.34"
},
"devDependencies": {
"@types/express": "^4.11.1",
"@types/express-graphql": "^0.0.36",
"@types/graphql": "^0.12.4",
"@types/express-graphql": "^0.6.1",
"@types/graphql": "^0.13.1",
"@types/lodash": "^4.14.104",
"dotenv": "^5.0.1",
"dotenv-cli": "^1.4.0",
"husky": "^0.14.3",
"lint-staged": "^7.1.2",
"nodemon": "^1.17.3",
"npm-run-all": "^4.1.2",
"prettier": "1.12.1",
"rimraf": "^2.6.2",
"ts-node": "^4.1.0",
"ts-node": "^6.0.3",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"typescript": "^2.7.2",
"web3-typescript-typings": "^0.9.11"
"web3-typescript-typings": "^0.10.2"
}
}
14 changes: 5 additions & 9 deletions src/account.ts
@@ -1,4 +1,4 @@
import { GraphQLString, GraphQLObjectType } from 'graphql';
import { GraphQLObjectType, GraphQLString } from 'graphql';
import { longType } from './common-types';
import { web3 } from './web3';

Expand All @@ -7,19 +7,15 @@ const accountFields = {
balance: {
type: longType,
args: { unit: { type: GraphQLString } },
resolve: ({ address }, { unit }) => {
const balance = web3.eth.getBalance(address);
return balance;
// return unit ? (web3 as any).utils.fromWei(balance, unit) : balance;
}
resolve: ({ address }, { unit }) => web3.eth.getBalance(address),
},
code: {
type: GraphQLString,
resolve: ({ address }) => web3.eth.getCode(address)
}
resolve: ({ address }) => web3.eth.getCode(address),
},
};

export const Account = new GraphQLObjectType({
name: 'Account',
fields: accountFields
fields: accountFields,
});
8 changes: 4 additions & 4 deletions src/block.ts
@@ -1,4 +1,4 @@
import { GraphQLInt, GraphQLString, GraphQLList, GraphQLObjectType, GraphQLFieldConfigMap } from 'graphql';
import { GraphQLFieldConfigMap, GraphQLInt, GraphQLList, GraphQLObjectType, GraphQLString } from 'graphql';
import { Transaction } from './transaction';

const blockFields: GraphQLFieldConfigMap<any, any> = {
Expand All @@ -17,11 +17,11 @@ const blockFields: GraphQLFieldConfigMap<any, any> = {
transactions: {
type: new GraphQLList(Transaction),
resolve: ({ transactions }) =>
transactions.map(tx => Object.assign(tx, { from: { address: tx.from }, to: { address: tx.to } }))
}
transactions.map(tx => ({ ...tx, from: { address: tx.from }, to: { address: tx.to } })),
},
};

export const Block = new GraphQLObjectType({
name: 'Block',
fields: blockFields
fields: blockFields,
});
2 changes: 1 addition & 1 deletion src/common-types.ts
Expand Up @@ -5,5 +5,5 @@ export const longType = new GraphQLScalarType({
description: '64-bit integral numbers',
serialize: Number,
parseValue: Number,
parseLiteral: ast => (ast.kind === Kind.INT ? parseInt(ast.value, 10) : null)
parseLiteral: ast => (ast.kind === Kind.INT ? parseInt(ast.value, 10) : undefined),
});
28 changes: 14 additions & 14 deletions src/index.ts
@@ -1,22 +1,22 @@
import * as _ from 'lodash';
import * as graphqlHTTP from 'express-graphql';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLFieldConfigMap,
GraphQLInt,
GraphQLInterfaceType,
GraphQLList,
GraphQLScalarType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLSchema,
GraphQLString,
Kind,
GraphQLFieldConfigMap,
GraphQLInterfaceType
} from 'graphql';
import * as _ from 'lodash';

import * as express from 'express';
import { Block } from './block';
import { web3 } from './web3';
import { DecodedTransaction, Erc20TransactionType } from './transaction';
import { web3 } from './web3';

const schema = new GraphQLSchema({
types: [DecodedTransaction, Erc20TransactionType],
Expand All @@ -27,19 +27,19 @@ const schema = new GraphQLSchema({
block: {
type: Block,
args: { number: { type: GraphQLInt } },
resolve: (obj, { number }) => web3.eth.getBlock(number, true)
resolve: (obj, { blockNumber }) => web3.eth.getBlock(blockNumber, true),
},
blocks: {
type: new GraphQLList(Block),
args: { from: { type: GraphQLInt }, to: { type: GraphQLInt } },
resolve: (obj, { from, to }) => Promise.all(_.range(from, to + 1).map(i => web3.eth.getBlock(i, true)))
}
}
})
resolve: (obj, { from, to }) => Promise.all(_.range(from, to + 1).map(i => web3.eth.getBlock(i, true))),
},
},
}),
});

const app = express();
app.use('/graphql', graphqlHTTP({ schema: schema, graphiql: true }));
app.use('/graphql', graphqlHTTP({ schema, graphiql: true }));

app.listen(4000);

Expand Down
50 changes: 26 additions & 24 deletions src/transaction.ts
@@ -1,24 +1,24 @@
import { DecodedMethod, DecodedParam } from 'abi-decoder';
import {
GraphQLObjectType,
GraphQLEnumType,
GraphQLFieldConfigMap,
GraphQLInt,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLFieldConfigMap,
GraphQLUnionType
GraphQLUnionType,
} from 'graphql';
import { Account } from './account';
import { DecodedMethod, DecodedParam } from 'abi-decoder';

export const DecodedTransaction = new GraphQLInterfaceType({
name: 'InterpretedTransaction',
resolveType: decoded => decoded.type,
fields: {
contractType: {
name: 'ContractType',
type: GraphQLString
}
}
type: GraphQLString,
},
},
});

export const Transaction = new GraphQLObjectType({
Expand All @@ -38,18 +38,20 @@ export const Transaction = new GraphQLObjectType({
decoded: {
type: DecodedTransaction,
resolve: tx => {
if (!tx.input) return;
// If we find a decoder for the transaction, we return the result of the ABI decoding
// enhanced with a 'type' property with the GraphQL type.
for (var entry of decoders) {
if (!tx.input) {
return;
}
/* If we find a decoder for the transaction, we return the result of the ABI decoding
enhanced with a 'type' property with the GraphQL type. */
for (const entry of decoders) {
const decoded = entry.decoder.decodeMethod(tx.input);
if (decoded) {
return Object.assign(decoded, { type: entry.type });
return { ...decoded, type: entry.type };
}
}
}
}
}
},
},
},
});

const extractParamValue = (params: [DecodedParam], name: string) => {
Expand All @@ -71,18 +73,18 @@ export const Erc20TransactionType = new GraphQLObjectType({
approveAndCall: { value: 'approveAndCall' },
approve: { value: 'approve' },
transferFrom: { value: 'transferFrom' },
transfer: { value: 'transfer' }
}
transfer: { value: 'transfer' },
},
}),
resolve: ({ name }) => name
resolve: ({ name }) => name,
},
from: {
type: GraphQLString,
resolve: ({ params }) => extractParamValue(params, 'from')
resolve: ({ params }) => extractParamValue(params, 'from'),
},
to: { type: GraphQLString, resolve: ({ params }) => extractParamValue(params, 'to') },
value: { type: GraphQLString, resolve: ({ params }) => extractParamValue(params, 'value') }
}
value: { type: GraphQLString, resolve: ({ params }) => extractParamValue(params, 'value') },
},
});

const decoders = [
Expand All @@ -92,6 +94,6 @@ const decoders = [
const decoder = require('abi-decoder');
decoder.addABI(require('../abi/erc20.json'));
return decoder;
})()
}
})(),
},
];
2 changes: 1 addition & 1 deletion src/web3.ts
@@ -1,5 +1,5 @@
import * as Web3 from 'web3';

export const web3: Web3 = new Web3(
new Web3.providers.HttpProvider(`https://mainnet.infura.io/${process.env.INFURA_ID}`)
new Web3.providers.HttpProvider(`https://mainnet.infura.io/${process.env.INFURA_ID}`),
);
48 changes: 48 additions & 0 deletions tslint.json
@@ -0,0 +1,48 @@
{
"defaultSeverity": "error",
"extends": ["tslint:latest", "tslint-config-prettier"],
"jsRules": {},
"rules": {
"class-name": true,
"comment-format": [true, "check-space", "check-uppercase"],
"indent": [true, "spaces"],
"one-line": [true, "check-open-brace", "check-whitespace"],
"no-var-keyword": true,
"quotemark": [true, "single", "avoid-escape"],
"semicolon": [true, "always", "ignore-bound-class-methods"],
"object-literal-sort-keys": false,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-separator",
"check-type"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "onespace",
"index-signature": "onespace",
"parameter": "onespace",
"property-declaration": "onespace",
"variable-declaration": "onespace"
}
],
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-null-keyword": true,
"prefer-const": true,
"jsdoc-format": true,
"no-console": false
},
"rulesDirectory": []
}

0 comments on commit 19875ba

Please sign in to comment.