derive.{balances, contracts, staking, session} returns Codec (#953)

* derive.{balances, contracts, staking, session} returns Codec

* Json -> StructAny (as per VectorAny)

* multi returns VectorAny

* VectorAny casts

* Bump common
This commit is contained in:
Jaco Greeff
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 069fddee64
commit 773b8654eb
30 changed files with 265 additions and 352 deletions
+1 -1
View File
@@ -18,6 +18,7 @@ These are the base types of the codec. They are typically not used directly, but
| [[Option]] | An Option is an optional field. The first byte indicates that there is is value to follow |
| [[Set]] | An Set is an array of string values, represented an an encoded type by a bitwise representation of the values |
| [[Struct]] | A Struct defines an Object with key-value pairs - where the values are Codec values. |
| [[StructAny]] | Wraps the a JSON structure retrieve via RPC. It extends the standard JS Map |
| [[Tuple]] | A Tuple defines an anonymous fixed-length array, where each element has its own type |
| [[U8a]] | A basic wrapper around Uint8Array. It will consume the full Uint8Array as passed to it |
| [[U8aFixed]] | A U8a that manages a a sequence of bytes up to the specified bitLength |
@@ -150,7 +151,6 @@ These types are not used in the runtime, but are rather used in RPC results:
| [[ExtrinsicStatus]] | An EnumType that indicates the status of the Extrinsic as been submitted |
| [[Header]] | A [[Block]] header |
| [[Health]] | A system health indicator, reported back over RPC |
| [[Json]] | Wraps the a JSON structure retrieve via RPC. It extends the standard JS Map |
| [[NetworkState]] | Wraps the properties retrieved from the chain via the `system.network_state` RPC call |
| [[Metadata]] | The versioned runtime metadata as a decoded structure |
| [[PeerInfo]] | A system peer info indicator, reported back over RPC |
@@ -3,7 +3,10 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '../types';
import { compareMap } from '../codec/utils';
import { isUndefined } from '@polkadot/util';
import { compareMap } from './utils';
/**
* @name Json
@@ -13,9 +16,24 @@ import { compareMap } from '../codec/utils';
* i.e. no hex decoding. Unlike a struct, this waps a JSON object with unknown keys
* @noInheritDoc
*/
export default class Json extends Map<string, any> implements Codec {
export default class StructAny extends Map<string, any> implements Codec {
constructor (value?: { [index: string]: any } | null) {
super(Json.decodeJson(value));
const decoded = StructAny.decodeJson(value);
super(decoded);
// like we are doing with structs, add the keys as getters
decoded.forEach(([key]) => {
// do not clobber existing properties on the object
if (!isUndefined((this as any)[key])) {
return;
}
Object.defineProperty(this, key, {
enumerable: true,
get: () => this.get(key)
});
});
}
private static decodeJson (value?: { [index: string]: any } | null): Array<[string, any]> {
+2
View File
@@ -14,8 +14,10 @@ export { default as EnumType } from './EnumType';
export { default as Option } from './Option';
export { default as Set } from './Set';
export { default as Struct } from './Struct';
export { default as StructAny } from './StructAny';
export { default as Tuple } from './Tuple';
export { default as Vector } from './Vector';
export { default as VectorAny } from './VectorAny';
// Convenience base classes, used as "anything of this type" bases
export { default as U8a } from './U8a';
+2 -2
View File
@@ -2,14 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Json from './Json';
import StructAny from '../codec/StructAny';
/**
* @name ChainProperties
* @description
* Wraps the properties retrieved from the chain via the `system.properties` RPC call.
*/
export default class ChainProperties extends Json {
export default class ChainProperties extends StructAny {
/**
* @description The token decimals, if defined (de-facto standard only)
*/
+2 -2
View File
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Json from './Json';
import StructAny from '../codec/StructAny';
/**
* @name NetworkState
* @description
* Wraps the properties retrieved from the chain via the `system.network_state` RPC call.
*/
export default class NetworkState extends Json {
export default class NetworkState extends StructAny {
}
-1
View File
@@ -14,7 +14,6 @@ export { default as Digest, DigestItem } from './Digest';
export { default as ExtrinsicStatus } from './ExtrinsicStatus';
export { default as Header } from './Header';
export { default as Health } from './Health';
export { default as Json } from './Json';
export { default as NetworkState } from './NetworkState';
export { default as PeerInfo } from './PeerInfo';
export { default as PendingExtrinsics } from './PendingExtrinsics';