diff --git a/lib/rpc/db/db.js b/lib/rpc/db/db.js index e2a94e2..f13c69b 100644 --- a/lib/rpc/db/db.js +++ b/lib/rpc/db/db.js @@ -1,21 +1,27 @@ +import { inHex } from '../../format/input'; + export default class Db { constructor (transport) { this._transport = transport; } getHex (dbName, keyName) { - return this._transport.execute('db_getHex', dbName, keyName); + return this._transport + .execute('db_getHex', dbName, keyName); } getString (dbName, keyName) { - return this._transport.execute('db_getString', dbName, keyName); + return this._transport + .execute('db_getString', dbName, keyName); } putHex (dbName, keyName, hexData) { - return this._transport.execute('db_putHex', dbName, keyName, hexData); + return this._transport + .execute('db_putHex', dbName, keyName, inHex(hexData)); } putString (dbName, keyName, stringData) { - return this._transport.execute('db_putString', dbName, keyName, stringData); + return this._transport + .execute('db_putString', dbName, keyName, stringData); } } diff --git a/lib/rpc/db/db.spec.js b/lib/rpc/db/db.spec.js new file mode 100644 index 0000000..6f9221a --- /dev/null +++ b/lib/rpc/db/db.spec.js @@ -0,0 +1,22 @@ +import { TEST_HOST, TEST_PORT, mockRpc } from '../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Db from './db'; + +const instance = new Db(new Http(TEST_HOST, TEST_PORT)); + +describe('lib/Db', () => { + let scope; + + describe('putHex', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'db_putHex', reply: { result: [] } }]); + }); + + it('formats the inputs correctly', () => { + return instance.putHex('db', 'key', '1234').then(() => { + expect(scope.body.db_putHex.params).to.deep.equal(['db', 'key', '0x1234']); + }); + }); + }); +}); diff --git a/lib/rpc/eth/eth.spec.js b/lib/rpc/eth/eth.spec.js index b408a8f..c409b93 100644 --- a/lib/rpc/eth/eth.spec.js +++ b/lib/rpc/eth/eth.spec.js @@ -15,7 +15,7 @@ describe('lib/Eth', () => { scope = mockRpc([{ method: 'eth_call', reply: { result: [] } }]); }); - it('options & blockNumber', () => { + it('formats the input options & blockNumber', () => { return instance.call({ data: '12345678' }, 'earliest').then(() => { expect(scope.body.eth_call.params).to.deep.equal([{ data: '0x12345678' }, 'earliest']); }); @@ -47,6 +47,19 @@ describe('lib/Eth', () => { }); }); + describe('gasPrice', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'eth_gasPrice', reply: { result: '0x123' } }]); + }); + + it('returns the fomratted price', () => { + return instance.gasPrice().then((price) => { + expect(isBigNumber(price)).to.be.true; + expect(price.toString(16)).to.deep.equal('123'); + }); + }); + }); + describe('getBalance', () => { beforeEach(() => { scope = mockRpc([{ method: 'eth_getBalance', reply: { result: '0x123' } }]); @@ -83,7 +96,7 @@ describe('lib/Eth', () => { }); }); - it('formats the input hash as a hash, default full = true', () => { + it('formats the input hash as a hash, full true', () => { return instance.getBlockByHash('1234', true).then(() => { expect(scope.body.eth_getBlockByHash.params).to.deep.equal(['0x1234', true]); }); @@ -95,4 +108,78 @@ describe('lib/Eth', () => { }); }); }); + + describe('getBlockByNumber', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'eth_getBlockByNumber', reply: { result: { miner: address.toLowerCase() } } }]); + }); + + it('assumes blockNumber latest & full false', () => { + return instance.getBlockByNumber().then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['latest', false]); + }); + }); + + it('uses input blockNumber & full false', () => { + return instance.getBlockByNumber('0x1234').then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', false]); + }); + }); + + it('formats the input blockNumber, full true', () => { + return instance.getBlockByNumber(0x1234, true).then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', true]); + }); + }); + + it('formats the output into block', () => { + return instance.getBlockByNumber(0x1234).then((block) => { + expect(block.miner).to.equal(address); + }); + }); + }); + + describe('getBlockTransactionCountByHash', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'eth_getBlockTransactionCountByHash', reply: { result: '0x123' } }]); + }); + + it('formats input hash properly', () => { + return instance.getBlockTransactionCountByHash('abcdef').then(() => { + expect(scope.body.eth_getBlockTransactionCountByHash.params).to.deep.equal(['0xabcdef']); + }); + }); + + it('formats the output number', () => { + return instance.getBlockTransactionCountByHash('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); + + describe('getBlockTransactionCountByNumber', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'eth_getBlockTransactionCountByNumber', reply: { result: '0x123' } }]); + }); + + it('specified blockNumber latest when none specified', () => { + return instance.getBlockTransactionCountByNumber().then(() => { + expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['latest']); + }); + }); + + it('formats input blockNumber properly', () => { + return instance.getBlockTransactionCountByNumber(0xabcdef).then(() => { + expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['0xabcdef']); + }); + }); + + it('formats the output number', () => { + return instance.getBlockTransactionCountByNumber('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); }); diff --git a/lib/rpc/web3/web3.spec.js b/lib/rpc/web3/web3.spec.js new file mode 100644 index 0000000..ad3d5f9 --- /dev/null +++ b/lib/rpc/web3/web3.spec.js @@ -0,0 +1,22 @@ +import { TEST_HOST, TEST_PORT, mockRpc } from '../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Web3 from './web3'; + +const instance = new Web3(new Http(TEST_HOST, TEST_PORT)); + +describe('lib/Web3', () => { + let scope; + + describe('sha3', () => { + beforeEach(() => { + scope = mockRpc([{ method: 'web3_sha3', reply: { result: [] } }]); + }); + + it('formats the inputs correctly', () => { + return instance.sha3('1234').then(() => { + expect(scope.body.web3_sha3.params).to.deep.equal(['0x1234']); + }); + }); + }); +});