/** biome-ignore-all lint/style/noNonNullAssertion: testing */ import type { ExtractAbiFunction } from 'abitype' import { decodeFunctionData, encodeAbiParameters, type Hex } from 'viem' import { CONTRACT_ABIS } from '../../../utils/constants.ts' import type { AbiToType, JSONRPCOptions } from './types.ts' /** * Warm Storage View ABI types */ export type isProviderApproved = ExtractAbiFunction export type railToDataSet = ExtractAbiFunction export type getClientDataSets = ExtractAbiFunction export type clientDataSets = ExtractAbiFunction export type getDataSet = ExtractAbiFunction export type getApprovedProviders = ExtractAbiFunction export type getAllDataSetMetadata = ExtractAbiFunction export type getDataSetMetadata = ExtractAbiFunction export type getAllPieceMetadata = ExtractAbiFunction export type getPieceMetadata = ExtractAbiFunction export type clientNonces = ExtractAbiFunction export type getMaxProvingPeriod = ExtractAbiFunction export type challengeWindow = ExtractAbiFunction export interface WarmStorageViewOptions { isProviderApproved?: (args: AbiToType) => AbiToType getClientDataSets?: (args: AbiToType) => AbiToType clientDataSets?: (args: AbiToType) => AbiToType getDataSet?: (args: AbiToType) => AbiToType railToDataSet?: (args: AbiToType) => AbiToType getApprovedProviders?: (args: AbiToType) => AbiToType getAllDataSetMetadata?: ( args: AbiToType ) => AbiToType getDataSetMetadata?: (args: AbiToType) => AbiToType getAllPieceMetadata?: (args: AbiToType) => AbiToType getPieceMetadata?: (args: AbiToType) => AbiToType clientNonces?: (args: AbiToType) => AbiToType getMaxProvingPeriod?: (args: AbiToType) => AbiToType challengeWindow?: (args: AbiToType) => AbiToType } /** * Warm Storage ABI types */ export type pdpVerifierAddress = ExtractAbiFunction export type paymentsContractAddress = ExtractAbiFunction export type usdfcTokenAddress = ExtractAbiFunction export type filBeamBeneficiaryAddress = ExtractAbiFunction< typeof CONTRACT_ABIS.WARM_STORAGE, 'filBeamBeneficiaryAddress' > export type viewContractAddress = ExtractAbiFunction export type serviceProviderRegistry = ExtractAbiFunction export type sessionKeyRegistry = ExtractAbiFunction export type getServicePrice = ExtractAbiFunction export type owner = ExtractAbiFunction export interface WarmStorageOptions { pdpVerifierAddress?: (args: AbiToType) => AbiToType paymentsContractAddress?: ( args: AbiToType ) => AbiToType usdfcTokenAddress?: (args: AbiToType) => AbiToType filBeamBeneficiaryAddress?: ( args: AbiToType ) => AbiToType viewContractAddress?: (args: AbiToType) => AbiToType serviceProviderRegistry?: ( args: AbiToType ) => AbiToType sessionKeyRegistry?: (args: AbiToType) => AbiToType getServicePrice?: (args: AbiToType) => AbiToType owner?: (args: AbiToType) => AbiToType } /** * Handle warm storage calls */ export function warmStorageCallHandler(data: Hex, options: JSONRPCOptions): Hex { const { functionName, args } = decodeFunctionData({ abi: CONTRACT_ABIS.WARM_STORAGE, data: data as Hex, }) if (options.debug) { console.debug('Warm Storage: calling function', functionName, 'with args', args) } switch (functionName) { case 'pdpVerifierAddress': { if (!options.warmStorage?.pdpVerifierAddress) { throw new Error('Warm Storage: pdpVerifierAddress is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.pdpVerifierAddress(args) ) } case 'paymentsContractAddress': { if (!options.warmStorage?.paymentsContractAddress) { throw new Error('Warm Storage: paymentsContractAddress is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.paymentsContractAddress(args) ) } case 'usdfcTokenAddress': { if (!options.warmStorage?.usdfcTokenAddress) { throw new Error('Warm Storage: usdfcTokenAddress is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.usdfcTokenAddress(args) ) } case 'filBeamBeneficiaryAddress': { if (!options.warmStorage?.filBeamBeneficiaryAddress) { throw new Error('Warm Storage: filBeamBeneficiaryAddress is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.filBeamBeneficiaryAddress(args) ) } case 'viewContractAddress': { if (!options.warmStorage?.viewContractAddress) { throw new Error('Warm Storage: viewContractAddress is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.viewContractAddress(args) ) } case 'serviceProviderRegistry': { if (!options.warmStorage?.serviceProviderRegistry) { throw new Error('Warm Storage: serviceProviderRegistry is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.serviceProviderRegistry(args) ) } case 'sessionKeyRegistry': { if (!options.warmStorage?.sessionKeyRegistry) { throw new Error('Warm Storage: sessionKeyRegistry is not defined') } return encodeAbiParameters( [{ name: '', internalType: 'address', type: 'address' }], options.warmStorage.sessionKeyRegistry(args) ) } case 'getServicePrice': { if (!options.warmStorage?.getServicePrice) { throw new Error('Warm Storage: getServicePrice is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE.find((abi) => abi.type === 'function' && abi.name === 'getServicePrice')!.outputs, options.warmStorage.getServicePrice(args) ) } case 'owner': { if (!options.warmStorage?.owner) { throw new Error('Warm Storage: owner is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE.find((abi) => abi.type === 'function' && abi.name === 'owner')!.outputs, options.warmStorage.owner(args) ) } default: { throw new Error(`Warm Storage: unknown function: ${functionName} with args: ${args}`) } } } /** * Handle warm storage calls */ export function warmStorageViewCallHandler(data: Hex, options: JSONRPCOptions): Hex { const { functionName, args } = decodeFunctionData({ abi: CONTRACT_ABIS.WARM_STORAGE_VIEW, data: data as Hex, }) if (options.debug) { console.debug('Warm Storage View: calling function', functionName, 'with args', args) } switch (functionName) { case 'isProviderApproved': { if (!options.warmStorageView?.isProviderApproved) { throw new Error('Warm Storage View: isProviderApproved is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'isProviderApproved')! .outputs, options.warmStorageView.isProviderApproved(args) ) } case 'getClientDataSets': { if (!options.warmStorageView?.getClientDataSets) { throw new Error('Warm Storage View: getClientDataSets is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getClientDataSets')! .outputs, options.warmStorageView.getClientDataSets(args) ) } case 'clientDataSets': { if (!options.warmStorageView?.clientDataSets) { throw new Error('Warm Storage View: clientDataSets is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'clientDataSets')! .outputs, options.warmStorageView.clientDataSets(args) ) } case 'getDataSet': { if (!options.warmStorageView?.getDataSet) { throw new Error('Warm Storage View: getDataSet is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getDataSet')!.outputs, options.warmStorageView.getDataSet(args) ) } case 'railToDataSet': { if (!options.warmStorageView?.railToDataSet) { throw new Error('Warm Storage View: railToDataSet is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'railToDataSet')!.outputs, options.warmStorageView.railToDataSet(args) ) } case 'getApprovedProviders': { if (!options.warmStorageView?.getApprovedProviders) { throw new Error('Warm Storage View: getApprovedProviders is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getApprovedProviders')! .outputs, options.warmStorageView.getApprovedProviders(args) ) } case 'getAllDataSetMetadata': { if (!options.warmStorageView?.getAllDataSetMetadata) { throw new Error('Warm Storage View: getAllDataSetMetadata is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getAllDataSetMetadata')! .outputs, options.warmStorageView.getAllDataSetMetadata(args) ) } case 'getDataSetMetadata': { if (!options.warmStorageView?.getDataSetMetadata) { throw new Error('Warm Storage View: getDataSetMetadata is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getDataSetMetadata')! .outputs, options.warmStorageView.getDataSetMetadata(args) ) } case 'getAllPieceMetadata': { if (!options.warmStorageView?.getAllPieceMetadata) { throw new Error('Warm Storage View: getAllPieceMetadata is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getAllPieceMetadata')! .outputs, options.warmStorageView.getAllPieceMetadata(args) ) } case 'getPieceMetadata': { if (!options.warmStorageView?.getPieceMetadata) { throw new Error('Warm Storage View: getPieceMetadata is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getPieceMetadata')! .outputs, options.warmStorageView.getPieceMetadata(args) ) } case 'clientNonces': { if (!options.warmStorageView?.clientNonces) { throw new Error('Warm Storage View: clientNonces is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'clientNonces')!.outputs, options.warmStorageView.clientNonces(args) ) } case 'getMaxProvingPeriod': { if (!options.warmStorageView?.getMaxProvingPeriod) { throw new Error('Warm Storage View: getMaxProvingPeriod is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'getMaxProvingPeriod')! .outputs, options.warmStorageView.getMaxProvingPeriod(args) ) } case 'challengeWindow': { if (!options.warmStorageView?.challengeWindow) { throw new Error('Warm Storage View: challengeWindow is not defined') } return encodeAbiParameters( CONTRACT_ABIS.WARM_STORAGE_VIEW.find((abi) => abi.type === 'function' && abi.name === 'challengeWindow')! .outputs, options.warmStorageView.challengeWindow(args) ) } default: { throw new Error(`Warm Storage View: unknown function: ${functionName} with args: ${args}`) } } }