@ic-reactor/candid 3.0.0-beta.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 +358 -0
- package/dist/adapter.d.ts +197 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +384 -0
- package/dist/adapter.js.map +1 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +11 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/reactor.d.ts +103 -0
- package/dist/reactor.d.ts.map +1 -0
- package/dist/reactor.js +173 -0
- package/dist/reactor.js.map +1 -0
- package/dist/types.d.ts +77 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +27 -0
- package/dist/utils.js.map +1 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# @ic-reactor/candid
|
|
2
|
+
|
|
3
|
+
Lightweight adapter for fetching and parsing Candid definitions from Internet Computer canisters.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Fetch Candid Definitions**: Retrieve Candid interface definitions from any canister
|
|
8
|
+
- **Multiple Retrieval Methods**: Supports both canister metadata and the temporary hack method
|
|
9
|
+
- **Local Parsing**: Use the optional WASM-based parser for fast, offline Candid compilation
|
|
10
|
+
- **Remote Fallback**: Falls back to the didjs canister for Candid-to-JavaScript compilation
|
|
11
|
+
- **Dynamic Reactor**: Includes `CandidReactor` for dynamic IDL fetching and interaction
|
|
12
|
+
- **Lightweight**: Uses raw `agent.query` calls - no Actor overhead
|
|
13
|
+
- **ClientManager Compatible**: Seamlessly integrates with `@ic-reactor/core`
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @ic-reactor/candid @icp-sdk/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Optional: Local Parser
|
|
22
|
+
|
|
23
|
+
For faster Candid parsing without network requests:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @ic-reactor/parser
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### With ClientManager (Recommended)
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { CandidAdapter } from "@ic-reactor/candid"
|
|
35
|
+
import { ClientManager } from "@ic-reactor/core"
|
|
36
|
+
import { QueryClient } from "@tanstack/query-core"
|
|
37
|
+
|
|
38
|
+
// Create and initialize ClientManager
|
|
39
|
+
const queryClient = new QueryClient()
|
|
40
|
+
const clientManager = new ClientManager({ queryClient })
|
|
41
|
+
await clientManager.initialize()
|
|
42
|
+
|
|
43
|
+
// Create the adapter
|
|
44
|
+
const adapter = new CandidAdapter({ clientManager })
|
|
45
|
+
|
|
46
|
+
// Get the Candid definition for a canister
|
|
47
|
+
const { idlFactory } = await adapter.getCandidDefinition(
|
|
48
|
+
"ryjl3-tyaaa-aaaaa-aaaba-cai"
|
|
49
|
+
)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### With Local Parser (Fastest)
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { CandidAdapter } from "@ic-reactor/candid"
|
|
56
|
+
import { ClientManager } from "@ic-reactor/core"
|
|
57
|
+
import { QueryClient } from "@tanstack/query-core"
|
|
58
|
+
|
|
59
|
+
const queryClient = new QueryClient()
|
|
60
|
+
const clientManager = new ClientManager({ queryClient })
|
|
61
|
+
await clientManager.initialize()
|
|
62
|
+
|
|
63
|
+
const adapter = new CandidAdapter({ clientManager })
|
|
64
|
+
|
|
65
|
+
// Load the local parser for faster processing
|
|
66
|
+
await adapter.loadParser()
|
|
67
|
+
|
|
68
|
+
// Now parsing happens locally - no network requests
|
|
69
|
+
const { idlFactory } = await adapter.getCandidDefinition(
|
|
70
|
+
"ryjl3-tyaaa-aaaaa-aaaba-cai"
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### CandidReactor (Dynamic Interaction)
|
|
75
|
+
|
|
76
|
+
`CandidReactor` extends the core `Reactor` class, allowing you to work with canisters without compile-time IDL. After initialization, **all standard Reactor methods work automatically**.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { CandidReactor } from "@ic-reactor/candid"
|
|
80
|
+
import { ClientManager } from "@ic-reactor/core"
|
|
81
|
+
|
|
82
|
+
const clientManager = new ClientManager()
|
|
83
|
+
await clientManager.initialize()
|
|
84
|
+
|
|
85
|
+
// Option 1: Fetch Candid from network
|
|
86
|
+
const reactor = new CandidReactor({
|
|
87
|
+
canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
88
|
+
clientManager,
|
|
89
|
+
})
|
|
90
|
+
await reactor.initialize() // Fetches IDL from network
|
|
91
|
+
|
|
92
|
+
// Option 2: Provide Candid string directly (no network fetch)
|
|
93
|
+
const reactor = new CandidReactor({
|
|
94
|
+
canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
95
|
+
clientManager,
|
|
96
|
+
candid: `service : {
|
|
97
|
+
icrc1_name : () -> (text) query;
|
|
98
|
+
icrc1_balance_of : (record { owner : principal }) -> (nat) query;
|
|
99
|
+
}`,
|
|
100
|
+
})
|
|
101
|
+
await reactor.initialize() // Parses provided Candid string
|
|
102
|
+
|
|
103
|
+
// Now use standard Reactor methods!
|
|
104
|
+
const name = await reactor.callMethod({ functionName: "icrc1_name" })
|
|
105
|
+
const balance = await reactor.fetchQuery({
|
|
106
|
+
functionName: "icrc1_balance_of",
|
|
107
|
+
args: [{ owner }],
|
|
108
|
+
})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### Registering Methods Dynamically
|
|
112
|
+
|
|
113
|
+
You can also register individual methods on-the-fly:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
// Start with just a canister ID
|
|
117
|
+
const reactor = new CandidReactor({
|
|
118
|
+
canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
119
|
+
clientManager,
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
// Register a method by its Candid signature
|
|
123
|
+
await reactor.registerMethod({
|
|
124
|
+
functionName: "icrc1_balance_of",
|
|
125
|
+
candid: "(record { owner : principal }) -> (nat) query",
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// Now all standard Reactor methods work with this method!
|
|
129
|
+
const balance = await reactor.callMethod({
|
|
130
|
+
functionName: "icrc1_balance_of",
|
|
131
|
+
args: [{ owner }],
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
// With TanStack Query caching
|
|
135
|
+
const cachedBalance = await reactor.fetchQuery({
|
|
136
|
+
functionName: "icrc1_balance_of",
|
|
137
|
+
args: [{ owner }],
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
// Check registered methods
|
|
141
|
+
console.log(reactor.getMethodNames())
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### One-Shot Dynamic Calls
|
|
145
|
+
|
|
146
|
+
For quick one-off calls, use convenience methods that register and call in one step:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
// queryDynamic - register + call in one step
|
|
150
|
+
const symbol = await reactor.queryDynamic({
|
|
151
|
+
functionName: "icrc1_symbol",
|
|
152
|
+
candid: "() -> (text) query",
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// callDynamic - for update calls
|
|
156
|
+
const result = await reactor.callDynamic({
|
|
157
|
+
functionName: "transfer",
|
|
158
|
+
candid:
|
|
159
|
+
"(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })",
|
|
160
|
+
args: [{ to: recipient, amount: 100n }],
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
// fetchQueryDynamic - with TanStack Query caching
|
|
164
|
+
const cachedBalance = await reactor.fetchQueryDynamic({
|
|
165
|
+
functionName: "icrc1_balance_of",
|
|
166
|
+
candid: "(record { owner : principal }) -> (nat) query",
|
|
167
|
+
args: [{ owner }],
|
|
168
|
+
})
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Fetch Raw Candid Source
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const candidSource = await adapter.fetchCandidSource(
|
|
175
|
+
"ryjl3-tyaaa-aaaaa-aaaba-cai"
|
|
176
|
+
)
|
|
177
|
+
console.log(candidSource)
|
|
178
|
+
// Output: service { greet: (text) -> (text) query; }
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Validate Candid Source
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
await adapter.loadParser()
|
|
185
|
+
|
|
186
|
+
const isValid = adapter.validateCandid(`
|
|
187
|
+
service {
|
|
188
|
+
greet: (text) -> (text) query;
|
|
189
|
+
}
|
|
190
|
+
`)
|
|
191
|
+
console.log(isValid) // true
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Compile Candid to JavaScript
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
// Local compilation (requires parser)
|
|
198
|
+
await adapter.loadParser()
|
|
199
|
+
const jsCode = adapter.compileLocal("service { greet: (text) -> (text) query }")
|
|
200
|
+
|
|
201
|
+
// Remote compilation (uses didjs canister)
|
|
202
|
+
const jsCode = await adapter.compileRemote(
|
|
203
|
+
"service { greet: (text) -> (text) query }"
|
|
204
|
+
)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## API Reference
|
|
208
|
+
|
|
209
|
+
### `CandidAdapter`
|
|
210
|
+
|
|
211
|
+
#### Constructor
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
new CandidAdapter(params: CandidAdapterParameters)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
| Parameter | Type | Required | Description |
|
|
218
|
+
| ----------------- | --------------------- | -------- | -------------------------------------------------- |
|
|
219
|
+
| `clientManager` | `CandidClientManager` | Yes | Client manager providing agent and identity access |
|
|
220
|
+
| `didjsCanisterId` | `string` | No | Custom didjs canister ID |
|
|
221
|
+
|
|
222
|
+
#### Properties
|
|
223
|
+
|
|
224
|
+
| Property | Type | Description |
|
|
225
|
+
| ----------------- | --------------------- | -------------------------------------------- |
|
|
226
|
+
| `clientManager` | `CandidClientManager` | The client manager instance |
|
|
227
|
+
| `agent` | `HttpAgent` | The HTTP agent from the client manager |
|
|
228
|
+
| `didjsCanisterId` | `string` | The didjs canister ID for remote compilation |
|
|
229
|
+
| `hasParser` | `boolean` | Whether the local parser is loaded |
|
|
230
|
+
|
|
231
|
+
#### Methods
|
|
232
|
+
|
|
233
|
+
##### Main API
|
|
234
|
+
|
|
235
|
+
| Method | Description |
|
|
236
|
+
| --------------------------------- | ------------------------------------------------ |
|
|
237
|
+
| `getCandidDefinition(canisterId)` | Get parsed Candid definition (idlFactory + init) |
|
|
238
|
+
| `fetchCandidSource(canisterId)` | Get raw Candid source string |
|
|
239
|
+
| `parseCandidSource(candidSource)` | Parse Candid source to definition |
|
|
240
|
+
|
|
241
|
+
##### Parser Methods
|
|
242
|
+
|
|
243
|
+
| Method | Description |
|
|
244
|
+
| ------------------------------ | ---------------------------------------- |
|
|
245
|
+
| `loadParser(module?)` | Load the local WASM parser |
|
|
246
|
+
| `compileLocal(candidSource)` | Compile Candid locally (requires parser) |
|
|
247
|
+
| `validateCandid(candidSource)` | Validate Candid source (requires parser) |
|
|
248
|
+
|
|
249
|
+
##### Fetch Methods
|
|
250
|
+
|
|
251
|
+
| Method | Description |
|
|
252
|
+
| ----------------------------------------------- | --------------------------------- |
|
|
253
|
+
| `fetchFromMetadata(canisterId)` | Get Candid from canister metadata |
|
|
254
|
+
| `fetchFromTmpHack(canisterId)` | Get Candid via tmp hack method |
|
|
255
|
+
| `compileRemote(candidSource, didjsCanisterId?)` | Compile Candid via didjs canister |
|
|
256
|
+
|
|
257
|
+
##### Cleanup
|
|
258
|
+
|
|
259
|
+
| Method | Description |
|
|
260
|
+
| --------------- | ------------------------------------ |
|
|
261
|
+
| `unsubscribe()` | Cleanup identity change subscription |
|
|
262
|
+
|
|
263
|
+
### `CandidReactor`
|
|
264
|
+
|
|
265
|
+
Extends `Reactor` from `@ic-reactor/core`.
|
|
266
|
+
|
|
267
|
+
#### Constructor
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
new CandidReactor(config: CandidReactorParameters)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
| Parameter | Type | Required | Description |
|
|
274
|
+
| --------------- | ------------------ | -------- | ------------------------------------------------ |
|
|
275
|
+
| `canisterId` | `CanisterId` | Yes | The canister ID to interact with |
|
|
276
|
+
| `clientManager` | `ClientManager` | Yes | Client manager from `@ic-reactor/core` |
|
|
277
|
+
| `candid` | `string` | No | Candid service definition (avoids network fetch) |
|
|
278
|
+
| `idlFactory` | `InterfaceFactory` | No | IDL factory (if already available) |
|
|
279
|
+
| `actor` | `A` | No | Existing actor instance |
|
|
280
|
+
|
|
281
|
+
#### Methods
|
|
282
|
+
|
|
283
|
+
| Method | Description |
|
|
284
|
+
| ---------------------------- | ----------------------------------------------------------- |
|
|
285
|
+
| `initialize()` | Parse provided Candid or fetch from network, update service |
|
|
286
|
+
| `registerMethod(options)` | Register a method by its Candid signature |
|
|
287
|
+
| `registerMethods(methods)` | Register multiple methods at once |
|
|
288
|
+
| `hasMethod(functionName)` | Check if a method is registered |
|
|
289
|
+
| `getMethodNames()` | Get all registered method names |
|
|
290
|
+
| `callDynamic(options)` | One-shot: register + update call |
|
|
291
|
+
| `queryDynamic(options)` | One-shot: register + query call |
|
|
292
|
+
| `fetchQueryDynamic(options)` | One-shot: register + cached query |
|
|
293
|
+
|
|
294
|
+
After initialization or registration, all standard `Reactor` methods work:
|
|
295
|
+
|
|
296
|
+
- `callMethod()` - Execute a method call
|
|
297
|
+
- `fetchQuery()` - Fetch with TanStack Query caching
|
|
298
|
+
- `getQueryOptions()` - Get query options for React hooks
|
|
299
|
+
- `invalidateQueries()` - Invalidate cached queries
|
|
300
|
+
- etc.
|
|
301
|
+
|
|
302
|
+
### Types
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
interface CandidDefinition {
|
|
306
|
+
idlFactory: IDL.InterfaceFactory
|
|
307
|
+
init?: (args: { IDL: typeof IDL }) => IDL.Type<unknown>[]
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
interface CandidAdapterParameters {
|
|
311
|
+
clientManager: CandidClientManager
|
|
312
|
+
didjsCanisterId?: string
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
interface CandidClientManager {
|
|
316
|
+
agent: HttpAgent
|
|
317
|
+
isLocal: boolean
|
|
318
|
+
subscribe(callback: (identity: Identity) => void): () => void
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
type CanisterId = string | Principal
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## How It Works
|
|
325
|
+
|
|
326
|
+
1. **Fetching Candid**: The adapter first tries to get the Candid definition from the canister's metadata. If that fails, it falls back to calling the `__get_candid_interface_tmp_hack` query method.
|
|
327
|
+
|
|
328
|
+
2. **Parsing Candid**: Once the raw Candid source is retrieved, it needs to be compiled to JavaScript:
|
|
329
|
+
- First tries the local WASM parser (if loaded) - instant, no network
|
|
330
|
+
- Falls back to the remote didjs canister - requires network request
|
|
331
|
+
|
|
332
|
+
3. **Evaluation**: The compiled JavaScript is dynamically imported to extract the `idlFactory` and optional `init` function.
|
|
333
|
+
|
|
334
|
+
4. **Dynamic Execution**: For `call` and `query` methods, the adapter wraps the provided Candid signature in a temporary service definition, compiles it to an `idlFactory`, and then uses an `Actor` to encode arguments and execute the call reliably.
|
|
335
|
+
|
|
336
|
+
5. **Identity Changes**: The adapter subscribes to identity changes from the ClientManager. When the identity changes, it re-evaluates the default didjs canister ID (unless a custom one was provided).
|
|
337
|
+
|
|
338
|
+
## Standalone Usage
|
|
339
|
+
|
|
340
|
+
The `CandidClientManager` interface is simple enough that you can implement it yourself without `@ic-reactor/core`:
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
import { HttpAgent } from "@icp-sdk/core/agent"
|
|
344
|
+
import { CandidAdapter } from "@ic-reactor/candid"
|
|
345
|
+
|
|
346
|
+
// Create a minimal client manager implementation
|
|
347
|
+
const clientManager = {
|
|
348
|
+
agent: await HttpAgent.create({ host: "https://ic0.app" }),
|
|
349
|
+
isLocal: false,
|
|
350
|
+
subscribe: () => () => {}, // No-op subscription
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const adapter = new CandidAdapter({ clientManager })
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## License
|
|
357
|
+
|
|
358
|
+
MIT
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { HttpAgent } from "@icp-sdk/core/agent";
|
|
2
|
+
import type { CandidAdapterParameters, CandidDefinition, CandidClientManager, ReactorParser } from "./types";
|
|
3
|
+
import { CanisterId } from "@ic-reactor/core";
|
|
4
|
+
/**
|
|
5
|
+
* CandidAdapter provides functionality to fetch and parse Candid definitions
|
|
6
|
+
* from Internet Computer canisters.
|
|
7
|
+
*
|
|
8
|
+
* It supports multiple methods for retrieving Candid definitions:
|
|
9
|
+
* 1. From canister metadata (preferred)
|
|
10
|
+
* 2. From the `__get_candid_interface_tmp_hack` method (fallback)
|
|
11
|
+
*
|
|
12
|
+
* It also supports parsing Candid to JavaScript using:
|
|
13
|
+
* 1. Local WASM parser (@ic-reactor/parser) - faster, no network request
|
|
14
|
+
* 2. Remote didjs canister - always available fallback
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { CandidAdapter } from "@ic-reactor/candid"
|
|
19
|
+
* import { ClientManager } from "@ic-reactor/core"
|
|
20
|
+
* import { QueryClient } from "@tanstack/query-core"
|
|
21
|
+
*
|
|
22
|
+
* const queryClient = new QueryClient()
|
|
23
|
+
* const clientManager = new ClientManager({ queryClient })
|
|
24
|
+
* await clientManager.initialize()
|
|
25
|
+
*
|
|
26
|
+
* const adapter = new CandidAdapter({ clientManager })
|
|
27
|
+
*
|
|
28
|
+
* // Optionally load the local parser for faster processing
|
|
29
|
+
* await adapter.loadParser()
|
|
30
|
+
*
|
|
31
|
+
* // Get the Candid definition for a canister
|
|
32
|
+
* const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class CandidAdapter {
|
|
36
|
+
/** The client manager providing agent and identity access. */
|
|
37
|
+
clientManager: CandidClientManager;
|
|
38
|
+
/** The canister ID of the didjs canister for remote Candid compilation. */
|
|
39
|
+
didjsCanisterId: CanisterId;
|
|
40
|
+
/** The optional local parser module. */
|
|
41
|
+
private parserModule?;
|
|
42
|
+
/** Whether parser auto-loading has been attempted. */
|
|
43
|
+
private parserLoadAttempted;
|
|
44
|
+
/** Function to unsubscribe from identity updates. */
|
|
45
|
+
unsubscribe: () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new CandidAdapter instance.
|
|
48
|
+
*
|
|
49
|
+
* @param params - The adapter parameters.
|
|
50
|
+
*/
|
|
51
|
+
constructor({ clientManager, didjsCanisterId }: CandidAdapterParameters);
|
|
52
|
+
/**
|
|
53
|
+
* The HTTP agent from the client manager.
|
|
54
|
+
*/
|
|
55
|
+
get agent(): HttpAgent;
|
|
56
|
+
/**
|
|
57
|
+
* Whether the local parser is available.
|
|
58
|
+
*/
|
|
59
|
+
get hasParser(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Loads the local parser module for converting Candid to JavaScript.
|
|
62
|
+
* If no module is provided, attempts to dynamically load @ic-reactor/parser.
|
|
63
|
+
*
|
|
64
|
+
* @param module - Optional parser module to use.
|
|
65
|
+
* @throws Error if the parser loading fails.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* // Load the default parser
|
|
70
|
+
* await adapter.loadParser()
|
|
71
|
+
*
|
|
72
|
+
* // Or provide a custom parser
|
|
73
|
+
* import * as parser from "@ic-reactor/parser"
|
|
74
|
+
* await adapter.loadParser(parser)
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
loadParser(module?: ReactorParser): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Attempts to load the parser silently (no error if not available).
|
|
80
|
+
* Useful for optional parser initialization.
|
|
81
|
+
*/
|
|
82
|
+
private tryLoadParser;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the default didjs canister ID based on whether the agent is local or not.
|
|
85
|
+
*/
|
|
86
|
+
private getDefaultDidJsId;
|
|
87
|
+
/**
|
|
88
|
+
* Gets the parsed Candid definition for a canister, ready for use with Actor.createActor.
|
|
89
|
+
* This is the main entry point for fetching a canister's interface.
|
|
90
|
+
*
|
|
91
|
+
* @param canisterId - The canister ID to get the Candid definition for.
|
|
92
|
+
* @returns The parsed Candid definition with idlFactory and optional init.
|
|
93
|
+
* @throws Error if fetching or parsing fails.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
getCandidDefinition(canisterId: CanisterId): Promise<CandidDefinition>;
|
|
101
|
+
/**
|
|
102
|
+
* Fetches the raw Candid source string for a canister.
|
|
103
|
+
* First attempts to get it from metadata, then falls back to the tmp hack method.
|
|
104
|
+
*
|
|
105
|
+
* @param canisterId - The canister ID to fetch the Candid source for.
|
|
106
|
+
* @returns The raw Candid source string (.did file contents).
|
|
107
|
+
* @throws Error if both methods fail.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const candidSource = await adapter.fetchCandidSource("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
112
|
+
* console.log(candidSource) // service { greet: (text) -> (text) query; }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
fetchCandidSource(canisterId: CanisterId): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* Parses Candid source string and returns the definition with idlFactory.
|
|
118
|
+
* First attempts to use the local parser, then falls back to the remote didjs canister.
|
|
119
|
+
*
|
|
120
|
+
* @param candidSource - The raw Candid source string.
|
|
121
|
+
* @returns The parsed Candid definition.
|
|
122
|
+
* @throws Error if parsing fails.
|
|
123
|
+
*/
|
|
124
|
+
parseCandidSource(candidSource: string): Promise<CandidDefinition>;
|
|
125
|
+
/**
|
|
126
|
+
* Fetches Candid source from the canister's metadata.
|
|
127
|
+
*
|
|
128
|
+
* @param canisterId - The canister ID to query.
|
|
129
|
+
* @returns The Candid source string, or undefined if not available.
|
|
130
|
+
*/
|
|
131
|
+
fetchFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
|
|
132
|
+
/**
|
|
133
|
+
* Fetches Candid source using the temporary hack method.
|
|
134
|
+
* This calls the `__get_candid_interface_tmp_hack` query method on the canister.
|
|
135
|
+
*
|
|
136
|
+
* @param canisterId - The canister ID to query.
|
|
137
|
+
* @returns The Candid source string.
|
|
138
|
+
*/
|
|
139
|
+
fetchFromTmpHack(canisterId: CanisterId): Promise<string>;
|
|
140
|
+
/**
|
|
141
|
+
* Compiles Candid source to JavaScript using the local WASM parser.
|
|
142
|
+
*
|
|
143
|
+
* @param candidSource - The Candid source to compile.
|
|
144
|
+
* @returns The compiled JavaScript code.
|
|
145
|
+
* @throws Error if the parser is not loaded.
|
|
146
|
+
*/
|
|
147
|
+
compileLocal(candidSource: string): string;
|
|
148
|
+
/**
|
|
149
|
+
* Compiles Candid source to JavaScript using the remote didjs canister.
|
|
150
|
+
*
|
|
151
|
+
* @param candidSource - The Candid source to compile.
|
|
152
|
+
* @param didjsCanisterId - Optional custom didjs canister ID.
|
|
153
|
+
* @returns The compiled JavaScript code, or undefined if compilation fails.
|
|
154
|
+
*/
|
|
155
|
+
compileRemote(candidSource: string, didjsCanisterId?: string): Promise<string | undefined>;
|
|
156
|
+
/**
|
|
157
|
+
* Validates Candid source using the local parser.
|
|
158
|
+
*
|
|
159
|
+
* @param candidSource - The Candid source to validate.
|
|
160
|
+
* @returns True if the source is valid, false otherwise.
|
|
161
|
+
* @throws Error if the parser is not loaded.
|
|
162
|
+
*/
|
|
163
|
+
validateCandid(candidSource: string): boolean;
|
|
164
|
+
/**
|
|
165
|
+
* @deprecated Use `loadParser()` instead.
|
|
166
|
+
*/
|
|
167
|
+
initializeParser(module?: ReactorParser): Promise<void>;
|
|
168
|
+
/**
|
|
169
|
+
* @deprecated Use `fetchCandidSource()` instead.
|
|
170
|
+
*/
|
|
171
|
+
fetchCandidDefinition(canisterId: CanisterId): Promise<string>;
|
|
172
|
+
/**
|
|
173
|
+
* @deprecated Use `fetchFromMetadata()` instead.
|
|
174
|
+
*/
|
|
175
|
+
getFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
|
|
176
|
+
/**
|
|
177
|
+
* @deprecated Use `fetchFromTmpHack()` instead.
|
|
178
|
+
*/
|
|
179
|
+
getFromTmpHack(canisterId: CanisterId): Promise<string>;
|
|
180
|
+
/**
|
|
181
|
+
* @deprecated Use `parseCandidSource()` instead.
|
|
182
|
+
*/
|
|
183
|
+
evaluateCandidDefinition(data: string): Promise<CandidDefinition>;
|
|
184
|
+
/**
|
|
185
|
+
* @deprecated Use `compileRemote()` instead.
|
|
186
|
+
*/
|
|
187
|
+
fetchDidTojs(candidSource: string, didjsCanisterId?: string): Promise<string | undefined>;
|
|
188
|
+
/**
|
|
189
|
+
* @deprecated Use `compileLocal()` instead.
|
|
190
|
+
*/
|
|
191
|
+
parseDidToJs(candidSource: string): string;
|
|
192
|
+
/**
|
|
193
|
+
* @deprecated Use `validateCandid()` instead.
|
|
194
|
+
*/
|
|
195
|
+
validateIDL(candidSource: string): boolean;
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