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

Commit

Permalink
Merge branch 'develop' into up-ganache-core
Browse files Browse the repository at this point in the history
  • Loading branch information
CruzMolina authored Apr 22, 2019
2 parents 9d2654e + 3ea0912 commit 099e913
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 66 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"build:decode-utils": "cd packages/truffle-decode-utils && npm run build",
"build:decoder": "cd packages/truffle-decoder && npm run build",
"build:truffle-contract-schema": "cd packages/truffle-contract-schema && npm run build",
"build:interface-adapter": "cd packages/truffle-interface-adapter && npm run build",
"lerna:bootstrap": "lerna bootstrap",
"bootstrap": "./scripts/bootstrap.sh",
"test": "lerna run test --stream --concurrency=1 -- --colors",
Expand Down
2 changes: 1 addition & 1 deletion packages/truffle-contract-schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| JSON Schema | [contract-object.spec.json](spec/contract-object.spec.json) |


[truffle-contract](https://github.com/trufflesuite/truffle-contract) uses a
[truffle-contract](https://github.com/trufflesuite/truffle/tree/develop/packages/truffle-contract) uses a
formally specified<sup>[1](#footnote-1)</sup> JSON object format to represent
Ethereum Virtual Machine (EVM) smart contracts. This representation is intended
to facilitate the use of general purpose smart contract abstractions
Expand Down
4 changes: 2 additions & 2 deletions packages/truffle-contract-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"author": "Tim Coulter <tim.coulter@consensys.net>",
"license": "MIT",
"bugs": {
"url": "https://github.com/trufflesuite/truffle-schema/issues"
"url": "https://github.com/trufflesuite/truffle/issues"
},
"homepage": "https://github.com/trufflesuite/truffle-schema#readme",
"homepage": "https://github.com/trufflesuite/truffle/blob/develop/packages/truffle-contract-schema#readme",
"dependencies": {
"ajv": "^5.1.1",
"crypto-js": "^3.1.9-1",
Expand Down
3 changes: 2 additions & 1 deletion packages/truffle-interface-adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const BN = require("bn.js");
import BN from "bn.js";
import Web3 from "web3";

module.exports.getBlock = web3 => {
// The ts-ignores are ignoring the checks that are
// saying that web3.eth.getBlock is a function and doesn't
// have a `method` property, which it does

export function getBlock(web3: Web3) {
// @ts-ignore
const _oldFormatter = web3.eth.getBlock.method.outputFormatter;

// @ts-ignore
web3.eth.getBlock.method.outputFormatter = block => {
// @ts-ignore
let result = _oldFormatter.call(web3.eth.getBlock.method, block);

// Perhaps there is a better method of doing this,
Expand All @@ -14,11 +23,15 @@ module.exports.getBlock = web3 => {
};
};

module.exports.getTransaction = web3 => {
export function getTransaction(web3: Web3) {
const _oldTransactionFormatter =
// @ts-ignore
web3.eth.getTransaction.method.outputFormatter;

// @ts-ignore
web3.eth.getTransaction.method.outputFormatter = tx => {
let result = _oldTransactionFormatter.call(
// @ts-ignore
web3.eth.getTransaction.method,
tx
);
Expand All @@ -31,11 +44,15 @@ module.exports.getTransaction = web3 => {
};
};

module.exports.getTransactionReceipt = web3 => {
export function getTransactionReceipt(web3: Web3) {
const _oldTransactionReceiptFormatter =
// @ts-ignore
web3.eth.getTransactionReceipt.method.outputFormatter;

// @ts-ignore
web3.eth.getTransactionReceipt.method.outputFormatter = receipt => {
let result = _oldTransactionReceiptFormatter.call(
// @ts-ignore
web3.eth.getTransactionReceipt.method,
receipt
);
Expand All @@ -46,4 +63,4 @@ module.exports.getTransactionReceipt = web3 => {

return result;
};
};
};
1 change: 1 addition & 0 deletions packages/truffle-interface-adapter/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Web3Shim } from "./web3-shim";
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const BN = require("bn.js");
import BN from "bn.js";
import Web3 from "web3";

module.exports.getBlock = web3 => {
// The ts-ignores are ignoring the checks that are
// saying that web3.eth.getBlock is a function and doesn't
// have a `method` property, which it does

export function getBlock(web3: Web3) {
// @ts-ignore
const _oldBlockFormatter = web3.eth.getBlock.method.outputFormatter;
web3.eth.getBlock.method.outputFormatter = block => {
// @ts-ignore
web3.eth.getBlock.method.outputFormatter = (block: any) => {
const _oldTimestamp = block.timestamp;
const _oldGasLimit = block.gasLimit;
const _oldGasUsed = block.gasUsed;

// Quorum uses nanoseconds instead of seconds in timestamp
let timestamp = new BN(block.timestamp.slice(2), 16);
timestamp = timestamp.div(new BN(10).pow(new BN(9)));

block.timestamp = "0x" + timestamp.toString(16);

// Since we're overwriting the gasLimit/Used later,
Expand All @@ -21,6 +29,7 @@ module.exports.getBlock = web3 => {
block.gasLimit = "0x0";
block.gasUsed = "0x0";

// @ts-ignore
let result = _oldBlockFormatter.call(web3.eth.getBlock.method, block);

// Perhaps there is a better method of doing this,
Expand All @@ -33,15 +42,19 @@ module.exports.getBlock = web3 => {
};
};

module.exports.getTransaction = web3 => {
export function getTransaction(web3: Web3) {
const _oldTransactionFormatter =
// @ts-ignore
web3.eth.getTransaction.method.outputFormatter;
web3.eth.getTransaction.method.outputFormatter = tx => {

// @ts-ignore
web3.eth.getTransaction.method.outputFormatter = (tx: any) => {
const _oldGas = tx.gas;

tx.gas = "0x0";

let result = _oldTransactionFormatter.call(
// @ts-ignore
web3.eth.getTransaction.method,
tx
);
Expand All @@ -54,15 +67,19 @@ module.exports.getTransaction = web3 => {
};
};

module.exports.getTransactionReceipt = web3 => {
export function getTransactionReceipt(web3: Web3) {
const _oldTransactionReceiptFormatter =
// @ts-ignore
web3.eth.getTransactionReceipt.method.outputFormatter;
web3.eth.getTransactionReceipt.method.outputFormatter = receipt => {

// @ts-ignore
web3.eth.getTransactionReceipt.method.outputFormatter = (receipt: any) => {
const _oldGasUsed = receipt.gasUsed;

receipt.gasUsed = "0x0";

let result = _oldTransactionReceiptFormatter.call(
// @ts-ignore
web3.eth.getTransactionReceipt.method,
receipt
);
Expand All @@ -73,4 +90,4 @@ module.exports.getTransactionReceipt = web3 => {

return result;
};
};
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const Web3 = require("web3");
import Web3 from "web3";
import { Provider } from "web3/providers";

const ethereumOverloads = require("./ethereum-overloads");
const quorumOverloads = require("./quorum-overloads");
import * as ethereumOverloads from "./ethereum-overloads";
import * as quorumOverloads from "./quorum-overloads";

// March 13, 2019 - Mike Seese:
// This is a temporary shim to support the basic, Ethereum-based
// multiledger integration. This whole adapter, including this shim,
// will undergo better architecture before TruffleCon to support
// other non-Ethereum-based ledgers.

export type NetworkType = "ethereum" | "quorum";

export interface Web3ShimOptions {
provider?: Provider;
networkType?: NetworkType;
};

// March 14, 2019 - Mike Seese:
// This shim was intended to be temporary (see the above comment)
// with the idea of a more robust implementation. That implementation
Expand All @@ -24,8 +32,10 @@ const quorumOverloads = require("./quorum-overloads");
// should drive the development of the correct architecture of
// `truffle-interface-adapter`that should use this work in a more
// sane and organized manner.
class Web3Shim extends Web3 {
constructor(options) {
export class Web3Shim extends Web3 {
public networkType: NetworkType;

constructor(options?: Web3ShimOptions) {
super();

if (options) {
Expand All @@ -41,7 +51,7 @@ class Web3Shim extends Web3 {
this.initInterface();
}

setNetworkType(networkType) {
setNetworkType(networkType: NetworkType) {
this.networkType = networkType;
this.initInterface();
}
Expand Down Expand Up @@ -74,6 +84,4 @@ class Web3Shim extends Web3 {
quorumOverloads.getTransaction(this);
quorumOverloads.getTransactionReceipt(this);
}
}

module.exports = Web3Shim;
}
14 changes: 11 additions & 3 deletions packages/truffle-interface-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "truffle-interface-adapter",
"version": "0.1.2",
"description": "A library that provides an adapter layer for Truffle to interace with different types of networks/ledgers.",
"main": "index.js",
"main": "dist/index.js",
"directories": {
"lib": "lib"
},
"scripts": {
"test": "mocha"
"build": "node_modules/.bin/tsc",
"test": "mocha --exit -r ts-node/register test/**/*.test.ts",
"prepare": "yarn build"
},
"repository": "https://github.com/trufflesuite/truffle/tree/master/packages/truffle-interface-adapter",
"license": "MIT",
Expand All @@ -16,7 +18,13 @@
},
"homepage": "https://github.com/trufflesuite/truffle#readme",
"devDependencies": {
"ganache-core": "2.5.5"
"@types/bn.js": "^4.11.4",
"@types/mocha": "^5.2.6",
"@types/web3": "^1.0.18",
"ganache-core": "2.5.5",
"mocha": "^6.0.2",
"ts-node": "^8.0.3",
"typescript": "^3.3.3333"
},
"dependencies": {
"bn.js": "^4.11.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const Ganache = require("ganache-core");
const Web3 = require("web3");
const Web3Shim = require("../lib/web3-shim");
const assert = require("assert");
const BN = require("bn.js");
import { describe, it } from "mocha";
import assert from "assert";

import { Server } from "http";
import BN from "bn.js";

import Web3 from "web3";
import Ganache from "ganache-core";

import { Web3Shim } from "../lib";

const genesisBlockTime = new Date();
const port = 12345;

async function prepareGanache(quorumEnabled) {
async function prepareGanache(quorumEnabled: boolean): Promise<{ server: Server, web3Shim: Web3Shim }> {
return new Promise((resolve, reject) => {
const server = Ganache.server({
time: genesisBlockTime
});
server.listen(port, err => {
server.listen(port, (err: Error) => {
if (err) reject(err);

web3Shim = new Web3Shim({
const web3Shim = new Web3Shim({
provider: new Web3.providers.HttpProvider(`http://127.0.0.1:${port}`),
networkType: quorumEnabled ? "quorum" : "ethereum"
});
Expand All @@ -27,8 +32,8 @@ async function prepareGanache(quorumEnabled) {
});
}

describe("Quorum getBlock Overload", () => {
it("recovers block timestamp as hexstring instead of number w/ quorum=true", async () => {
describe("Quorum getBlock Overload", function() {
it("recovers block timestamp as hexstring instead of number w/ quorum=true", async function() {
return new Promise(async (resolve, reject) => {
let preparedGanache;
try {
Expand All @@ -48,7 +53,7 @@ describe("Quorum getBlock Overload", () => {
});
});

it("recovers block timestamp as number w/ quorum=false", async () => {
it("recovers block timestamp as number w/ quorum=false", async function() {
return new Promise(async (resolve, reject) => {
let preparedGanache;
try {
Expand All @@ -64,4 +69,4 @@ describe("Quorum getBlock Overload", () => {
}
});
});
});
});
22 changes: 22 additions & 0 deletions packages/truffle-interface-adapter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"lib": ["es2017"],
"paths": {
"web3": ["../../node_modules/@types/web3/index", "node_modules/web3/index"]
},
"rootDir": "lib",
"typeRoots": ["./typings", "./node_modules/@types", "../../node_modules/@types"],
},
"include": [
"./lib/**/*.ts",
"./typings/**/*.d.ts"
]
}
Loading

0 comments on commit 099e913

Please sign in to comment.