Remove non-API specific contract call results (#2704)

This commit is contained in:
Jaco Greeff
2020-10-11 11:26:23 +02:00
committed by GitHub
parent a1bab9edcf
commit 4b8634b88e
3 changed files with 25 additions and 67 deletions
+7 -23
View File
@@ -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<ApiType extends ApiTypes> extends Base<ApiType> {
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
}))
)
)
};
+1 -12
View File
@@ -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;
}
+17 -32
View File
@@ -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 = <T> (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<TypeDef, any>, 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<TypeDef, any>, 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<string, string>, type: TypeDef): Record<string, string> => {
return {
...result,
// eslint-disable-next-line @typescript-eslint/no-use-before-define
[type.name as string]: encodeTypeDef(type)
};
},
{}
);
const inner = sub.reduce((result: Record<string, string>, type): Record<string, string> => ({
...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<TypeDef, any>): string {
function encodeStruct (typeDef: Pick<TypeDef, any>): 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<TypeDef, any>): 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<TypeDef, any>, type: 'Int' | 'UInt'): string {