Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# ethapi-js

A thin, fast low-level Promise-based wrapper around the Eth APIs.
A thin, fast, low-level Promise-based wrapper around the Ethereum APIs.

[![Build Status](https://travis-ci.org/jacogr/ethapi-js.svg?branch=master)](https://travis-ci.org/jacogr/ethapi-js)
[![Coverage Status](https://coveralls.io/repos/github/jacogr/ethapi-js/badge.svg?branch=master)](https://coveralls.io/github/jacogr/ethapi-js?branch=master)
[![Dependency Status](https://david-dm.org/jacogr/ethapi-js.svg)](https://david-dm.org/jacogr/ethapi-js)
[![devDependency Status](https://david-dm.org/jacogr/ethapi-js/dev-status.svg)](https://david-dm.org/jacogr/ethapi-js#info=devDependencies)

## getting going
## contributing

- clone
- `npm install`
- `npm run testOnce`
Clone the repo and install dependencies via `npm install`. Tests can be executed via

- `npm run testOnce` (100% covered unit tests)
- `npm run testE2E` (E2E against a running RPC-enabled testnet Parity/Geth instance, `parity --testnet --jsonrpc`)

## installation

Install the package with `npm install --save ethapi-js` from the [npm registry ethapi-js](https://www.npmjs.com/package/ethapi-js)

## usage

Expand Down
17 changes: 17 additions & 0 deletions lib/transport/jsonRpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default class JsonRpc {
this._host = host;
this._port = port;
this._id = 1;
this._debug = false;
}

_encodeBody (method, params) {
Expand Down Expand Up @@ -37,17 +38,33 @@ export default class JsonRpc {
return fetch(this._getEndpoint(), this._encodeOptions(method, params))
.then((response) => {
if (response.status !== 200) {
if (this._debug) {
console.error(' ', `${method}(${params}) throws`, response.status, response.statusText);
}

throw new Error(response.statusText);
}

return response.json();
})
.then((result) => {
if (result.error) {
if (this._debug) {
console.error(' ', `${method}(${params}) =`, result);
}

throw new Error(result.error);
}

if (this._debug) {
console.log(' ', `${method}(${params}) =`, result.result);
}

return result.result;
});
}

setDebug (flag) {
this._debug = flag;
}
}
68 changes: 68 additions & 0 deletions lib/transport/jsonRpc.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sinon from 'sinon';

import { TEST_HOST, TEST_PORT, mockRpc } from '../../test/mockRpc';
import JsonRpc from './jsonRpc';

Expand Down Expand Up @@ -114,4 +116,70 @@ describe('transport/JsonRPC', () => {
expect(error.message).to.equal(ERROR);
});
});

describe('setDebug', () => {
it('starts with disabled flag', () => {
expect(transport._debug).to.be.false;
});

it('true flag switches on', () => {
transport.setDebug(true);
expect(transport._debug).to.be.true;
});

it('false flag switches off', () => {
transport.setDebug(true);
expect(transport._debug).to.be.true;
transport.setDebug(false);
expect(transport._debug).to.be.false;
});

describe('logging', () => {
beforeEach(() => {
sinon.spy(console, 'log');
sinon.spy(console, 'error');
});

afterEach(() => {
console.log.restore();
console.error.restore();
});

it('logs the HTTP error', () => {
const scope = mockRpc([{ method: 'eth_call', reply: {}, code: 500 }]);

transport.setDebug(true);
return transport
.execute('eth_call')
.catch(() => {
expect(scope.isDone()).to.be.true;
expect(console.error).to.be.called;
});
});

it('logs the RPC error', () => {
const scope = mockRpc([{ method: 'eth_call', reply: { error: 'test error' } }]);

transport.setDebug(true);
return transport
.execute('eth_call')
.catch(() => {
expect(scope.isDone()).to.be.true;
expect(console.error).to.be.called;
});
});

it('logs the RPC result', () => {
const scope = mockRpc([{ method: 'eth_call', reply: { result: '0x123456789' } }]);

transport.setDebug(true);
return transport
.execute('eth_call')
.then(() => {
expect(scope.isDone()).to.be.true;
expect(console.log).to.be.called;
});
});
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lint": "eslint --ignore-path .gitignore ./lib/",
"release": "./scripts/release.sh",
"testCoverage": "istanbul cover _mocha -- 'lib/**/*.spec.js'",
"testOnce": "mocha 'lib/**/*.spec.js'"
"testOnce": "mocha 'lib/**/*.spec.js'",
"testE2E": "mocha 'test/e2e/**/*.spec.js'"
},
"devDependencies": {
"babel-cli": "^6.9.0",
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/rpc/eth.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ethapi from '../../ethapi';

describe('ethapi.eth.accounts()', () => {
it('returns the available accounts', () => {
return ethapi.eth
.accounts()
.then((accounts) => {
expect(accounts).to.be.ok;
});
});
});

describe('ethapi.eth.blockNumber()', () => {
it('returns the current blockNumber', () => {
return ethapi.eth
.blockNumber()
.then((blockNumber) => {
expect(blockNumber).to.be.ok;
});
});
});
8 changes: 8 additions & 0 deletions test/ethapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import EthApi from '../lib';

const transport = new EthApi.Transports.JsonRpc('127.0.0.1', 8545);
const ethapi = new EthApi(transport);

transport.setDebug(true);

export default ethapi;