Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"packages/cre-sdk": {
"name": "@chainlink/cre-sdk",
"version": "0.0.4-alpha",
"version": "0.0.5-alpha",
"bin": {
"cre-compile": "bin/cre-compile.ts",
},
Expand All @@ -37,13 +37,16 @@
},
"packages/cre-sdk-examples": {
"name": "@chainlink/cre-sdk-examples",
"version": "0.0.4-alpha",
"version": "0.0.5-alpha",
"dependencies": {
"@bufbuild/protobuf": "2.6.3",
"@chainlink/cre-sdk": "workspace:*",
"viem": "2.34.0",
"zod": "3.25.76",
},
"devDependencies": {
"@types/bun": "1.2.21",
},
},
"packages/cre-sdk-javy-plugin": {
"name": "@chainlink/cre-sdk-javy-plugin",
Expand Down
6 changes: 6 additions & 0 deletions packages/cre-sdk-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ cre workflow simulate ./src/workflows/on-chain-write
cre workflow simulate ./src/workflows/proof-of-reserve
```

[Star Wars character example](https://github.com/smartcontractkit/cre-sdk-typescript/blob/main/packages/cre-sdk-examples/src/workflows/star-wars/index.ts):

```zsh
cre workflow simulate ./src/workflows/star-wars
```

## Testing workflow compilation only

If you want use the CRE SDK to compile your workflows to WASM, choose any workflow you want to compile and run the following command:
Expand Down
5 changes: 4 additions & 1 deletion packages/cre-sdk-examples/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@chainlink/cre-sdk-examples",
"private": true,
"version": "0.0.5-alpha",
"version": "0.0.6-alpha",
"type": "module",
"author": "Ernest Nowacki",
"license": "BUSL-1.1",
Expand All @@ -19,6 +19,9 @@
"viem": "2.34.0",
"zod": "3.25.76"
},
"devDependencies": {
"@types/bun": "1.2.21"
},
"engines": {
"bun": ">=1.2.21"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://swapi.info/api/people/{characterId}/"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"characterId": 1
}
88 changes: 88 additions & 0 deletions packages/cre-sdk-examples/src/workflows/star-wars/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
consensusIdenticalAggregation,
cre,
decodeJson,
type HTTPPayload,
type HTTPSendRequester,
json,
ok,
Runner,
type Runtime,
} from '@chainlink/cre-sdk'
import { z } from 'zod'

const configSchema = z.object({
url: z.string(),
})

type Config = z.infer<typeof configSchema>

const responseSchema = z.object({
name: z.string(),
height: z.string(),
mass: z.string(),
hair_color: z.string(),
skin_color: z.string(),
eye_color: z.string(),
birth_year: z.string(),
gender: z.string(),
homeworld: z.string(),
films: z.array(z.string()),
species: z.array(z.string()),
vehicles: z.array(z.string()),
starships: z.array(z.string()),
created: z.string().datetime(),
edited: z.string().datetime(),
url: z.string(),
})

type StarWarsCharacter = z.infer<typeof responseSchema>

const fetchStarWarsCharacter = (
sendRequester: HTTPSendRequester,
config: Config,
payload: HTTPPayload,
): StarWarsCharacter => {
const input = decodeJson(payload.input)
const url = config.url.replace('{characterId}', input.characterId)

const response = sendRequester.sendRequest({ url }).result()

// Check if the response is successful using the helper function
if (!ok(response)) {
throw new Error(`HTTP request failed with status: ${response.statusCode}`)
}

const character = responseSchema.parse(json(response))

return character
}

const onHTTPTrigger = async (runtime: Runtime<Config>, payload: HTTPPayload) => {
const httpCapability = new cre.capabilities.HTTPClient()

const result: StarWarsCharacter = httpCapability
.sendRequest(
runtime,
fetchStarWarsCharacter,
consensusIdenticalAggregation(),
)(runtime.config, payload)
.result()

return result
}

const initWorkflow = () => {
const httpTrigger = new cre.capabilities.HTTPCapability()

return [cre.handler(httpTrigger.trigger({}), onHTTPTrigger)]
}

export async function main() {
const runner = await Runner.newRunner<Config>({
configSchema,
})
await runner.run(initWorkflow)
}

main()
38 changes: 38 additions & 0 deletions packages/cre-sdk-examples/src/workflows/star-wars/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ==========================================================================
# CRE WORKFLOW SETTINGS FILE
# ==========================================================================
# This file defines environment-specific workflow settings used by the CRE CLI.
#
# Each top-level key is a target (e.g., `production`, `production-testnet`, etc.).
# You can also define your own custom targets, such as `my-target`, and
# point the CLI to it via an environment variable.
#
# Note: If any setting in this file conflicts with a setting in the CRE Project Settings File,
# the value defined here in the workflow settings file will take precedence.
#
# Below is an example `my-target`:
#
# my-target:
# user-workflow:
# # Optional: The address of the workflow owner (wallet or MSIG contract).
# # Used to establish ownership for encrypting the workflow's secrets.
# # If omitted, defaults to an empty string.
# workflow-owner-address: "0x1234567890abcdef1234567890abcdef12345678"
#
# # Required: The name of the workflow to register with the Workflow Registry contract.
# workflow-name: "MyExampleWorkflow"

# ==========================================================================
local-simulation:
user-workflow:
workflow-owner-address: "(optional) Multi-signature contract address"
workflow-name: "star-wars"
workflow-artifacts:
workflow-path: "./index.ts"
config-path: "./config.json"

# ==========================================================================
production-testnet:
user-workflow:
workflow-owner-address: "(optional) Multi-signature contract address"
workflow-name: "star-wars"
3 changes: 1 addition & 2 deletions packages/cre-sdk-examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
Expand Down
Binary file modified packages/cre-sdk-javy-plugin/dist/javy_chainlink_sdk.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/cre-sdk-javy-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainlink/cre-sdk-javy-plugin",
"version": "0.0.4-alpha",
"version": "0.0.5-alpha",
"type": "module",
"bin": {
"cre-setup": "bin/setup.ts",
Expand Down
3 changes: 1 addition & 2 deletions packages/cre-sdk-javy-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
Expand Down
8 changes: 6 additions & 2 deletions packages/cre-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "@chainlink/cre-sdk",
"version": "0.0.5-alpha",
"version": "0.0.6-alpha",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./restricted-apis": {
"types": "./dist/restricted-apis.d.ts"
}
},
"bin": {
Expand All @@ -22,7 +25,8 @@
"LICENSE.md"
],
"scripts": {
"build": "bun run clean && bun run compile:build && bun run fix-imports",
"build": "bun run clean && bun run compile:build && bun run build:types && bun run fix-imports",
"build:types": "bun run scripts/run.ts build-types",
"check": "biome check --write ${BIOME_PATHS:-.}",
"clean": "rm -rf dist",
"compile:all-standard-tests": "bun scripts/run.ts compile-all-standard-tests",
Expand Down
4 changes: 2 additions & 2 deletions packages/cre-sdk/scripts/run.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env bun

const availableScripts = [
'build-types',
'compile-to-js',
'compile-to-wasm',
'compile-workflow', // TS -> JS -> WASM compilation in single script
'compile-all-standard-tests', // Do the above but for all standard tests

'fix-imports', // Fix @cre/* imports to relative paths
'generate-chain-selectors',
'generate-sdks',
'fix-imports', // Fix @cre/* imports to relative paths
]

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/cre-sdk/scripts/src/build-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { glob } from 'fast-glob'
import { copyFile, mkdir } from 'fs/promises'
import { dirname, join, relative } from 'path'

const buildTypes = async () => {
console.log('🔧 Including restricted-apis type in built files...')

// Define paths relative to the scripts directory
const packageRoot = join(import.meta.dir, '../..')
const sourceFile = join(packageRoot, 'src/sdk/types/restricted-apis.d.ts')
const destFile = join(packageRoot, 'dist/restricted-apis.d.ts')

// Ensure the dist directory exists
await mkdir(dirname(destFile), { recursive: true })

// Copy the file
await copyFile(sourceFile, destFile)

console.log('✅ Included restricted-apis type in the build.')
}

export const main = buildTypes
1 change: 1 addition & 0 deletions packages/cre-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="./sdk/types/global" />
/// <reference types="./sdk/types/restricted-apis" />

export * from './sdk'
export * from './sdk/runtime'
Expand Down
10 changes: 5 additions & 5 deletions packages/cre-sdk/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './cre'
export * from './report'
export type * from './runtime'
export * from './runtime'
export * from './workflow'
export * from "./cre";
export * from "./report";
export type * from "./runtime";
export * from "./runtime";
export * from "./workflow";
27 changes: 27 additions & 0 deletions packages/cre-sdk/src/sdk/types/restricted-apis.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
declare global {
/** @deprecated fetch is not available in CRE WASM workflows. Use cre.capabilities.HTTPClient instead. */
const fetch: never

/** @deprecated window is not available in CRE WASM workflows. */
const window: never

/** @deprecated document is not available in CRE WASM workflows. */
const document: never

/** @deprecated XMLHttpRequest is not available in CRE WASM workflows. Use cre.capabilities.HTTPClient instead. */
const XMLHttpRequest: never

/** @deprecated localStorage is not available in CRE WASM workflows. */
const localStorage: never

/** @deprecated sessionStorage is not available in CRE WASM workflows. */
const sessionStorage: never

/** @deprecated setTimeout is not available in CRE WASM workflows. Use cre.capabilities.CronCapability for scheduling. */
const setTimeout: never

/** @deprecated setInterval is not available in CRE WASM workflows. Use cre.capabilities.CronCapability for scheduling. */
const setInterval: never
}

export {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from '@cre/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen'
import type { NodeRuntime } from '@cre/sdk'
import type { Report } from '@cre/sdk/report'
import { decodeJson } from '@cre/sdk/utils/decode-json'

/**
* HTTP Response Helper Functions
Expand Down Expand Up @@ -105,11 +106,9 @@ export function json(
return {
result: () => json(responseOrFn().result),
}
} else {
const decoder = new TextDecoder('utf-8')
const textBody = decoder.decode(responseOrFn.body)
return JSON.parse(textBody)
}

return decodeJson(responseOrFn.body)
}

/**
Expand Down
Loading