Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor serialization debugging for IDL #16

Merged
merged 24 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hylo-fix"
version = "0.1.8"
version = "0.2.0"
edition = "2021"
description = "Fixed-point number types with Solana Anchor support"
authors = ["0xPlish <plish@hylo.so>", "Curtis McEnroe <programble@gmail.com>"]
Expand All @@ -14,10 +14,11 @@ categories = ["data-structures"]
name = "fix"

[features]
anchor = ["dep:anchor-lang"]
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = { version = "0.30.0", optional = true }
anchor-lang = "0.30"
anyhow = "1.0.82"
muldiv = "1.0.1"
num-traits = "0.2.17"
paste = "1.0.14"
Expand Down
23 changes: 0 additions & 23 deletions src/anchor.rs

This file was deleted.

100 changes: 100 additions & 0 deletions src/fix_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::typenum::{Integer, U10};
use crate::Fix;
use anchor_lang::prelude::{borsh, AnchorDeserialize, AnchorSerialize, InitSpace};
use paste::paste;

macro_rules! impl_fix_value {
($sign:ident, $bits:expr) => {
paste! {
/// A value-space `Fix` where base is always 10 and bits are a concrete type.
/// Intended for serialized storage in Solana accounts where generics won't work.
#[derive(PartialEq, Eq, Copy, Clone, Debug, AnchorSerialize, AnchorDeserialize, InitSpace)]
pub struct [<$sign FixValue $bits>] {
pub bits: [<$sign:lower $bits>],
pub exp: i8,
}

impl [<$sign FixValue $bits>] {
pub fn new(bits: [<$sign:lower $bits>], exp: i8) -> Self {
Self { bits, exp }
}
}

impl<Bits, Exp> From<Fix<Bits, U10, Exp>> for [<$sign FixValue $bits>]
where
Bits: Into<[<$sign:lower $bits>]>,
Exp: Integer,
{
fn from(fix: Fix<Bits, U10, Exp>) -> Self {
Self {
bits: fix.bits.into(),
exp: Exp::to_i8(),
}
}
}

impl<Bits, Exp> From<[<$sign FixValue $bits>]> for Fix<Bits, U10, Exp>
where
Bits: From<[<$sign:lower $bits>]>,
Exp: Integer,
{
fn from(value: [<$sign FixValue $bits>]) -> Fix<Bits, U10, Exp> {
Fix::new(value.bits.into())
}
}
}
};
}

impl_fix_value!(U, 8);
impl_fix_value!(U, 16);
impl_fix_value!(U, 32);
impl_fix_value!(U, 64);
impl_fix_value!(U, 128);
impl_fix_value!(I, 8);
impl_fix_value!(I, 16);
impl_fix_value!(I, 32);
impl_fix_value!(I, 64);
impl_fix_value!(I, 128);

#[cfg(test)]
mod tests {
use super::*;
use crate::aliases::si::Kilo;
use anyhow::Result;
use borsh::to_vec;

macro_rules! fix_value_tests {
($sign:ident, $bits:expr) => {
paste! {
#[test]
fn [<roundtrip_into_ $sign:lower $bits>]() -> Result<()> {
let start = Kilo::new([<69 $sign:lower $bits>]);
let there: [<$sign FixValue $bits>] = start.into();
let back: Kilo<[<$sign:lower $bits>]> = there.into();
assert_eq!(there, [<$sign FixValue $bits>]::new(69, 3));
Ok(assert_eq!(start, back))
}

#[test]
fn [<roundtrip_serialize_ $sign:lower $bits>]() -> Result<()> {
let start = [<$sign FixValue $bits>]::new(20, -2);
let bytes = to_vec(&start)?;
let back = AnchorDeserialize::deserialize(&mut bytes.as_slice())?;
Ok(assert_eq!(start, back))
}
}
};
}

fix_value_tests!(U, 8);
fix_value_tests!(U, 16);
fix_value_tests!(U, 32);
fix_value_tests!(U, 64);
fix_value_tests!(U, 128);
fix_value_tests!(I, 8);
fix_value_tests!(I, 16);
fix_value_tests!(I, 32);
fix_value_tests!(I, 64);
fix_value_tests!(I, 128);
}
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@
//!
//! This crate is `no_std`.

#![no_std]
//#![no_std]

pub extern crate muldiv;
pub extern crate num_traits;
pub extern crate typenum;

pub mod aliases;
#[cfg(feature = "anchor")]
pub mod anchor;
pub mod fix_value;
pub mod prelude;
pub mod util;

use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use core::fmt::{Debug, Error, Formatter};
Expand All @@ -80,8 +80,6 @@ use core::marker::PhantomData;
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};

#[cfg(feature = "anchor")]
use anchor_lang::prelude::{borsh, AnchorDeserialize, AnchorSerialize};
use muldiv::MulDiv;
use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub};
use paste::paste;
Expand Down Expand Up @@ -117,7 +115,6 @@ use typenum::type_operators::{Abs, IsLess};
/// - _(x B<sup>E</sup>) × y = (x × y) B<sup>E</sup>_
/// - _(x B<sup>E</sup>) ÷ y = (x ÷ y) B<sup>E</sup>_
/// - _(x B<sup>E</sup>) % y = (x % y) B<sup>E</sup>_
#[cfg_attr(feature = "anchor", derive(AnchorSerialize, AnchorDeserialize))]
pub struct Fix<Bits, Base, Exp> {
/// The underlying integer.
pub bits: Bits,
Expand Down
7 changes: 4 additions & 3 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[allow(unused)]
#[cfg(feature = "anchor")]
pub use crate::anchor::*;
pub use crate::aliases::decimal::*;
pub use crate::fix_value::*;
pub use crate::muldiv::MulDiv;
pub use crate::num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub};
pub use crate::typenum::{N1, N10, N11, N12, N2, N3, N4, N5, N6, N7, N8, N9};
pub use crate::util::*;
pub use crate::*;
25 changes: 25 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use typenum::NInt;

use crate::typenum::{IsLess, NonZero, Unsigned, U10, U20};
use crate::Fix;

/// Domain specific extensions to the `Fix` type as it's used in this project.
pub trait FixExt: Sized {
/// This precision's equivalent of 1.
fn one() -> Self;
fn zero() -> Self;
}

impl<Bits, Exp> FixExt for Fix<Bits, U10, NInt<Exp>>
where
Exp: Unsigned + NonZero + IsLess<U20>,
Bits: From<u64>,
{
fn one() -> Fix<Bits, U10, NInt<Exp>> {
Fix::new(U10::to_u64().pow(Exp::to_u32()).into())
}

fn zero() -> Self {
Fix::new(0.into())
}
}
Loading