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

Using PreciseNumber for underlaying calculations #2

Merged
merged 6 commits into from
Nov 8, 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
198 changes: 142 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ serde = { version = "^1.0", features = ["derive"] }
serde_with = "3.9.0"
shank = "0.4.2"
shank_idl = "0.4.2"
spl-math = { version = "0.3.0", features = ["no-entrypoint"] }
solana-account-decoder = "~1.18"
solana-cli-config = "~1.18"
solana-program = "~1.18"
Expand All @@ -68,14 +69,14 @@ jito-tip-router-client = { path = "./clients/rust/jito_tip_router", version = "0
jito-tip-router-core = { path = "./core", version = "=0.0.1" }
jito-tip-router-program = { path = "./program", version = "=0.0.1" }
jito-tip-router-shank-cli = { path = "./shank_cli", version = "=0.0.1" }
jito-bytemuck = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-account-traits-derive = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-jsm-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-restaking-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-restaking-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-restaking-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3", features = ["no-entrypoint"] }
jito-restaking-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-vault-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-vault-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-vault-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3", features = ["no-entrypoint"] }
jito-vault-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.3" }
jito-bytemuck = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-account-traits-derive = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-jsm-core = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-restaking-client = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-restaking-core = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-restaking-program = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d", features = ["no-entrypoint"] }
jito-restaking-sdk = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-vault-client = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-vault-core = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
jito-vault-program = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d", features = ["no-entrypoint"] }
jito-vault-sdk = { git = "https://github.com/jito-foundation/restaking.git", rev = "eaf88e7e5ca2845fe3108c4fc4a06a25f9a8514d" }
4 changes: 0 additions & 4 deletions clients/js/jito_tip_router/accounts/weightTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export type WeightTable = {
ncn: Address;
ncnEpoch: bigint;
slotCreated: bigint;
slotFinalized: bigint;
bump: number;
reserved: Array<number>;
table: Array<WeightEntry>;
Expand All @@ -57,7 +56,6 @@ export type WeightTableArgs = {
ncn: Address;
ncnEpoch: number | bigint;
slotCreated: number | bigint;
slotFinalized: number | bigint;
bump: number;
reserved: Array<number>;
table: Array<WeightEntryArgs>;
Expand All @@ -69,7 +67,6 @@ export function getWeightTableEncoder(): Encoder<WeightTableArgs> {
['ncn', getAddressEncoder()],
['ncnEpoch', getU64Encoder()],
['slotCreated', getU64Encoder()],
['slotFinalized', getU64Encoder()],
['bump', getU8Encoder()],
['reserved', getArrayEncoder(getU8Encoder(), { size: 128 })],
['table', getArrayEncoder(getWeightEntryEncoder(), { size: 32 })],
Expand All @@ -82,7 +79,6 @@ export function getWeightTableDecoder(): Decoder<WeightTable> {
['ncn', getAddressDecoder()],
['ncnEpoch', getU64Decoder()],
['slotCreated', getU64Decoder()],
['slotFinalized', getU64Decoder()],
['bump', getU8Decoder()],
['reserved', getArrayDecoder(getU8Decoder(), { size: 128 })],
['table', getArrayDecoder(getWeightEntryDecoder(), { size: 32 })],
Expand Down
42 changes: 37 additions & 5 deletions clients/js/jito_tip_router/errors/jitoTipRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,34 @@ import {
} from '@solana/web3.js';
import { JITO_TIP_ROUTER_PROGRAM_ADDRESS } from '../programs';

/** NoMoreTableSlots: No more table slots available */
export const JITO_TIP_ROUTER_ERROR__NO_MORE_TABLE_SLOTS = 0x2000; // 8192
/** DenominatorIsZero: Zero in the denominator */
export const JITO_TIP_ROUTER_ERROR__DENOMINATOR_IS_ZERO = 0x2100; // 8448
/** ArithmeticOverflow: Overflow */
export const JITO_TIP_ROUTER_ERROR__ARITHMETIC_OVERFLOW = 0x2101; // 8449
/** ModuloOverflow: Modulo Overflow */
export const JITO_TIP_ROUTER_ERROR__MODULO_OVERFLOW = 0x2102; // 8450
/** NewPreciseNumberError: New precise number error */
export const JITO_TIP_ROUTER_ERROR__NEW_PRECISE_NUMBER_ERROR = 0x2103; // 8451
/** CastToImpreciseNumberError: Cast to imprecise number error */
export const JITO_TIP_ROUTER_ERROR__CAST_TO_IMPRECISE_NUMBER_ERROR = 0x2104; // 8452
/** IncorrectWeightTableAdmin: Incorrect weight table admin */
export const JITO_TIP_ROUTER_ERROR__INCORRECT_WEIGHT_TABLE_ADMIN = 0x2200; // 8704
/** DuplicateMintsInTable: Duplicate mints in table */
export const JITO_TIP_ROUTER_ERROR__DUPLICATE_MINTS_IN_TABLE = 0x2201; // 8705
/** NoMintsInTable: There are no mints in the table */
export const JITO_TIP_ROUTER_ERROR__NO_MINTS_IN_TABLE = 0x2202; // 8706
/** TooManyMintsForTable: Too many mints for table */
export const JITO_TIP_ROUTER_ERROR__TOO_MANY_MINTS_FOR_TABLE = 0x2203; // 8707
/** WeightTableAlreadyInitialized: Weight table already initialized */
export const JITO_TIP_ROUTER_ERROR__WEIGHT_TABLE_ALREADY_INITIALIZED = 0x2204; // 8708
/** CannotCreateFutureWeightTables: Cannnot create future weight tables */
export const JITO_TIP_ROUTER_ERROR__CANNOT_CREATE_FUTURE_WEIGHT_TABLES = 0x2201; // 8705
export const JITO_TIP_ROUTER_ERROR__CANNOT_CREATE_FUTURE_WEIGHT_TABLES = 0x2205; // 8709
/** WeightMintsDoNotMatchLength: Weight mints do not match - length */
export const JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_LENGTH = 0x2206; // 8710
/** WeightMintsDoNotMatchMintHash: Weight mints do not match - mint hash */
export const JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_MINT_HASH = 0x2207; // 8711
/** InvalidMintForWeightTable: Invalid mint for weight table */
export const JITO_TIP_ROUTER_ERROR__INVALID_MINT_FOR_WEIGHT_TABLE = 0x2208; // 8712
/** FeeCapExceeded: Fee cap exceeded */
export const JITO_TIP_ROUTER_ERROR__FEE_CAP_EXCEEDED = 0x2300; // 8960
/** IncorrectNcnAdmin: Incorrect NCN Admin */
Expand All @@ -38,28 +54,44 @@ export const JITO_TIP_ROUTER_ERROR__INCORRECT_FEE_ADMIN = 0x2402; // 9218
export type JitoTipRouterError =
| typeof JITO_TIP_ROUTER_ERROR__ARITHMETIC_OVERFLOW
| typeof JITO_TIP_ROUTER_ERROR__CANNOT_CREATE_FUTURE_WEIGHT_TABLES
| typeof JITO_TIP_ROUTER_ERROR__CAST_TO_IMPRECISE_NUMBER_ERROR
| typeof JITO_TIP_ROUTER_ERROR__DENOMINATOR_IS_ZERO
| typeof JITO_TIP_ROUTER_ERROR__DUPLICATE_MINTS_IN_TABLE
| typeof JITO_TIP_ROUTER_ERROR__FEE_CAP_EXCEEDED
| typeof JITO_TIP_ROUTER_ERROR__INCORRECT_FEE_ADMIN
| typeof JITO_TIP_ROUTER_ERROR__INCORRECT_NCN
| typeof JITO_TIP_ROUTER_ERROR__INCORRECT_NCN_ADMIN
| typeof JITO_TIP_ROUTER_ERROR__INCORRECT_WEIGHT_TABLE_ADMIN
| typeof JITO_TIP_ROUTER_ERROR__INVALID_MINT_FOR_WEIGHT_TABLE
| typeof JITO_TIP_ROUTER_ERROR__MODULO_OVERFLOW
| typeof JITO_TIP_ROUTER_ERROR__NO_MORE_TABLE_SLOTS;
| typeof JITO_TIP_ROUTER_ERROR__NEW_PRECISE_NUMBER_ERROR
| typeof JITO_TIP_ROUTER_ERROR__NO_MINTS_IN_TABLE
| typeof JITO_TIP_ROUTER_ERROR__TOO_MANY_MINTS_FOR_TABLE
| typeof JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_LENGTH
| typeof JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_MINT_HASH
| typeof JITO_TIP_ROUTER_ERROR__WEIGHT_TABLE_ALREADY_INITIALIZED;

let jitoTipRouterErrorMessages: Record<JitoTipRouterError, string> | undefined;
if (process.env.NODE_ENV !== 'production') {
jitoTipRouterErrorMessages = {
[JITO_TIP_ROUTER_ERROR__ARITHMETIC_OVERFLOW]: `Overflow`,
[JITO_TIP_ROUTER_ERROR__CANNOT_CREATE_FUTURE_WEIGHT_TABLES]: `Cannnot create future weight tables`,
[JITO_TIP_ROUTER_ERROR__CAST_TO_IMPRECISE_NUMBER_ERROR]: `Cast to imprecise number error`,
[JITO_TIP_ROUTER_ERROR__DENOMINATOR_IS_ZERO]: `Zero in the denominator`,
[JITO_TIP_ROUTER_ERROR__DUPLICATE_MINTS_IN_TABLE]: `Duplicate mints in table`,
[JITO_TIP_ROUTER_ERROR__FEE_CAP_EXCEEDED]: `Fee cap exceeded`,
[JITO_TIP_ROUTER_ERROR__INCORRECT_FEE_ADMIN]: `Incorrect fee admin`,
[JITO_TIP_ROUTER_ERROR__INCORRECT_NCN]: `Incorrect NCN`,
[JITO_TIP_ROUTER_ERROR__INCORRECT_NCN_ADMIN]: `Incorrect NCN Admin`,
[JITO_TIP_ROUTER_ERROR__INCORRECT_WEIGHT_TABLE_ADMIN]: `Incorrect weight table admin`,
[JITO_TIP_ROUTER_ERROR__INVALID_MINT_FOR_WEIGHT_TABLE]: `Invalid mint for weight table`,
[JITO_TIP_ROUTER_ERROR__MODULO_OVERFLOW]: `Modulo Overflow`,
[JITO_TIP_ROUTER_ERROR__NO_MORE_TABLE_SLOTS]: `No more table slots available`,
[JITO_TIP_ROUTER_ERROR__NEW_PRECISE_NUMBER_ERROR]: `New precise number error`,
[JITO_TIP_ROUTER_ERROR__NO_MINTS_IN_TABLE]: `There are no mints in the table`,
[JITO_TIP_ROUTER_ERROR__TOO_MANY_MINTS_FOR_TABLE]: `Too many mints for table`,
[JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_LENGTH]: `Weight mints do not match - length`,
[JITO_TIP_ROUTER_ERROR__WEIGHT_MINTS_DO_NOT_MATCH_MINT_HASH]: `Weight mints do not match - mint hash`,
[JITO_TIP_ROUTER_ERROR__WEIGHT_TABLE_ALREADY_INITIALIZED]: `Weight table already initialized`,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
combineCodec,
getStructDecoder,
getStructEncoder,
getU128Decoder,
getU128Encoder,
getU64Decoder,
getU64Encoder,
getU8Decoder,
Expand All @@ -32,17 +34,18 @@ import {
import { JITO_TIP_ROUTER_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const FINALIZE_WEIGHT_TABLE_DISCRIMINATOR = 3;
export const ADMIN_UPDATE_WEIGHT_TABLE_DISCRIMINATOR = 4;

export function getFinalizeWeightTableDiscriminatorBytes() {
return getU8Encoder().encode(FINALIZE_WEIGHT_TABLE_DISCRIMINATOR);
export function getAdminUpdateWeightTableDiscriminatorBytes() {
return getU8Encoder().encode(ADMIN_UPDATE_WEIGHT_TABLE_DISCRIMINATOR);
}

export type FinalizeWeightTableInstruction<
export type AdminUpdateWeightTableInstruction<
TProgram extends string = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
TAccountNcn extends string | IAccountMeta<string> = string,
TAccountWeightTable extends string | IAccountMeta<string> = string,
TAccountWeightTableAdmin extends string | IAccountMeta<string> = string,
TAccountMint extends string | IAccountMeta<string> = string,
TAccountRestakingProgramId extends string | IAccountMeta<string> = string,
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
Expand All @@ -57,84 +60,97 @@ export type FinalizeWeightTableInstruction<
? ReadonlySignerAccount<TAccountWeightTableAdmin> &
IAccountSignerMeta<TAccountWeightTableAdmin>
: TAccountWeightTableAdmin,
TAccountMint extends string
? ReadonlyAccount<TAccountMint>
: TAccountMint,
TAccountRestakingProgramId extends string
? ReadonlyAccount<TAccountRestakingProgramId>
: TAccountRestakingProgramId,
...TRemainingAccounts,
]
>;

export type FinalizeWeightTableInstructionData = {
export type AdminUpdateWeightTableInstructionData = {
discriminator: number;
ncnEpoch: bigint;
weight: bigint;
};

export type FinalizeWeightTableInstructionDataArgs = {
export type AdminUpdateWeightTableInstructionDataArgs = {
ncnEpoch: number | bigint;
weight: number | bigint;
};

export function getFinalizeWeightTableInstructionDataEncoder(): Encoder<FinalizeWeightTableInstructionDataArgs> {
export function getAdminUpdateWeightTableInstructionDataEncoder(): Encoder<AdminUpdateWeightTableInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['ncnEpoch', getU64Encoder()],
['weight', getU128Encoder()],
]),
(value) => ({
...value,
discriminator: FINALIZE_WEIGHT_TABLE_DISCRIMINATOR,
discriminator: ADMIN_UPDATE_WEIGHT_TABLE_DISCRIMINATOR,
})
);
}

export function getFinalizeWeightTableInstructionDataDecoder(): Decoder<FinalizeWeightTableInstructionData> {
export function getAdminUpdateWeightTableInstructionDataDecoder(): Decoder<AdminUpdateWeightTableInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['ncnEpoch', getU64Decoder()],
['weight', getU128Decoder()],
]);
}

export function getFinalizeWeightTableInstructionDataCodec(): Codec<
FinalizeWeightTableInstructionDataArgs,
FinalizeWeightTableInstructionData
export function getAdminUpdateWeightTableInstructionDataCodec(): Codec<
AdminUpdateWeightTableInstructionDataArgs,
AdminUpdateWeightTableInstructionData
> {
return combineCodec(
getFinalizeWeightTableInstructionDataEncoder(),
getFinalizeWeightTableInstructionDataDecoder()
getAdminUpdateWeightTableInstructionDataEncoder(),
getAdminUpdateWeightTableInstructionDataDecoder()
);
}

export type FinalizeWeightTableInput<
export type AdminUpdateWeightTableInput<
TAccountNcn extends string = string,
TAccountWeightTable extends string = string,
TAccountWeightTableAdmin extends string = string,
TAccountMint extends string = string,
TAccountRestakingProgramId extends string = string,
> = {
ncn: Address<TAccountNcn>;
weightTable: Address<TAccountWeightTable>;
weightTableAdmin: TransactionSigner<TAccountWeightTableAdmin>;
mint: Address<TAccountMint>;
restakingProgramId: Address<TAccountRestakingProgramId>;
ncnEpoch: FinalizeWeightTableInstructionDataArgs['ncnEpoch'];
ncnEpoch: AdminUpdateWeightTableInstructionDataArgs['ncnEpoch'];
weight: AdminUpdateWeightTableInstructionDataArgs['weight'];
};

export function getFinalizeWeightTableInstruction<
export function getAdminUpdateWeightTableInstruction<
TAccountNcn extends string,
TAccountWeightTable extends string,
TAccountWeightTableAdmin extends string,
TAccountMint extends string,
TAccountRestakingProgramId extends string,
TProgramAddress extends Address = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
>(
input: FinalizeWeightTableInput<
input: AdminUpdateWeightTableInput<
TAccountNcn,
TAccountWeightTable,
TAccountWeightTableAdmin,
TAccountMint,
TAccountRestakingProgramId
>,
config?: { programAddress?: TProgramAddress }
): FinalizeWeightTableInstruction<
): AdminUpdateWeightTableInstruction<
TProgramAddress,
TAccountNcn,
TAccountWeightTable,
TAccountWeightTableAdmin,
TAccountMint,
TAccountRestakingProgramId
> {
// Program address.
Expand All @@ -149,6 +165,7 @@ export function getFinalizeWeightTableInstruction<
value: input.weightTableAdmin ?? null,
isWritable: false,
},
mint: { value: input.mint ?? null, isWritable: false },
restakingProgramId: {
value: input.restakingProgramId ?? null,
isWritable: false,
Expand All @@ -168,24 +185,26 @@ export function getFinalizeWeightTableInstruction<
getAccountMeta(accounts.ncn),
getAccountMeta(accounts.weightTable),
getAccountMeta(accounts.weightTableAdmin),
getAccountMeta(accounts.mint),
getAccountMeta(accounts.restakingProgramId),
],
programAddress,
data: getFinalizeWeightTableInstructionDataEncoder().encode(
args as FinalizeWeightTableInstructionDataArgs
data: getAdminUpdateWeightTableInstructionDataEncoder().encode(
args as AdminUpdateWeightTableInstructionDataArgs
),
} as FinalizeWeightTableInstruction<
} as AdminUpdateWeightTableInstruction<
TProgramAddress,
TAccountNcn,
TAccountWeightTable,
TAccountWeightTableAdmin,
TAccountMint,
TAccountRestakingProgramId
>;

return instruction;
}

export type ParsedFinalizeWeightTableInstruction<
export type ParsedAdminUpdateWeightTableInstruction<
TProgram extends string = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
Expand All @@ -194,20 +213,21 @@ export type ParsedFinalizeWeightTableInstruction<
ncn: TAccountMetas[0];
weightTable: TAccountMetas[1];
weightTableAdmin: TAccountMetas[2];
restakingProgramId: TAccountMetas[3];
mint: TAccountMetas[3];
restakingProgramId: TAccountMetas[4];
};
data: FinalizeWeightTableInstructionData;
data: AdminUpdateWeightTableInstructionData;
};

export function parseFinalizeWeightTableInstruction<
export function parseAdminUpdateWeightTableInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedFinalizeWeightTableInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 4) {
): ParsedAdminUpdateWeightTableInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 5) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
Expand All @@ -223,9 +243,10 @@ export function parseFinalizeWeightTableInstruction<
ncn: getNextAccount(),
weightTable: getNextAccount(),
weightTableAdmin: getNextAccount(),
mint: getNextAccount(),
restakingProgramId: getNextAccount(),
},
data: getFinalizeWeightTableInstructionDataDecoder().decode(
data: getAdminUpdateWeightTableInstructionDataDecoder().decode(
instruction.data
),
};
Expand Down
5 changes: 2 additions & 3 deletions clients/js/jito_tip_router/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* @see https://github.com/kinobi-so/kinobi
*/

export * from './finalizeWeightTable';
export * from './initializeConfig';
export * from './adminUpdateWeightTable';
export * from './initializeNCNConfig';
export * from './initializeWeightTable';
export * from './setConfigFees';
export * from './setNewAdmin';
export * from './updateWeightTable';
Loading
Loading