/** 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' export type accounts = ExtractAbiFunction export type operatorApprovals = ExtractAbiFunction export type getRail = ExtractAbiFunction export type getRailsForPayerAndToken = ExtractAbiFunction export type getRailsForPayeeAndToken = ExtractAbiFunction export type settleRail = ExtractAbiFunction export type settleTerminatedRailWithoutValidation = ExtractAbiFunction< typeof CONTRACT_ABIS.PAYMENTS, 'settleTerminatedRailWithoutValidation' > export interface PaymentsOptions { accounts?: (args: AbiToType) => AbiToType operatorApprovals?: (args: AbiToType) => AbiToType getRail?: (args: AbiToType) => AbiToType getRailsForPayerAndToken?: ( args: AbiToType ) => AbiToType getRailsForPayeeAndToken?: ( args: AbiToType ) => AbiToType settleRail?: (args: AbiToType) => AbiToType settleTerminatedRailWithoutValidation?: ( args: AbiToType ) => AbiToType NETWORK_FEE?: () => bigint } /** * Handle payments contract calls */ export function paymentsCallHandler(data: Hex, options: JSONRPCOptions): Hex { // Check for NETWORK_FEE constant (function selector: 0x9be5c024) - constants are accessed as functions but may not be in ABI if (data.startsWith('0x9be5c024')) { if (!options.payments?.NETWORK_FEE) { throw new Error('Payments: NETWORK_FEE is not defined') } const fee = options.payments.NETWORK_FEE() return encodeAbiParameters([{ type: 'uint256' }], [fee]) } const { functionName, args } = decodeFunctionData({ abi: CONTRACT_ABIS.PAYMENTS, data: data as Hex, }) if (options.debug) { console.debug('Payments: calling function', functionName, 'with args', args) } switch (functionName) { case 'operatorApprovals': { if (!options.payments?.operatorApprovals) { throw new Error('Payments: operatorApprovals is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'operatorApprovals')!.outputs, options.payments.operatorApprovals(args) ) } case 'accounts': { if (!options.payments?.accounts) { throw new Error('Payments: accounts is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'accounts')!.outputs, options.payments.accounts(args) ) } case 'getRail': { if (!options.payments?.getRail) { throw new Error('Payments: getRail is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'getRail')!.outputs, options.payments.getRail(args) ) } case 'getRailsForPayerAndToken': { if (!options.payments?.getRailsForPayerAndToken) { throw new Error('Payments: getRailsForPayerAndToken is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'getRailsForPayerAndToken')! .outputs, options.payments.getRailsForPayerAndToken(args) ) } case 'getRailsForPayeeAndToken': { if (!options.payments?.getRailsForPayeeAndToken) { throw new Error('Payments: getRailsForPayeeAndToken is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'getRailsForPayeeAndToken')! .outputs, options.payments.getRailsForPayeeAndToken(args) ) } case 'settleRail': { if (!options.payments?.settleRail) { throw new Error('Payments: settleRail is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find((abi) => abi.type === 'function' && abi.name === 'settleRail')!.outputs, options.payments.settleRail(args) ) } case 'settleTerminatedRailWithoutValidation': { if (!options.payments?.settleTerminatedRailWithoutValidation) { throw new Error('Payments: settleTerminatedRailWithoutValidation is not defined') } return encodeAbiParameters( CONTRACT_ABIS.PAYMENTS.find( (abi) => abi.type === 'function' && abi.name === 'settleTerminatedRailWithoutValidation' )!.outputs, options.payments.settleTerminatedRailWithoutValidation(args) ) } default: { throw new Error(`Payments: unknown function: ${functionName} with args: ${args}`) } } }