Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changes:
- Add `.multi` support on `api.at(...).<section>.<method>`
- Add support for ink! metadata V3 with payable constructors
- Cleanup ink! metadata parsing, allowing for easier extension
- Expose `contract.{query, tx}.<method>.meta`, aligning with API
- Fix storage metadata, aligning method with decorated name
- Adjust typegen, only using exportInterface
- Added Kusama/Polkadot 9151 upgrade block (known types)
Expand Down
1 change: 1 addition & 0 deletions packages/api-contract/src/Abi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class Abi {
identifier,
index,
method: stringCamelCase(identifier),
path: identifier.split('::').map((s) => stringCamelCase(s)),
selector: spec.selector,
toU8a: (params: unknown[]) =>
this.#encodeArgs(spec, args, params)
Expand Down
24 changes: 16 additions & 8 deletions packages/api-contract/src/base/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ const ERROR_NO_CALL = 'Your node does not expose the contracts.call RPC. This is

const l = logger('Contract');

function createQuery <ApiType extends ApiTypes> (fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult<ApiType, ContractCallOutcome>): ContractQuery<ApiType> {
return (origin: string | AccountId | Uint8Array, options: bigint | string | number | BN | ContractOptions, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome> =>
function withMeta <T extends { meta: AbiMessage }> (meta: AbiMessage, creator: Omit<T, 'meta'>): T {
(creator as T).meta = meta;

return creator as T;
}

function createQuery <ApiType extends ApiTypes> (meta: AbiMessage, fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult<ApiType, ContractCallOutcome>): ContractQuery<ApiType> {
return withMeta(meta, (origin: string | AccountId | Uint8Array, options: bigint | string | number | BN | ContractOptions, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome> =>
isOptions(options)
? fn(origin, options, params)
: fn(origin, ...extractOptions<ContractOptions>(options, params));
: fn(origin, ...extractOptions<ContractOptions>(options, params))
);
}

function createTx <ApiType extends ApiTypes> (fn: (options: ContractOptions, params: unknown[]) => SubmittableExtrinsic<ApiType>): ContractTx<ApiType> {
return (options: bigint | string | number | BN | ContractOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType> =>
function createTx <ApiType extends ApiTypes> (meta: AbiMessage, fn: (options: ContractOptions, params: unknown[]) => SubmittableExtrinsic<ApiType>): ContractTx<ApiType> {
return withMeta(meta, (options: bigint | string | number | BN | ContractOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType> =>
isOptions(options)
? fn(options, params)
: fn(...extractOptions<ContractOptions>(options, params));
: fn(...extractOptions<ContractOptions>(options, params))
);
}

export class ContractSubmittableResult extends SubmittableResult {
Expand Down Expand Up @@ -70,11 +78,11 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {

this.abi.messages.forEach((m): void => {
if (isUndefined(this.#tx[m.method])) {
this.#tx[m.method] = createTx((o, p) => this.#exec(m, o, p));
this.#tx[m.method] = createTx(m, (o, p) => this.#exec(m, o, p));
}

if (isUndefined(this.#query[m.method])) {
this.#query[m.method] = createQuery((f, o, p) => this.#read(m, o, p).send(f));
this.#query[m.method] = createQuery(m, (f, o, p) => this.#read(m, o, p).send(f));
}
});
}
Expand Down
8 changes: 6 additions & 2 deletions packages/api-contract/src/base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ export interface BlueprintDeploy<ApiType extends ApiTypes> {
(value: bigint | string | number | BN, gasLimit: bigint | string | number | BN, ...params: unknown[]): SubmittableExtrinsic<ApiType>;
}

export interface ContractQuery<ApiType extends ApiTypes> {
export interface MessageMeta {
readonly meta: AbiMessage;
}

export interface ContractQuery<ApiType extends ApiTypes> extends MessageMeta {
(origin: AccountId | string | Uint8Array, options: ContractOptions, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome>;
// @deprecated Use options form (to be dropped in a major update)
(origin: AccountId | string | Uint8Array, value: bigint | BN | string | number, gasLimit: bigint | BN | string | number, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome>;
}

export interface ContractTx<ApiType extends ApiTypes> {
export interface ContractTx<ApiType extends ApiTypes> extends MessageMeta {
(options: ContractOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType>;
// @deprecated Use options form (to be dropped in a major update)
(value: bigint | BN | string | number, gasLimit: bigint | BN | string | number, ...params: unknown[]): SubmittableExtrinsic<ApiType>;
Expand Down
1 change: 1 addition & 0 deletions packages/api-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface AbiMessage {
isMutating?: boolean;
isPayable?: boolean;
method: string;
path: string[];
returnType?: TypeDef | null;
selector: ContractSelector;
toU8a: (params: unknown[]) => Uint8Array;
Expand Down