Add Codec toPrimitive (#5080)

This commit is contained in:
Jaco
2022-07-18 14:10:46 +03:00
committed by GitHub
parent 434f969208
commit 91a04ad159
30 changed files with 250 additions and 14 deletions
+7
View File
@@ -53,6 +53,13 @@ export class GenericEthereumAccountId extends U8aFixed {
return this.toString();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): string {
return this.toJSON();
}
/**
* @description Returns the string representation of the value
*/
@@ -43,6 +43,7 @@ describe('Extrinsic', (): void => {
expect(extrinsic.callIndex).toEqual(new Uint8Array([6, 0]));
expect(extrinsic.args[0].toHex()).toEqual('0x00495e1e506f266418af07fa0c5c108dd436f2faa59fe7d9e54403779f5bbd7718');
expect(extrinsic.args[1].toHuman()).toEqual('104,560,923,320,000'); // ('104.5609 Unit');
expect(extrinsic.toPrimitive()).toEqual({ method: { args: { dest: { id: '5DiuK2zR4asj2CEh77SKtUgTswTLkD8eiAKrByg5G3wL5w9b' }, value: 104560923320000 }, callIndex: '0x0600' }, signature: { era: { mortalEra: [1024, 186] }, nonce: 68, signature: { ed25519: '0xd99ffe3e610ad234e1414bda5831395a6df9098bf80b01561ce89a5065ae89d5c10e1619c6c99131b0bea4fb73ef04d07c07770e2ae9df5c325c331769ccb300' }, signer: { id: '5Hn8KKEp8qruCGWaN9MEsjTs4FXB4wv9xn7g1RWkNeKKNXCr' }, tip: 30000000000 } });
});
});
});
+7
View File
@@ -62,6 +62,13 @@ export class GenericAccountId extends U8aFixed {
return this.toString();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): string {
return this.toJSON();
}
/**
* @description Returns the string representation of the value
*/
@@ -104,6 +104,13 @@ export class GenericAccountIndex extends u32 {
return this.toString();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): string {
return this.toJSON();
}
/**
* @description Returns the string representation of the value
*/
+7
View File
@@ -104,6 +104,13 @@ export class GenericCallIndex extends U8aFixed {
constructor (registry: Registry, value?: AnyU8a) {
super(registry, value, 16);
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): string {
return this.toHex();
}
}
/**
+14 -6
View File
@@ -7,7 +7,7 @@ import { GenericVote } from '.';
describe('GenericVote', (): void => {
const registry = new TypeRegistry();
describe.only('construction', (): void => {
describe('construction', (): void => {
it('constructs via boolean true', (): void => {
expect(new GenericVote(registry, true).toU8a()).toEqual(new Uint8Array([128]));
expect(new GenericVote(registry, true).isAye).toBe(true);
@@ -58,7 +58,7 @@ describe('GenericVote', (): void => {
expect(new GenericVote(registry, new Uint8Array([1])).isNay).toBe(true);
});
it.only('constructs via Uint8Array (aye)', (): void => {
it('constructs via Uint8Array (aye)', (): void => {
const test = new GenericVote(registry, new Uint8Array([0b10000010]));
expect(test.isNay).toBe(false);
@@ -68,12 +68,20 @@ describe('GenericVote', (): void => {
describe('Vote with conviction', (): void => {
it('constructs Vote with raw boolean', (): void => {
const vote = new GenericVote(registry, {
aye: true,
conviction: 'Locked1x'
});
expect(
new GenericVote(registry, {
aye: true,
conviction: 'Locked1x'
}).toU8a()
vote.toU8a()
).toEqual(new Uint8Array([0b10000001]));
expect(
vote.toPrimitive()
).toEqual({
aye: true,
conviction: 'Locked1x'
});
});
it('constructs with Vote aye is false, conviction is None', (): void => {
+10
View File
@@ -115,6 +115,16 @@ export class GenericVote extends U8aFixed {
};
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): any {
return {
aye: this.isAye,
conviction: this.conviction.toPrimitive()
};
}
/**
* @description Returns the base runtime type name for this instance
*/