@chainlink/cre-sdk 1.1.0 → 1.1.2
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/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.d.ts +2 -2
- package/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.js +0 -2
- package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.d.ts +1 -1
- package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.js +0 -1
- package/dist/generated-sdk/capabilities/networking/confidentialhttp/v1alpha/client_sdk_gen.d.ts +1 -1
- package/dist/generated-sdk/capabilities/networking/confidentialhttp/v1alpha/client_sdk_gen.js +0 -1
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.d.ts +1 -1
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.js +0 -1
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.d.ts +1 -1
- package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.js +0 -1
- package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.d.ts +1 -1
- package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.js +0 -1
- package/dist/sdk/test/evm-contract-mock.d.ts +68 -0
- package/dist/sdk/test/evm-contract-mock.js +114 -0
- package/dist/sdk/test/index.d.ts +1 -0
- package/dist/sdk/test/index.js +1 -0
- package/package.json +2 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type Any } from '@bufbuild/protobuf/wkt';
|
|
2
2
|
import { type BalanceAtReply, type BalanceAtRequest, type BalanceAtRequestJson, type CallContractReply, type CallContractRequest, type CallContractRequestJson, type EstimateGasReply, type EstimateGasRequest, type EstimateGasRequestJson, type FilterLogsReply, type FilterLogsRequest, type FilterLogsRequestJson, type FilterLogTriggerRequest, type FilterLogTriggerRequestJson, type GasConfig, type GasConfigJson, type GetTransactionByHashReply, type GetTransactionByHashRequest, type GetTransactionByHashRequestJson, type GetTransactionReceiptReply, type GetTransactionReceiptRequest, type GetTransactionReceiptRequestJson, type HeaderByNumberReply, type HeaderByNumberRequest, type HeaderByNumberRequestJson, type Log, type WriteReportReply, type WriteReportRequest } from '../../../../../generated/capabilities/blockchain/evm/v1alpha/client_pb';
|
|
3
|
-
import {
|
|
3
|
+
import type { Runtime } from '../../../../../sdk';
|
|
4
4
|
import { Report } from '../../../../../sdk/report';
|
|
5
|
-
import {
|
|
5
|
+
import type { Trigger } from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
6
6
|
export type WriteCreReportRequest = {
|
|
7
7
|
receiver: Uint8Array;
|
|
8
8
|
report?: Report;
|
|
@@ -2,10 +2,8 @@ import { create, fromJson } from '@bufbuild/protobuf';
|
|
|
2
2
|
import { AnySchema, anyPack } from '@bufbuild/protobuf/wkt';
|
|
3
3
|
import { BalanceAtReplySchema, BalanceAtRequestSchema, CallContractReplySchema, CallContractRequestSchema, EstimateGasReplySchema, EstimateGasRequestSchema, FilterLogsReplySchema, FilterLogsRequestSchema, FilterLogTriggerRequestSchema, GasConfigSchema, GetTransactionByHashReplySchema, GetTransactionByHashRequestSchema, GetTransactionReceiptReplySchema, GetTransactionReceiptRequestSchema, HeaderByNumberReplySchema, HeaderByNumberRequestSchema, LogSchema, WriteReportReplySchema, WriteReportRequestSchema, } from '../../../../../generated/capabilities/blockchain/evm/v1alpha/client_pb';
|
|
4
4
|
import { ReportResponseSchema, } from '../../../../../generated/sdk/v1alpha/sdk_pb';
|
|
5
|
-
import {} from '../../../../../sdk';
|
|
6
5
|
import { Report } from '../../../../../sdk/report';
|
|
7
6
|
import { hexToBytes } from '../../../../../sdk/utils/hex-utils';
|
|
8
|
-
import {} from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
9
7
|
export function x_generatedCodeOnly_wrap_WriteCreReportRequest(input) {
|
|
10
8
|
return {
|
|
11
9
|
receiver: input.receiver,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ReportRequest, type ReportRequestJson, type SimpleConsensusInputs, type SimpleConsensusInputsJson } from '../../../../../generated/sdk/v1alpha/sdk_pb';
|
|
2
2
|
import { type Value } from '../../../../../generated/values/v1/values_pb';
|
|
3
|
-
import {
|
|
3
|
+
import type { Runtime } from '../../../../../sdk';
|
|
4
4
|
import { Report } from '../../../../../sdk/report';
|
|
5
5
|
/**
|
|
6
6
|
* Consensus Capability
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { fromJson } from '@bufbuild/protobuf';
|
|
2
2
|
import { ReportRequestSchema, ReportResponseSchema, SimpleConsensusInputsSchema, } from '../../../../../generated/sdk/v1alpha/sdk_pb';
|
|
3
3
|
import { ValueSchema } from '../../../../../generated/values/v1/values_pb';
|
|
4
|
-
import {} from '../../../../../sdk';
|
|
5
4
|
import { Report } from '../../../../../sdk/report';
|
|
6
5
|
import { hexToBytes } from '../../../../../sdk/utils/hex-utils';
|
|
7
6
|
/**
|
package/dist/generated-sdk/capabilities/networking/confidentialhttp/v1alpha/client_sdk_gen.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ConfidentialHTTPRequest, type ConfidentialHTTPRequestJson, type HTTPResponse } from '../../../../../generated/capabilities/networking/confidentialhttp/v1alpha/client_pb';
|
|
2
|
-
import {
|
|
2
|
+
import type { Runtime } from '../../../../../sdk';
|
|
3
3
|
/**
|
|
4
4
|
* Client Capability
|
|
5
5
|
*
|
package/dist/generated-sdk/capabilities/networking/confidentialhttp/v1alpha/client_sdk_gen.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { fromJson } from '@bufbuild/protobuf';
|
|
2
2
|
import { ConfidentialHTTPRequestSchema, HTTPResponseSchema, } from '../../../../../generated/capabilities/networking/confidentialhttp/v1alpha/client_pb';
|
|
3
|
-
import {} from '../../../../../sdk';
|
|
4
3
|
import { Report } from '../../../../../sdk/report';
|
|
5
4
|
import { hexToBytes } from '../../../../../sdk/utils/hex-utils';
|
|
6
5
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Request, type RequestJson, type Response } from '../../../../../generated/capabilities/networking/http/v1alpha/client_pb';
|
|
2
|
-
import {
|
|
2
|
+
import type { NodeRuntime, Runtime } from '../../../../../sdk';
|
|
3
3
|
import type { ConsensusAggregation, PrimitiveTypes, UnwrapOptions } from '../../../../../sdk/utils';
|
|
4
4
|
export declare class SendRequester {
|
|
5
5
|
private readonly runtime;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { fromJson } from '@bufbuild/protobuf';
|
|
2
2
|
import { RequestSchema, ResponseSchema, } from '../../../../../generated/capabilities/networking/http/v1alpha/client_pb';
|
|
3
|
-
import {} from '../../../../../sdk';
|
|
4
3
|
import { Report } from '../../../../../sdk/report';
|
|
5
4
|
export class SendRequester {
|
|
6
5
|
runtime;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Any } from '@bufbuild/protobuf/wkt';
|
|
2
2
|
import { type Config, type ConfigJson, type Payload } from '../../../../../generated/capabilities/networking/http/v1alpha/trigger_pb';
|
|
3
|
-
import {
|
|
3
|
+
import type { Trigger } from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
4
4
|
/**
|
|
5
5
|
* HTTP Capability
|
|
6
6
|
*
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { create, fromJson } from '@bufbuild/protobuf';
|
|
2
2
|
import { AnySchema, anyPack } from '@bufbuild/protobuf/wkt';
|
|
3
3
|
import { ConfigSchema, PayloadSchema, } from '../../../../../generated/capabilities/networking/http/v1alpha/trigger_pb';
|
|
4
|
-
import {} from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
5
4
|
/**
|
|
6
5
|
* HTTP Capability
|
|
7
6
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Any } from '@bufbuild/protobuf/wkt';
|
|
2
2
|
import { type Config, type ConfigJson, type Payload } from '../../../../../generated/capabilities/scheduler/cron/v1/trigger_pb';
|
|
3
|
-
import {
|
|
3
|
+
import type { Trigger } from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
4
4
|
/**
|
|
5
5
|
* Cron Capability
|
|
6
6
|
*
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { create, fromJson } from '@bufbuild/protobuf';
|
|
2
2
|
import { AnySchema, anyPack } from '@bufbuild/protobuf/wkt';
|
|
3
3
|
import { ConfigSchema, LegacyPayloadSchema, PayloadSchema, } from '../../../../../generated/capabilities/scheduler/cron/v1/trigger_pb';
|
|
4
|
-
import {} from '../../../../../sdk/utils/triggers/trigger-interface';
|
|
5
4
|
/**
|
|
6
5
|
* Cron Capability
|
|
7
6
|
*
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { GasConfig, WriteReportReply, WriteReportReplyJson } from '../../generated/capabilities/blockchain/evm/v1alpha/client_pb';
|
|
2
|
+
import type { ReportResponse } from '../../generated/sdk/v1alpha/sdk_pb';
|
|
3
|
+
import { type Abi, type AbiFunction, type Address } from 'viem';
|
|
4
|
+
import type { EvmMock } from './generated';
|
|
5
|
+
type ViewFunction = AbiFunction & {
|
|
6
|
+
stateMutability: 'view' | 'pure';
|
|
7
|
+
};
|
|
8
|
+
type ExtractViewFunctionNames<TAbi extends Abi> = Extract<TAbi[number], ViewFunction>['name'];
|
|
9
|
+
/**
|
|
10
|
+
* Strict version of {@link WriteReportRequest} where `report` and `gasConfig`
|
|
11
|
+
* are guaranteed to be present. Used by mock handlers so tests don't need
|
|
12
|
+
* to check for undefined.
|
|
13
|
+
*/
|
|
14
|
+
export interface WriteReportMockInput {
|
|
15
|
+
receiver: Uint8Array;
|
|
16
|
+
report: ReportResponse;
|
|
17
|
+
gasConfig: GasConfig;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A contract mock returned by {@link addContractMock}.
|
|
21
|
+
*
|
|
22
|
+
* Each view/pure function in the ABI becomes an optional property whose value
|
|
23
|
+
* is a handler function. When set, calls to that function on the mock EVM
|
|
24
|
+
* client are automatically routed, decoded, and re-encoded.
|
|
25
|
+
*
|
|
26
|
+
* A special `writeReport` property handles write-report calls to this
|
|
27
|
+
* contract's address. Its handler receives a {@link WriteReportMockInput}
|
|
28
|
+
* where `report` and `gasConfig` are guaranteed to be present.
|
|
29
|
+
*/
|
|
30
|
+
export type ContractMock<TAbi extends Abi> = {
|
|
31
|
+
[K in ExtractViewFunctionNames<TAbi>]?: (...args: readonly unknown[]) => unknown;
|
|
32
|
+
} & {
|
|
33
|
+
writeReport?: (input: WriteReportMockInput) => WriteReportReply | WriteReportReplyJson;
|
|
34
|
+
};
|
|
35
|
+
export interface AddContractMockOptions<TAbi extends Abi> {
|
|
36
|
+
address: Address;
|
|
37
|
+
abi: TAbi;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Registers a typed contract mock on an {@link EvmMock} instance.
|
|
41
|
+
*
|
|
42
|
+
* This is the TypeScript equivalent of Go's `evmmock.AddContractMock`.
|
|
43
|
+
* It intercepts `callContract` and `writeReport` on the provided mock,
|
|
44
|
+
* routing calls by contract address and ABI method selector.
|
|
45
|
+
*
|
|
46
|
+
* Multiple contracts can be mocked on the same `EvmMock` — each call to
|
|
47
|
+
* `addContractMock` chains with the previous handler.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const evmMock = EvmMock.testInstance(chainSelector);
|
|
52
|
+
*
|
|
53
|
+
* const balanceMock = addContractMock(evmMock, {
|
|
54
|
+
* address: "0x1234...",
|
|
55
|
+
* abi: BalanceReader,
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* balanceMock.getNativeBalances = (addresses) => {
|
|
59
|
+
* return [500000000000000000n];
|
|
60
|
+
* };
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @param evmMock - The `EvmMock` instance to attach to.
|
|
64
|
+
* @param options - Contract address and viem-compatible ABI.
|
|
65
|
+
* @returns A mock object with settable handler properties for each view/pure function.
|
|
66
|
+
*/
|
|
67
|
+
export declare function addContractMock<const TAbi extends Abi>(evmMock: EvmMock, options: AddContractMockOptions<TAbi>): ContractMock<TAbi>;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { decodeFunctionData, encodeFunctionResult, } from 'viem';
|
|
2
|
+
function bytesToHexAddress(bytes) {
|
|
3
|
+
return `0x${Array.from(bytes)
|
|
4
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
5
|
+
.join('')}`.toLowerCase();
|
|
6
|
+
}
|
|
7
|
+
function bytesToHex(bytes) {
|
|
8
|
+
return `0x${Array.from(bytes)
|
|
9
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
10
|
+
.join('')}`;
|
|
11
|
+
}
|
|
12
|
+
function hexToUint8Array(hex) {
|
|
13
|
+
const clean = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
14
|
+
const bytes = new Uint8Array(clean.length / 2);
|
|
15
|
+
for (let i = 0; i < clean.length; i += 2) {
|
|
16
|
+
bytes[i / 2] = Number.parseInt(clean.slice(i, i + 2), 16);
|
|
17
|
+
}
|
|
18
|
+
return bytes;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Registers a typed contract mock on an {@link EvmMock} instance.
|
|
22
|
+
*
|
|
23
|
+
* This is the TypeScript equivalent of Go's `evmmock.AddContractMock`.
|
|
24
|
+
* It intercepts `callContract` and `writeReport` on the provided mock,
|
|
25
|
+
* routing calls by contract address and ABI method selector.
|
|
26
|
+
*
|
|
27
|
+
* Multiple contracts can be mocked on the same `EvmMock` — each call to
|
|
28
|
+
* `addContractMock` chains with the previous handler.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const evmMock = EvmMock.testInstance(chainSelector);
|
|
33
|
+
*
|
|
34
|
+
* const balanceMock = addContractMock(evmMock, {
|
|
35
|
+
* address: "0x1234...",
|
|
36
|
+
* abi: BalanceReader,
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* balanceMock.getNativeBalances = (addresses) => {
|
|
40
|
+
* return [500000000000000000n];
|
|
41
|
+
* };
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param evmMock - The `EvmMock` instance to attach to.
|
|
45
|
+
* @param options - Contract address and viem-compatible ABI.
|
|
46
|
+
* @returns A mock object with settable handler properties for each view/pure function.
|
|
47
|
+
*/
|
|
48
|
+
export function addContractMock(evmMock, options) {
|
|
49
|
+
const mock = {};
|
|
50
|
+
const normalizedAddress = options.address.toLowerCase();
|
|
51
|
+
const previousCallContract = evmMock.callContract;
|
|
52
|
+
evmMock.callContract = (req) => {
|
|
53
|
+
const toBytes = req.call?.to;
|
|
54
|
+
if (!toBytes || bytesToHexAddress(toBytes) !== normalizedAddress) {
|
|
55
|
+
if (previousCallContract)
|
|
56
|
+
return previousCallContract(req);
|
|
57
|
+
throw new Error(`addContractMock: no mock registered for address ${toBytes ? bytesToHexAddress(toBytes) : '(empty)'}`);
|
|
58
|
+
}
|
|
59
|
+
const dataBytes = req.call?.data;
|
|
60
|
+
if (!dataBytes || dataBytes.length < 4) {
|
|
61
|
+
throw new Error('addContractMock: call data too short (need at least 4 bytes for selector)');
|
|
62
|
+
}
|
|
63
|
+
const callDataHex = bytesToHex(dataBytes);
|
|
64
|
+
let decoded;
|
|
65
|
+
try {
|
|
66
|
+
decoded = decodeFunctionData({
|
|
67
|
+
abi: options.abi,
|
|
68
|
+
data: callDataHex,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
if (previousCallContract)
|
|
73
|
+
return previousCallContract(req);
|
|
74
|
+
throw new Error(`addContractMock: failed to decode function data for ${options.address}: ${e instanceof Error ? e.message : e}`);
|
|
75
|
+
}
|
|
76
|
+
const handler = mock[decoded.functionName];
|
|
77
|
+
if (typeof handler !== 'function') {
|
|
78
|
+
throw new Error(`addContractMock: no handler set for ${decoded.functionName} on ${options.address}`);
|
|
79
|
+
}
|
|
80
|
+
const result = handler(...(decoded.args ?? []));
|
|
81
|
+
const encoded = encodeFunctionResult({
|
|
82
|
+
abi: options.abi,
|
|
83
|
+
functionName: decoded.functionName,
|
|
84
|
+
result: result,
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
data: hexToUint8Array(encoded),
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const previousWriteReport = evmMock.writeReport;
|
|
91
|
+
evmMock.writeReport = (req) => {
|
|
92
|
+
const receiverBytes = req.receiver;
|
|
93
|
+
if (!receiverBytes || bytesToHexAddress(receiverBytes) !== normalizedAddress) {
|
|
94
|
+
if (previousWriteReport)
|
|
95
|
+
return previousWriteReport(req);
|
|
96
|
+
throw new Error(`addContractMock: no writeReport mock registered for receiver ${receiverBytes ? bytesToHexAddress(receiverBytes) : '(empty)'}`);
|
|
97
|
+
}
|
|
98
|
+
if (typeof mock.writeReport !== 'function') {
|
|
99
|
+
throw new Error(`addContractMock: no writeReport handler set for ${options.address}`);
|
|
100
|
+
}
|
|
101
|
+
if (!req.report) {
|
|
102
|
+
throw new Error(`addContractMock: writeReport called without report for ${options.address}`);
|
|
103
|
+
}
|
|
104
|
+
if (!req.gasConfig) {
|
|
105
|
+
throw new Error(`addContractMock: writeReport called without gasConfig for ${options.address}`);
|
|
106
|
+
}
|
|
107
|
+
return mock.writeReport({
|
|
108
|
+
receiver: req.receiver,
|
|
109
|
+
report: req.report,
|
|
110
|
+
gasConfig: req.gasConfig,
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
return mock;
|
|
114
|
+
}
|
package/dist/sdk/test/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { type CapabilityHandler, DEFAULT_MAX_RESPONSE_SIZE_BYTES, getTestCapabilityHandler, type NewTestRuntimeOptions, newTestRuntime, REPORT_METADATA_HEADER_LENGTH, RESPONSE_BUFFER_TOO_SMALL, registerTestCapability, type Secrets, TestRuntime, type TestRuntimeState, test, } from '../testutils/test-runtime';
|
|
6
6
|
export { TestWriter } from '../testutils/test-writer';
|
|
7
|
+
export { type AddContractMockOptions, addContractMock, type ContractMock, type WriteReportMockInput, } from './evm-contract-mock';
|
|
7
8
|
export * from './generated';
|
package/dist/sdk/test/index.js
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { DEFAULT_MAX_RESPONSE_SIZE_BYTES, getTestCapabilityHandler, newTestRuntime, REPORT_METADATA_HEADER_LENGTH, RESPONSE_BUFFER_TOO_SMALL, registerTestCapability, TestRuntime, test, } from '../testutils/test-runtime';
|
|
6
6
|
export { TestWriter } from '../testutils/test-writer';
|
|
7
|
+
export { addContractMock, } from './evm-contract-mock';
|
|
7
8
|
export * from './generated';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainlink/cre-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@bufbuild/protobuf": "2.6.3",
|
|
62
62
|
"@bufbuild/protoc-gen-es": "2.6.3",
|
|
63
|
-
"@chainlink/cre-sdk-javy-plugin": "1.1.
|
|
63
|
+
"@chainlink/cre-sdk-javy-plugin": "1.1.1",
|
|
64
64
|
"@standard-schema/spec": "1.0.0",
|
|
65
65
|
"viem": "2.34.0",
|
|
66
66
|
"zod": "3.25.76"
|