@parity/product-sdk-contracts 0.2.2 → 0.4.0

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/dist/index.d.ts CHANGED
@@ -1,10 +1,187 @@
1
- import { PolkadotClient, HexString } from 'polkadot-api';
2
- import { InkSdk } from '@polkadot-api/sdk-ink';
3
- import { C as CdmJson, a as ContractManagerOptions, b as ContractDefaults, c as Contracts, d as Contract, e as ContractDef, A as AbiEntry, f as ContractOptions } from './codegen-BPDBGrJC.js';
4
- export { g as AbiParam, h as CdmJsonContract, i as CdmJsonTarget, P as PrepareOptions, Q as QueryOptions, j as QueryResult, T as TxOptions, k as generateContractTypes } from './codegen-BPDBGrJC.js';
1
+ import { HexString, SS58String, PolkadotClient, PolkadotSigner } from 'polkadot-api';
2
+ import { Weight, SubmittableTransaction, TxResult } from '@parity/product-sdk-tx';
5
3
  export { BatchableCall, TxResult } from '@parity/product-sdk-tx';
4
+ import { C as CdmJson, a as ContractManagerOptions, b as ContractDefaults, c as Contracts, d as Contract, e as ContractDef, A as AbiEntry, f as ContractOptions } from './types-DJDMz91q.js';
5
+ export { g as AbiParam, h as CdmJsonContract, i as CdmJsonTarget, P as PrepareOptions, Q as QueryOptions, j as QueryResult, T as TxOptions } from './types-DJDMz91q.js';
6
6
  import '@parity/product-sdk-signer';
7
7
 
8
+ /**
9
+ * Result of a `Revive.call` extrinsic — present on the typed API as
10
+ * `api.tx.Revive.call(args)`. Returned object is a PAPI submittable that
11
+ * `submitAndWatch` consumes natively.
12
+ *
13
+ * `dest` is an H160 hex string and `data` is a raw `Uint8Array`: this matches
14
+ * what `polkadot-api` ≥2.0 codecs accept and produce. The class-based
15
+ * `Binary` / `FixedSizeBinary` wrappers from `@polkadot-api/substrate-bindings`
16
+ * 0.12 are *not* accepted by PAPI 2.x's compatibility check.
17
+ */
18
+ type ReviveCallTx = (args: {
19
+ dest: HexString;
20
+ value: bigint;
21
+ weight_limit: Weight;
22
+ storage_deposit_limit: bigint;
23
+ data: Uint8Array;
24
+ }) => SubmittableTransaction;
25
+ /**
26
+ * Dry-run result returned by `ReviveApi.call`. Mirrors the shape exposed by
27
+ * descriptors (`paseo-asset-hub`, `polkadot-asset-hub`, `kusama-asset-hub`).
28
+ *
29
+ * `data` is a raw `Uint8Array` because PAPI ≥2.0 dropped the `Binary` class
30
+ * wrapper for `Vec<u8>` codecs.
31
+ */
32
+ interface ReviveDryRunResult {
33
+ weight_consumed: Weight;
34
+ weight_required: Weight;
35
+ storage_deposit: {
36
+ type: "Refund" | "Charge";
37
+ value: bigint;
38
+ };
39
+ max_storage_deposit: {
40
+ type: "Refund" | "Charge";
41
+ value: bigint;
42
+ };
43
+ gas_consumed: bigint;
44
+ /**
45
+ * `success: true` carries `{ flags, data }`; `success: false` carries the
46
+ * dispatch error as the chain encoded it.
47
+ */
48
+ result: {
49
+ success: true;
50
+ value: {
51
+ flags: number;
52
+ data: Uint8Array;
53
+ };
54
+ } | {
55
+ success: false;
56
+ value: unknown;
57
+ };
58
+ }
59
+ /** Structural shape consumed by `ContractManager` / `createContract`. */
60
+ interface ReviveTypedApi {
61
+ tx: {
62
+ Revive: {
63
+ call: ReviveCallTx;
64
+ map_account(): SubmittableTransaction;
65
+ };
66
+ };
67
+ query: {
68
+ Revive: {
69
+ OriginalAccount: {
70
+ getValue(address: HexString): Promise<SS58String | undefined>;
71
+ };
72
+ };
73
+ };
74
+ apis: {
75
+ ReviveApi: {
76
+ call(origin: SS58String, dest: HexString, value: bigint, gas_limit: Weight | undefined, storage_deposit_limit: bigint | undefined, input_data: Uint8Array): Promise<ReviveDryRunResult>;
77
+ };
78
+ };
79
+ }
80
+ /**
81
+ * Signature of a `ReviveApi.call` dry-run, used by the wrapped contract layer
82
+ * to estimate weight + storage deposit and surface revert / OOG /
83
+ * `AccountNotMapped` failures before a tx is signed.
84
+ *
85
+ * Identical to `ReviveTypedApi.apis.ReviveApi.call`, but extracted so the
86
+ * runtime can route this single hot call through PAPI's *unsafe* API
87
+ * (skipping compatibility-token checks) on production runtimes whose
88
+ * descriptors lag a chain upgrade — every other surface still uses the
89
+ * compat-checked typed API.
90
+ */
91
+ type ReviveDryRunCall = (origin: SS58String, dest: HexString, value: bigint, gas_limit: Weight | undefined, storage_deposit_limit: bigint | undefined, input_data: Uint8Array) => Promise<ReviveDryRunResult>;
92
+ /**
93
+ * Runtime handle that drives queries and transactions against a
94
+ * pallet-revive-capable chain.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { createChainClient } from "@parity/product-sdk-chain-client";
99
+ * import { paseo_asset_hub } from "@parity/product-sdk-descriptors/paseo-asset-hub";
100
+ * import { createContractRuntimeFromClient } from "@parity/product-sdk-contracts";
101
+ *
102
+ * const client = await createChainClient({
103
+ * chains: { assetHub: paseo_asset_hub },
104
+ * rpcs: { assetHub: ["wss://sys.ibp.network/asset-hub-paseo"] },
105
+ * });
106
+ * const runtime = createContractRuntimeFromClient(client.raw.assetHub, paseo_asset_hub);
107
+ * ```
108
+ */
109
+ interface ContractRuntime {
110
+ readonly api: ReviveTypedApi;
111
+ /**
112
+ * Dry-run entry point. Production factories route this through the
113
+ * *unsafe* API to avoid compatibility-token failures when the descriptor
114
+ * trails a runtime upgrade. The {@link createContractRuntime} test factory
115
+ * delegates to `api.apis.ReviveApi.call`.
116
+ */
117
+ readonly dryRunCall: ReviveDryRunCall;
118
+ }
119
+ /**
120
+ * Wrap a typed PAPI API as a `ContractRuntime`. Intended for tests and
121
+ * advanced setups where the caller already holds a typed API. Routes the
122
+ * dry-run through the typed (compatibility-token-checked) `ReviveApi.call`
123
+ * — fine for mocks but susceptible to `Incompatible runtime entry` errors
124
+ * on a live chain whose descriptor lags. Prefer
125
+ * {@link createContractRuntimeFromClient} for production use.
126
+ */
127
+ declare function createContractRuntime(api: ReviveTypedApi): ContractRuntime;
128
+ /**
129
+ * Build a `ContractRuntime` from a raw `PolkadotClient` plus its descriptor.
130
+ *
131
+ * The typed API powers `tx.Revive.call`, `tx.Revive.map_account`, and
132
+ * `query.Revive.OriginalAccount` (extrinsics + storage are tolerant of
133
+ * descriptor drift). The runtime-API dry-run, which is *not* tolerant of
134
+ * descriptor drift on PAPI's compat-token path, is routed through
135
+ * `client.getUnsafeApi()` — bypassing the compat check while preserving
136
+ * argument and return shapes.
137
+ *
138
+ * Use this on every production code path that calls a contract's `.tx()` or
139
+ * `.query()` against a live chain.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * import { paseo_asset_hub } from "@parity/product-sdk-descriptors/paseo-asset-hub";
144
+ * import { createContractRuntimeFromClient } from "@parity/product-sdk-contracts";
145
+ *
146
+ * const runtime = createContractRuntimeFromClient(rawClient, paseo_asset_hub);
147
+ * ```
148
+ */
149
+ declare function createContractRuntimeFromClient<TDescriptor>(client: PolkadotClient, descriptor: TDescriptor): ContractRuntime;
150
+ /**
151
+ * Ensure the SS58 account is mapped to its derived H160 on `pallet-revive`.
152
+ *
153
+ * `pallet-revive` requires every signing account to have a registered
154
+ * `OriginalAccount` mapping before the runtime accepts its `Revive.call`
155
+ * extrinsics. The mapping is one-time and cheap. This helper:
156
+ *
157
+ * 1. Reads `Revive.OriginalAccount` for the H160 derived from `address`.
158
+ * 2. Returns `null` if already mapped (idempotent fast-path).
159
+ * 3. Otherwise submits `Revive.map_account()` and waits for inclusion.
160
+ *
161
+ * Call this once per signing account at app startup — after that, every
162
+ * subsequent `contract.<method>.tx({ signer })` against the same chain will
163
+ * succeed without further mapping work.
164
+ *
165
+ * @param runtime - The contract runtime (typically `createContractRuntime(...)`).
166
+ * @param address - The SS58 address of the account to map.
167
+ * @param signer - A signer matching `address`.
168
+ * @param options - Optional timeout / status callback (forwarded to the underlying tx).
169
+ * @returns The `TxResult` from the mapping extrinsic, or `null` if already mapped.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * import { createContractRuntime, ensureContractAccountMapped } from "@parity/product-sdk-contracts";
174
+ *
175
+ * const runtime = createContractRuntime(client.getTypedApi(paseo_asset_hub));
176
+ * await ensureContractAccountMapped(runtime, signerManager.getState().selectedAccount!.address, signer);
177
+ * // now safe to call contract.<method>.tx({ signer })
178
+ * ```
179
+ */
180
+ declare function ensureContractAccountMapped(runtime: ContractRuntime, address: SS58String, signer: PolkadotSigner, options?: {
181
+ timeoutMs?: number;
182
+ onStatus?: (s: string) => void;
183
+ }): Promise<TxResult | null>;
184
+
8
185
  /**
9
186
  * Manages typed contract interactions backed by a `cdm.json` manifest.
10
187
  *
@@ -15,21 +192,19 @@ import '@parity/product-sdk-signer';
15
192
  * @example
16
193
  * ```ts
17
194
  * import { createChainClient } from "@parity/product-sdk-chain-client";
18
- * import { createInkSdk } from "@polkadot-api/sdk-ink";
19
195
  * import { paseo_asset_hub } from "@parity/product-sdk-descriptors/paseo-asset-hub";
20
- * import { ContractManager } from "@parity/product-sdk-contracts";
196
+ * import { ContractManager, createContractRuntime } from "@parity/product-sdk-contracts";
21
197
  * import cdmJson from "./cdm.json";
22
198
  *
23
199
  * const client = await createChainClient({
24
200
  * chains: { assetHub: paseo_asset_hub },
25
- * rpcs: { assetHub: ["wss://sys.ibp.network/asset-hub-paseo"] },
201
+ * rpcs: { assetHub: ["wss://paseo-asset-hub-next-rpc.polkadot.io"] },
26
202
  * });
27
- * const inkSdk = createInkSdk(client.raw.assetHub, { atBest: true });
28
- * const manager = new ContractManager(cdmJson, inkSdk, {
29
- * signerManager: signerManager, // from @parity/product-sdk-signer
203
+ * const runtime = createContractRuntime(client.assetHub);
204
+ * const manager = new ContractManager(cdmJson, runtime, {
205
+ * signerManager,
30
206
  * });
31
207
  *
32
- * // Uses the host's logged-in account automatically
33
208
  * const counter = manager.getContract("@example/counter");
34
209
  * const { value } = await counter.getCount.query();
35
210
  * await counter.increment.tx();
@@ -38,41 +213,25 @@ import '@parity/product-sdk-signer';
38
213
  declare class ContractManager {
39
214
  private cdmJson;
40
215
  private targetHash;
41
- private inkSdk;
216
+ private runtime;
42
217
  private defaults;
43
- constructor(cdmJson: CdmJson, inkSdk: InkSdk, options?: ContractManagerOptions);
218
+ constructor(cdmJson: CdmJson, runtime: ContractRuntime, options?: ContractManagerOptions);
44
219
  /** Update the default origin, signer, or signerManager used by all contract handles. */
45
220
  setDefaults(defaults: ContractDefaults): void;
46
221
  /**
47
- * Create a ContractManager from a raw `PolkadotClient`.
48
- *
49
- * Convenience factory that creates the InkSdk internally via dynamic import
50
- * of `@polkadot-api/sdk-ink`. The ~4 MB sdk-ink metadata is loaded lazily
51
- * only when this method is called.
222
+ * Create a `ContractManager` from a raw `PolkadotClient`.
52
223
  *
53
- * For size-sensitive apps, prefer the constructor with a pre-created `InkSdk`
54
- * to control exactly when `@polkadot-api/sdk-ink` is loaded.
224
+ * Convenience factory: builds a `ContractRuntime` internally from the
225
+ * client's typed API. Requires that the chain's typed API exposes the
226
+ * `Revive` pallet and `ReviveApi` runtime API (Asset Hub Paseo /
227
+ * Polkadot / Kusama).
55
228
  *
56
229
  * @param cdmJson - The CDM manifest.
57
- * @param client - A `PolkadotClient` for the chain where contracts are deployed (e.g., `client.raw.assetHub`).
230
+ * @param client - A `PolkadotClient` for the chain where contracts are deployed.
231
+ * @param descriptor - The chain descriptor used to derive the typed API.
58
232
  * @param options - Optional configuration (signerManager, defaults).
59
- *
60
- * @example
61
- * ```ts
62
- * import { createChainClient } from "@parity/product-sdk-chain-client";
63
- * import { paseo_asset_hub } from "@parity/product-sdk-descriptors/paseo-asset-hub";
64
- * import { ContractManager } from "@parity/product-sdk-contracts";
65
- *
66
- * const client = await createChainClient({
67
- * chains: { assetHub: paseo_asset_hub },
68
- * rpcs: { assetHub: ["wss://sys.ibp.network/asset-hub-paseo"] },
69
- * });
70
- * const manager = await ContractManager.fromClient(cdmJson, client.raw.assetHub, {
71
- * signerManager,
72
- * });
73
- * ```
74
233
  */
75
- static fromClient(cdmJson: CdmJson, client: PolkadotClient, options?: ContractManagerOptions): Promise<ContractManager>;
234
+ static fromClient<TDescriptor>(cdmJson: CdmJson, client: PolkadotClient, descriptor: TDescriptor, options?: ContractManagerOptions): ContractManager;
76
235
  private getContractData;
77
236
  /**
78
237
  * Get a typed contract handle.
@@ -87,36 +246,43 @@ declare class ContractManager {
87
246
  getContract(library: string): Contract<ContractDef>;
88
247
  /** Get the on-chain address of an installed contract. */
89
248
  getAddress(library: string): HexString;
249
+ /**
250
+ * Get the underlying {@link ContractRuntime} backing this manager.
251
+ *
252
+ * Useful when a consumer needs to call helpers that take a runtime
253
+ * directly — most commonly {@link ensureContractAccountMapped} at app
254
+ * boot. Avoids the alternative of building a second runtime against the
255
+ * same client and descriptor.
256
+ */
257
+ getRuntime(): ContractRuntime;
90
258
  }
91
259
  /**
92
- * Create a contract handle from a raw address and ABI — no `cdm.json` needed.
260
+ * Create a contract handle from a raw H160 address and ABI — no `cdm.json` needed.
93
261
  *
94
262
  * @example
95
263
  * ```ts
96
- * import { createInkSdk } from "@polkadot-api/sdk-ink";
264
+ * import { createContractRuntime, createContract } from "@parity/product-sdk-contracts";
97
265
  *
98
- * const inkSdk = createInkSdk(client.raw.assetHub, { atBest: true });
99
- * const counter = createContract(inkSdk, "0xC472...", abi, {
100
- * signerManager: signerManager,
101
- * });
266
+ * const runtime = createContractRuntime(client.assetHub);
267
+ * const counter = createContract(runtime, "0xC472...", abi, { signerManager });
102
268
  * await counter.getCount.query();
103
269
  * await counter.increment.tx();
104
270
  * ```
105
271
  */
106
- declare function createContract(inkSdk: InkSdk, address: HexString, abi: AbiEntry[], options?: ContractOptions): Contract<ContractDef>;
272
+ declare function createContract(runtime: ContractRuntime, address: HexString, abi: AbiEntry[], options?: ContractOptions): Contract<ContractDef>;
107
273
  /**
108
- * Create a contract handle from a raw `PolkadotClient`, address, and ABI.
274
+ * Create a contract handle from a raw `PolkadotClient`, descriptor, address, and ABI.
109
275
  *
110
- * Convenience wrapper that creates the InkSdk internally via dynamic import.
111
- * For size-sensitive apps, use {@link createContract} with a pre-created `InkSdk`.
276
+ * Convenience wrapper that builds the `ContractRuntime` from the client's
277
+ * typed API. The chain must expose `Revive` + `ReviveApi`.
112
278
  *
113
279
  * @example
114
280
  * ```ts
115
- * const counter = await createContractFromClient(client.raw.assetHub, "0xC472...", abi);
281
+ * const counter = createContractFromClient(client, paseo_asset_hub, "0xC472...", abi);
116
282
  * const { value } = await counter.getCount.query();
117
283
  * ```
118
284
  */
119
- declare function createContractFromClient(client: PolkadotClient, address: HexString, abi: AbiEntry[], options?: ContractOptions): Promise<Contract<ContractDef>>;
285
+ declare function createContractFromClient<TDescriptor>(client: PolkadotClient, descriptor: TDescriptor, address: HexString, abi: AbiEntry[], options?: ContractOptions): Contract<ContractDef>;
120
286
 
121
287
  /** Base class for all contract errors. Use `instanceof ContractError` to catch any contract-related error. */
122
288
  declare class ContractError extends Error {
@@ -132,5 +298,19 @@ declare class ContractNotFoundError extends ContractError {
132
298
  readonly targetHash: string;
133
299
  constructor(library: string, targetHash: string);
134
300
  }
301
+ /**
302
+ * A pre-flight `ReviveApi.call` dry-run reported failure. Thrown from the `.tx()`
303
+ * path before the extrinsic is built — prevents callers from paying gas on a
304
+ * transaction the chain already told us would revert.
305
+ *
306
+ * `dispatchError` carries the chain's encoded error (typically `ModuleError`,
307
+ * `ContractReverted`, `OutOfGas`, or `AccountNotMapped` — see the `Revive`
308
+ * pallet error variants).
309
+ */
310
+ declare class ContractDryRunFailedError extends ContractError {
311
+ readonly methodName: string;
312
+ readonly dispatchError: unknown;
313
+ constructor(methodName: string, dispatchError: unknown);
314
+ }
135
315
 
136
- export { AbiEntry, CdmJson, Contract, ContractDef, ContractDefaults, ContractError, ContractManager, ContractManagerOptions, ContractNotFoundError, ContractOptions, ContractSignerMissingError, Contracts, createContract, createContractFromClient };
316
+ export { AbiEntry, CdmJson, Contract, ContractDef, ContractDefaults, ContractDryRunFailedError, ContractError, ContractManager, ContractManagerOptions, ContractNotFoundError, ContractOptions, type ContractRuntime, ContractSignerMissingError, Contracts, type ReviveDryRunCall, type ReviveDryRunResult, type ReviveTypedApi, createContract, createContractFromClient, createContractRuntime, createContractRuntimeFromClient, ensureContractAccountMapped };