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
3 changes: 2 additions & 1 deletion packages/cre-sdk-examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
dist
.turbo
tmp.js
tmp.wasm
tmp.wasm
.cre_build_tmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"schedule": "0 */1 * * * *",
"url": "https://api.mathjs.org/v4?expr=randomInt(1,101)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
ConfidentialHTTPClient,
CronCapability,
handler,
httpRequest,
json,
ok,
Runner,
type Runtime,
text,
} from '@chainlink/cre-sdk'
import { z } from 'zod'

const configSchema = z.object({
schedule: z.string(),
url: z.string(),
})
type Config = z.infer<typeof configSchema>

// Workflow demonstrate a usage of `httpRequest` helper
// to build type-safe payloads for `ConfidentialHTTPClient`.
const onCronTrigger = (runtime: Runtime<Config>) => {
const client = new ConfidentialHTTPClient()

// Example 1: request config as separate variable
const separateRequestConfig = {
request: httpRequest({
url: runtime.config.url,
method: 'POST',
bodyString: '{ hello: "world" }',
multiHeaders: {
'content-type': { values: ['application/json'] },
},
}),
}

client.sendRequest(runtime, separateRequestConfig).result()

// Example 2: using helper inline
client
.sendRequest(runtime, {
request: httpRequest({
url: runtime.config.url,
method: 'POST',
body: { hello: 'world' },
multiHeaders: {
'content-type': { values: ['application/json'] },
},
}),
})
.result()

// Example 3: not using helper at all
client
.sendRequest(runtime, {
request: {
url: runtime.config.url,
method: 'POST',
// no helper -> must use bodyString/bodyBytes (proto oneof keys)
bodyString: JSON.stringify({ hello: 'world' }),
multiHeaders: {
'content-type': { values: ['application/json'] },
},
},
})
.result()

return { success: true }
}

const initWorkflow = (config: Config) => {
const cron = new CronCapability()
return [handler(cron.trigger({ schedule: config.schedule }), onCronTrigger)]
}

export async function main() {
const runner = await Runner.newRunner<Config>({ configSchema })
await runner.run(initWorkflow)
}
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: "confidential-http-with-body"
workflow-artifacts:
workflow-path: "./index.ts"
config-path: "./config.json"

# ==========================================================================
production-testnet:
user-workflow:
workflow-owner-address: "(optional) Multi-signature contract address"
workflow-name: "confidential-http-with-body"
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import type { Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import { hexToBytes } from '@cre/sdk/utils/hex-utils'
import type { Trigger } from '@cre/sdk/utils/triggers/trigger-interface'
import type { CapabilityInput, NoExcess } from '@cre/sdk/utils/types/no-excess'

export type WriteCreReportRequest = {
receiver: Uint8Array
Expand Down Expand Up @@ -180,6 +181,10 @@ export class ClientCapability {

constructor(private readonly ChainSelector: bigint) {}

callContract<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, CallContractRequest, CallContractRequestJson>,
): { result: () => CallContractReply }
callContract(
runtime: Runtime<unknown>,
input: CallContractRequest | CallContractRequestJson,
Expand Down Expand Up @@ -215,6 +220,10 @@ export class ClientCapability {
}
}

filterLogs<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, FilterLogsRequest, FilterLogsRequestJson>,
): { result: () => FilterLogsReply }
filterLogs(
runtime: Runtime<unknown>,
input: FilterLogsRequest | FilterLogsRequestJson,
Expand Down Expand Up @@ -250,6 +259,10 @@ export class ClientCapability {
}
}

balanceAt<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, BalanceAtRequest, BalanceAtRequestJson>,
): { result: () => BalanceAtReply }
balanceAt(
runtime: Runtime<unknown>,
input: BalanceAtRequest | BalanceAtRequestJson,
Expand Down Expand Up @@ -285,6 +298,10 @@ export class ClientCapability {
}
}

estimateGas<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, EstimateGasRequest, EstimateGasRequestJson>,
): { result: () => EstimateGasReply }
estimateGas(
runtime: Runtime<unknown>,
input: EstimateGasRequest | EstimateGasRequestJson,
Expand Down Expand Up @@ -320,6 +337,10 @@ export class ClientCapability {
}
}

getTransactionByHash<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, GetTransactionByHashRequest, GetTransactionByHashRequestJson>,
): { result: () => GetTransactionByHashReply }
getTransactionByHash(
runtime: Runtime<unknown>,
input: GetTransactionByHashRequest | GetTransactionByHashRequestJson,
Expand Down Expand Up @@ -361,6 +382,10 @@ export class ClientCapability {
}
}

getTransactionReceipt<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, GetTransactionReceiptRequest, GetTransactionReceiptRequestJson>,
): { result: () => GetTransactionReceiptReply }
getTransactionReceipt(
runtime: Runtime<unknown>,
input: GetTransactionReceiptRequest | GetTransactionReceiptRequestJson,
Expand Down Expand Up @@ -402,6 +427,10 @@ export class ClientCapability {
}
}

headerByNumber<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, HeaderByNumberRequest, HeaderByNumberRequestJson>,
): { result: () => HeaderByNumberReply }
headerByNumber(
runtime: Runtime<unknown>,
input: HeaderByNumberRequest | HeaderByNumberRequestJson,
Expand Down Expand Up @@ -437,12 +466,23 @@ export class ClientCapability {
}
}

logTrigger(config: FilterLogTriggerRequestJson): ClientLogTrigger {
logTrigger<TConfig extends FilterLogTriggerRequestJson>(
config: NoExcess<TConfig, FilterLogTriggerRequestJson>,
): ClientLogTrigger {
// Include all labels in capability ID for routing when specified
const capabilityId = `${ClientCapability.CAPABILITY_NAME}:ChainSelector:${this.ChainSelector}@${ClientCapability.CAPABILITY_VERSION}`
return new ClientLogTrigger(config, capabilityId, 'LogTrigger', this.ChainSelector)
return new ClientLogTrigger(
config as FilterLogTriggerRequestJson,
capabilityId,
'LogTrigger',
this.ChainSelector,
)
}

writeReport<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, WriteCreReportRequest, WriteCreReportRequestJson>,
): { result: () => WriteReportReply }
writeReport(
runtime: Runtime<unknown>,
input: WriteCreReportRequest | WriteCreReportRequestJson,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import { hexToBytes } from '@cre/sdk/utils/hex-utils'
import type { Trigger } from '@cre/sdk/utils/triggers/trigger-interface'
import type { CapabilityInput, NoExcess } from '@cre/sdk/utils/types/no-excess'

/**
* Basic Capability
Expand All @@ -31,6 +32,10 @@ export class BasicCapability {
static readonly CAPABILITY_NAME = 'basic-test-action-trigger'
static readonly CAPABILITY_VERSION = '1.0.0'

action<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, Input, InputJson>,
): { result: () => Output }
action(runtime: Runtime<unknown>, input: Input | InputJson): { result: () => Output } {
// Handle input conversion - unwrap if it's a wrapped type, convert from JSON if needed
let payload: Input
Expand Down Expand Up @@ -62,9 +67,9 @@ export class BasicCapability {
}
}

trigger(config: ConfigJson): BasicTrigger {
trigger<TConfig extends ConfigJson>(config: NoExcess<TConfig, ConfigJson>): BasicTrigger {
const capabilityId = BasicCapability.CAPABILITY_ID
return new BasicTrigger(config, capabilityId, 'Trigger')
return new BasicTrigger(config as ConfigJson, capabilityId, 'Trigger')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import type { Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import { hexToBytes } from '@cre/sdk/utils/hex-utils'
import type { CapabilityInput } from '@cre/sdk/utils/types/no-excess'

/**
* BasicAction Capability
Expand All @@ -24,6 +25,10 @@ export class BasicActionCapability {
static readonly CAPABILITY_NAME = 'basic-test-action'
static readonly CAPABILITY_VERSION = '1.0.0'

performAction<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, Inputs, InputsJson>,
): { result: () => Outputs }
performAction(runtime: Runtime<unknown>, input: Inputs | InputsJson): { result: () => Outputs } {
// Handle input conversion - unwrap if it's a wrapped type, convert from JSON if needed
let payload: Inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OutputsSchema,
} from '@cre/generated/capabilities/internal/basictrigger/v1/basic_trigger_pb'
import type { Trigger } from '@cre/sdk/utils/triggers/trigger-interface'
import type { NoExcess } from '@cre/sdk/utils/types/no-excess'

/**
* Basic Capability
Expand All @@ -23,9 +24,9 @@ export class BasicCapability {
static readonly CAPABILITY_NAME = 'basic-test-trigger'
static readonly CAPABILITY_VERSION = '1.0.0'

trigger(config: ConfigJson): BasicTrigger {
trigger<TConfig extends ConfigJson>(config: NoExcess<TConfig, ConfigJson>): BasicTrigger {
const capabilityId = BasicCapability.CAPABILITY_ID
return new BasicTrigger(config, capabilityId, 'Trigger')
return new BasicTrigger(config as ConfigJson, capabilityId, 'Trigger')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { type Value, ValueSchema } from '@cre/generated/values/v1/values_pb'
import type { Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import { hexToBytes } from '@cre/sdk/utils/hex-utils'
import type { CapabilityInput } from '@cre/sdk/utils/types/no-excess'

/**
* Consensus Capability
Expand All @@ -28,6 +29,10 @@ export class ConsensusCapability {
static readonly CAPABILITY_NAME = 'consensus'
static readonly CAPABILITY_VERSION = '1.0.0-alpha'

simple<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, SimpleConsensusInputs, SimpleConsensusInputsJson>,
): { result: () => Value }
simple(
runtime: Runtime<unknown>,
input: SimpleConsensusInputs | SimpleConsensusInputsJson,
Expand Down Expand Up @@ -62,6 +67,10 @@ export class ConsensusCapability {
}
}

report<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, ReportRequest, ReportRequestJson>,
): { result: () => Report }
report(
runtime: Runtime<unknown>,
input: ReportRequest | ReportRequestJson,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import {
import type { NodeRuntime, Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import type { ConsensusAggregation, PrimitiveTypes, UnwrapOptions } from '@cre/sdk/utils'
import type { CapabilityInput } from '@cre/sdk/utils/types/no-excess'

export class PerformActioner {
constructor(
private readonly runtime: NodeRuntime<unknown>,
private readonly client: BasicActionCapability,
) {}
performAction(input: NodeInputs | NodeInputsJson): { result: () => NodeOutputs } {
return this.client.performAction(this.runtime, input)
performAction<TInput>(input: CapabilityInput<TInput, NodeInputs, NodeInputsJson>): {
result: () => NodeOutputs
} {
return this.client.performAction<TInput>(this.runtime, input)
}
}

Expand All @@ -34,9 +37,9 @@ export class BasicActionCapability {
static readonly CAPABILITY_NAME = 'basic-test-node-action'
static readonly CAPABILITY_VERSION = '1.0.0'

performAction(
performAction<TInput>(
runtime: NodeRuntime<unknown>,
input: NodeInputs | NodeInputsJson,
input: CapabilityInput<TInput, NodeInputs, NodeInputsJson>,
): { result: () => NodeOutputs }
performAction<TArgs extends unknown[], TOutput>(
runtime: Runtime<unknown>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import type { Runtime } from '@cre/sdk'
import { Report } from '@cre/sdk/report'
import { hexToBytes } from '@cre/sdk/utils/hex-utils'
import type { CapabilityInput } from '@cre/sdk/utils/types/no-excess'

/**
* Client Capability
Expand All @@ -24,6 +25,10 @@ export class ClientCapability {
static readonly CAPABILITY_NAME = 'confidential-http'
static readonly CAPABILITY_VERSION = '1.0.0-alpha'

sendRequest<TInput>(
runtime: Runtime<unknown>,
input: CapabilityInput<TInput, ConfidentialHTTPRequest, ConfidentialHTTPRequestJson>,
): { result: () => HTTPResponse }
sendRequest(
runtime: Runtime<unknown>,
input: ConfidentialHTTPRequest | ConfidentialHTTPRequestJson,
Expand Down
Loading
Loading