fermat-solana 0.1.1

Solana/Anchor integration for fermat-core (Borsh, SPL tokens)
Documentation

fermat-solana

Solana/Anchor integration for fermat-core.

Provides Borsh serialization, SPL token amount conversions, and Anchor account helpers for the Decimal type.

crates.io docs.rs License: MIT OR Apache-2.0

Features

  • DecimalBorsh — 17-byte on-chain encoding (i128 LE + u8 scale), validates scale <= 28 on deserialization
  • SPL token conversionstoken_amount_to_decimal, decimal_to_token_amount with explicit rounding
  • Anchor account helpersDECIMAL_SPACE = 17, DecimalBorsh::zero_with_scale for account init

Installation

[dependencies]
fermat-core   = "0.1"
fermat-solana = "0.1"

Quick Start

use fermat_core::RoundingMode;
use fermat_solana::token::{token_amount_to_decimal, decimal_to_token_amount};

// 1_500_000 raw USDC lamports -> 1.500000 Decimal
let price = token_amount_to_decimal(1_500_000u64, 6)?;

// Convert back — round down on withdrawal (conservative)
let raw = decimal_to_token_amount(price, 6, RoundingMode::Down)?;
assert_eq!(raw, 1_500_000u64);

Anchor Account Usage

use fermat_solana::{DecimalBorsh, DECIMAL_SPACE};

#[account]
pub struct PriceOracle {
    pub authority:  Pubkey,        // 32 bytes
    pub price:      DecimalBorsh,  // 17 bytes
    pub confidence: DecimalBorsh,  // 17 bytes
    pub bump:       u8,            //  1 byte
}

impl PriceOracle {
    pub const SPACE: usize = 8 + 32 + DECIMAL_SPACE + DECIMAL_SPACE + 1;
}

Modules

Module Contents
borsh_impl DecimalBorsh wrapper — 17-byte encoding, adversarial scale validation
token token_amount_to_decimal, decimal_to_token_amount, align_to_mint
account DECIMAL_SPACE = 17, DecimalBorsh::zero_with_scale for Anchor init

See Also

License

Licensed under either MIT or Apache-2.0 at your option.

Copyright 2026 XXIX Labs