Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3297 from trufflesuite/truffle-db
Browse files Browse the repository at this point in the history
Merge `truffle-db` into `develop`
  • Loading branch information
gnidan authored Sep 23, 2020
2 parents c28ff45 + 4233ce8 commit a3d5ebc
Show file tree
Hide file tree
Showing 113 changed files with 54,860 additions and 80 deletions.
3 changes: 2 additions & 1 deletion packages/core/lib/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var command = {
}
},
displayCommandHelp: function (selectedCommand) {
var commands = require("./index");
let commands = require("./index");
var commandHelp = commands[selectedCommand].help;

console.log(`\n Usage: ${commandHelp.usage}`);
console.log(` Description: ${commands[selectedCommand].description}`);

Expand Down
2 changes: 2 additions & 0 deletions packages/db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
types/schema.d.ts
5 changes: 5 additions & 0 deletions packages/db/.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist
types/schema.d.ts
node_modules
src/loaders/schema/test/workflowCompileOutputMock
src/loaders/schema/test/compilationSources/build/contracts
11 changes: 11 additions & 0 deletions packages/db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@truffle/db`

> TODO: description
## Usage

```
const { TruffleDB } = require('@truffle/db');
// TODO: DEMONSTRATE API
```
10 changes: 10 additions & 0 deletions packages/db/bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# stop if any commands fail
set -ex

mkdir -p dist/src/
cp ./src/schema.graphql ./dist/src/schema.graphql
tsc --project tsconfig.codegen.json
node ./bin/codegen.js
tsc $@
14 changes: 14 additions & 0 deletions packages/db/bin/codegen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const path = require("path");
const { generateNamespace } = require("@gql2ts/from-schema");

// for path setup
require("@truffle/db");

const { schema } = require("@truffle/db/data/schema");

const dataModel = generateNamespace("DataModel", schema, {
ignoreTypeNameDeclaration: true
});

fs.writeFileSync(path.join(__dirname, "..", "types", "schema.d.ts"), dataModel);
24 changes: 24 additions & 0 deletions packages/db/bin/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ApolloServer } = require("apollo-server");

const { TruffleDB } = require("@truffle/db");
const Config = require("@truffle/config");

const port = 4444;

const config = Config.detect({
workingDirectory: process.argv[2] || process.cwd()
});

const db = new TruffleDB(config);

const { schema, context } = db;

const server = new ApolloServer({
tracing: true,
schema,
context
});

server.listen({ port }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
34 changes: 34 additions & 0 deletions packages/db/configs/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"compilerOptions": {
"sourceMap": true,
"declaration": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"target": "es6",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"outDir": "../dist",
"strictBindCallApply": true,
"paths": {
"@truffle/db": ["../src"],
"@truffle/db/*": ["../src/*"],
"test/*": ["../test/*"]
},
"rootDir": "..",
"baseUrl": ".",
"types": [
"express",
"jest",
"node",
"pouchdb"
]
},
"include": [
"src/**/*",
"test/**/*"
],
"exclude": [
"node_modules"
]
}
111 changes: 111 additions & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"name": "@truffle/db",
"version": "0.1.0-0",
"description": "Smart contract data aggregation",
"keywords": [
"truffle",
"smart-contracts",
"ethereum",
"database"
],
"author": "g. nicholas d'andrea <gnidan@users.noreply.github.com>",
"homepage": "https://github.com/trufflesuite/truffle#readme",
"license": "MIT",
"main": "dist/src/index.js",
"directories": {
"dist": "dist"
},
"files": [
"dist",
"types/schema.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/trufflesuite/truffle.git"
},
"scripts": {
"clean": "rm -rf ./dist ./types/schema.d.ts",
"prepare": "yarn build",
"build": "./bin/build",
"start": "ts-node-dev -r tsconfig-paths/register ./bin/server",
"start:debug": "DEBUG=pouchdb:api ts-node-dev -r tsconfig-paths/register ./bin/server",
"start:drizzle": "ts-node-dev -r tsconfig-paths/register ./bin/server ./test/truffle-projects/drizzle-box",
"start:drizzle:debug": "DEBUG=pouchdb:api ts-node-dev -r tsconfig-paths/register ./bin/server ./test/truffle-projects/drizzle-box",
"test": "jest --verbose --detectOpenHandles"
},
"bugs": {
"url": "https://github.com/trufflesuite/truffle/issues"
},
"devDependencies": {
"@types/express": "^4.16.0",
"@types/graphql": "^14.0.4",
"@types/jest": "^23.3.11",
"@types/node": "^10.12.18",
"@types/pouchdb": "^6.3.2",
"@types/pouchdb-adapter-leveldb": "^6.1.3",
"@types/web3": "1.0.20",
"ganache-core": "2.8.0",
"gql2ts": "^1.10.1",
"jest": "^23.6.0",
"jsondown": "^1.0.0",
"ts-jest": "^23.10.5",
"ts-node-dev": "^1.0.0-pre.32",
"tsconfig-paths": "^3.7.0",
"typescript": "^3.6.3"
},
"dependencies": {
"@gnd/graphql-tools": "^4.0.5-fix.0",
"@gnd/jsonschema2graphql": "^1.0.15",
"@truffle/artifactor": "^4.0.74",
"@truffle/compile-common": "^0.3.10",
"@truffle/workflow-compile": "^3.0.0",
"apollo-server": "^2.3.1",
"fse": "^4.0.1",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.1",
"jsondown": "^1.0.0",
"leveldown": "^5.2.0",
"module-alias": "^2.1.0",
"pascal-case": "^2.0.1",
"pouchdb": "7.1.1",
"pouchdb-adapter-memory": "^7.1.1",
"pouchdb-adapter-node-websql": "^7.0.0",
"pouchdb-debug": "^7.1.1",
"pouchdb-find": "^7.0.0",
"source-map-support": "^0.5.9",
"web3": "1.2.2",
"web3-utils": "1.2.2"
},
"jest": {
"moduleFileExtensions": [
"ts",
"js",
"json",
"node"
],
"testEnvironment": "node",
"transform": {
"^.+\\.ts$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/tsconfig.json",
"diagnostics": true
}
},
"moduleNameMapper": {
"^@truffle/db/(.*)": "<rootDir>/src/$1",
"^@truffle/db$": "<rootDir>/src",
"^test/(.*)": "<rootDir>/test/$1"
},
"testMatch": [
"<rootDir>/src/**/test/index.(ts|js)",
"<rootDir>/test/**/test/index.(ts|js)",
"<rootDir>/src/**/test/*\\.(spec|test)\\.(ts|js)",
"<rootDir>/test/**/test/*\\.(spec|test)\\.(ts|js)"
]
},
"_moduleAliases": {
"@truffle/db": "db/dist/src"
}
}
2 changes: 2 additions & 0 deletions packages/db/src/artifacts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { abiSchema } from "./json";
export { schema } from "./schema";
1 change: 1 addition & 0 deletions packages/db/src/artifacts/json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { schema, abiSchema } from "./schema";
93 changes: 93 additions & 0 deletions packages/db/src/artifacts/json/resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as fse from "fs-extra";
import { Shims } from "@truffle/compile-common";

const TruffleResolver = require("@truffle/resolver");

export interface ITruffleResolver {
require(name: string): any;
}

interface IContext {
artifactsDirectory: string;
workingDirectory?: string;
}

export const resolvers = {
Query: {
contract: {
resolve(_, { name, networkId }, context: IContext) {
const truffleResolver: ITruffleResolver = new TruffleResolver({
contracts_build_directory: context.artifactsDirectory,
working_directory: context.workingDirectory || process.cwd()
});

const artifact = truffleResolver.require(name)._json;

const linkedBytecodeCreate = Shims.LegacyToNew.forBytecode(
artifact.bytecode
);
const linkedBytecodeCall = Shims.LegacyToNew.forBytecode(
artifact.deployedBytecode
);

const result = {
...artifact,

abi: {
json: JSON.stringify(artifact.abi)
},
ast: {
json: JSON.stringify(artifact.ast)
},
source: {
contents: artifact.source,
sourcePath: artifact.sourcePath
},
bytecode: {
bytes: linkedBytecodeCreate.bytes,
linkReferences: linkedBytecodeCreate.linkReferences
},
deployedBytecode: {
bytes: linkedBytecodeCall.bytes,
linkReferences: linkedBytecodeCall.linkReferences
},
sourceMap: {
json: JSON.stringify(artifact.sourceMap)
}
};

if (networkId) {
return result.networks[networkId]
? {
...result,
networks: {
[networkId]: result.networks[networkId]
}
}
: null;
}
return result;
}
},
contractNames: {
resolve(_, {}, context: IContext): string[] {
const contents = fse.readdirSync(context.artifactsDirectory);

return contents
.filter(filename => filename.endsWith(".json"))
.map(filename => filename.slice(0, -5));
}
}
},

ContractObject: {
networks: {
resolve({ networks }) {
return Object.entries(networks).map(([networkId, networkObject]) => ({
networkId,
networkObject
}));
}
}
}
};
Loading

0 comments on commit a3d5ebc

Please sign in to comment.