@chainlink/cre-sdk 1.0.0-beta.0 → 1.0.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 +138 -33
- package/package.json +2 -2
- package/scripts/src/compile-to-js.ts +1 -1
package/README.md
CHANGED
|
@@ -18,12 +18,16 @@ The Chainlink Runtime Environment (CRE) SDK for TypeScript enables developers to
|
|
|
18
18
|
- [Configuration & Type Safety](#configuration--type-safety)
|
|
19
19
|
- [Consensus & Aggregation](#consensus--aggregation)
|
|
20
20
|
- [Utility Functions](#utility-functions)
|
|
21
|
+
- [HTTP Response Helpers](#http-response-helpers)
|
|
22
|
+
- [Blockchain Helpers](#blockchain-helpers)
|
|
21
23
|
- [Hex Utilities](#hex-utilities)
|
|
22
24
|
- [Chain Selectors](#chain-selectors)
|
|
23
25
|
- [Example Workflows](#example-workflows)
|
|
24
|
-
- [1. Simple
|
|
26
|
+
- [1. Simple Cron-scheduled task](#1-simple-cron-scheduled-task)
|
|
25
27
|
- [2. API Data Aggregation](#2-api-data-aggregation)
|
|
26
28
|
- [3. On-Chain Data Integration](#3-on-chain-data-integration)
|
|
29
|
+
- [4. Proof of Reserve](#4-proof-of-reserve)
|
|
30
|
+
- [5. Star Wars API](#5-star-wars-api)
|
|
27
31
|
- [API Reference](#api-reference)
|
|
28
32
|
- [Core Functions](#core-functions)
|
|
29
33
|
- [Capabilities](#capabilities)
|
|
@@ -124,13 +128,22 @@ import {
|
|
|
124
128
|
consensusMedianAggregation,
|
|
125
129
|
type HTTPSendRequester,
|
|
126
130
|
type Runtime,
|
|
131
|
+
ok,
|
|
132
|
+
text,
|
|
127
133
|
} from "@chainlink/cre-sdk";
|
|
128
134
|
|
|
129
135
|
type Config = { apiUrl: string };
|
|
130
136
|
|
|
131
137
|
const fetchData = (sendRequester: HTTPSendRequester, config: Config) => {
|
|
132
|
-
const response = sendRequester
|
|
133
|
-
|
|
138
|
+
const response = sendRequester
|
|
139
|
+
.sendRequest({ url: config.apiUrl, method: "GET" })
|
|
140
|
+
.result();
|
|
141
|
+
|
|
142
|
+
if (!ok(response)) {
|
|
143
|
+
throw new Error(`HTTP request failed with status: ${response.statusCode}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return Number.parseFloat(text(response));
|
|
134
147
|
};
|
|
135
148
|
|
|
136
149
|
const onCronTrigger = (runtime: Runtime<Config>) => {
|
|
@@ -153,15 +166,21 @@ Read from and write to EVM-compatible blockchains:
|
|
|
153
166
|
import {
|
|
154
167
|
bytesToHex,
|
|
155
168
|
cre,
|
|
169
|
+
encodeCallMsg,
|
|
156
170
|
getNetwork,
|
|
157
|
-
|
|
171
|
+
LAST_FINALIZED_BLOCK_NUMBER,
|
|
158
172
|
type Runtime,
|
|
159
173
|
} from "@chainlink/cre-sdk";
|
|
160
|
-
import {
|
|
174
|
+
import {
|
|
175
|
+
type Address,
|
|
176
|
+
decodeFunctionResult,
|
|
177
|
+
encodeFunctionData,
|
|
178
|
+
zeroAddress,
|
|
179
|
+
} from "viem";
|
|
161
180
|
|
|
162
181
|
type Config = { evm: { chainSelectorName: string; contractAddress: string } };
|
|
163
182
|
|
|
164
|
-
const onCronTrigger =
|
|
183
|
+
const onCronTrigger = (runtime: Runtime<Config>) => {
|
|
165
184
|
const { chainSelectorName, contractAddress } = runtime.config.evm;
|
|
166
185
|
const network = getNetwork({
|
|
167
186
|
chainFamily: "evm",
|
|
@@ -174,6 +193,7 @@ const onCronTrigger = async (runtime: Runtime<Config>) => {
|
|
|
174
193
|
network.chainSelector.selector
|
|
175
194
|
);
|
|
176
195
|
|
|
196
|
+
// Read from blockchain
|
|
177
197
|
const callData = encodeFunctionData({
|
|
178
198
|
abi: CONTRACT_ABI,
|
|
179
199
|
functionName: "getValue",
|
|
@@ -181,12 +201,12 @@ const onCronTrigger = async (runtime: Runtime<Config>) => {
|
|
|
181
201
|
|
|
182
202
|
const contractCall = evmClient
|
|
183
203
|
.callContract(runtime, {
|
|
184
|
-
call: {
|
|
185
|
-
from:
|
|
186
|
-
to:
|
|
187
|
-
data:
|
|
188
|
-
},
|
|
189
|
-
blockNumber:
|
|
204
|
+
call: encodeCallMsg({
|
|
205
|
+
from: zeroAddress,
|
|
206
|
+
to: contractAddress as Address,
|
|
207
|
+
data: callData,
|
|
208
|
+
}),
|
|
209
|
+
blockNumber: LAST_FINALIZED_BLOCK_NUMBER,
|
|
190
210
|
})
|
|
191
211
|
.result();
|
|
192
212
|
|
|
@@ -196,19 +216,8 @@ const onCronTrigger = async (runtime: Runtime<Config>) => {
|
|
|
196
216
|
data: bytesToHex(contractCall.data),
|
|
197
217
|
});
|
|
198
218
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
abi: CONTRACT_ABI,
|
|
202
|
-
functionName: "setValue",
|
|
203
|
-
args: [onchainValue],
|
|
204
|
-
});
|
|
205
|
-
const tx = evmClient
|
|
206
|
-
.writeReport(runtime, {
|
|
207
|
-
receiver: contractAddress,
|
|
208
|
-
report: { rawReport: writeData },
|
|
209
|
-
})
|
|
210
|
-
.result();
|
|
211
|
-
return { onchainValue, txHash: tx.txHash?.toString() };
|
|
219
|
+
runtime.log(`Successfully read onchain value: ${onchainValue}`);
|
|
220
|
+
return onchainValue;
|
|
212
221
|
};
|
|
213
222
|
```
|
|
214
223
|
|
|
@@ -264,24 +273,88 @@ const aggregatedValue = await runtime.runInNodeMode(
|
|
|
264
273
|
|
|
265
274
|
## Utility Functions
|
|
266
275
|
|
|
267
|
-
###
|
|
276
|
+
### HTTP Response Helpers
|
|
277
|
+
|
|
278
|
+
Work with HTTP responses using convenient helper functions:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { ok, text, json, getHeader } from "@chainlink/cre-sdk";
|
|
282
|
+
|
|
283
|
+
const response = sendRequester
|
|
284
|
+
.sendRequest({ url: "https://api.example.com" })
|
|
285
|
+
.result();
|
|
286
|
+
|
|
287
|
+
// Check if response is successful (200-299 status)
|
|
288
|
+
if (!ok(response)) {
|
|
289
|
+
throw new Error(`Request failed with status: ${response.statusCode}`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Get response as trimmed text
|
|
293
|
+
const responseText = text(response);
|
|
294
|
+
|
|
295
|
+
// Parse JSON response
|
|
296
|
+
const data = json(response);
|
|
268
297
|
|
|
269
|
-
|
|
298
|
+
// Get specific header
|
|
299
|
+
const contentType = getHeader(response, "content-type");
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Blockchain Helpers
|
|
270
303
|
|
|
271
|
-
|
|
304
|
+
Helper functions for EVM blockchain interactions:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import {
|
|
308
|
+
encodeCallMsg,
|
|
309
|
+
prepareReportRequest,
|
|
310
|
+
LAST_FINALIZED_BLOCK_NUMBER,
|
|
311
|
+
LATEST_BLOCK_NUMBER,
|
|
312
|
+
} from "@chainlink/cre-sdk";
|
|
313
|
+
import { encodeFunctionData } from "viem";
|
|
314
|
+
|
|
315
|
+
// Encode call message for contract reads
|
|
316
|
+
const callMsg = encodeCallMsg({
|
|
317
|
+
from: zeroAddress,
|
|
318
|
+
to: contractAddress,
|
|
319
|
+
data: callData,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// Use block number constants
|
|
323
|
+
const response = evmClient
|
|
324
|
+
.callContract(runtime, {
|
|
325
|
+
call: callMsg,
|
|
326
|
+
blockNumber: LAST_FINALIZED_BLOCK_NUMBER, // or LATEST_BLOCK_NUMBER
|
|
327
|
+
})
|
|
328
|
+
.result();
|
|
329
|
+
|
|
330
|
+
// Prepare report for contract writes
|
|
331
|
+
const writeData = encodeFunctionData({
|
|
332
|
+
abi: CONTRACT_ABI,
|
|
333
|
+
functionName: "setValue",
|
|
334
|
+
args: [value],
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const report = runtime.report(prepareReportRequest(writeData)).result();
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### Hex Utilities
|
|
341
|
+
|
|
342
|
+
Convert between hex and base64 formats for CRE protocol communication:
|
|
272
343
|
|
|
273
344
|
```typescript
|
|
274
345
|
import { hexToBase64, bytesToHex } from "@chainlink/cre-sdk";
|
|
275
346
|
|
|
276
|
-
//
|
|
347
|
+
// Hex to Base64: "0x1234567890abcdef" → "EjRWeJCrze8="
|
|
277
348
|
const base64Data = hexToBase64("0x1234567890abcdef");
|
|
278
349
|
|
|
279
|
-
//
|
|
350
|
+
// Bytes to Hex: Uint8Array([18, 52, 86...]) → "0x1234567890abcdef"
|
|
280
351
|
const hexData = bytesToHex(buffer);
|
|
281
352
|
```
|
|
282
353
|
|
|
283
354
|
### Chain Selectors
|
|
284
355
|
|
|
356
|
+
Access blockchain network metadata:
|
|
357
|
+
|
|
285
358
|
```typescript
|
|
286
359
|
import { getAllNetworks, getNetwork } from "@chainlink/cre-sdk";
|
|
287
360
|
|
|
@@ -297,7 +370,7 @@ const ethereumSepolia = getNetwork({
|
|
|
297
370
|
|
|
298
371
|
### 1. Simple Cron-scheduled task
|
|
299
372
|
|
|
300
|
-
See the [hello-world](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/hello-world) example
|
|
373
|
+
See the [hello-world](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/hello-world) example that runs a cron-based operation on CRE at intervals you define in the `config.json` file.
|
|
301
374
|
|
|
302
375
|
### 2. API Data Aggregation
|
|
303
376
|
|
|
@@ -307,6 +380,14 @@ See the [http-fetch example](https://github.com/smartcontractkit/cre-sdk-typescr
|
|
|
307
380
|
|
|
308
381
|
See the [on-chain example](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/workflows/on-chain) for reading from smart contracts, and the [on-chain-write example](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/workflows/on-chain-write) for writing to smart contracts.
|
|
309
382
|
|
|
383
|
+
### 4. Proof of Reserve
|
|
384
|
+
|
|
385
|
+
See the [proof-of-reserve example](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/workflows/proof-of-reserve) for a complete implementation demonstrating reserve validation using on-chain data verification and off-chain API integration.
|
|
386
|
+
|
|
387
|
+
### 5. Star Wars API
|
|
388
|
+
|
|
389
|
+
See the [star-wars example](https://github.com/smartcontractkit/cre-sdk-typescript/tree/main/packages/cre-sdk-examples/src/workflows/star-wars) for an easy-to-follow example, known for being the default code used in [Chainlink Functions' Playground](https://functions.chain.link/playground).
|
|
390
|
+
|
|
310
391
|
## API Reference
|
|
311
392
|
|
|
312
393
|
### Core Functions
|
|
@@ -323,9 +404,33 @@ See the [on-chain example](https://github.com/smartcontractkit/cre-sdk-typescrip
|
|
|
323
404
|
|
|
324
405
|
### Utilities
|
|
325
406
|
|
|
407
|
+
**HTTP Helpers:**
|
|
408
|
+
|
|
409
|
+
- `ok(response)`: Check if HTTP status is successful (200-299)
|
|
410
|
+
- `text(response)`: Get response body as trimmed text
|
|
411
|
+
- `json(response)`: Parse response body as JSON
|
|
412
|
+
- `getHeader(response, name)`: Get specific header value
|
|
413
|
+
|
|
414
|
+
**Blockchain Helpers:**
|
|
415
|
+
|
|
416
|
+
- `encodeCallMsg({ from, to, data })`: Encode call message for EVM reads
|
|
417
|
+
- `prepareReportRequest(hexPayload)`: Prepare report for EVM writes
|
|
418
|
+
- `LAST_FINALIZED_BLOCK_NUMBER`: Constant for finalized block reads
|
|
419
|
+
- `LATEST_BLOCK_NUMBER`: Constant for latest block reads
|
|
420
|
+
|
|
421
|
+
**Data Conversion:**
|
|
422
|
+
|
|
423
|
+
- `hexToBase64(hex)`: Convert hex string to base64
|
|
424
|
+
- `bytesToHex(bytes)`: Convert bytes to hex string
|
|
425
|
+
|
|
426
|
+
**Consensus:**
|
|
427
|
+
|
|
326
428
|
- `consensusMedianAggregation()`: Median consensus aggregator
|
|
327
|
-
|
|
328
|
-
|
|
429
|
+
|
|
430
|
+
**Chain Selectors:**
|
|
431
|
+
|
|
432
|
+
- `getAllNetworks()`: Get all supported networks
|
|
433
|
+
- `getNetwork(options)`: Get specific network metadata
|
|
329
434
|
|
|
330
435
|
## Building from Source
|
|
331
436
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainlink/cre-sdk",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@bufbuild/protobuf": "2.6.3",
|
|
57
57
|
"@bufbuild/protoc-gen-es": "2.6.3",
|
|
58
|
-
"@chainlink/cre-sdk-javy-plugin": "1.0.0
|
|
58
|
+
"@chainlink/cre-sdk-javy-plugin": "1.0.0",
|
|
59
59
|
"@standard-schema/spec": "1.0.0",
|
|
60
60
|
"viem": "2.34.0",
|
|
61
61
|
"zod": "3.25.76"
|
|
@@ -36,7 +36,7 @@ export const main = async (tsFilePath?: string, outputFilePath?: string) => {
|
|
|
36
36
|
await Bun.build({
|
|
37
37
|
entrypoints: [resolvedInput],
|
|
38
38
|
outdir: path.dirname(resolvedOutput),
|
|
39
|
-
target: '
|
|
39
|
+
target: 'browser',
|
|
40
40
|
format: 'esm',
|
|
41
41
|
naming: path.basename(resolvedOutput),
|
|
42
42
|
})
|