Expand ExtrinsicStatus to mirror Rust types (#494)

* Expand ExtrinsicStatus to mirror Rust types

* Remove .only

* Update test expectation

* typo

* Invalid extends Null

* Remove debug log
This commit is contained in:
Jaco Greeff
2018-12-19 13:39:20 +01:00
committed by GitHub
parent 274359eac8
commit 33a4280028
8 changed files with 68 additions and 28 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
# 0.34.1
- Changed the send signature (for future expansion of eg. events) to return `result: { status: ExtrinsicStatus }` instead of `status: ExtrinsicStatus`. For most cases where only status `toString` checks are used, i.e. `status.toString() === 'Finalised'` this should not be a breaking change. Deep inspection of the status object however will need to adapt.
- Changed the send signature (for future expansion of eg. events) to return `result: { status: ExtrinsicStatus }` instead of `status: ExtrinsicStatus`. For most cases where only status `type` checks are used, i.e. `status.type === 'Finalised'` this should not be a breaking change. Deep inspection of the status object however will need to adapt.
@@ -10,8 +10,7 @@ import { Extrinsic, ExtrinsicStatus, Hash } from '@polkadot/types/index';
type SendResult = {
status: ExtrinsicStatus,
// toString for backwards compat, i.e. result.toString() === 'finalised'
toString (): string
type: string
};
export default class SubmittableExtrinsic extends Extrinsic {
@@ -31,7 +30,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
return this._api.rpc.author.submitAndWatchExtrinsic(this, (status: ExtrinsicStatus) =>
statusCb({
status,
toString: status.toString
type: status.type
})
);
}
+2 -3
View File
@@ -12,8 +12,7 @@ import { Extrinsic, ExtrinsicStatus } from '@polkadot/types/index';
type SendResult = {
status: ExtrinsicStatus,
// toString for backwards compat, i.e. result.toString() === 'finalised'
toString (): string
type: string
};
export default class SubmittableExtrinsic extends Extrinsic {
@@ -31,7 +30,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
.pipe(
map((status: ExtrinsicStatus) => ({
status,
toString: status.toString
type: status.type
}))
);
}
+6 -8
View File
@@ -22,17 +22,15 @@ describe.skip('e2e transactions', () => {
});
it('makes a transfer', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.alice.address());
const nonce = await api.query.system.accountNonce(keyring.dave.address());
await api.tx.balances
.transfer(keyring.bob.address(), 12345)
.sign(keyring.alice, nonce)
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, nonce)
.send((status) => {
expect(
status.type.toString()
).toEqual('Finalised');
done();
if (status.type === 'Finalised') {
done();
}
});
});
+2 -2
View File
@@ -34,7 +34,7 @@ describe.skip('e2e transactions', () => {
)
)
.subscribe((status) => {
if (status && status.type.toString() === 'Finalised') {
if (status.type === 'Finalised') {
done();
}
});
@@ -53,7 +53,7 @@ describe.skip('e2e transactions', () => {
)
)
.subscribe((status) => {
if (status && status.type.toString() === 'Finalised') {
if (status && status.type === 'Finalised') {
done();
}
});
+29 -2
View File
@@ -32,6 +32,30 @@ export class Dropped extends Null {
export class Finalised extends Hash {
}
/**
* @name Future
* @description
* An [[ExtrinsicStatus]] indicating that the [[Extrinsic]] has been added to the future queue
*/
export class Future extends Null {
}
/**
* @name Ready
* @description
* An [[ExtrinsicStatus]] indicating that the [[Extrinsic]] has been added to the ready queue
*/
export class Ready extends Null {
}
/**
* @name Invalid
* @description
* An [[ExtrinsicStatus]] indicating that the [[Extrinsic]] is invalid
*/
export class Invalid extends Null {
}
/**
* @name Usurped
* @description
@@ -45,13 +69,16 @@ export class Usurped extends Hash {
* @description
* An [[EnumType]] that indicates the status of the [[Extrinsic]] as been submitted
*/
export default class ExtrinsicStatus extends EnumType<Finalised | Usurped | Broadcast | Dropped> {
export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalised | Usurped | Broadcast | Dropped | Invalid> {
constructor (value: any, index?: number) {
super({
Future,
Ready,
Finalised,
Usurped,
Broadcast,
Dropped
Dropped,
Invalid
}, value, index);
}
}
+10
View File
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import EnumType from './EnumType';
import Null from '../Null';
import Text from '../Text';
import U32 from '../U32';
@@ -43,6 +44,15 @@ describe('Struct', () => {
).toEqual('42');
});
it('decodes from JSON string', () => {
expect(
new EnumType(
{ Null, U32 },
'null'
).type
).toEqual('Null');
});
// We are currently not using this approach, none of the types in Substrate currently
// have any overrides. Insteda of trying to support it (just-in-case), rather have it
// removed to simplyfy the code - it can be pulled-back if needed
+16 -9
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert, isNumber, isObject, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, isNumber, isObject, isString, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import Base from './Base';
import Null from '../Null';
@@ -60,23 +60,30 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
return EnumType.createValue(def, value[0], value.subarray(1));
} else if (isNumber(value)) {
return EnumType.createValue(def, value);
} else if (isString(value)) {
return EnumType.createViaJSON(def, value.toString());
} else if (isObject(value)) {
// JSON comes in the form of { "<type (lowercased)>": "<value for type>" }, here we
// additionally force to lower to ensure forward compat
const key = Object.keys(value)[0];
const lowerKey = key.toLowerCase();
const keys = Object.keys(def).map((k) => k.toLowerCase());
const index = keys.indexOf(lowerKey);
assert(index !== -1, 'Unable to reliably map input on JSON');
return EnumType.createValue(def, index, value[key]);
return EnumType.createViaJSON(def, key, value[key]);
}
// Worst-case scenario, return the first with default
return EnumType.createValue(def, 0);
}
private static createViaJSON (def: TypesDef, key: string, value?: any) {
// JSON comes in the form of { "<type (lowercased)>": "<value for type>" }, here we
// additionally force to lower to ensure forward compat
const lowerKey = key.toLowerCase();
const keys = Object.keys(def).map((k) => k.toLowerCase());
const index = keys.indexOf(lowerKey);
assert(index !== -1, 'Unable to reliably map input on JSON');
return EnumType.createValue(def, index, value);
}
private static createValue (def: TypesDef, index: number, value?: any): Decoded {
return {
index,