@d9-network/ink 1.0.2 → 1.1.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/README.md +96 -0
- package/dist/index.cjs +612 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +705 -1
- package/dist/index.d.mts +705 -1
- package/dist/index.mjs +598 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -2
package/dist/index.d.cts
CHANGED
|
@@ -127,6 +127,180 @@ interface EventSubscriptionOptions {
|
|
|
127
127
|
*/
|
|
128
128
|
getEvents: (blockHash: string) => Promise<unknown[]>;
|
|
129
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Chain event record structure (matches polkadot-api format)
|
|
132
|
+
*
|
|
133
|
+
* Use this type when processing raw chain events to avoid `as any` casts.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* // Instead of: const events = await api.query.System.Events.getValue() as any[];
|
|
138
|
+
* // Use: const events: ChainEventRecord[] = await api.query.System.Events.getValue();
|
|
139
|
+
*
|
|
140
|
+
* for (const event of events) {
|
|
141
|
+
* if (isContractEmittedEvent(event)) {
|
|
142
|
+
* // event is now properly typed!
|
|
143
|
+
* }
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
interface ChainEventRecord {
|
|
148
|
+
/** The event phase */
|
|
149
|
+
phase?: {
|
|
150
|
+
type: string;
|
|
151
|
+
value?: number;
|
|
152
|
+
};
|
|
153
|
+
/** The event pallet and variant */
|
|
154
|
+
event: {
|
|
155
|
+
type: string;
|
|
156
|
+
value: {
|
|
157
|
+
type: string;
|
|
158
|
+
value: unknown;
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
/** Event topics (for indexed fields) */
|
|
162
|
+
topics?: Array<Uint8Array | {
|
|
163
|
+
asBytes?: () => Uint8Array;
|
|
164
|
+
}>;
|
|
165
|
+
/** Block number (if available) */
|
|
166
|
+
blockNumber?: number;
|
|
167
|
+
/** Block hash (if available) */
|
|
168
|
+
blockHash?: string;
|
|
169
|
+
/** Event index in block (if available) */
|
|
170
|
+
eventIndex?: number;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Contracts.ContractEmitted event value
|
|
174
|
+
*/
|
|
175
|
+
interface ContractEmittedValue {
|
|
176
|
+
/** Contract address (SS58 or Uint8Array) */
|
|
177
|
+
contract: SS58String | Uint8Array;
|
|
178
|
+
/** Event data (Binary or Uint8Array) */
|
|
179
|
+
data: Uint8Array | {
|
|
180
|
+
asBytes: () => Uint8Array;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Contracts.Called event value
|
|
185
|
+
*/
|
|
186
|
+
interface ContractCalledValue {
|
|
187
|
+
/** Caller address */
|
|
188
|
+
caller: SS58String;
|
|
189
|
+
/** Contract address being called */
|
|
190
|
+
contract: SS58String;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Typed chain event for Contracts.ContractEmitted
|
|
194
|
+
*/
|
|
195
|
+
interface ContractEmittedChainEvent extends ChainEventRecord {
|
|
196
|
+
event: {
|
|
197
|
+
type: "Contracts";
|
|
198
|
+
value: {
|
|
199
|
+
type: "ContractEmitted";
|
|
200
|
+
value: ContractEmittedValue;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Typed chain event for Contracts.Called
|
|
206
|
+
*/
|
|
207
|
+
interface ContractCalledChainEvent extends ChainEventRecord {
|
|
208
|
+
event: {
|
|
209
|
+
type: "Contracts";
|
|
210
|
+
value: {
|
|
211
|
+
type: "Called";
|
|
212
|
+
value: ContractCalledValue;
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Typed chain event for Balances.Transfer
|
|
218
|
+
*/
|
|
219
|
+
interface BalancesTransferChainEvent extends ChainEventRecord {
|
|
220
|
+
event: {
|
|
221
|
+
type: "Balances";
|
|
222
|
+
value: {
|
|
223
|
+
type: "Transfer";
|
|
224
|
+
value: {
|
|
225
|
+
from: SS58String;
|
|
226
|
+
to: SS58String;
|
|
227
|
+
amount: bigint;
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Type guard for Contracts.ContractEmitted events
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* for (const event of events) {
|
|
238
|
+
* if (isContractEmittedEvent(event)) {
|
|
239
|
+
* // event.event.value.value is ContractEmittedValue
|
|
240
|
+
* const { contract, data } = event.event.value.value;
|
|
241
|
+
* }
|
|
242
|
+
* }
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
declare function isContractEmittedEvent(event: ChainEventRecord): event is ContractEmittedChainEvent;
|
|
246
|
+
/**
|
|
247
|
+
* Type guard for Contracts.Called events
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* for (const event of events) {
|
|
252
|
+
* if (isContractCalledEvent(event)) {
|
|
253
|
+
* // event.event.value.value is ContractCalledValue
|
|
254
|
+
* const { caller, contract } = event.event.value.value;
|
|
255
|
+
* }
|
|
256
|
+
* }
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
declare function isContractCalledEvent(event: ChainEventRecord): event is ContractCalledChainEvent;
|
|
260
|
+
/**
|
|
261
|
+
* Type guard for Balances.Transfer events
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* for (const event of events) {
|
|
266
|
+
* if (isBalancesTransferEvent(event)) {
|
|
267
|
+
* const { from, to, amount } = event.event.value.value;
|
|
268
|
+
* }
|
|
269
|
+
* }
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
declare function isBalancesTransferEvent(event: ChainEventRecord): event is BalancesTransferChainEvent;
|
|
273
|
+
/**
|
|
274
|
+
* Check if event is in ApplyExtrinsic phase
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* for (const event of events) {
|
|
279
|
+
* if (isApplyExtrinsicPhase(event)) {
|
|
280
|
+
* const extrinsicIndex = event.phase.value;
|
|
281
|
+
* }
|
|
282
|
+
* }
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
declare function isApplyExtrinsicPhase(event: ChainEventRecord): event is ChainEventRecord & {
|
|
286
|
+
phase: {
|
|
287
|
+
type: "ApplyExtrinsic";
|
|
288
|
+
value: number;
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Extract contract emitted data with proper typing
|
|
293
|
+
*
|
|
294
|
+
* Handles both Uint8Array and Binary object formats.
|
|
295
|
+
*
|
|
296
|
+
* @param event - Chain event record
|
|
297
|
+
* @returns Extracted data or null if not a ContractEmitted event
|
|
298
|
+
*/
|
|
299
|
+
declare function extractContractEmittedData(event: ChainEventRecord): {
|
|
300
|
+
contract: Uint8Array;
|
|
301
|
+
data: Uint8Array;
|
|
302
|
+
topics: Uint8Array[];
|
|
303
|
+
} | null;
|
|
130
304
|
//#endregion
|
|
131
305
|
//#region src/call-types.d.ts
|
|
132
306
|
/**
|
|
@@ -1088,10 +1262,40 @@ declare class ContractEventParser<S extends InkStorageDescriptor = InkStorageDes
|
|
|
1088
1262
|
filterByType<L extends string>(chainEvents: unknown[], label: L): Array<TypedContractEvent<E> & {
|
|
1089
1263
|
type: L;
|
|
1090
1264
|
}>;
|
|
1265
|
+
/**
|
|
1266
|
+
* Parse chain event records with proper typing
|
|
1267
|
+
*
|
|
1268
|
+
* Unlike parseEvent/filterEvents which accept `unknown[]`, this method
|
|
1269
|
+
* accepts typed `ChainEventRecord[]` which eliminates the need for
|
|
1270
|
+
* `as any` casts in user code.
|
|
1271
|
+
*
|
|
1272
|
+
* @param events - Array of typed chain event records
|
|
1273
|
+
* @param options - Optional filter criteria
|
|
1274
|
+
* @returns Array of typed contract events
|
|
1275
|
+
*
|
|
1276
|
+
* @example
|
|
1277
|
+
* ```ts
|
|
1278
|
+
* // No more `as any` needed!
|
|
1279
|
+
* const events: ChainEventRecord[] = await api.query.System.Events.getValue();
|
|
1280
|
+
* const contractEvents = parser.parseChainEvents(events);
|
|
1281
|
+
*
|
|
1282
|
+
* for (const event of contractEvents) {
|
|
1283
|
+
* if (event.type === "Transfer") {
|
|
1284
|
+
* // Fully typed!
|
|
1285
|
+
* console.log(event.value.from, event.value.to, event.value.value);
|
|
1286
|
+
* }
|
|
1287
|
+
* }
|
|
1288
|
+
* ```
|
|
1289
|
+
*/
|
|
1290
|
+
parseChainEvents(events: ChainEventRecord[], options?: EventFilterOptions): TypedContractEvent<E>[];
|
|
1091
1291
|
/**
|
|
1092
1292
|
* Get the contract address as SS58 string
|
|
1093
1293
|
*/
|
|
1094
1294
|
private getContractAddress;
|
|
1295
|
+
/**
|
|
1296
|
+
* Get the contract address as bytes
|
|
1297
|
+
*/
|
|
1298
|
+
getContractAddressBytes(): Uint8Array;
|
|
1095
1299
|
/**
|
|
1096
1300
|
* Extract ContractEmitted event from chain event structure
|
|
1097
1301
|
* Based on polkadot-api event format
|
|
@@ -1262,6 +1466,80 @@ declare class ContractCallParser<S extends InkStorageDescriptor = InkStorageDesc
|
|
|
1262
1466
|
* Check if a selector matches a specific message
|
|
1263
1467
|
*/
|
|
1264
1468
|
matchesMessage(selector: Uint8Array, label: string): boolean;
|
|
1469
|
+
/**
|
|
1470
|
+
* Get selector for a message label
|
|
1471
|
+
*
|
|
1472
|
+
* @param label - Message label (e.g., "PSP22::transfer")
|
|
1473
|
+
* @returns Selector as Uint8Array or null if not found
|
|
1474
|
+
*
|
|
1475
|
+
* @example
|
|
1476
|
+
* ```ts
|
|
1477
|
+
* const selector = parser.getSelector("PSP22::transfer");
|
|
1478
|
+
* // selector is Uint8Array([0xdb, 0x20, 0xf9, 0xf5])
|
|
1479
|
+
* ```
|
|
1480
|
+
*/
|
|
1481
|
+
getSelector(label: string): Uint8Array | null;
|
|
1482
|
+
/**
|
|
1483
|
+
* Get selector as hex string for a message label
|
|
1484
|
+
*
|
|
1485
|
+
* @param label - Message label (e.g., "PSP22::transfer")
|
|
1486
|
+
* @returns Selector hex string (without 0x) or null if not found
|
|
1487
|
+
*
|
|
1488
|
+
* @example
|
|
1489
|
+
* ```ts
|
|
1490
|
+
* const selectorHex = parser.getSelectorHex("PSP22::transfer");
|
|
1491
|
+
* // selectorHex is "db20f9f5"
|
|
1492
|
+
* ```
|
|
1493
|
+
*/
|
|
1494
|
+
getSelectorHex(label: string): string | null;
|
|
1495
|
+
/**
|
|
1496
|
+
* Get the label for a selector
|
|
1497
|
+
*
|
|
1498
|
+
* @param selector - Selector as Uint8Array or hex string (with or without 0x)
|
|
1499
|
+
* @returns Message label or null if not found
|
|
1500
|
+
*
|
|
1501
|
+
* @example
|
|
1502
|
+
* ```ts
|
|
1503
|
+
* const label = parser.getLabel("db20f9f5");
|
|
1504
|
+
* // label is "PSP22::transfer"
|
|
1505
|
+
*
|
|
1506
|
+
* // Also works with Uint8Array
|
|
1507
|
+
* const label2 = parser.getLabel(callData.slice(0, 4));
|
|
1508
|
+
* ```
|
|
1509
|
+
*/
|
|
1510
|
+
getLabel(selector: Uint8Array | string): string | null;
|
|
1511
|
+
/**
|
|
1512
|
+
* Get all selector-to-label mappings
|
|
1513
|
+
*
|
|
1514
|
+
* @returns Map of selector hex (without 0x) -> label
|
|
1515
|
+
*
|
|
1516
|
+
* @example
|
|
1517
|
+
* ```ts
|
|
1518
|
+
* const map = parser.getSelectorMap();
|
|
1519
|
+
* for (const [selectorHex, label] of map) {
|
|
1520
|
+
* console.log(`${selectorHex} -> ${label}`);
|
|
1521
|
+
* }
|
|
1522
|
+
* ```
|
|
1523
|
+
*/
|
|
1524
|
+
getSelectorMap(): Map<string, string>;
|
|
1525
|
+
/**
|
|
1526
|
+
* Check if a selector exists in this contract
|
|
1527
|
+
*
|
|
1528
|
+
* @param selector - Selector to check (Uint8Array or hex string)
|
|
1529
|
+
* @returns True if selector is valid for this contract
|
|
1530
|
+
*
|
|
1531
|
+
* @example
|
|
1532
|
+
* ```ts
|
|
1533
|
+
* if (parser.hasSelector("db20f9f5")) {
|
|
1534
|
+
* // This selector is valid for this contract
|
|
1535
|
+
* }
|
|
1536
|
+
* ```
|
|
1537
|
+
*/
|
|
1538
|
+
hasSelector(selector: Uint8Array | string): boolean;
|
|
1539
|
+
/**
|
|
1540
|
+
* Normalize selector to hex string (without 0x prefix)
|
|
1541
|
+
*/
|
|
1542
|
+
private normalizeSelector;
|
|
1265
1543
|
}
|
|
1266
1544
|
/**
|
|
1267
1545
|
* Type guard for narrowing call types
|
|
@@ -1291,6 +1569,432 @@ declare function isCallType<M$1 extends InkCallableDescriptor, L extends Extract
|
|
|
1291
1569
|
type: L;
|
|
1292
1570
|
}>;
|
|
1293
1571
|
//#endregion
|
|
1572
|
+
//#region src/infer-types.d.ts
|
|
1573
|
+
/**
|
|
1574
|
+
* Infer ContractCallParser type from a contract descriptor
|
|
1575
|
+
*
|
|
1576
|
+
* Use this when you need to declare a property type without instantiation.
|
|
1577
|
+
*
|
|
1578
|
+
* @typeParam D - The contract descriptor type (e.g., typeof contracts.usdt)
|
|
1579
|
+
*
|
|
1580
|
+
* @example
|
|
1581
|
+
* ```ts
|
|
1582
|
+
* import { contracts } from "@d9-network/spec";
|
|
1583
|
+
* import type { ContractCallParserOf } from "@d9-network/ink";
|
|
1584
|
+
*
|
|
1585
|
+
* class USDTIndexer {
|
|
1586
|
+
* private callParser: ContractCallParserOf<typeof contracts.usdt>;
|
|
1587
|
+
*
|
|
1588
|
+
* constructor() {
|
|
1589
|
+
* this.callParser = new ContractCallParser(contracts.usdt);
|
|
1590
|
+
* }
|
|
1591
|
+
* }
|
|
1592
|
+
* ```
|
|
1593
|
+
*/
|
|
1594
|
+
type ContractCallParserOf<D> = D extends InkDescriptors<infer S, infer M, infer C, infer E> ? ContractCallParser<S extends InkStorageDescriptor ? S : InkStorageDescriptor, M extends InkCallableDescriptor ? M : InkCallableDescriptor, C extends InkCallableDescriptor ? C : InkCallableDescriptor, E extends Event ? E : Event> : never;
|
|
1595
|
+
/**
|
|
1596
|
+
* Infer ContractEventParser type from a contract descriptor
|
|
1597
|
+
*
|
|
1598
|
+
* Use this when you need to declare a property type without instantiation.
|
|
1599
|
+
*
|
|
1600
|
+
* @typeParam D - The contract descriptor type (e.g., typeof contracts.usdt)
|
|
1601
|
+
*
|
|
1602
|
+
* @example
|
|
1603
|
+
* ```ts
|
|
1604
|
+
* import { contracts } from "@d9-network/spec";
|
|
1605
|
+
* import type { ContractEventParserOf } from "@d9-network/ink";
|
|
1606
|
+
*
|
|
1607
|
+
* class USDTIndexer {
|
|
1608
|
+
* private eventParser: ContractEventParserOf<typeof contracts.usdt>;
|
|
1609
|
+
*
|
|
1610
|
+
* constructor(contractAddress: SS58String) {
|
|
1611
|
+
* this.eventParser = new ContractEventParser(contracts.usdt, contractAddress);
|
|
1612
|
+
* }
|
|
1613
|
+
* }
|
|
1614
|
+
* ```
|
|
1615
|
+
*/
|
|
1616
|
+
type ContractEventParserOf<D> = D extends InkDescriptors<infer S, infer M, infer C, infer E> ? ContractEventParser<S extends InkStorageDescriptor ? S : InkStorageDescriptor, M extends InkCallableDescriptor ? M : InkCallableDescriptor, C extends InkCallableDescriptor ? C : InkCallableDescriptor, E extends Event ? E : Event> : never;
|
|
1617
|
+
/**
|
|
1618
|
+
* Infer D9InkContract type from a contract descriptor
|
|
1619
|
+
*
|
|
1620
|
+
* Use this when you need to declare a property type without instantiation.
|
|
1621
|
+
*
|
|
1622
|
+
* @typeParam D - The contract descriptor type (e.g., typeof contracts.usdt)
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```ts
|
|
1626
|
+
* import { contracts } from "@d9-network/spec";
|
|
1627
|
+
* import type { D9InkContractOf } from "@d9-network/ink";
|
|
1628
|
+
*
|
|
1629
|
+
* class WalletService {
|
|
1630
|
+
* private usdtContract: D9InkContractOf<typeof contracts.usdt>;
|
|
1631
|
+
*
|
|
1632
|
+
* constructor(sdk: D9InkSdk, contractAddress: SS58String) {
|
|
1633
|
+
* this.usdtContract = sdk.getContract(contracts.usdt, contractAddress);
|
|
1634
|
+
* }
|
|
1635
|
+
* }
|
|
1636
|
+
* ```
|
|
1637
|
+
*/
|
|
1638
|
+
type D9InkContractOf<D> = D extends InkDescriptors<infer _S, infer M, infer _C, infer E> ? D9InkContract<M extends InkCallableDescriptor ? M : InkCallableDescriptor, E extends Event ? E : Event> : never;
|
|
1639
|
+
//#endregion
|
|
1640
|
+
//#region src/selectors.d.ts
|
|
1641
|
+
/**
|
|
1642
|
+
* Information about a contract message selector
|
|
1643
|
+
*/
|
|
1644
|
+
interface SelectorInfo {
|
|
1645
|
+
/** Message label (e.g., "PSP22::transfer") */
|
|
1646
|
+
label: string;
|
|
1647
|
+
/** 4-byte selector */
|
|
1648
|
+
selector: Uint8Array;
|
|
1649
|
+
/** Hex string without 0x prefix (e.g., "db20f9f5") */
|
|
1650
|
+
selectorHex: string;
|
|
1651
|
+
/** Whether the message mutates state */
|
|
1652
|
+
mutates: boolean;
|
|
1653
|
+
/** Whether the message is payable */
|
|
1654
|
+
payable: boolean;
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* PSP22 standard selectors for quick reference
|
|
1658
|
+
*
|
|
1659
|
+
* These are derived from the PSP22 standard and are consistent across implementations.
|
|
1660
|
+
* Use these when you need to quickly check if call data matches a specific PSP22 method.
|
|
1661
|
+
*
|
|
1662
|
+
* @example
|
|
1663
|
+
* ```ts
|
|
1664
|
+
* import { PSP22_SELECTORS, extractSelectorHex } from "@d9-network/ink";
|
|
1665
|
+
*
|
|
1666
|
+
* const selectorHex = extractSelectorHex(callData);
|
|
1667
|
+
* if (selectorHex === PSP22_SELECTORS.transfer) {
|
|
1668
|
+
* // This is a PSP22::transfer call
|
|
1669
|
+
* }
|
|
1670
|
+
* ```
|
|
1671
|
+
*/
|
|
1672
|
+
declare const PSP22_SELECTORS: {
|
|
1673
|
+
/** PSP22::total_supply - 0x162df8c2 */
|
|
1674
|
+
readonly totalSupply: "162df8c2";
|
|
1675
|
+
/** PSP22::balance_of - 0x6568382f */
|
|
1676
|
+
readonly balanceOf: "6568382f";
|
|
1677
|
+
/** PSP22::allowance - 0x4d47d921 */
|
|
1678
|
+
readonly allowance: "4d47d921";
|
|
1679
|
+
/** PSP22::transfer - 0xdb20f9f5 */
|
|
1680
|
+
readonly transfer: "db20f9f5";
|
|
1681
|
+
/** PSP22::transfer_from - 0x54b3c76e */
|
|
1682
|
+
readonly transferFrom: "54b3c76e";
|
|
1683
|
+
/** PSP22::approve - 0xb20f1bbd */
|
|
1684
|
+
readonly approve: "b20f1bbd";
|
|
1685
|
+
/** PSP22::increase_allowance - 0x96d6b57a */
|
|
1686
|
+
readonly increaseAllowance: "96d6b57a";
|
|
1687
|
+
/** PSP22::decrease_allowance - 0xfecb57d5 */
|
|
1688
|
+
readonly decreaseAllowance: "fecb57d5";
|
|
1689
|
+
};
|
|
1690
|
+
/**
|
|
1691
|
+
* PSP22 selector type for type safety
|
|
1692
|
+
*/
|
|
1693
|
+
type PSP22SelectorKey = keyof typeof PSP22_SELECTORS;
|
|
1694
|
+
/**
|
|
1695
|
+
* Build a selector lookup map from contract metadata
|
|
1696
|
+
*
|
|
1697
|
+
* @param metadata - Contract metadata
|
|
1698
|
+
* @returns Map of selector hex (without 0x) -> SelectorInfo
|
|
1699
|
+
*
|
|
1700
|
+
* @example
|
|
1701
|
+
* ```ts
|
|
1702
|
+
* import { contracts } from "@d9-network/spec";
|
|
1703
|
+
* import { buildSelectorMap } from "@d9-network/ink";
|
|
1704
|
+
*
|
|
1705
|
+
* const selectors = buildSelectorMap(contracts.usdt.metadata);
|
|
1706
|
+
* const info = selectors.get("db20f9f5");
|
|
1707
|
+
* if (info) {
|
|
1708
|
+
* console.log(info.label); // "PSP22::transfer"
|
|
1709
|
+
* }
|
|
1710
|
+
* ```
|
|
1711
|
+
*/
|
|
1712
|
+
declare function buildSelectorMap(metadata: InkMetadata): Map<string, SelectorInfo>;
|
|
1713
|
+
/**
|
|
1714
|
+
* Build a reverse lookup map (label -> selector info)
|
|
1715
|
+
*
|
|
1716
|
+
* @param metadata - Contract metadata
|
|
1717
|
+
* @returns Map of label -> SelectorInfo
|
|
1718
|
+
*
|
|
1719
|
+
* @example
|
|
1720
|
+
* ```ts
|
|
1721
|
+
* const labelMap = buildLabelMap(contracts.usdt.metadata);
|
|
1722
|
+
* const info = labelMap.get("PSP22::transfer");
|
|
1723
|
+
* console.log(info?.selectorHex); // "db20f9f5"
|
|
1724
|
+
* ```
|
|
1725
|
+
*/
|
|
1726
|
+
declare function buildLabelMap(metadata: InkMetadata): Map<string, SelectorInfo>;
|
|
1727
|
+
/**
|
|
1728
|
+
* Get selector for a message label
|
|
1729
|
+
*
|
|
1730
|
+
* @param metadata - Contract metadata
|
|
1731
|
+
* @param label - Message label (e.g., "PSP22::transfer")
|
|
1732
|
+
* @returns Selector info or null if not found
|
|
1733
|
+
*
|
|
1734
|
+
* @example
|
|
1735
|
+
* ```ts
|
|
1736
|
+
* const info = getSelectorForLabel(contracts.usdt.metadata, "PSP22::transfer");
|
|
1737
|
+
* console.log(info?.selectorHex); // "db20f9f5"
|
|
1738
|
+
* ```
|
|
1739
|
+
*/
|
|
1740
|
+
declare function getSelectorForLabel(metadata: InkMetadata, label: string): SelectorInfo | null;
|
|
1741
|
+
/**
|
|
1742
|
+
* Get message label for a selector
|
|
1743
|
+
*
|
|
1744
|
+
* @param metadata - Contract metadata
|
|
1745
|
+
* @param selector - Selector as Uint8Array or hex string (with or without 0x)
|
|
1746
|
+
* @returns Selector info or null if not found
|
|
1747
|
+
*
|
|
1748
|
+
* @example
|
|
1749
|
+
* ```ts
|
|
1750
|
+
* const info = getLabelForSelector(contracts.usdt.metadata, "db20f9f5");
|
|
1751
|
+
* console.log(info?.label); // "PSP22::transfer"
|
|
1752
|
+
*
|
|
1753
|
+
* // Also works with Uint8Array
|
|
1754
|
+
* const info2 = getLabelForSelector(contracts.usdt.metadata, callData.slice(0, 4));
|
|
1755
|
+
* ```
|
|
1756
|
+
*/
|
|
1757
|
+
declare function getLabelForSelector(metadata: InkMetadata, selector: Uint8Array | string): SelectorInfo | null;
|
|
1758
|
+
/**
|
|
1759
|
+
* Check if call data matches a specific message
|
|
1760
|
+
*
|
|
1761
|
+
* @param metadata - Contract metadata
|
|
1762
|
+
* @param callData - Call data bytes or hex string
|
|
1763
|
+
* @param label - Message label to check
|
|
1764
|
+
* @returns True if the call data selector matches the label
|
|
1765
|
+
*
|
|
1766
|
+
* @example
|
|
1767
|
+
* ```ts
|
|
1768
|
+
* if (isMessageCall(contracts.usdt.metadata, callData, "PSP22::transfer")) {
|
|
1769
|
+
* // This is a transfer call
|
|
1770
|
+
* }
|
|
1771
|
+
* ```
|
|
1772
|
+
*/
|
|
1773
|
+
declare function isMessageCall(metadata: InkMetadata, callData: Uint8Array | string, label: string): boolean;
|
|
1774
|
+
/**
|
|
1775
|
+
* Check if call data matches a PSP22 method
|
|
1776
|
+
*
|
|
1777
|
+
* @param callData - Call data bytes or hex string
|
|
1778
|
+
* @param method - PSP22 method name (e.g., "transfer", "transferFrom")
|
|
1779
|
+
* @returns True if the call data selector matches the PSP22 method
|
|
1780
|
+
*
|
|
1781
|
+
* @example
|
|
1782
|
+
* ```ts
|
|
1783
|
+
* if (isPSP22Call(callData, "transfer")) {
|
|
1784
|
+
* // This is a PSP22::transfer call
|
|
1785
|
+
* }
|
|
1786
|
+
* ```
|
|
1787
|
+
*/
|
|
1788
|
+
declare function isPSP22Call(callData: Uint8Array | string, method: PSP22SelectorKey): boolean;
|
|
1789
|
+
/**
|
|
1790
|
+
* Get all selectors from contract metadata
|
|
1791
|
+
*
|
|
1792
|
+
* @param metadata - Contract metadata
|
|
1793
|
+
* @returns Array of SelectorInfo for all messages
|
|
1794
|
+
*/
|
|
1795
|
+
declare function getAllSelectors(metadata: InkMetadata): SelectorInfo[];
|
|
1796
|
+
//#endregion
|
|
1797
|
+
//#region src/extrinsic-parser.d.ts
|
|
1798
|
+
/**
|
|
1799
|
+
* Raw extrinsic data that may contain a contract call
|
|
1800
|
+
*
|
|
1801
|
+
* This interface is designed to work with various extrinsic formats
|
|
1802
|
+
* from different data sources (polkadot-api, subsquid, etc.)
|
|
1803
|
+
*/
|
|
1804
|
+
interface RawExtrinsic {
|
|
1805
|
+
/** Extrinsic hash */
|
|
1806
|
+
hash?: string;
|
|
1807
|
+
/** Block number */
|
|
1808
|
+
blockNumber?: number;
|
|
1809
|
+
/** Block hash */
|
|
1810
|
+
blockHash?: string;
|
|
1811
|
+
/** Extrinsic index in block */
|
|
1812
|
+
index?: number;
|
|
1813
|
+
/** Signer address */
|
|
1814
|
+
signer?: SS58String | string;
|
|
1815
|
+
/** The call structure - can be nested */
|
|
1816
|
+
call?: ExtrinsicCall;
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
* Extrinsic call structure (matches polkadot-api format)
|
|
1820
|
+
*/
|
|
1821
|
+
interface ExtrinsicCall {
|
|
1822
|
+
type?: string;
|
|
1823
|
+
value?: {
|
|
1824
|
+
type?: string;
|
|
1825
|
+
value?: ContractsCallValue;
|
|
1826
|
+
};
|
|
1827
|
+
}
|
|
1828
|
+
/**
|
|
1829
|
+
* Contracts.call extrinsic value
|
|
1830
|
+
*/
|
|
1831
|
+
interface ContractsCallValue {
|
|
1832
|
+
/** Contract address (SS58 or other format) */
|
|
1833
|
+
dest?: SS58String | {
|
|
1834
|
+
type: string;
|
|
1835
|
+
value: SS58String;
|
|
1836
|
+
} | {
|
|
1837
|
+
Id: SS58String;
|
|
1838
|
+
};
|
|
1839
|
+
/** Value transferred with the call */
|
|
1840
|
+
value?: bigint | string | number;
|
|
1841
|
+
/** Call data (Binary or Uint8Array) */
|
|
1842
|
+
data?: Uint8Array | {
|
|
1843
|
+
asBytes?: () => Uint8Array;
|
|
1844
|
+
};
|
|
1845
|
+
/** Gas limit */
|
|
1846
|
+
gas_limit?: unknown;
|
|
1847
|
+
gasLimit?: unknown;
|
|
1848
|
+
/** Storage deposit limit */
|
|
1849
|
+
storage_deposit_limit?: unknown;
|
|
1850
|
+
storageDepositLimit?: unknown;
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Parsed contract extrinsic result
|
|
1854
|
+
*/
|
|
1855
|
+
interface ParsedContractExtrinsic<M$1 extends InkCallableDescriptor = InkCallableDescriptor> {
|
|
1856
|
+
/** The parsed contract call */
|
|
1857
|
+
call: TypedContractCall<M$1>;
|
|
1858
|
+
/** Extrinsic metadata */
|
|
1859
|
+
extrinsic: {
|
|
1860
|
+
/** Extrinsic hash */
|
|
1861
|
+
hash: string;
|
|
1862
|
+
/** Block number */
|
|
1863
|
+
blockNumber: number;
|
|
1864
|
+
/** Block hash */
|
|
1865
|
+
blockHash: string;
|
|
1866
|
+
/** Extrinsic index in block */
|
|
1867
|
+
index: number;
|
|
1868
|
+
/** Signer address */
|
|
1869
|
+
signer: string;
|
|
1870
|
+
};
|
|
1871
|
+
/** Contract address being called */
|
|
1872
|
+
contractAddress: SS58String;
|
|
1873
|
+
/** Value transferred with call */
|
|
1874
|
+
value: bigint;
|
|
1875
|
+
}
|
|
1876
|
+
/**
|
|
1877
|
+
* Extrinsic parser options
|
|
1878
|
+
*/
|
|
1879
|
+
interface ExtrinsicParserOptions {
|
|
1880
|
+
/** Contract addresses to filter for (if not provided, parses all contract calls) */
|
|
1881
|
+
contractAddresses?: SS58String[];
|
|
1882
|
+
/** Message labels to filter for */
|
|
1883
|
+
messageLabels?: string[];
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* Parser for extracting contract calls from extrinsics
|
|
1887
|
+
*
|
|
1888
|
+
* This class provides a high-level API for indexers to parse contract
|
|
1889
|
+
* interactions from blockchain extrinsics without manual hex manipulation.
|
|
1890
|
+
*
|
|
1891
|
+
* @typeParam S - The storage descriptor type
|
|
1892
|
+
* @typeParam M - The InkCallableDescriptor type (message definitions)
|
|
1893
|
+
* @typeParam C - The constructors descriptor type
|
|
1894
|
+
* @typeParam E - The events type
|
|
1895
|
+
*
|
|
1896
|
+
* @example
|
|
1897
|
+
* ```ts
|
|
1898
|
+
* import { ExtrinsicParser } from "@d9-network/ink";
|
|
1899
|
+
* import { contracts } from "@d9-network/spec";
|
|
1900
|
+
*
|
|
1901
|
+
* // Create parser for USDT contract
|
|
1902
|
+
* const parser = new ExtrinsicParser(contracts.usdt, {
|
|
1903
|
+
* contractAddresses: [USDT_ADDRESS],
|
|
1904
|
+
* });
|
|
1905
|
+
*
|
|
1906
|
+
* // Parse extrinsics from a block
|
|
1907
|
+
* for (const extrinsic of block.extrinsics) {
|
|
1908
|
+
* const parsed = parser.parseExtrinsic(extrinsic);
|
|
1909
|
+
* if (parsed?.call.type === "PSP22::transfer") {
|
|
1910
|
+
* console.log(`Transfer: ${parsed.call.args.value} to ${parsed.call.args.to}`);
|
|
1911
|
+
* }
|
|
1912
|
+
* }
|
|
1913
|
+
* ```
|
|
1914
|
+
*/
|
|
1915
|
+
declare class ExtrinsicParser<S extends InkStorageDescriptor = InkStorageDescriptor, M$1 extends InkCallableDescriptor = InkCallableDescriptor, C extends InkCallableDescriptor = InkCallableDescriptor, E extends Event = Event> {
|
|
1916
|
+
private callParser;
|
|
1917
|
+
private options;
|
|
1918
|
+
constructor(descriptor: InkDescriptors<S, M$1, C, E>, options?: ExtrinsicParserOptions);
|
|
1919
|
+
/**
|
|
1920
|
+
* Parse a single extrinsic for contract calls
|
|
1921
|
+
*
|
|
1922
|
+
* @param extrinsic - Raw extrinsic data
|
|
1923
|
+
* @returns Parsed contract extrinsic or null if not a valid contract call
|
|
1924
|
+
*/
|
|
1925
|
+
parseExtrinsic(extrinsic: RawExtrinsic): ParsedContractExtrinsic<M$1> | null;
|
|
1926
|
+
/**
|
|
1927
|
+
* Parse multiple extrinsics and filter contract calls
|
|
1928
|
+
*
|
|
1929
|
+
* @param extrinsics - Array of raw extrinsics
|
|
1930
|
+
* @returns Array of parsed contract extrinsics
|
|
1931
|
+
*/
|
|
1932
|
+
parseExtrinsics(extrinsics: RawExtrinsic[]): ParsedContractExtrinsic<M$1>[];
|
|
1933
|
+
/**
|
|
1934
|
+
* Filter extrinsics by message type with type narrowing
|
|
1935
|
+
*
|
|
1936
|
+
* @param extrinsics - Array of raw extrinsics
|
|
1937
|
+
* @param label - Message label to filter by
|
|
1938
|
+
* @returns Filtered and typed extrinsics
|
|
1939
|
+
*
|
|
1940
|
+
* @example
|
|
1941
|
+
* ```ts
|
|
1942
|
+
* const transfers = parser.filterByMessageType(extrinsics, "PSP22::transfer");
|
|
1943
|
+
* for (const tx of transfers) {
|
|
1944
|
+
* // tx.call.args is fully typed
|
|
1945
|
+
* console.log(tx.call.args.to, tx.call.args.value);
|
|
1946
|
+
* }
|
|
1947
|
+
* ```
|
|
1948
|
+
*/
|
|
1949
|
+
filterByMessageType<L extends string>(extrinsics: RawExtrinsic[], label: L): Array<ParsedContractExtrinsic<M$1> & {
|
|
1950
|
+
call: {
|
|
1951
|
+
type: L;
|
|
1952
|
+
};
|
|
1953
|
+
}>;
|
|
1954
|
+
/**
|
|
1955
|
+
* Check if an extrinsic is a contract call
|
|
1956
|
+
*
|
|
1957
|
+
* @param extrinsic - Raw extrinsic
|
|
1958
|
+
* @returns True if this is a Contracts.call extrinsic
|
|
1959
|
+
*/
|
|
1960
|
+
isContractCall(extrinsic: RawExtrinsic): boolean;
|
|
1961
|
+
/**
|
|
1962
|
+
* Extract call data from an extrinsic structure
|
|
1963
|
+
*
|
|
1964
|
+
* Handles various extrinsic formats (polkadot-api, subsquid, etc.)
|
|
1965
|
+
*
|
|
1966
|
+
* @param extrinsic - Raw extrinsic
|
|
1967
|
+
* @returns Extracted call data or null
|
|
1968
|
+
*/
|
|
1969
|
+
extractCallData(extrinsic: RawExtrinsic): {
|
|
1970
|
+
data: Uint8Array;
|
|
1971
|
+
contractAddress: SS58String;
|
|
1972
|
+
value: bigint;
|
|
1973
|
+
} | null;
|
|
1974
|
+
/**
|
|
1975
|
+
* Get access to the underlying call parser
|
|
1976
|
+
*/
|
|
1977
|
+
getCallParser(): ContractCallParser<S, M$1, C, E>;
|
|
1978
|
+
private extractContractAddress;
|
|
1979
|
+
private extractBytes;
|
|
1980
|
+
private extractBigInt;
|
|
1981
|
+
}
|
|
1982
|
+
/**
|
|
1983
|
+
* Create an extrinsic parser from a contract descriptor
|
|
1984
|
+
*
|
|
1985
|
+
* @param descriptor - Contract descriptor
|
|
1986
|
+
* @param options - Parser options
|
|
1987
|
+
* @returns ExtrinsicParser instance
|
|
1988
|
+
*
|
|
1989
|
+
* @example
|
|
1990
|
+
* ```ts
|
|
1991
|
+
* const parser = createExtrinsicParser(contracts.usdt, {
|
|
1992
|
+
* contractAddresses: [USDT_ADDRESS],
|
|
1993
|
+
* });
|
|
1994
|
+
* ```
|
|
1995
|
+
*/
|
|
1996
|
+
declare function createExtrinsicParser<S extends InkStorageDescriptor, M$1 extends InkCallableDescriptor, C extends InkCallableDescriptor, E extends Event>(descriptor: InkDescriptors<S, M$1, C, E>, options?: ExtrinsicParserOptions): ExtrinsicParser<S, M$1, C, E>;
|
|
1997
|
+
//#endregion
|
|
1294
1998
|
//#region src/rpc.d.ts
|
|
1295
1999
|
/**
|
|
1296
2000
|
* Create a type-safe RPC proxy from a PolkadotClient
|
|
@@ -1424,5 +2128,5 @@ declare function compareGasWeight(a: GasWeight, b: GasWeight): -1 | 0 | 1;
|
|
|
1424
2128
|
*/
|
|
1425
2129
|
declare function gasExceedsLimit(gas: GasWeight, limit: GasWeight): boolean;
|
|
1426
2130
|
//#endregion
|
|
1427
|
-
export { AbortedError, type BlockHeader, type CallFilterOptions, ContractCallParser, type ContractCallResult, ContractError, type ContractErrorType, ContractEventParser, ContractExecutionError, type ContractMessageBuilder, type ContractStorage, type CreateContractOptions, type CreateD9InkSdkOptions, type D9InkContract, type D9InkContractFromDescriptor, type D9InkSdk, type D9InkSdkOptions, type D9InkSdkWithRpc, DecodeError, type DecodedContractEvent, EncodeError, EstimatedCost, type EventFilterOptions, type EventSubscriptionOptions, type ExtractEnumDef, type ExtractEventLabels, type ExtractMessageLabels, FormattedGasInfo, type GasInfo, GasWeight, type InferEvents, type InferMessages, InkCodecs, LangError, type MessageAttributes, type MessageInfo, MetadataError, NetworkError, type QueryOptions, type QueryResult, type QuerySuccessValue, type RawContractCall, type RawContractEvent, type ResponseDecoder, type RuntimeVersion, type SendOptions, type SendableTransaction, SignerError, type StorageChangeSet, type StorageDepositInfo, type SubstrateRpcMethods, type SystemHealth, TimeoutError, TransactionError, type TxResult, type TypedCallFilterOptions, type TypedContractCall, type TypedContractEvent, type TypedEventFilterOptions, type TypedMessage, type TypedRpc, applyGasMargin, buildAllEventDecoders, buildAllMessageDecoders, buildEventDecoder, buildMessageDecoder, compareGasWeight, createAsciiEventTopic, createCodecRegistry, createContractEventStream, createD9InkContract, createD9InkSdk, createMessageBuilder, createNativeTransferStream, createPSP22TransferStream, createTypedRpc, decodeContractCallResult, decodeInkValue, encodeCall, encodeContractCall, encodeContractCallWithLimits, estimateTransactionCost, formatGasInfo, gasExceedsLimit, getEventSignature, isCallType, isContractError, isErrorType, isEventType, isLangError, unwrapInkResult };
|
|
2131
|
+
export { AbortedError, type BalancesTransferChainEvent, type BlockHeader, type CallFilterOptions, type ChainEventRecord, ContractCallParser, type ContractCallParserOf, type ContractCallResult, type ContractCalledChainEvent, type ContractCalledValue, type ContractEmittedChainEvent, type ContractEmittedValue, ContractError, type ContractErrorType, ContractEventParser, type ContractEventParserOf, ContractExecutionError, type ContractMessageBuilder, type ContractStorage, type ContractsCallValue, type CreateContractOptions, type CreateD9InkSdkOptions, type D9InkContract, type D9InkContractFromDescriptor, type D9InkContractOf, type D9InkSdk, type D9InkSdkOptions, type D9InkSdkWithRpc, DecodeError, type DecodedContractEvent, EncodeError, EstimatedCost, type EventFilterOptions, type EventSubscriptionOptions, type ExtractEnumDef, type ExtractEventLabels, type ExtractMessageLabels, type ExtrinsicCall, ExtrinsicParser, type ExtrinsicParserOptions, FormattedGasInfo, type GasInfo, GasWeight, type InferEvents, type InferMessages, InkCodecs, LangError, type MessageAttributes, type MessageInfo, MetadataError, NetworkError, type PSP22SelectorKey, PSP22_SELECTORS, type ParsedContractExtrinsic, type QueryOptions, type QueryResult, type QuerySuccessValue, type RawContractCall, type RawContractEvent, type RawExtrinsic, type ResponseDecoder, type RuntimeVersion, type SelectorInfo, type SendOptions, type SendableTransaction, SignerError, type StorageChangeSet, type StorageDepositInfo, type SubstrateRpcMethods, type SystemHealth, TimeoutError, TransactionError, type TxResult, type TypedCallFilterOptions, type TypedContractCall, type TypedContractEvent, type TypedEventFilterOptions, type TypedMessage, type TypedRpc, applyGasMargin, buildAllEventDecoders, buildAllMessageDecoders, buildEventDecoder, buildLabelMap, buildMessageDecoder, buildSelectorMap, compareGasWeight, createAsciiEventTopic, createCodecRegistry, createContractEventStream, createD9InkContract, createD9InkSdk, createExtrinsicParser, createMessageBuilder, createNativeTransferStream, createPSP22TransferStream, createTypedRpc, decodeContractCallResult, decodeInkValue, encodeCall, encodeContractCall, encodeContractCallWithLimits, estimateTransactionCost, extractContractEmittedData, formatGasInfo, gasExceedsLimit, getAllSelectors, getEventSignature, getLabelForSelector, getSelectorForLabel, isApplyExtrinsicPhase, isBalancesTransferEvent, isCallType, isContractCalledEvent, isContractEmittedEvent, isContractError, isErrorType, isEventType, isLangError, isMessageCall, isPSP22Call, unwrapInkResult };
|
|
1428
2132
|
//# sourceMappingURL=index.d.cts.map
|