Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Nov 6, 2024
1 parent 3a7f258 commit 736e293
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 62 deletions.
4 changes: 3 additions & 1 deletion clients/js/jito_tip_router/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
* @see https://github.com/kinobi-so/kinobi
*/

export * from './jitoNumber';
export * from './preciseNumberWrapper';
export * from './preciseTokenSupply';
export * from './preciseWeight';
export * from './weightEntry';
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ import {
type Encoder,
} from '@solana/web3.js';

export type JitoNumber = { value: Array<number> };
export type PreciseNumberWrapper = { value: Array<number> };

export type JitoNumberArgs = JitoNumber;
export type PreciseNumberWrapperArgs = PreciseNumberWrapper;

export function getJitoNumberEncoder(): Encoder<JitoNumberArgs> {
export function getPreciseNumberWrapperEncoder(): Encoder<PreciseNumberWrapperArgs> {
return getStructEncoder([
['value', getArrayEncoder(getU8Encoder(), { size: 16 })],
]);
}

export function getJitoNumberDecoder(): Decoder<JitoNumber> {
export function getPreciseNumberWrapperDecoder(): Decoder<PreciseNumberWrapper> {
return getStructDecoder([
['value', getArrayDecoder(getU8Decoder(), { size: 16 })],
]);
}

export function getJitoNumberCodec(): Codec<JitoNumberArgs, JitoNumber> {
return combineCodec(getJitoNumberEncoder(), getJitoNumberDecoder());
export function getPreciseNumberWrapperCodec(): Codec<
PreciseNumberWrapperArgs,
PreciseNumberWrapper
> {
return combineCodec(
getPreciseNumberWrapperEncoder(),
getPreciseNumberWrapperDecoder()
);
}
44 changes: 44 additions & 0 deletions clients/js/jito_tip_router/types/preciseTokenSupply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/kinobi-so/kinobi
*/

import {
combineCodec,
getStructDecoder,
getStructEncoder,
type Codec,
type Decoder,
type Encoder,
} from '@solana/web3.js';
import {
getPreciseNumberWrapperDecoder,
getPreciseNumberWrapperEncoder,
type PreciseNumberWrapper,
type PreciseNumberWrapperArgs,
} from '.';

export type PreciseTokenSupply = { number: PreciseNumberWrapper };

export type PreciseTokenSupplyArgs = { number: PreciseNumberWrapperArgs };

export function getPreciseTokenSupplyEncoder(): Encoder<PreciseTokenSupplyArgs> {
return getStructEncoder([['number', getPreciseNumberWrapperEncoder()]]);
}

export function getPreciseTokenSupplyDecoder(): Decoder<PreciseTokenSupply> {
return getStructDecoder([['number', getPreciseNumberWrapperDecoder()]]);
}

export function getPreciseTokenSupplyCodec(): Codec<
PreciseTokenSupplyArgs,
PreciseTokenSupply
> {
return combineCodec(
getPreciseTokenSupplyEncoder(),
getPreciseTokenSupplyDecoder()
);
}
41 changes: 41 additions & 0 deletions clients/js/jito_tip_router/types/preciseWeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/kinobi-so/kinobi
*/

import {
combineCodec,
getStructDecoder,
getStructEncoder,
type Codec,
type Decoder,
type Encoder,
} from '@solana/web3.js';
import {
getPreciseNumberWrapperDecoder,
getPreciseNumberWrapperEncoder,
type PreciseNumberWrapper,
type PreciseNumberWrapperArgs,
} from '.';

export type PreciseWeight = { number: PreciseNumberWrapper };

export type PreciseWeightArgs = { number: PreciseNumberWrapperArgs };

export function getPreciseWeightEncoder(): Encoder<PreciseWeightArgs> {
return getStructEncoder([['number', getPreciseNumberWrapperEncoder()]]);
}

export function getPreciseWeightDecoder(): Decoder<PreciseWeight> {
return getStructDecoder([['number', getPreciseNumberWrapperDecoder()]]);
}

export function getPreciseWeightCodec(): Codec<
PreciseWeightArgs,
PreciseWeight
> {
return combineCodec(getPreciseWeightEncoder(), getPreciseWeightDecoder());
}
16 changes: 8 additions & 8 deletions clients/js/jito_tip_router/types/weightEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ import {
type Encoder,
} from '@solana/web3.js';
import {
getJitoNumberDecoder,
getJitoNumberEncoder,
type JitoNumber,
type JitoNumberArgs,
getPreciseWeightDecoder,
getPreciseWeightEncoder,
type PreciseWeight,
type PreciseWeightArgs,
} from '.';

export type WeightEntry = { mint: Address; weight: JitoNumber };
export type WeightEntry = { mint: Address; weight: PreciseWeight };

export type WeightEntryArgs = { mint: Address; weight: JitoNumberArgs };
export type WeightEntryArgs = { mint: Address; weight: PreciseWeightArgs };

export function getWeightEntryEncoder(): Encoder<WeightEntryArgs> {
return getStructEncoder([
['mint', getAddressEncoder()],
['weight', getJitoNumberEncoder()],
['weight', getPreciseWeightEncoder()],
]);
}

export function getWeightEntryDecoder(): Decoder<WeightEntry> {
return getStructDecoder([
['mint', getAddressDecoder()],
['weight', getJitoNumberDecoder()],
['weight', getPreciseWeightDecoder()],
]);
}

Expand Down
8 changes: 6 additions & 2 deletions clients/rust/jito_tip_router/src/generated/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
//!
//! <https://github.com/kinobi-so/kinobi>
pub(crate) mod r#jito_number;
pub(crate) mod r#precise_number_wrapper;
pub(crate) mod r#precise_token_supply;
pub(crate) mod r#precise_weight;
pub(crate) mod r#weight_entry;

pub use self::{r#jito_number::*, r#weight_entry::*};
pub use self::{
r#precise_number_wrapper::*, r#precise_token_supply::*, r#precise_weight::*, r#weight_entry::*,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ use borsh::{BorshDeserialize, BorshSerialize};

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JitoNumber {
pub struct PreciseNumberWrapper {
pub value: [u8; 16],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! <https://github.com/kinobi-so/kinobi>
use borsh::{BorshDeserialize, BorshSerialize};

use crate::generated::types::PreciseNumberWrapper;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PreciseTokenSupply {
pub number: PreciseNumberWrapper,
}
15 changes: 15 additions & 0 deletions clients/rust/jito_tip_router/src/generated/types/precise_weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! <https://github.com/kinobi-so/kinobi>
use borsh::{BorshDeserialize, BorshSerialize};

use crate::generated::types::PreciseNumberWrapper;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PreciseWeight {
pub number: PreciseNumberWrapper,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;

use crate::generated::types::JitoNumber;
use crate::generated::types::PreciseWeight;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -17,5 +17,5 @@ pub struct WeightEntry {
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub mint: Pubkey,
pub weight: JitoNumber,
pub weight: PreciseWeight,
}
3 changes: 1 addition & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod discriminators;
pub mod error;
pub mod instruction;
// pub mod depreciated_weight;
pub mod jito_number;
pub mod precise_numbers;
pub mod weight_table;
86 changes: 55 additions & 31 deletions core/src/jito_number.rs → core/src/precise_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,85 @@ use spl_math::precise_number::PreciseNumber;

use crate::error::TipRouterError;

// Weights are stored as the number of tokens
// All arithmetic should be done with PreciseNumber, and cast down to the appropriate types only until all
// calculations are complete.

#[derive(Default, Debug, Clone, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct JitoNumber {
pub struct PreciseNumberWrapper {
value: [u8; 16], // 128
}

impl JitoNumber {
pub const PRECISION_FACTOR: u128 = 1_000_000_000;

const fn _from_u128(value: u128) -> Self {
impl PreciseNumberWrapper {
const fn from_u128(value: u128) -> Self {
let value = value.to_le_bytes();
Self { value }
}

const fn _to_u128(&self) -> u128 {
const fn to_u128(&self) -> u128 {

Check failure on line 22 in core/src/precise_numbers.rs

View workflow job for this annotation

GitHub Actions / lint

methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
u128::from_le_bytes(self.value)
}

fn _to_precise_number(&self) -> Result<PreciseNumber, TipRouterError> {
PreciseNumber::new(self._to_u128()).ok_or(TipRouterError::NewPreciseNumberError)
fn to_precise_number(&self) -> Result<PreciseNumber, TipRouterError> {

Check failure on line 26 in core/src/precise_numbers.rs

View workflow job for this annotation

GitHub Actions / lint

methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
PreciseNumber::new(self.to_u128()).ok_or(TipRouterError::NewPreciseNumberError)
}

fn _from_precise_number(precise_number: PreciseNumber) -> Result<Self, TipRouterError> {
fn from_precise_number(precise_number: PreciseNumber) -> Result<Self, TipRouterError> {
let value = precise_number
.to_imprecise()
.ok_or(TipRouterError::CastToImpreciseNumberError)?;

Ok(Self::_from_u128(value))
Ok(Self::from_u128(value))
}
}

#[derive(Default, Debug, Clone, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct PreciseWeight {
number: PreciseNumberWrapper, // 128
}

impl PreciseWeight {
pub const fn from_weight(weight: u128) -> Self {
let number = PreciseNumberWrapper::from_u128(weight);
Self { number }
}

pub const fn to_weight(&self) -> u128 {
self.number.to_u128()
}

pub fn weight_to_precise_number(&self) -> Result<PreciseNumber, TipRouterError> {
self.number.to_precise_number()
}

pub fn precise_number_to_weight(precise_number: PreciseNumber) -> Result<Self, TipRouterError> {
let number = PreciseNumberWrapper::from_precise_number(precise_number)?;
Ok(Self { number })
}
}

#[derive(Default, Debug, Clone, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct PreciseTokenSupply {
number: PreciseNumberWrapper, // 128
}

impl PreciseTokenSupply {
pub const PRECISION_FACTOR: u128 = 1_000_000_000;

fn from_token_supply(token_supply: u64) -> Result<Self, TipRouterError> {
let value = (token_supply as u128)
.checked_mul(Self::PRECISION_FACTOR)
.ok_or(TipRouterError::ArithmeticOverflow)?;

Ok(Self::_from_u128(value))
let number = PreciseNumberWrapper::from_u128(value);

Ok(Self { number })
}

fn to_token_supply(self) -> Result<u64, TipRouterError> {
let value = self._to_u128();
let value = self.number.to_u128();
let token_supply = value
.checked_div(Self::PRECISION_FACTOR)
.ok_or(TipRouterError::ArithmeticOverflow)?;
Expand All @@ -58,29 +96,15 @@ impl JitoNumber {
token_supply: u64,
) -> Result<PreciseNumber, TipRouterError> {
let value = Self::from_token_supply(token_supply)?;
value._to_precise_number()

value.number.to_precise_number()
}

pub fn precise_number_to_token_supply(
precise_number: PreciseNumber,
) -> Result<u64, TipRouterError> {
let value = Self::_from_precise_number(precise_number)?;
let number = PreciseNumberWrapper::from_precise_number(precise_number)?;
let value = Self { number };
value.to_token_supply()
}

pub const fn from_weight(weight: u128) -> Self {
Self::_from_u128(weight)
}

pub const fn to_weight(&self) -> u128 {
self._to_u128()
}

pub fn weight_to_precise_number(&self) -> Result<PreciseNumber, TipRouterError> {
self._to_precise_number()
}

pub fn precise_number_to_weight(precise_number: PreciseNumber) -> Result<Self, TipRouterError> {
Self::_from_precise_number(precise_number)
}
}
Loading

0 comments on commit 736e293

Please sign in to comment.