diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index 40d2a32ba7..fde3fd68d7 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -5,7 +5,7 @@ import { ApiTypes, DecorateMethod, ObsInnerType } from '@polkadot/api/types'; import { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import { AccountId, ContractExecResult } from '@polkadot/types/interfaces'; -import { AnyJson, Codec, CodecArg, IKeyringPair } from '@polkadot/types/types'; +import { AnyJson, CodecArg, IKeyringPair } from '@polkadot/types/types'; import { ApiObject, AbiMessage, ContractCallOutcome } from '../types'; import BN from 'bn.js'; @@ -82,28 +82,12 @@ export default class Contract extends Base { value }) ).pipe( - map((result: ContractExecResult): ContractCallOutcome => { - let output: Codec | null = null; - - if (result.isSuccess) { - const { data } = result.asSuccess; - - output = messageFn.returnType - ? formatData(this.registry, data, messageFn.returnType) - : this.registry.createType('Raw', data); - } - - return { - isSuccess: result.isSuccess, - message: messageFn, - origin: this.registry.createType('AccountId', account), - output, - params, - result, - time: Date.now(), - type: messageFn.returnType || null - }; - }) + map((result: ContractExecResult): ContractCallOutcome => ({ + output: result.isSuccess && messageFn.returnType + ? formatData(this.registry, result.asSuccess.data, messageFn.returnType) + : null, + result + })) ) ) }; diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 1f62d1d38f..3025624d0c 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { ApiTypes } from '@polkadot/api/types'; -import { AccountId, Address, ContractExecResult } from '@polkadot/types/interfaces'; +import { ContractExecResult } from '@polkadot/types/interfaces'; import { Codec, CodecArg, TypeDef } from '@polkadot/types/types'; import { ApiPromise, ApiRx } from '@polkadot/api'; @@ -46,18 +46,7 @@ export interface InterfaceContractCalls { [index: string]: Function; } -export interface InterfaceContract { - readonly address: Address; - readonly calls: InterfaceContractCalls; -} - export interface ContractCallOutcome { - isSuccess: boolean; - message: AbiMessage; - origin: AccountId; output: Codec | null; - params: any[]; result: ContractExecResult; - time: number; - type: TypeDef | null; } diff --git a/packages/types/src/codec/utils/encodeTypes.ts b/packages/types/src/codec/utils/encodeTypes.ts index 0aa88566eb..f39798abf5 100644 --- a/packages/types/src/codec/utils/encodeTypes.ts +++ b/packages/types/src/codec/utils/encodeTypes.ts @@ -7,16 +7,14 @@ import { assert, isNumber, isUndefined } from '@polkadot/util'; export const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance']; -const identity = (value: string): string => value; +const identity = (value: T): T => value; export function paramsNotation (outer: string, inner?: string | any[], transform: (_: any) => string = identity): string { - let arrayStr = ''; - - if (inner) { - arrayStr = '<' + (Array.isArray(inner) ? inner : [inner]).map(transform).join(', ') + '>'; - } - - return `${outer}${arrayStr}`; + return `${outer}${ + inner + ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` + : '' + }`; } // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment @@ -32,12 +30,9 @@ function encodeWithParams (typeDef: Pick, outer = typeDef.displayN case TypeDefInfo.Option: case TypeDefInfo.Result: case TypeDefInfo.Vec: - return paramsNotation( - outer, - params || sub, - // eslint-disable-next-line @typescript-eslint/no-use-before-define - (param: TypeDef) => encodeTypeDef(param) - ); + // eslint-disable-next-line @typescript-eslint/no-use-before-define + return paramsNotation(outer, params || sub, (param) => encodeTypeDef(param)); + default: // eslint-disable-next-line @typescript-eslint/no-unsafe-return return outer; @@ -45,21 +40,15 @@ function encodeWithParams (typeDef: Pick, outer = typeDef.displayN } function encodeDoNotConstruct ({ displayName }: TypeDef): string { - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - return `DoNotEncode<${displayName}>`; + return `DoNotEncode<${displayName || 'Unknown'}>`; } function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string { - const inner = sub.reduce( - (result: Record, type: TypeDef): Record => { - return { - ...result, - // eslint-disable-next-line @typescript-eslint/no-use-before-define - [type.name as string]: encodeTypeDef(type) - }; - }, - {} - ); + const inner = sub.reduce((result: Record, type): Record => ({ + ...result, + // eslint-disable-next-line @typescript-eslint/no-use-before-define + [type.name as string]: encodeTypeDef(type) + }), {}); return JSON.stringify( asEnum @@ -83,18 +72,14 @@ function encodeEnum (typeDef: Pick): string { function encodeStruct (typeDef: Pick): string { assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Struct type'); - const sub = typeDef.sub as TypeDef[]; - - return encodeSubTypes(sub); + return encodeSubTypes(typeDef.sub as TypeDef[]); } function encodeTuple (typeDef: Pick): string { assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Tuple type'); - const sub = typeDef.sub as TypeDef[]; - // eslint-disable-next-line @typescript-eslint/no-use-before-define - return `(${sub.map((type: TypeDef) => encodeTypeDef(type)).join(', ')})`; + return `(${(typeDef.sub as TypeDef[]).map((type) => encodeTypeDef(type)).join(', ')})`; } function encodeUInt ({ length }: Pick, type: 'Int' | 'UInt'): string {