|
| 1 | +import { create, fromJson } from '@bufbuild/protobuf' |
| 2 | +import { |
| 3 | + type AccountMeta, |
| 4 | + type AccountMetaJson, |
| 5 | + AccountMetaSchema, |
| 6 | + type ComputeConfig, |
| 7 | + type ComputeConfigJson, |
| 8 | + ComputeConfigSchema, |
| 9 | + type WriteReportReply, |
| 10 | + WriteReportReplySchema, |
| 11 | + type WriteReportRequest, |
| 12 | + type WriteReportRequestJson, |
| 13 | + WriteReportRequestSchema, |
| 14 | +} from '@cre/generated/capabilities/blockchain/solana/v1alpha/client_pb' |
| 15 | +import { |
| 16 | + type ReportResponse, |
| 17 | + type ReportResponseJson, |
| 18 | + ReportResponseSchema, |
| 19 | +} from '@cre/generated/sdk/v1alpha/sdk_pb' |
| 20 | +import type { Runtime } from '@cre/sdk' |
| 21 | +import { Report } from '@cre/sdk/report' |
| 22 | +import { hexToBytes } from '@cre/sdk/utils/hex-utils' |
| 23 | + |
| 24 | +export type WriteCreReportRequest = { |
| 25 | + remainingAccounts: AccountMeta[] |
| 26 | + receiver: Uint8Array |
| 27 | + computeConfig?: ComputeConfig |
| 28 | + report?: Report |
| 29 | + $report: true |
| 30 | +} |
| 31 | + |
| 32 | +export type WriteCreReportRequestJson = { |
| 33 | + remainingAccounts: AccountMetaJson[] |
| 34 | + receiver: string |
| 35 | + computeConfig?: ComputeConfigJson |
| 36 | + report?: Report |
| 37 | +} |
| 38 | + |
| 39 | +export function x_generatedCodeOnly_wrap_WriteCreReportRequest( |
| 40 | + input: WriteReportRequest, |
| 41 | +): WriteCreReportRequest { |
| 42 | + return { |
| 43 | + remainingAccounts: input.remainingAccounts, |
| 44 | + receiver: input.receiver, |
| 45 | + computeConfig: input.computeConfig, |
| 46 | + report: input.report !== undefined ? new Report(input.report) : undefined, |
| 47 | + $report: true, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function createWriteCreReportRequest( |
| 52 | + input: WriteCreReportRequestJson, |
| 53 | +): WriteCreReportRequest { |
| 54 | + return { |
| 55 | + remainingAccounts: (input.remainingAccounts ?? []).map((v) => fromJson(AccountMetaSchema, v)), |
| 56 | + receiver: hexToBytes(input.receiver), |
| 57 | + computeConfig: |
| 58 | + input.computeConfig !== undefined |
| 59 | + ? fromJson(ComputeConfigSchema, input.computeConfig) |
| 60 | + : undefined, |
| 61 | + report: input.report, |
| 62 | + $report: true, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export function x_generatedCodeOnly_unwrap_WriteCreReportRequest( |
| 67 | + input: WriteCreReportRequest, |
| 68 | +): WriteReportRequest { |
| 69 | + return create(WriteReportRequestSchema, { |
| 70 | + remainingAccounts: input.remainingAccounts, |
| 71 | + receiver: input.receiver, |
| 72 | + computeConfig: input.computeConfig, |
| 73 | + report: input.report !== undefined ? input.report.x_generatedCodeOnly_unwrap() : undefined, |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Client Capability |
| 79 | + * |
| 80 | + * Capability ID: solana@1.0.0 |
| 81 | + * Capability Name: solana |
| 82 | + * Capability Version: 1.0.0 |
| 83 | + */ |
| 84 | +export class ClientCapability { |
| 85 | + /** The capability ID for this service */ |
| 86 | + static readonly CAPABILITY_ID = 'solana@1.0.0' |
| 87 | + |
| 88 | + static readonly CAPABILITY_NAME = 'solana' |
| 89 | + static readonly CAPABILITY_VERSION = '1.0.0' |
| 90 | + |
| 91 | + /** Available ChainSelector values */ |
| 92 | + static readonly SUPPORTED_CHAIN_SELECTORS = { |
| 93 | + 'solana-devnet': 16423721717087811551n, |
| 94 | + } as const |
| 95 | + |
| 96 | + constructor(private readonly ChainSelector: bigint) {} |
| 97 | + |
| 98 | + writeReport( |
| 99 | + runtime: Runtime<unknown>, |
| 100 | + input: WriteCreReportRequest | WriteCreReportRequestJson, |
| 101 | + ): { result: () => WriteReportReply } { |
| 102 | + // Handle input conversion - unwrap if it's a wrapped type, convert from JSON if needed |
| 103 | + let payload: WriteReportRequest |
| 104 | + |
| 105 | + // Check if it's a wrapped type by looking for the $report property |
| 106 | + if ((input as unknown as { $report?: boolean }).$report) { |
| 107 | + // It's a wrapped type, unwrap it |
| 108 | + payload = x_generatedCodeOnly_unwrap_WriteCreReportRequest(input as WriteCreReportRequest) |
| 109 | + } else { |
| 110 | + // It's wrapped JSON, convert using create function |
| 111 | + payload = x_generatedCodeOnly_unwrap_WriteCreReportRequest( |
| 112 | + createWriteCreReportRequest(input as WriteCreReportRequestJson), |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + // Include all labels in capability ID for routing when specified |
| 117 | + const capabilityId = `${ClientCapability.CAPABILITY_NAME}:ChainSelector:${this.ChainSelector}@${ClientCapability.CAPABILITY_VERSION}` |
| 118 | + |
| 119 | + const capabilityResponse = runtime.callCapability<WriteReportRequest, WriteReportReply>({ |
| 120 | + capabilityId, |
| 121 | + method: 'WriteReport', |
| 122 | + payload, |
| 123 | + inputSchema: WriteReportRequestSchema, |
| 124 | + outputSchema: WriteReportReplySchema, |
| 125 | + }) |
| 126 | + |
| 127 | + return { |
| 128 | + result: () => { |
| 129 | + const result = capabilityResponse.result() |
| 130 | + |
| 131 | + return result |
| 132 | + }, |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments