@pezkuwi/api-contract 16.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/build/Abi/index.d.ts +31 -0
- package/build/Abi/toLatestCompatible.d.ts +15 -0
- package/build/Abi/toV1.d.ts +3 -0
- package/build/Abi/toV2.d.ts +3 -0
- package/build/Abi/toV3.d.ts +3 -0
- package/build/Abi/toV4.d.ts +3 -0
- package/build/augment.d.ts +1 -0
- package/build/base/Base.d.ts +13 -0
- package/build/base/Blueprint.d.ts +24 -0
- package/build/base/Code.d.ts +22 -0
- package/build/base/Contract.d.ts +25 -0
- package/build/base/index.d.ts +3 -0
- package/build/base/mock.d.ts +3 -0
- package/build/base/types.d.ts +25 -0
- package/build/base/util.d.ts +16 -0
- package/build/bundle.d.ts +4 -0
- package/build/index.d.ts +2 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/promise/index.d.ts +13 -0
- package/build/promise/types.d.ts +3 -0
- package/build/rx/index.d.ts +13 -0
- package/build/rx/types.d.ts +3 -0
- package/build/types.d.ts +79 -0
- package/build/util.d.ts +5 -0
- package/package.json +39 -0
- package/src/Abi/Abi.spec.ts +235 -0
- package/src/Abi/index.ts +477 -0
- package/src/Abi/toLatestCompatible.spec.ts +219 -0
- package/src/Abi/toLatestCompatible.ts +52 -0
- package/src/Abi/toV1.ts +35 -0
- package/src/Abi/toV2.ts +58 -0
- package/src/Abi/toV3.ts +18 -0
- package/src/Abi/toV4.ts +21 -0
- package/src/augment.ts +4 -0
- package/src/base/Base.ts +52 -0
- package/src/base/Blueprint.ts +90 -0
- package/src/base/Code.spec.ts +47 -0
- package/src/base/Code.ts +142 -0
- package/src/base/Contract.ts +197 -0
- package/src/base/index.ts +6 -0
- package/src/base/mock.ts +48 -0
- package/src/base/types.ts +40 -0
- package/src/base/util.ts +56 -0
- package/src/bundle.ts +10 -0
- package/src/checkTypes.manual.ts +45 -0
- package/src/index.ts +6 -0
- package/src/mod.ts +4 -0
- package/src/packageDetect.ts +13 -0
- package/src/packageInfo.ts +6 -0
- package/src/promise/index.ts +28 -0
- package/src/promise/types.ts +7 -0
- package/src/rx/index.ts +28 -0
- package/src/rx/types.ts +7 -0
- package/src/test/compare/ink_v0_delegator.test.json +47 -0
- package/src/test/compare/ink_v0_dns.test.json +232 -0
- package/src/test/compare/ink_v0_erc20.test.json +253 -0
- package/src/test/compare/ink_v0_erc721.test.json +415 -0
- package/src/test/compare/ink_v0_flipper.test.json +9 -0
- package/src/test/compare/ink_v0_flipperBundle.test.json +9 -0
- package/src/test/compare/ink_v0_incrementer.test.json +9 -0
- package/src/test/compare/ink_v0_multisigPlain.test.json +562 -0
- package/src/test/compare/ink_v1_flipper.test.json +9 -0
- package/src/test/compare/ink_v1_psp22.test.json +531 -0
- package/src/test/compare/ink_v2_erc20.test.json +205 -0
- package/src/test/compare/ink_v2_flipper.test.json +9 -0
- package/src/test/compare/ink_v3_flipper.test.json +9 -0
- package/src/test/compare/ink_v3_traitErc20.test.json +205 -0
- package/src/test/compare/ink_v4_erc20Contract.test.json +253 -0
- package/src/test/compare/ink_v4_erc20Metadata.test.json +253 -0
- package/src/test/compare/ink_v4_flipperContract.test.json +155 -0
- package/src/test/compare/ink_v4_flipperMetadata.test.json +155 -0
- package/src/test/compare/ink_v5_erc20.test.json +370 -0
- package/src/test/compare/ink_v5_erc20AnonymousTransferMetadata.test.json +370 -0
- package/src/test/compare/ink_v5_erc20Contract.test.json +370 -0
- package/src/test/compare/ink_v5_erc20Metadata.test.json +370 -0
- package/src/test/compare/ink_v5_flipperContract.test.json +174 -0
- package/src/test/compare/ink_v5_flipperMetadata.test.json +174 -0
- package/src/test/compare/ink_v6_erc20Contract.test.json +418 -0
- package/src/test/compare/ink_v6_erc20Metadata.test.json +418 -0
- package/src/test/compare/solang_v0_ints256.test.json +9 -0
- package/src/test/compare/user_v0_assetTransfer.test.json +54 -0
- package/src/test/compare/user_v0_enumExample.test.json +303 -0
- package/src/test/compare/user_v0_recursive.test.json +27 -0
- package/src/test/compare/user_v0_withString.test.json +260 -0
- package/src/test/compare/user_v3_ask.test.json +71 -0
- package/src/test/compare/user_v4_events.test.json +1328 -0
- package/src/test/contracts/index.ts +20 -0
- package/src/test/contracts/ink/index.ts +13 -0
- package/src/test/contracts/ink/v0/accumulator.wasm +0 -0
- package/src/test/contracts/ink/v0/adder.wasm +0 -0
- package/src/test/contracts/ink/v0/delegator.json +252 -0
- package/src/test/contracts/ink/v0/delegator.wasm +0 -0
- package/src/test/contracts/ink/v0/dns.json +713 -0
- package/src/test/contracts/ink/v0/dns.wasm +0 -0
- package/src/test/contracts/ink/v0/erc20.json +704 -0
- package/src/test/contracts/ink/v0/erc20.wasm +0 -0
- package/src/test/contracts/ink/v0/erc721.json +1197 -0
- package/src/test/contracts/ink/v0/erc721.wasm +0 -0
- package/src/test/contracts/ink/v0/flipper.contract.json +107 -0
- package/src/test/contracts/ink/v0/flipper.json +106 -0
- package/src/test/contracts/ink/v0/flipper.wasm +0 -0
- package/src/test/contracts/ink/v0/incrementer.json +108 -0
- package/src/test/contracts/ink/v0/incrementer.wasm +0 -0
- package/src/test/contracts/ink/v0/index.ts +11 -0
- package/src/test/contracts/ink/v0/multisig_plain.json +1466 -0
- package/src/test/contracts/ink/v0/multisig_plain.wasm +0 -0
- package/src/test/contracts/ink/v0/subber.wasm +0 -0
- package/src/test/contracts/ink/v0/trait-flipper.json +103 -0
- package/src/test/contracts/ink/v0/trait-flipper.wasm +0 -0
- package/src/test/contracts/ink/v1/flipper.contract.json +111 -0
- package/src/test/contracts/ink/v1/index.ts +6 -0
- package/src/test/contracts/ink/v1/psp22_minter_pauser.contract.json +1722 -0
- package/src/test/contracts/ink/v2/erc20.contract.json +630 -0
- package/src/test/contracts/ink/v2/flipper.contract.json +103 -0
- package/src/test/contracts/ink/v2/index.ts +5 -0
- package/src/test/contracts/ink/v3/flipper.contract.json +105 -0
- package/src/test/contracts/ink/v3/index.ts +6 -0
- package/src/test/contracts/ink/v3/trait_erc20.contract.json +631 -0
- package/src/test/contracts/ink/v4/erc20.contract.json +1 -0
- package/src/test/contracts/ink/v4/erc20.json +821 -0
- package/src/test/contracts/ink/v4/erc20.wasm +0 -0
- package/src/test/contracts/ink/v4/flipper.contract.json +1 -0
- package/src/test/contracts/ink/v4/flipper.json +396 -0
- package/src/test/contracts/ink/v4/flipper.wasm +0 -0
- package/src/test/contracts/ink/v4/index.ts +7 -0
- package/src/test/contracts/ink/v5/erc20.contract.json +1 -0
- package/src/test/contracts/ink/v5/erc20.json +1025 -0
- package/src/test/contracts/ink/v5/erc20.wasm +0 -0
- package/src/test/contracts/ink/v5/erc20_anonymous_transfer.json +1025 -0
- package/src/test/contracts/ink/v5/flipper.contract.json +1 -0
- package/src/test/contracts/ink/v5/flipper.json +420 -0
- package/src/test/contracts/ink/v5/flipper.wasm +0 -0
- package/src/test/contracts/ink/v5/index.ts +8 -0
- package/src/test/contracts/ink/v6/erc20.contract.json +1 -0
- package/src/test/contracts/ink/v6/erc20.json +1081 -0
- package/src/test/contracts/ink/v6/erc20.polkavm +0 -0
- package/src/test/contracts/ink/v6/index.ts +5 -0
- package/src/test/contracts/solang/index.ts +7 -0
- package/src/test/contracts/solang/v0/index.ts +4 -0
- package/src/test/contracts/solang/v0/ints256.json +113 -0
- package/src/test/contracts/solang/v0/ints256.sol +13 -0
- package/src/test/contracts/solang/v0/ints256.wasm +0 -0
- package/src/test/contracts/user/index.ts +9 -0
- package/src/test/contracts/user/v0/assetTransfer.json +299 -0
- package/src/test/contracts/user/v0/assetTransfer.wasm +0 -0
- package/src/test/contracts/user/v0/enumExample.json +528 -0
- package/src/test/contracts/user/v0/enumExample.wasm +0 -0
- package/src/test/contracts/user/v0/index.ts +7 -0
- package/src/test/contracts/user/v0/recursive.contract.json +1 -0
- package/src/test/contracts/user/v0/withString.json +777 -0
- package/src/test/contracts/user/v3/ask.json +550 -0
- package/src/test/contracts/user/v3/index.ts +4 -0
- package/src/test/contracts/user/v4/events.contract.json +2990 -0
- package/src/test/contracts/user/v4/index.ts +4 -0
- package/src/test/contracts/util.ts +14 -0
- package/src/types.ts +98 -0
- package/src/util.ts +20 -0
- package/tsconfig.build.json +22 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +26 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ApiBase } from '@pezkuwi/api/base';
|
|
5
|
+
import type { SubmittableExtrinsic } from '@pezkuwi/api/submittable/types';
|
|
6
|
+
import type { ApiTypes, DecorateMethod } from '@pezkuwi/api/types';
|
|
7
|
+
import type { AccountId, AccountId20, ContractExecResult, EventRecord, Weight, WeightV2 } from '@pezkuwi/types/interfaces';
|
|
8
|
+
import type { ISubmittableResult } from '@pezkuwi/types/types';
|
|
9
|
+
import type { Abi } from '../Abi/index.js';
|
|
10
|
+
import type { AbiMessage, ContractCallOutcome, ContractOptions, DecodedEvent, WeightAll } from '../types.js';
|
|
11
|
+
import type { ContractCallResult, ContractCallSend, ContractQuery, ContractTx, MapMessageQuery, MapMessageTx } from './types.js';
|
|
12
|
+
|
|
13
|
+
import { map } from 'rxjs';
|
|
14
|
+
|
|
15
|
+
import { SubmittableResult } from '@pezkuwi/api';
|
|
16
|
+
import { BN, BN_HUNDRED, BN_ONE, BN_ZERO, isUndefined, logger } from '@pezkuwi/util';
|
|
17
|
+
|
|
18
|
+
import { applyOnEvent } from '../util.js';
|
|
19
|
+
import { Base } from './Base.js';
|
|
20
|
+
import { convertWeight, withMeta } from './util.js';
|
|
21
|
+
|
|
22
|
+
export type ContractConstructor<ApiType extends ApiTypes> = new(api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, address: string | AccountId) => Contract<ApiType>;
|
|
23
|
+
|
|
24
|
+
// As per Rust, 5 * GAS_PER_SEC
|
|
25
|
+
const MAX_CALL_GAS = new BN(5_000_000_000_000).isub(BN_ONE);
|
|
26
|
+
|
|
27
|
+
const l = logger('Contract');
|
|
28
|
+
|
|
29
|
+
function createQuery <ApiType extends ApiTypes> (meta: AbiMessage, fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult<ApiType, ContractCallOutcome>): ContractQuery<ApiType> {
|
|
30
|
+
return withMeta(meta, (origin: string | AccountId | Uint8Array, options: ContractOptions, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome> =>
|
|
31
|
+
fn(origin, options, params)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createTx <ApiType extends ApiTypes> (meta: AbiMessage, fn: (options: ContractOptions, params: unknown[]) => SubmittableExtrinsic<ApiType>): ContractTx<ApiType> {
|
|
36
|
+
return withMeta(meta, (options: ContractOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType> =>
|
|
37
|
+
fn(options, params)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class ContractSubmittableResult extends SubmittableResult {
|
|
42
|
+
readonly contractEvents?: DecodedEvent[] | undefined;
|
|
43
|
+
|
|
44
|
+
constructor (result: ISubmittableResult, contractEvents?: DecodedEvent[]) {
|
|
45
|
+
super(result);
|
|
46
|
+
|
|
47
|
+
this.contractEvents = contractEvents;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
|
|
52
|
+
/**
|
|
53
|
+
* @description The on-chain address for this contract
|
|
54
|
+
*/
|
|
55
|
+
readonly address: AccountId | AccountId20;
|
|
56
|
+
|
|
57
|
+
readonly #query: MapMessageQuery<ApiType> = {};
|
|
58
|
+
readonly #tx: MapMessageTx<ApiType> = {};
|
|
59
|
+
|
|
60
|
+
constructor (api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, address: string | AccountId | AccountId20, decorateMethod: DecorateMethod<ApiType>) {
|
|
61
|
+
super(api, abi, decorateMethod);
|
|
62
|
+
|
|
63
|
+
this.address = this.registry.createType(this._isRevive ? 'AccountId20' : 'AccountId', address);
|
|
64
|
+
|
|
65
|
+
this.abi.messages.forEach((m): void => {
|
|
66
|
+
if (isUndefined(this.#tx[m.method])) {
|
|
67
|
+
this.#tx[m.method] = createTx(m, (o, p) => this.#exec(m, o, p));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isUndefined(this.#query[m.method])) {
|
|
71
|
+
this.#query[m.method] = createQuery(m, (f, o, p) => this.#read(m, o, p).send(f));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get query (): MapMessageQuery<ApiType> {
|
|
77
|
+
return this.#query;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public get tx (): MapMessageTx<ApiType> {
|
|
81
|
+
return this.#tx;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#getGas = (_gasLimit: bigint | BN | string | number | WeightV2, isCall = false): WeightAll => {
|
|
85
|
+
const weight = convertWeight(_gasLimit);
|
|
86
|
+
|
|
87
|
+
if (weight.v1Weight.gt(BN_ZERO)) {
|
|
88
|
+
return weight;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return convertWeight(
|
|
92
|
+
isCall
|
|
93
|
+
? MAX_CALL_GAS
|
|
94
|
+
: convertWeight(
|
|
95
|
+
this.api.consts.system.blockWeights
|
|
96
|
+
? (this.api.consts.system.blockWeights as unknown as { maxBlock: WeightV2 }).maxBlock
|
|
97
|
+
: this.api.consts.system['maximumBlockWeight'] as Weight
|
|
98
|
+
).v1Weight.muln(64).div(BN_HUNDRED)
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
#exec = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): SubmittableExtrinsic<ApiType> => {
|
|
103
|
+
const palletTx = this._isRevive ? this.api.tx.revive : this.api.tx.contracts;
|
|
104
|
+
|
|
105
|
+
return palletTx.call(
|
|
106
|
+
this.address,
|
|
107
|
+
value,
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
109
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
110
|
+
this._isWeightV1
|
|
111
|
+
? convertWeight(gasLimit).v1Weight
|
|
112
|
+
: convertWeight(gasLimit).v2Weight,
|
|
113
|
+
storageDepositLimit,
|
|
114
|
+
this.abi.findMessage(messageOrId).toU8a(params)
|
|
115
|
+
).withResultTransform((result: ISubmittableResult) =>
|
|
116
|
+
// ContractEmitted is the current generation, ContractExecution is the previous generation
|
|
117
|
+
new ContractSubmittableResult(result, applyOnEvent(result, ['ContractEmitted', 'ContractExecution'], (records: EventRecord[]) =>
|
|
118
|
+
records
|
|
119
|
+
// Filter to only decode events emitted by this specific contract instance.
|
|
120
|
+
.filter((record): boolean => {
|
|
121
|
+
try {
|
|
122
|
+
const contractAddress = record.event.data[0];
|
|
123
|
+
|
|
124
|
+
if (this.address.eq(contractAddress)) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
l.debug(`Skipping event from different contract ${contractAddress.toString()} (this contract: ${this.address.toString()})`);
|
|
129
|
+
|
|
130
|
+
return false;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
l.warn(`Unable to extract contract address from event: ${(error as Error).message}`);
|
|
133
|
+
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
.map((record): DecodedEvent | null => {
|
|
138
|
+
try {
|
|
139
|
+
return this.abi.decodeEvent(record);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
l.error(`Unable to decode contract event: ${(error as Error).message}`);
|
|
142
|
+
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
.filter((decoded): decoded is DecodedEvent => !!decoded), this._isRevive
|
|
147
|
+
))
|
|
148
|
+
);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
#read = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): ContractCallSend<ApiType> => {
|
|
152
|
+
const message = this.abi.findMessage(messageOrId);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
156
|
+
send: this._decorateMethod((origin: string | AccountId | Uint8Array) =>
|
|
157
|
+
(this._isRevive
|
|
158
|
+
? this.api.rx.call.reviveApi.call
|
|
159
|
+
: this.api.rx.call.contractsApi.call)<ContractExecResult>(
|
|
160
|
+
origin,
|
|
161
|
+
this.address,
|
|
162
|
+
value,
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
164
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
165
|
+
this._isWeightV1
|
|
166
|
+
? this.#getGas(gasLimit, true).v1Weight
|
|
167
|
+
: this.#getGas(gasLimit, true).v2Weight,
|
|
168
|
+
storageDepositLimit,
|
|
169
|
+
message.toU8a(params)
|
|
170
|
+
).pipe(
|
|
171
|
+
map(({ debugMessage, gasConsumed, gasRequired, result, storageDeposit }): ContractCallOutcome => ({
|
|
172
|
+
debugMessage,
|
|
173
|
+
gasConsumed,
|
|
174
|
+
gasRequired: gasRequired && !convertWeight(gasRequired).v1Weight.isZero()
|
|
175
|
+
? gasRequired
|
|
176
|
+
: gasConsumed,
|
|
177
|
+
output: result.isOk && message.returnType
|
|
178
|
+
? this.abi.registry.createTypeUnsafe(message.returnType.lookupName || message.returnType.type, [result.asOk.data.toU8a(true)], { isPedantic: true })
|
|
179
|
+
: null,
|
|
180
|
+
result,
|
|
181
|
+
storageDeposit
|
|
182
|
+
}))
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function extendContract <ApiType extends ApiTypes> (type: ApiType, decorateMethod: DecorateMethod<ApiType>): ContractConstructor<ApiType> {
|
|
190
|
+
return class extends Contract<ApiType> {
|
|
191
|
+
static __ContractType = type;
|
|
192
|
+
|
|
193
|
+
constructor (api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, address: string | AccountId) {
|
|
194
|
+
super(api, abi, address, decorateMethod);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { Blueprint, BlueprintSubmittableResult, extendBlueprint } from './Blueprint.js';
|
|
5
|
+
export { Code, CodeSubmittableResult, extendCode } from './Code.js';
|
|
6
|
+
export { Contract, extendContract } from './Contract.js';
|
package/src/base/mock.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ApiBase } from '@pezkuwi/api/base';
|
|
5
|
+
|
|
6
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
7
|
+
|
|
8
|
+
const registry = new TypeRegistry();
|
|
9
|
+
|
|
10
|
+
const instantiateWithCode = (): never => {
|
|
11
|
+
throw new Error('mock');
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
instantiateWithCode.meta = { args: new Array(6) };
|
|
15
|
+
|
|
16
|
+
export const mockApi = {
|
|
17
|
+
call: {
|
|
18
|
+
contractsApi: {
|
|
19
|
+
call: (): never => {
|
|
20
|
+
throw new Error('mock');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
isConnected: true,
|
|
25
|
+
registry,
|
|
26
|
+
tx: {
|
|
27
|
+
contracts: {
|
|
28
|
+
instantiateWithCode
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} as unknown as ApiBase<'promise'>;
|
|
32
|
+
|
|
33
|
+
export const mockReviveApi = {
|
|
34
|
+
call: {
|
|
35
|
+
reviveApi: {
|
|
36
|
+
call: (): never => {
|
|
37
|
+
throw new Error('mock');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
isConnected: true,
|
|
42
|
+
registry,
|
|
43
|
+
tx: {
|
|
44
|
+
revive: {
|
|
45
|
+
instantiateWithCode
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} as unknown as ApiBase<'promise'>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Observable } from 'rxjs';
|
|
5
|
+
import type { SubmittableExtrinsic } from '@pezkuwi/api/submittable/types';
|
|
6
|
+
import type { ApiTypes, ObsInnerType } from '@pezkuwi/api/types';
|
|
7
|
+
import type { AccountId } from '@pezkuwi/types/interfaces';
|
|
8
|
+
import type { AbiMessage, BlueprintOptions, ContractCallOutcome, ContractOptions } from '../types.js';
|
|
9
|
+
|
|
10
|
+
export interface MessageMeta {
|
|
11
|
+
readonly meta: AbiMessage;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface BlueprintDeploy<ApiType extends ApiTypes> extends MessageMeta {
|
|
15
|
+
(options: BlueprintOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ContractQuery<ApiType extends ApiTypes> extends MessageMeta {
|
|
19
|
+
(origin: AccountId | string | Uint8Array, options: ContractOptions, ...params: unknown[]): ContractCallResult<ApiType, ContractCallOutcome>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ContractTx<ApiType extends ApiTypes> extends MessageMeta {
|
|
23
|
+
(options: ContractOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ContractGeneric<O, T> = (messageOrId: AbiMessage | string | number, options: O, ...params: unknown[]) => T;
|
|
27
|
+
|
|
28
|
+
export type ContractCallResult<ApiType extends ApiTypes, T> = ApiType extends 'rxjs'
|
|
29
|
+
? Observable<T>
|
|
30
|
+
: Promise<ObsInnerType<Observable<T>>>;
|
|
31
|
+
|
|
32
|
+
export interface ContractCallSend<ApiType extends ApiTypes> {
|
|
33
|
+
send (account: string | AccountId | Uint8Array): ContractCallResult<ApiType, ContractCallOutcome>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type MapConstructorExec<ApiType extends ApiTypes> = Record<string, BlueprintDeploy<ApiType>>;
|
|
37
|
+
|
|
38
|
+
export type MapMessageTx<ApiType extends ApiTypes> = Record<string, ContractTx<ApiType>>;
|
|
39
|
+
|
|
40
|
+
export type MapMessageQuery<ApiType extends ApiTypes> = Record<string, ContractQuery<ApiType>>;
|
package/src/base/util.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { SubmittableResult } from '@pezkuwi/api';
|
|
5
|
+
import type { SubmittableExtrinsic } from '@pezkuwi/api/submittable/types';
|
|
6
|
+
import type { ApiTypes } from '@pezkuwi/api/types';
|
|
7
|
+
import type { WeightV1, WeightV2 } from '@pezkuwi/types/interfaces';
|
|
8
|
+
import type { BN } from '@pezkuwi/util';
|
|
9
|
+
import type { AbiConstructor, AbiMessage, BlueprintOptions, WeightAll } from '../types.js';
|
|
10
|
+
import type { BlueprintDeploy, ContractGeneric } from './types.js';
|
|
11
|
+
|
|
12
|
+
import { Bytes } from '@pezkuwi/types';
|
|
13
|
+
import { bnToBn, compactAddLength, u8aToU8a } from '@pezkuwi/util';
|
|
14
|
+
import { randomAsU8a } from '@pezkuwi/util-crypto';
|
|
15
|
+
|
|
16
|
+
export const EMPTY_SALT = new Uint8Array();
|
|
17
|
+
|
|
18
|
+
export function withMeta <T extends { meta: AbiMessage }> (meta: AbiMessage, creator: Omit<T, 'meta'>): T {
|
|
19
|
+
(creator as T).meta = meta;
|
|
20
|
+
|
|
21
|
+
return creator as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createBluePrintTx <ApiType extends ApiTypes, R extends SubmittableResult> (meta: AbiMessage, fn: (options: BlueprintOptions, params: unknown[]) => SubmittableExtrinsic<ApiType, R>): BlueprintDeploy<ApiType> {
|
|
25
|
+
return withMeta(meta, (options: BlueprintOptions, ...params: unknown[]): SubmittableExtrinsic<ApiType, R> =>
|
|
26
|
+
fn(options, params)
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createBluePrintWithId <T> (fn: (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, params: unknown[]) => T): ContractGeneric<BlueprintOptions, T> {
|
|
31
|
+
return (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, ...params: unknown[]): T =>
|
|
32
|
+
fn(constructorOrId, options, params);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function encodeSalt (salt: Uint8Array | string | null = randomAsU8a()): Uint8Array {
|
|
36
|
+
return salt instanceof Bytes
|
|
37
|
+
? salt
|
|
38
|
+
: salt?.length
|
|
39
|
+
? compactAddLength(u8aToU8a(salt))
|
|
40
|
+
: EMPTY_SALT;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function convertWeight (weight: WeightV1 | WeightV2 | bigint | string | number | BN): WeightAll {
|
|
44
|
+
const [refTime, proofSize] = isWeightV2(weight)
|
|
45
|
+
? [weight.refTime.toBn(), weight.proofSize.toBn()]
|
|
46
|
+
: [bnToBn(weight), undefined];
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
v1Weight: refTime,
|
|
50
|
+
v2Weight: { proofSize, refTime }
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function isWeightV2 (weight: WeightV1 | WeightV2 | bigint | string | number | BN): weight is WeightV2 {
|
|
55
|
+
return !!(weight as WeightV2).proofSize;
|
|
56
|
+
}
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// all named
|
|
5
|
+
export { Abi } from './Abi/index.js';
|
|
6
|
+
export { packageInfo } from './packageInfo.js';
|
|
7
|
+
|
|
8
|
+
// all starred
|
|
9
|
+
export * from './promise/index.js';
|
|
10
|
+
export * from './rx/index.js';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Simple non-runnable checks to test type definitions in the editor itself
|
|
5
|
+
|
|
6
|
+
import '@pezkuwi/api-augment';
|
|
7
|
+
|
|
8
|
+
import type { TestKeyringMapSubstrate } from '@pezkuwi/keyring/testingPairs';
|
|
9
|
+
|
|
10
|
+
import { ApiPromise } from '@pezkuwi/api';
|
|
11
|
+
import { BlueprintPromise, ContractPromise } from '@pezkuwi/api-contract';
|
|
12
|
+
import { createTestPairs } from '@pezkuwi/keyring/testingPairs';
|
|
13
|
+
|
|
14
|
+
import abiIncrementer from './test/contracts/ink/v0/incrementer.json' assert { type: 'json' };
|
|
15
|
+
|
|
16
|
+
async function checkBlueprint (api: ApiPromise, pairs: TestKeyringMapSubstrate): Promise<void> {
|
|
17
|
+
const blueprint = new BlueprintPromise(api, abiIncrementer as Record<string, unknown>, '0x1234');
|
|
18
|
+
|
|
19
|
+
await blueprint.tx['new']({ gasLimit: 456, salt: '0x1234', value: 123 }, 42).signAndSend(pairs.bob);
|
|
20
|
+
await blueprint.tx['new']({ gasLimit: 456, value: 123 }, 42).signAndSend(pairs.bob);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function checkContract (api: ApiPromise, pairs: TestKeyringMapSubstrate): Promise<void> {
|
|
24
|
+
const contract = new ContractPromise(api, abiIncrementer as Record<string, unknown>, '0x1234');
|
|
25
|
+
|
|
26
|
+
// queries
|
|
27
|
+
await contract.query['get'](pairs.alice.address, {});
|
|
28
|
+
|
|
29
|
+
// execute
|
|
30
|
+
await contract.tx['inc']({ gasLimit: 1234 }, 123).signAndSend(pairs.eve);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function main (): Promise<void> {
|
|
34
|
+
const api = await ApiPromise.create({
|
|
35
|
+
hasher: (data: Uint8Array): Uint8Array => data
|
|
36
|
+
});
|
|
37
|
+
const pairs = createTestPairs();
|
|
38
|
+
|
|
39
|
+
await Promise.all([
|
|
40
|
+
checkBlueprint(api, pairs),
|
|
41
|
+
checkContract(api, pairs)
|
|
42
|
+
]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
main().catch(console.error);
|
package/src/index.ts
ADDED
package/src/mod.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
// (packageInfo imports will be kept as-is, user-editable)
|
|
6
|
+
|
|
7
|
+
import { packageInfo as apiInfo } from '@pezkuwi/api/packageInfo';
|
|
8
|
+
import { packageInfo as typesInfo } from '@pezkuwi/types/packageInfo';
|
|
9
|
+
import { detectPackage } from '@pezkuwi/util';
|
|
10
|
+
|
|
11
|
+
import { packageInfo } from './packageInfo.js';
|
|
12
|
+
|
|
13
|
+
detectPackage(packageInfo, null, [apiInfo, typesInfo]);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
|
|
6
|
+
export const packageInfo = { name: '@pezkuwi/api-contract', path: 'auto', type: 'auto', version: '16.5.5' };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ApiPromise } from '@pezkuwi/api';
|
|
5
|
+
import type { AccountId, AccountId20, Hash } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { Abi } from '../Abi/index.js';
|
|
7
|
+
|
|
8
|
+
import { toPromiseMethod } from '@pezkuwi/api';
|
|
9
|
+
|
|
10
|
+
import { Blueprint, Code, Contract } from '../base/index.js';
|
|
11
|
+
|
|
12
|
+
export class BlueprintPromise extends Blueprint<'promise'> {
|
|
13
|
+
constructor (api: ApiPromise, abi: string | Record<string, unknown> | Abi, codeHash: string | Hash) {
|
|
14
|
+
super(api, abi, codeHash, toPromiseMethod);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class CodePromise extends Code<'promise'> {
|
|
19
|
+
constructor (api: ApiPromise, abi: string | Record<string, unknown> | Abi, wasm: Uint8Array | string | Buffer | null | undefined) {
|
|
20
|
+
super(api, abi, wasm, toPromiseMethod);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ContractPromise extends Contract<'promise'> {
|
|
25
|
+
constructor (api: ApiPromise, abi: string | Record<string, unknown> | Abi, address: string | AccountId | AccountId20) {
|
|
26
|
+
super(api, abi, address, toPromiseMethod);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { BlueprintSubmittableResult as BaseBlueprintSubmittableResult, CodeSubmittableResult as BaseCodeSubmittableResult } from '../base/index.js';
|
|
5
|
+
|
|
6
|
+
export type BlueprintSubmittableResult = BaseBlueprintSubmittableResult<'promise'>;
|
|
7
|
+
export type CodeSubmittableResult = BaseCodeSubmittableResult<'promise'>;
|
package/src/rx/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ApiRx } from '@pezkuwi/api';
|
|
5
|
+
import type { AccountId, Hash } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { Abi } from '../Abi/index.js';
|
|
7
|
+
|
|
8
|
+
import { toRxMethod } from '@pezkuwi/api';
|
|
9
|
+
|
|
10
|
+
import { Blueprint, Code, Contract } from '../base/index.js';
|
|
11
|
+
|
|
12
|
+
export class BlueprintRx extends Blueprint<'rxjs'> {
|
|
13
|
+
constructor (api: ApiRx, abi: string | Record<string, unknown> | Abi, codeHash: string | Hash) {
|
|
14
|
+
super(api, abi, codeHash, toRxMethod);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class CodeRx extends Code<'rxjs'> {
|
|
19
|
+
constructor (api: ApiRx, abi: string | Record<string, unknown> | Abi, wasm: Uint8Array | string | Buffer | null | undefined) {
|
|
20
|
+
super(api, abi, wasm, toRxMethod);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ContractRx extends Contract<'rxjs'> {
|
|
25
|
+
constructor (api: ApiRx, abi: string | Record<string, unknown> | Abi, address: string | AccountId) {
|
|
26
|
+
super(api, abi, address, toRxMethod);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/rx/types.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-contract authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { BlueprintSubmittableResult as BaseBlueprintSubmittableResult, CodeSubmittableResult as BaseCodeSubmittableResult } from '../base/index.js';
|
|
5
|
+
|
|
6
|
+
export type BlueprintSubmittableResult = BaseBlueprintSubmittableResult<'promise'>;
|
|
7
|
+
export type CodeSubmittableResult = BaseCodeSubmittableResult<'promise'>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"info": "Plain",
|
|
4
|
+
"lookupIndex": 1,
|
|
5
|
+
"type": "AccountId",
|
|
6
|
+
"docs": [],
|
|
7
|
+
"namespace": "ink_env::types::AccountId",
|
|
8
|
+
"lookupNameRoot": "InkEnvAccountId"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"info": "VecFixed",
|
|
12
|
+
"lookupIndex": 2,
|
|
13
|
+
"type": "[u8;32]",
|
|
14
|
+
"docs": [],
|
|
15
|
+
"namespace": "",
|
|
16
|
+
"length": 32,
|
|
17
|
+
"sub": {
|
|
18
|
+
"info": "Plain",
|
|
19
|
+
"lookupIndex": 3,
|
|
20
|
+
"type": "u8",
|
|
21
|
+
"docs": [],
|
|
22
|
+
"namespace": ""
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"info": "Plain",
|
|
27
|
+
"lookupIndex": 3,
|
|
28
|
+
"type": "u8",
|
|
29
|
+
"docs": [],
|
|
30
|
+
"namespace": ""
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"info": "Plain",
|
|
34
|
+
"lookupIndex": 4,
|
|
35
|
+
"type": "i32",
|
|
36
|
+
"docs": [],
|
|
37
|
+
"namespace": ""
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"info": "Plain",
|
|
41
|
+
"lookupIndex": 5,
|
|
42
|
+
"type": "Hash",
|
|
43
|
+
"docs": [],
|
|
44
|
+
"namespace": "ink_env::types::Hash",
|
|
45
|
+
"lookupNameRoot": "InkEnvHash"
|
|
46
|
+
}
|
|
47
|
+
]
|