import { combineCodec, FixedSizeCodec, getI128Decoder, getI128Encoder, getI32Decoder, getI32Encoder, getI64Decoder, getI64Encoder, getU16Decoder, getU16Encoder, getU32Decoder, getU32Encoder, getU64Decoder, getU64Encoder, transformCodec, } from '@solana/kit'; import { FixedPoint } from './FixedPoint.js'; type FixedSpec = { fractionalBits: number; totalBits: number; signed: boolean; }; type FixedArg = FixedPoint | number | bigint; // Type aliases for generated code export type U32F32 = FixedPoint; export type U32F32Args = FixedArg; export type U16F48 = FixedPoint; export type U16F48Args = FixedArg; export type U8F56 = FixedPoint; export type U8F56Args = FixedArg; export type U16F16 = FixedPoint; export type U16F16Args = FixedArg; export type U26F6 = FixedPoint; export type U26F6Args = FixedArg; export type I32F32 = FixedPoint; export type I32F32Args = FixedArg; export type I8F56 = FixedPoint; export type I8F56Args = FixedArg; export type U54F10 = FixedPoint; export type U54F10Args = FixedArg; export type I54F10 = FixedPoint; export type I54F10Args = FixedArg; export type U0F32 = FixedPoint; export type U0F32Args = FixedArg; export type U18F14 = FixedPoint; export type U18F14Args = FixedArg; export type U8F8 = FixedPoint; export type U8F8Args = FixedArg; export type I16F16 = FixedPoint; export type I16F16Args = FixedArg; export type I72F56 = FixedPoint; export type I72F56Args = FixedArg; // Codec helpers const getU16Codec = (): FixedSizeCodec => combineCodec(getU16Encoder(), getU16Decoder()) as FixedSizeCodec< number | bigint >; const getU32Codec = (): FixedSizeCodec => combineCodec(getU32Encoder(), getU32Decoder()) as FixedSizeCodec< number | bigint >; const getU64Codec = (): FixedSizeCodec => combineCodec(getU64Encoder(), getU64Decoder()) as FixedSizeCodec< number | bigint >; const getI32Codec = (): FixedSizeCodec => combineCodec(getI32Encoder(), getI32Decoder()) as FixedSizeCodec< number | bigint >; const getI64Codec = (): FixedSizeCodec => combineCodec(getI64Encoder(), getI64Decoder()) as FixedSizeCodec< number | bigint >; const getI128Codec = (): FixedSizeCodec => combineCodec(getI128Encoder(), getI128Decoder()) as FixedSizeCodec; const createFixedCodec = ( backingCodec: FixedSizeCodec, spec: FixedSpec, ): FixedSizeCodec => transformCodec( backingCodec, (value: FixedArg) => { if (value instanceof FixedPoint) return value.raw as T; if (typeof value === 'bigint') { return FixedPoint.fromRaw(value, spec.fractionalBits).raw as T; } return FixedPoint.fromNumber(value, spec).raw as T; }, (raw: T) => FixedPoint.fromRaw(BigInt(raw), spec.fractionalBits), ) as FixedSizeCodec; // Fixed-point specifications const SPECS = { U8F8: { fractionalBits: 8, totalBits: 16, signed: false } satisfies FixedSpec, U16F16: { fractionalBits: 16, totalBits: 32, signed: false, } satisfies FixedSpec, U18F14: { fractionalBits: 14, totalBits: 32, signed: false, } satisfies FixedSpec, U26F6: { fractionalBits: 6, totalBits: 32, signed: false, } satisfies FixedSpec, U32F32: { fractionalBits: 32, totalBits: 64, signed: false, } satisfies FixedSpec, U16F48: { fractionalBits: 48, totalBits: 64, signed: false, } satisfies FixedSpec, U8F56: { fractionalBits: 56, totalBits: 64, signed: false, } satisfies FixedSpec, U0F32: { fractionalBits: 32, totalBits: 32, signed: false, } satisfies FixedSpec, U54F10: { fractionalBits: 10, totalBits: 64, signed: false, } satisfies FixedSpec, I16F16: { fractionalBits: 16, totalBits: 32, signed: true, } satisfies FixedSpec, I32F32: { fractionalBits: 32, totalBits: 64, signed: true, } satisfies FixedSpec, I8F56: { fractionalBits: 56, totalBits: 64, signed: true, } satisfies FixedSpec, I54F10: { fractionalBits: 10, totalBits: 64, signed: true, } satisfies FixedSpec, I72F56: { fractionalBits: 56, totalBits: 128, signed: true, } satisfies FixedSpec, }; // Unsigned codecs export const getU8F8Codec = () => createFixedCodec(getU16Codec(), SPECS.U8F8); export const getU16F16Codec = () => createFixedCodec(getU32Codec(), SPECS.U16F16); export const getU18F14Codec = () => createFixedCodec(getU32Codec(), SPECS.U18F14); export const getU26F6Codec = () => createFixedCodec(getU32Codec(), SPECS.U26F6); export const getU32F32Codec = () => createFixedCodec(getU64Codec(), SPECS.U32F32); export const getU16F48Codec = () => createFixedCodec(getU64Codec(), SPECS.U16F48); export const getU8F56Codec = () => createFixedCodec(getU64Codec(), SPECS.U8F56); export const getU0F32Codec = () => createFixedCodec(getU32Codec(), SPECS.U0F32); export const getU54F10Codec = () => createFixedCodec(getU64Codec(), SPECS.U54F10); // Signed codecs export const getI16F16Codec = () => createFixedCodec(getI32Codec(), SPECS.I16F16); export const getI32F32Codec = () => createFixedCodec(getI64Codec(), SPECS.I32F32); export const getI8F56Codec = () => createFixedCodec(getI64Codec(), SPECS.I8F56); export const getI54F10Codec = () => createFixedCodec(getI64Codec(), SPECS.I54F10); export const getI72F56Codec = () => createFixedCodec(getI128Codec(), SPECS.I72F56); // Encoder/Decoder aliases (Codama expects getXxxEncoder and getXxxDecoder) export const getU32F32Encoder = getU32F32Codec; export const getU32F32Decoder = getU32F32Codec; export const getU16F48Encoder = getU16F48Codec; export const getU16F48Decoder = getU16F48Codec; export const getU8F56Encoder = getU8F56Codec; export const getU8F56Decoder = getU8F56Codec; export const getU16F16Encoder = getU16F16Codec; export const getU16F16Decoder = getU16F16Codec; export const getU26F6Encoder = getU26F6Codec; export const getU26F6Decoder = getU26F6Codec; export const getI32F32Encoder = getI32F32Codec; export const getI32F32Decoder = getI32F32Codec; export const getI8F56Encoder = getI8F56Codec; export const getI8F56Decoder = getI8F56Codec; export const getU0F32Encoder = getU0F32Codec; export const getU0F32Decoder = getU0F32Codec; export const getU18F14Encoder = getU18F14Codec; export const getU18F14Decoder = getU18F14Codec; export const getU8F8Encoder = getU8F8Codec; export const getU8F8Decoder = getU8F8Codec; export const getU54F10Encoder = getU54F10Codec; export const getU54F10Decoder = getU54F10Codec; export const getI54F10Encoder = getI54F10Codec; export const getI54F10Decoder = getI54F10Codec; export const getI16F16Encoder = getI16F16Codec; export const getI16F16Decoder = getI16F16Codec; export const getI72F56Encoder = getI72F56Codec; export const getI72F56Decoder = getI72F56Codec;