Don't use fromJSON/fromU8a anywhere in code (#298)

* Don't use fromJSON/u8a anywhere

* Remove useless cast

* Nicer formatting
This commit is contained in:
Amaury Martiny
2018-10-16 12:35:40 +02:00
committed by Jaco Greeff
parent 484ab9401c
commit 106c59d341
22 changed files with 37 additions and 39 deletions
+1 -1
View File
@@ -177,7 +177,7 @@ export default class Rpc implements RpcInterface {
}
private formatOutput (method: RpcMethod, params: Array<Base>, result?: any): Base | Array<Base | null | undefined> {
const base = createType(method.type as string).fromJSON(result);
const base = createType(method.type as string, result);
if (method.type === 'StorageData') {
// single return value (via state.getStorage), decode the value based on the
@@ -8,7 +8,7 @@ import json from '@polkadot/types/Metadata.rpc';
import fromMetadata from './fromMetadata';
// Use the pre-generated metadata
const metadata = new Metadata().fromJSON(json);
const metadata = new Metadata(json);
const newExtrinsics = fromMetadata(metadata);
describe('fromMetadata', () => {
+1 -1
View File
@@ -8,5 +8,5 @@ import Metadata from '@polkadot/types/Metadata';
import fromMetadata from './fromMetadata';
export default fromMetadata(
new Metadata().fromJSON(metadataRpc)
new Metadata(metadataRpc)
);
@@ -8,7 +8,7 @@ import json from '@polkadot/types/Metadata.rpc';
import fromMetadata from './fromMetadata';
// Use the pre-generated metadata
const metadata = new Metadata().fromJSON(json);
const metadata = new Metadata(json);
const newStorage = fromMetadata(metadata);
describe('fromMetadata', () => {
+1 -1
View File
@@ -8,5 +8,5 @@ import Metadata from '@polkadot/types/Metadata';
import fromMetadata from './fromMetadata';
export default fromMetadata(
new Metadata().fromJSON(metadataRpc)
new Metadata(metadataRpc)
);
+1 -1
View File
@@ -20,7 +20,7 @@ const createRuntimeFunction = (method: string, key: string, { documentation, typ
new Text(key),
{
documentation: new Vector(Text, [documentation]),
modifier: new StorageFunctionModifier().fromJSON(0),
modifier: new StorageFunctionModifier(0),
type: new StorageFunctionType(type, 0),
toJSON: (): any =>
key
+1 -1
View File
@@ -35,7 +35,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
} else if (Array.isArray(value)) {
return Address.decodeAddress(u8aToU8a(value));
} else if (isU8a(value)) {
if (value.length === 33 && value[0] === 0xff) {
if (value[0] === 0xff) {
return new AccountId(value.subarray(1));
}
return value.length === 32
+1 -1
View File
@@ -6,7 +6,7 @@ import rpc from './ExtrinsicStatus.rpc.json';
import ExtrinsicStatus from './ExtrinsicStatus';
describe('ExtrinsicStatus', () => {
const status = new ExtrinsicStatus().fromJSON(rpc.params.result);
const status = new ExtrinsicStatus(rpc.params.result);
it('has the correct type', () => {
expect(
+1 -3
View File
@@ -7,9 +7,7 @@ import json from './Header.only.json';
describe('Header', () => {
it('decodes an actual JSON response', () => {
const header = new Header();
header.fromJSON(json);
const header = new Header(json);
expect(
header.blockNumber.toNumber()
+2 -2
View File
@@ -9,7 +9,7 @@ import rpcdata from './Metadata.rpc';
describe('Metadata', () => {
it('decodes properly', () => {
const decoded = new Metadata().fromJSON(rpcdata);
const decoded = new Metadata(rpcdata);
const str = JSON.stringify(decoded.toJSON());
console.error(str);
@@ -20,7 +20,7 @@ describe('Metadata', () => {
it('decodes when length not present (HACK)', () => {
const u8a = hexToU8a(rpcdata);
const decoded = new Metadata().fromU8a(u8a.subarray(2));
const decoded = new Metadata(u8a.subarray(2));
expect(decoded.events.length).not.toBe(0);
});
+1 -1
View File
@@ -55,7 +55,7 @@ export default class Method extends MethodIndex {
let offset = 0;
return Method.filterOrigin(meta).map(({ type }) => {
const base = createType(type).fromU8a(data.subarray(offset));
const base = createType(type, data.subarray(offset));
offset += base.byteLength();
+1 -1
View File
@@ -6,7 +6,7 @@ import rpc from './RuntimeVersion.rpc.json';
import RuntimeVersion from './RuntimeVersion';
describe('RuntimeVersion', () => {
const version = new RuntimeVersion().fromJSON(rpc.result);
const version = new RuntimeVersion(rpc.result);
it('has the correct authoring', () => {
expect(version.authoringVersion.toNumber()).toEqual(1);
+2 -2
View File
@@ -14,7 +14,7 @@ describe('SignedBlock', () => {
beforeEach(() => {
Method.injectExtrinsics(extrinsics);
block = new SignedBlock().fromJSON(rpc.result);
block = new SignedBlock(rpc.result);
});
it('has the correct stateRoot', () => {
@@ -40,7 +40,7 @@ describe('SignedBlock', () => {
const x = extrinsics.get(0);
expect(x.callIndex).toEqual(new Uint8Array([2, 0]));
expect(x.method.args[0].toU8a()).toEqual(new Uint8Array([ 70, 41, 195, 91, 0, 0, 0, 0 ]));
expect(x.method.args[0].toU8a()).toEqual(new Uint8Array([70, 41, 195, 91, 0, 0, 0, 0]));
});
it('has the correct data for the second', () => {
+3 -3
View File
@@ -8,19 +8,19 @@ import Type from './Type';
describe('Type', () => {
it('fails to cleanup invalid boxes', () => {
expect(
() => new Type().fromJSON('Box<Proposal')
() => new Type('Box<Proposal')
).toThrow(/find closing matching/);
});
it('handles nested types', () => {
expect(
new Type().fromJSON('Box<Vec<AccountId>>').toString()
new Type('Box<Vec<AccountId>>').toString()
).toEqual('Vec<AccountId>');
});
it('handles aliasses, multiples per line', () => {
expect(
new Type().fromJSON('(Vec<u8>, AccountId, Vec<u8>)').toString()
new Type('(Vec<u8>, AccountId, Vec<u8>)').toString()
).toEqual('(Bytes, AccountId, Bytes)');
});
+1 -1
View File
@@ -104,7 +104,7 @@ describe('Compact', () => {
it('constructs properly via fromU8a', () => {
expect(
new Compact().fromU8a(new Uint8Array([254, 255, 3, 0])).raw
new Compact(new Uint8Array([254, 255, 3, 0])).raw
).toEqual(new BN(0xffff));
});
+2 -2
View File
@@ -44,13 +44,13 @@ describe('Enum', () => {
it('converts to and from U8a', () => {
expect(
new Enum(['foo', 'bar']).fromU8a(new Uint8Array([1])).toU8a()
new Enum(['foo', 'bar'], new Uint8Array([1])).toU8a()
).toEqual(new Uint8Array([1]));
});
it('converts from JSON', () => {
expect(
new Enum(['foo', 'bar']).fromJSON(5).toString()
new Enum(['foo', 'bar'], 5).toString()
).toEqual('5');
});
});
+9 -6
View File
@@ -10,24 +10,27 @@ describe('Struct', () => {
it('provides a clean toString() (raw)', () => {
expect(
new EnumType(
[Text, U32]
).fromU8a(new Uint8Array([0, 2 << 2, 49, 50])).raw.toString()
[Text, U32],
new Uint8Array([0, 2 << 2, 49, 50])
).raw.toString()
).toEqual('12');
});
it('provides a clean toString() (enum)', () => {
expect(
new EnumType(
[Text, U32]
).fromU8a(new Uint8Array([1, 2 << 2, 49, 50])).toString()
[Text, U32],
new Uint8Array([1, 2 << 2, 49, 50])
).toString()
).toEqual('U32');
});
it('allows checking against defined indexes', () => {
expect(
new EnumType(
{ 1: Text, 5: U32 }
).fromU8a(new Uint8Array([1, 2 << 2, 49, 50])).toString()
{ 1: Text, 5: U32 },
new Uint8Array([1, 2 << 2, 49, 50])
).toString()
).toEqual('Text');
});
});
+1 -1
View File
@@ -44,7 +44,7 @@ describe('Option', () => {
it('has value toString() (provided)', () => {
expect(
new Option(Text).fromU8a(new Uint8Array([1, 4 << 2, 49, 50, 51, 52])).toString()
new Option(Text, new Uint8Array([1, 4 << 2, 49, 50, 51, 52])).toString()
).toEqual('1234');
});
+1 -1
View File
@@ -39,7 +39,7 @@ describe('Vector', () => {
it('allows contruction via JSON', () => {
expect(
new Vector(Text).fromJSON(['6', '7']).toString()
new Vector(Text, ['6', '7']).toString()
).toEqual('[6, 7]');
});
+2 -5
View File
@@ -44,12 +44,9 @@ export default class Vector<
const result = [];
for (let index = 0; index < length; index++) {
// FIXME replace by
// const decoded = new Type(u8a.subarray(offset));
// @ts-ignore Not sure why we get "Property 'fromU8a' does not exist on type 'T'.", T extends Base in def?
const decoded = new Type().fromU8a(u8a.subarray(offset));
const decoded = new Type(u8a.subarray(offset));
result.push(decoded as T);
result.push(decoded);
offset += decoded.byteLength();
}
@@ -8,7 +8,7 @@ import Metadata from '../Metadata';
import rpcdata from '../Metadata.rpc';
function metadataStringifyAsJson () {
const decoded = new Metadata().fromJSON(rpcdata);
const decoded = new Metadata(rpcdata);
return JSON.stringify(decoded.toJSON(), null, 2);
}
+2 -2
View File
@@ -123,12 +123,12 @@ function addStorage (metadata: any) {
}
function metadataStorageMethodsAsText () {
const metadata = new Metadata().fromJSON(rpcdata);
const metadata = new Metadata(rpcdata);
return addStorage(metadata);
}
function metadataExtrinsicsMethodsAsText () {
const metadata = new Metadata().fromJSON(rpcdata);
const metadata = new Metadata(rpcdata);
return addExtrinsics(metadata);
}