Use formatters from @polkadot/api-format (#3)

* Use formatters from @polkadot/api-format

* No checks for specs (atm)
This commit is contained in:
Jaco Greeff
2017-11-18 17:36:30 +01:00
committed by GitHub
parent 7ccdbcdb6f
commit 648925cd18
19 changed files with 960 additions and 317 deletions
+2 -1
View File
@@ -5,10 +5,11 @@ import type { InterfaceDefinition } from '@polkadot/jsonrpc/src/types';
import type { ApiInterface } from './types';
import type { ProviderInterface } from './provider/types';
const { formatInputs, formatOutput } = require('@polkadot/api-format');
const interfaces = require('@polkadot/jsonrpc');
const { callSignature } = require('@polkadot/jsonrpc/lib/util');
const { isFunction } = require('@polkadot/util/lib/is');
const { formatInputs, formatOutput } = require('./format');
const { HttpProvider } = require('./provider');
module.exports = class Api implements ApiInterface {
-10
View File
@@ -1,10 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const formatInputs = require('./input');
const formatOutput = require('./output');
module.exports = {
formatInputs,
formatOutput
};
-10
View File
@@ -1,10 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const { formatH160 } = require('./hex');
// TODO: Currently the format assumes 160-bit values (like Ethereum)
// this will probably change along the way as things get firmed up
module.exports = function format (value: ?string): string {
return formatH160(value);
};
-13
View File
@@ -1,13 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./address');
describe('format/input/address', () => {
describe('format', () => {
it('pads to H160 value', () => {
expect(
format('0x1234567890')
).to.equal('0x0000000000000000000000000000001234567890');
});
});
});
-33
View File
@@ -1,33 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('@polkadot/util/lib/hex');
const H64_ZERO: string = '00000000000000000000000000000000';
const H128_ZERO: string = `${H64_ZERO}${H64_ZERO}`;
const H256_ZERO: string = `${H128_ZERO}${H128_ZERO}`;
// const H512_ZERO: string = `${H256_ZERO}${H256_ZERO}`;
function leftHexPad (value: ?string, bitLength: number): string {
const length = 2 * bitLength / 8;
if (hasHexPrefix(value)) {
value = stripHexPrefix(value);
}
return addHexPrefix(`${H256_ZERO}${value || ''}`.slice(-length));
}
function formatH160 (value: ?string): string {
return leftHexPad(value, 160);
}
function formatH256 (value: ?string): string {
return leftHexPad(value, 256);
}
module.exports = {
leftHexPad,
formatH160,
formatH256
};
-53
View File
@@ -1,53 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const { leftHexPad, formatH160, formatH256 } = require('./hex');
describe('format/input/hex', () => {
describe('leftHexPad', () => {
it('padds to the required length', () => {
expect(
leftHexPad('0x123', 16)
).to.equal('0x0123');
});
it('padds to the required length (no prefix)', () => {
expect(
leftHexPad('123', 16)
).to.equal('0x0123');
});
it('pads null values correctly', () => {
expect(
leftHexPad(null, 16)
).to.equal('0x0000');
});
});
describe('formatH160', () => {
it('pads to 40 bytes', () => {
expect(
formatH160('0x1234567890')
).to.equal('0x0000000000000000000000000000001234567890');
});
it('pads null values correctly', () => {
expect(
formatH160(null)
).to.match(/^0x(00){20,}$/);
});
});
describe('formatH256', () => {
it('pads to 64 bytes', () => {
expect(
formatH256('0x1234567890')
).to.equal('0x0000000000000000000000000000000000000000000000000000001234567890');
});
it('pads null values correctly', () => {
expect(
formatH256(null)
).to.match(/^0x(00){32,}$/);
});
});
});
-24
View File
@@ -1,24 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatInputType, InterfaceInputType } from '@polkadot/jsonrpc/src/types';
import type { FormatterFunction } from '../types';
const formatAddress = require('./address');
const { formatH256 } = require('./hex');
const formatNoop = require('../noop');
const util = require('../util');
const formatters: { [FormatInputType]: FormatterFunction } = {
'Address': formatAddress,
'CallData': formatNoop,
'H256': formatH256,
'HeaderHash': formatH256,
'String': formatNoop
};
module.exports = function format (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
const types = inputs.map(({ type }) => type);
return util.formatArray(formatters, types, values);
};
-22
View File
@@ -1,22 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./index');
describe('format/input', () => {
describe('format', () => {
it('formats each value in an array', () => {
expect(
format(
[
{ name: 'foo', type: 'Address' },
{ name: 'bar', type: 'H256' }
],
['0x1234', '0xabcd']
)
).to.deep.equal([
'0x0000000000000000000000000000000000001234',
'0x000000000000000000000000000000000000000000000000000000000000abcd'
]);
});
});
});
-6
View File
@@ -1,6 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
module.exports = function format (value: any): any {
return value;
};
-13
View File
@@ -1,13 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./noop');
describe('format/noop', () => {
it('returns input value as output value', () => {
const input = { 'some': 'object' };
expect(
format(input)
).to.equal(input);
});
});
-18
View File
@@ -1,18 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatOutputType, InterfaceOutputType } from '@polkadot/jsonrpc/src/types';
import type { FormatterFunction } from '../types';
const formatNoop = require('../noop');
const util = require('../util');
const formatters: { [FormatOutputType]: FormatterFunction } = {
'Header': formatNoop,
'OutData': formatNoop,
'StorageData': formatNoop
};
module.exports = function format (output: InterfaceOutputType, value: any): any {
return util.format(formatters, output.type, value);
};
-13
View File
@@ -1,13 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./index');
describe('format/output', () => {
describe('format', () => {
it('formats the value', () => {
expect(
format('Header', 'test')
).to.deep.equal('test');
});
});
});
-4
View File
@@ -1,4 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
export type FormatterFunction = (value: any) => any;
-36
View File
@@ -1,36 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatterFunction } from './types';
const { isUndefined } = require('@polkadot/util/lib/is');
const formatNoop = require('./noop');
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
const formatter = formatters[type];
if (isUndefined(formatter)) {
console.warn(`Unable to find default formatter for '${type}', falling back to noop`);
return formatNoop(value);
}
try {
return formatter(value);
} catch (error) {
throw new Error(`Error formatting '${value}' as '${type}'': ${error.message}`);
}
}
function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {
return types.map((type, index) => {
const value = values[index];
return format(formatters, type, value);
});
}
module.exports = {
format,
formatArray
};
-59
View File
@@ -1,59 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
const sinon = require('sinon');
const { format, formatArray } = require('./util');
describe('format/util', () => {
let formatters;
let addressStub;
let warnSpy;
beforeEach(() => {
addressStub = sinon.stub();
formatters = {
'Address': addressStub,
'Exception': () => {
throw new Error('something went wrong');
}
};
warnSpy = sinon.spy(console, 'warn');
});
afterEach(() => {
console.warn.restore();
});
describe('format', () => {
it('formats unknown types with a fallback', () => {
expect(
format(formatters, 'Unknown', 'test')
).to.equal('test');
});
it('logs a warning with unknown types', () => {
format(formatters, 'Unknown', 'test');
expect(warnSpy).to.have.been.calledWith("Unable to find default formatter for 'Unknown', falling back to noop");
});
it('formats known types using the supplied formatter', () => {
format(formatters, 'Address', '0xaddress');
expect(addressStub).to.have.been.calledWith('0xaddress');
});
it('wraps exceptions with the type', () => {
expect(
() => format(formatters, 'Exception', 'test')
).to.throw(/Error formatting 'test' as 'Exception'/);
});
});
describe('formatArray', () => {
it('formats each value in an array', () => {
expect(
formatArray(formatters, ['Unknown', 'Unknown'], ['test', 'blah'])
).to.deep.equal(['test', 'blah']);
});
});
});
+15
View File
@@ -0,0 +1,15 @@
// ISC, Copyright 2017 Jaco Greeff
/* eslint-disable no-unused-expressions */
describe('polyfill', () => {
it('adds polyfills with no exceptions', () => {
expect(require('./polyfill')).to.be.ok;
});
describe('interfaces', () => {
it('fetch is available', () => {
expect(fetch).to.be.ok;
});
});
});
+21 -2
View File
@@ -4,6 +4,7 @@
const { mockWs, TEST_WS_URL } = require('../../test/mockWs');
const { isUndefined } = require('@polkadot/util/lib/is');
const sinon = require('sinon');
const Ws = require('./ws');
@@ -11,8 +12,8 @@ const Ws = require('./ws');
let ws;
let mock;
function createWs (requests, autoConnect = true) {
mock = autoConnect
function createWs (requests, autoConnect) {
mock = autoConnect || isUndefined(autoConnect)
? mockWs(requests)
: null;
@@ -69,6 +70,24 @@ describe('provider/Ws', () => {
clock.restore();
});
it('does not reconnect when autoConnect false', () => {
mock.done();
ws = createWs([], false);
const clock = sinon.useFakeTimers();
ws.connect = sinon.stub();
ws._onClose();
expect(ws.connect).not.to.have.been.called;
clock.tick(1001);
expect(ws.connect).not.to.have.been.called;
clock.restore();
});
});
describe('_onError', () => {