@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/README.md
CHANGED
|
@@ -44,6 +44,9 @@ if (result.success) {
|
|
|
44
44
|
- **AccountId with D9 prefix**: All addresses automatically use D9 SS58 prefix (9)
|
|
45
45
|
- **Event parsing**: Type-safe decoding of contract events
|
|
46
46
|
- **Comprehensive error handling**: Detailed error types for debugging
|
|
47
|
+
- **PSP22 selector constants**: Pre-defined selectors for PSP22 token standard
|
|
48
|
+
- **Type inference utilities**: Declare typed properties without instantiation
|
|
49
|
+
- **Extrinsic parser**: High-level API for indexers to parse contract calls
|
|
47
50
|
|
|
48
51
|
## API Overview
|
|
49
52
|
|
|
@@ -71,6 +74,99 @@ if (result.success) {
|
|
|
71
74
|
| `LangError` | ink! language error |
|
|
72
75
|
| `NetworkError` | RPC/WebSocket error |
|
|
73
76
|
|
|
77
|
+
### Type Inference Utilities
|
|
78
|
+
|
|
79
|
+
Use these to declare typed properties without manual generic parameters:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { contracts } from "@d9-network/spec";
|
|
83
|
+
import type { ContractCallParserOf, ContractEventParserOf } from "@d9-network/ink";
|
|
84
|
+
|
|
85
|
+
class MyIndexer {
|
|
86
|
+
// Fully typed without specifying generics!
|
|
87
|
+
private callParser: ContractCallParserOf<typeof contracts.usdt>;
|
|
88
|
+
private eventParser: ContractEventParserOf<typeof contracts.usdt>;
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
| Export | Description |
|
|
93
|
+
| ---------------------- | ----------------------------------------- |
|
|
94
|
+
| `ContractCallParserOf` | Infer ContractCallParser type from descriptor |
|
|
95
|
+
| `ContractEventParserOf`| Infer ContractEventParser type from descriptor |
|
|
96
|
+
| `D9InkContractOf` | Infer D9InkContract type from descriptor |
|
|
97
|
+
|
|
98
|
+
### Selector Utilities
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { PSP22_SELECTORS, isPSP22Call, extractSelectorHex } from "@d9-network/ink";
|
|
102
|
+
|
|
103
|
+
// Check if call data is a PSP22 transfer
|
|
104
|
+
if (isPSP22Call(callData, "transfer")) {
|
|
105
|
+
console.log("This is a transfer!");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Or compare selectors directly
|
|
109
|
+
const selector = extractSelectorHex(callData);
|
|
110
|
+
if (selector === PSP22_SELECTORS.transfer) {
|
|
111
|
+
console.log("Transfer detected");
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
| Export | Description |
|
|
116
|
+
| -------------------- | ---------------------------------------- |
|
|
117
|
+
| `PSP22_SELECTORS` | Pre-defined PSP22 selectors |
|
|
118
|
+
| `isPSP22Call` | Check if call matches PSP22 method |
|
|
119
|
+
| `getSelectorForLabel`| Get selector for a message label |
|
|
120
|
+
| `getLabelForSelector`| Get message label for a selector |
|
|
121
|
+
| `buildSelectorMap` | Build selector lookup map from metadata |
|
|
122
|
+
|
|
123
|
+
### Extrinsic Parser (for Indexers)
|
|
124
|
+
|
|
125
|
+
High-level API for parsing contract calls from blockchain extrinsics:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { ExtrinsicParser } from "@d9-network/ink";
|
|
129
|
+
import { contracts } from "@d9-network/spec";
|
|
130
|
+
|
|
131
|
+
const parser = new ExtrinsicParser(contracts.usdt, {
|
|
132
|
+
contractAddresses: [USDT_ADDRESS],
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Parse extrinsics from a block
|
|
136
|
+
for (const extrinsic of block.extrinsics) {
|
|
137
|
+
const parsed = parser.parseExtrinsic(extrinsic);
|
|
138
|
+
if (parsed?.call.type === "PSP22::transfer") {
|
|
139
|
+
// Fully typed args!
|
|
140
|
+
console.log(`Transfer: ${parsed.call.args.value} to ${parsed.call.args.to}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Chain Event Type Guards
|
|
146
|
+
|
|
147
|
+
Eliminate `as any` casts when handling chain events:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { isContractCalledEvent, isContractEmittedEvent, type ChainEventRecord } from "@d9-network/ink";
|
|
151
|
+
|
|
152
|
+
const events: ChainEventRecord[] = await api.query.System.Events.getValue();
|
|
153
|
+
|
|
154
|
+
for (const event of events) {
|
|
155
|
+
if (isContractCalledEvent(event)) {
|
|
156
|
+
// event.event.value.value is properly typed!
|
|
157
|
+
const { caller, contract } = event.event.value.value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
| Export | Description |
|
|
163
|
+
| ------------------------- | ---------------------------------- |
|
|
164
|
+
| `ChainEventRecord` | Typed chain event structure |
|
|
165
|
+
| `isContractEmittedEvent` | Type guard for ContractEmitted |
|
|
166
|
+
| `isContractCalledEvent` | Type guard for Contracts.Called |
|
|
167
|
+
| `isBalancesTransferEvent` | Type guard for Balances.Transfer |
|
|
168
|
+
| `isApplyExtrinsicPhase` | Check if event is in ApplyExtrinsic phase |
|
|
169
|
+
|
|
74
170
|
## Documentation
|
|
75
171
|
|
|
76
172
|
For complete documentation, examples, and API reference, visit:
|