diff --git a/packages/types/src/Method.spec.ts b/packages/types/src/Method.spec.ts index 15ec24fcf7..fff6931420 100644 --- a/packages/types/src/Method.spec.ts +++ b/packages/types/src/Method.spec.ts @@ -25,4 +25,35 @@ describe('Method', () => { }).toU8a() ).toEqual(new Uint8Array([1, 2])); }); + + it('handles creation from a hex value properly', () => { + expect( + new Method('0x0102').toU8a() + ).toEqual(new Uint8Array([1, 2])); + }); + + describe('hasOrigin', () => { + const test = { + args: [], + callIndex: [1, 2] + }; + + it('is false with no arguments', () => { + expect( + new Method(test, { arguments: [] } as any).hasOrigin + ).toEqual(false); + }); + + it('is false with first argument as non-Origin', () => { + expect( + new Method(test, { arguments: [{ name: 'a', type: 'u32' }] } as any).hasOrigin + ).toEqual(false); + }); + + it('is false with first argument as non-Origin', () => { + expect( + new Method(test, { arguments: [{ name: 'a', type: 'Origin' }] } as any).hasOrigin + ).toEqual(true); + }); + }); }); diff --git a/packages/types/src/Method.ts b/packages/types/src/Method.ts index 636a058df7..cf65515b3c 100644 --- a/packages/types/src/Method.ts +++ b/packages/types/src/Method.ts @@ -4,7 +4,7 @@ import { AnyU8a, Codec, Constructor } from './types'; -import { assert, isHex, isObject, isU8a } from '@polkadot/util'; +import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util'; import { getTypeDef, getTypeClass } from './codec/createType'; import Struct from './codec/Struct'; @@ -93,7 +93,7 @@ export default class Method extends Struct { */ private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: FunctionMetadata): DecodedMethod { if (isHex(value)) { - return Method.decodeMethod(value, _meta); + return Method.decodeMethod(hexToU8a(value), _meta); } else if (isU8a(value)) { // The first 2 bytes are the callIndex const callIndex = value.subarray(0, 2); @@ -219,6 +219,15 @@ export default class Method extends Struct { return (this.get('args') as Struct).toU8a(); } + /** + * @description `true` if the `Origin` type is on the method (extrinsic method) + */ + get hasOrigin (): boolean { + const firstArg = this.meta.arguments[0]; + + return !!firstArg && firstArg.type.toString() === 'Origin'; + } + /** * @description The [[FunctionMetadata]] */