Skip to content

Commit

Permalink
class Struct extends Map (#321)
Browse files Browse the repository at this point in the history
* Rename to getAtIndex

* Add codec interface

* Fix stuff

* Fix struct

* Allow ts

* Add useful types

* Fix related types

* Fix test

* Small fix

* Use this.get everywhere

* Fix import

* Typos

* Fix ts ignore

* Add more tests

* Update polkadot dev

* Don't change jest config

* Fix eol
  • Loading branch information
amaury1093 authored and jacogr committed Oct 19, 2018
1 parent 174978a commit f2287f3
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 208 deletions.
18 changes: 9 additions & 9 deletions packages/api-observable/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export class RxProposal extends Struct.with({ id: PropIndex, proposal: Proposal,
}

get address (): AccountId {
return this.raw.address as AccountId;
return this.get('address') as AccountId;
}

get id (): PropIndex {
return this.raw.id as PropIndex;
return this.get('id') as PropIndex;
}

get proposal (): Proposal {
return this.raw.proposal as Proposal;
return this.get('proposal') as Proposal;
}
}

Expand All @@ -37,11 +37,11 @@ export class RxProposalDeposits extends Struct.with({ balance: Balance, addresse
}

get addresses (): Vector<AccountId> {
return this.raw.addresses as Vector<AccountId>;
return this.get('addresses') as Vector<AccountId>;
}

get balance (): Balance {
return this.raw.balance as Balance;
return this.get('balance') as Balance;
}
}

Expand All @@ -56,18 +56,18 @@ export class RxReferendum extends Struct.with({ blockNumber: BlockNumber, propos
}

get blockNumber (): BlockNumber {
return this.raw.blockNumber as BlockNumber;
return this.get('blockNumber') as BlockNumber;
}

get id (): ReferendumIndex {
return this.raw.id as ReferendumIndex;
return this.get('id') as ReferendumIndex;
}

get proposal (): Proposal {
return this.raw.proposal as Proposal;
return this.get('proposal') as Proposal;
}

get voteThreshold (): VoteThreshold {
return this.raw.voteThreshold as VoteThreshold;
return this.get('voteThreshold') as VoteThreshold;
}
}
14 changes: 7 additions & 7 deletions packages/types/src/Bft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export class BftAuthoritySignature extends Tuple {
}

get authorityId (): AuthorityId {
return this.raw.authorityId as AuthorityId;
return this.getAtIndex(0) as AuthorityId;
}

get signature (): Signature {
return this.raw.signature as Signature;
return this.getAtIndex(1) as Signature;
}
}

Expand All @@ -52,11 +52,11 @@ export class BftHashSignature extends Tuple {
}

get hash (): Hash {
return this.raw.hash as Hash;
return this.getAtIndex(0) as Hash;
}

get signature (): Signature {
return this.raw.signature as Signature;
return this.getAtIndex(1) as Signature;
}
}

Expand All @@ -78,14 +78,14 @@ export class Justification extends Struct {
}

get hash (): Hash {
return this.raw.hash as Hash;
return this.get('hash') as Hash;
}

get round (): U32 {
return this.raw.round as U32;
return this.get('round') as U32;
}

get signatures (): Vector<BftAuthoritySignature> {
return this.raw.signatures as Vector<BftAuthoritySignature>;
return this.get('signatures') as Vector<BftAuthoritySignature>;
}
}
4 changes: 2 additions & 2 deletions packages/types/src/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Block extends Struct {
}

get extrinsics (): Extrinsics {
return this.raw.extrinsics as Extrinsics;
return this.get('extrinsics') as Extrinsics;
}

// convenience, encodes the header and returns the actual hash
Expand All @@ -37,6 +37,6 @@ export default class Block extends Struct {
}

get header (): Header {
return this.raw.header as Header;
return this.get('header') as Header;
}
}
4 changes: 2 additions & 2 deletions packages/types/src/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export default class Extrinsic extends Struct {
}

get method (): Method {
return this.raw.method as Method;
return this.get('method') as Method;
}

get signature (): ExtrinsicSignature {
return this.raw.signature as ExtrinsicSignature;
return this.get('signature') as ExtrinsicSignature;
}

get encodedLength (): number {
Expand Down
16 changes: 8 additions & 8 deletions packages/types/src/ExtrinsicSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ export default class ExtrinsicSignature extends Struct {
}

get era (): ExtrinsicEra {
return this.raw.era as ExtrinsicEra;
return this.get('era') as ExtrinsicEra;
}

get nonce (): Nonce {
return this.raw.nonce as Nonce;
return this.get('nonce') as Nonce;
}

get signature (): Signature {
return this.raw.signature as Signature;
return this.get('signature') as Signature;
}

get signer (): Address {
return this.raw.signer as Address;
return this.get('signer') as Address;
}

get version (): number {
Expand All @@ -112,10 +112,10 @@ export default class ExtrinsicSignature extends Struct {
});
const signature = new Signature(signingPayload.sign(signerPair));

this.raw.era = signingPayload.era;
this.raw.nonce = signingPayload.nonce;
this.raw.signer = signer;
this.raw.signature = signature;
this.set('era', signingPayload.era);
this.set('nonce', signingPayload.nonce);
this.set('signer', signer);
this.set('signature', signature);

return this;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/types/src/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Digest extends Struct {
}

get logs (): Vector<Bytes> {
return this.raw.logs as Vector<Bytes>;
return this.get('logs') as Vector<Bytes>;
}
}

Expand All @@ -53,15 +53,15 @@ export default class Header extends Struct {
}

get blockNumber (): BlockNumber {
return this.raw.number as BlockNumber;
return this.get('number') as BlockNumber;
}

get digest (): Digest {
return this.raw.digest as Digest;
return this.get('digest') as Digest;
}

get extrinsicsRoot (): Hash {
return this.raw.extrinsicsRoot as Hash;
return this.get('extrinsicsRoot') as Hash;
}

// convenience, encodes the header and returns the actual hash
Expand All @@ -72,10 +72,10 @@ export default class Header extends Struct {
}

get parentHash (): Hash {
return this.raw.parentHash as Hash;
return this.get('parentHash') as Hash;
}

get stateRoot (): Hash {
return this.raw.stateRoot as Hash;
return this.get('stateRoot') as Hash;
}
}
8 changes: 4 additions & 4 deletions packages/types/src/KeyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export default class KeyValue extends Struct {
}

get key (): StorageKey {
return this.raw.key as StorageKey;
return this.get('key') as StorageKey;
}

get value (): StorageData {
return this.raw.value as StorageData;
return this.get('value') as StorageData;
}
}

Expand All @@ -53,10 +53,10 @@ export class KeyValueOption extends Tuple {
}

get key (): StorageKey {
return this.raw.key as StorageKey;
return this.getAtIndex(0) as StorageKey;
}

get value (): Option<StorageData> {
return this.raw.value as Option<StorageData>;
return this.getAtIndex(1) as Option<StorageData>;
}
}
Loading

0 comments on commit f2287f3

Please sign in to comment.