Skip to content

Commit

Permalink
clients: Add client generation (#18)
Browse files Browse the repository at this point in the history
* Use individual dependencies

* Rearrange dependencies

* Add client generation

* Tweak formatting

* Add shank dependency

* Update codama configuration

* Bump codama version

* Update generated clients

* Add trait overrides

* Add stake account

* Update generated clients

* Fix account removal

* Rename stake state account

* Fix name conflict

* Add caret

* Fix typo

* Remove shank dependency

* Remove skip formatting
  • Loading branch information
febo authored Dec 5, 2024
1 parent eef358e commit e3f9f20
Show file tree
Hide file tree
Showing 78 changed files with 6,453 additions and 2,273 deletions.
6 changes: 3 additions & 3 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
},
"license": "MIT",
"peerDependencies": {
"@solana/web3.js": "2.0.0-rc.1"
"@solana/web3.js": "^2.0.0"
},
"devDependencies": {
"@ava/typescript": "^4.1.0",
"@solana/eslint-config-solana": "^3.0.3",
"@solana/web3.js": "2.0.0-rc.1",
"@solana/webcrypto-ed25519-polyfill": "2.0.0-rc.1",
"@solana/web3.js": "^2.0.0",
"@solana/webcrypto-ed25519-polyfill": "^2.0.0",
"@types/node": "^20",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
Expand Down
581 changes: 318 additions & 263 deletions clients/js/pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions clients/js/src/generated/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

export * from './stakeStateAccount';
119 changes: 119 additions & 0 deletions clients/js/src/generated/accounts/stakeStateAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

import {
assertAccountExists,
assertAccountsExist,
combineCodec,
decodeAccount,
fetchEncodedAccount,
fetchEncodedAccounts,
getStructDecoder,
getStructEncoder,
type Account,
type Address,
type Codec,
type Decoder,
type EncodedAccount,
type Encoder,
type FetchAccountConfig,
type FetchAccountsConfig,
type MaybeAccount,
type MaybeEncodedAccount,
} from '@solana/web3.js';
import {
getStakeStateV2Decoder,
getStakeStateV2Encoder,
type StakeStateV2,
type StakeStateV2Args,
} from '../types';

export type StakeStateAccount = { state: StakeStateV2 };

export type StakeStateAccountArgs = { state: StakeStateV2Args };

export function getStakeStateAccountEncoder(): Encoder<StakeStateAccountArgs> {
return getStructEncoder([['state', getStakeStateV2Encoder()]]);
}

export function getStakeStateAccountDecoder(): Decoder<StakeStateAccount> {
return getStructDecoder([['state', getStakeStateV2Decoder()]]);
}

export function getStakeStateAccountCodec(): Codec<
StakeStateAccountArgs,
StakeStateAccount
> {
return combineCodec(
getStakeStateAccountEncoder(),
getStakeStateAccountDecoder()
);
}

export function decodeStakeStateAccount<TAddress extends string = string>(
encodedAccount: EncodedAccount<TAddress>
): Account<StakeStateAccount, TAddress>;
export function decodeStakeStateAccount<TAddress extends string = string>(
encodedAccount: MaybeEncodedAccount<TAddress>
): MaybeAccount<StakeStateAccount, TAddress>;
export function decodeStakeStateAccount<TAddress extends string = string>(
encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>
):
| Account<StakeStateAccount, TAddress>
| MaybeAccount<StakeStateAccount, TAddress> {
return decodeAccount(
encodedAccount as MaybeEncodedAccount<TAddress>,
getStakeStateAccountDecoder()
);
}

export async function fetchStakeStateAccount<TAddress extends string = string>(
rpc: Parameters<typeof fetchEncodedAccount>[0],
address: Address<TAddress>,
config?: FetchAccountConfig
): Promise<Account<StakeStateAccount, TAddress>> {
const maybeAccount = await fetchMaybeStakeStateAccount(rpc, address, config);
assertAccountExists(maybeAccount);
return maybeAccount;
}

export async function fetchMaybeStakeStateAccount<
TAddress extends string = string,
>(
rpc: Parameters<typeof fetchEncodedAccount>[0],
address: Address<TAddress>,
config?: FetchAccountConfig
): Promise<MaybeAccount<StakeStateAccount, TAddress>> {
const maybeAccount = await fetchEncodedAccount(rpc, address, config);
return decodeStakeStateAccount(maybeAccount);
}

export async function fetchAllStakeStateAccount(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig
): Promise<Account<StakeStateAccount>[]> {
const maybeAccounts = await fetchAllMaybeStakeStateAccount(
rpc,
addresses,
config
);
assertAccountsExist(maybeAccounts);
return maybeAccounts;
}

export async function fetchAllMaybeStakeStateAccount(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig
): Promise<MaybeAccount<StakeStateAccount>[]> {
const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
return maybeAccounts.map((maybeAccount) =>
decodeStakeStateAccount(maybeAccount)
);
}
9 changes: 9 additions & 0 deletions clients/js/src/generated/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

export * from './stake';
115 changes: 115 additions & 0 deletions clients/js/src/generated/errors/stake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

import {
isProgramError,
type Address,
type SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM,
type SolanaError,
} from '@solana/web3.js';
import { STAKE_PROGRAM_ADDRESS } from '../programs';

export const STAKE_ERROR__NO_CREDITS_TO_REDEEM = 0x0; // 0

export const STAKE_ERROR__LOCKUP_IN_FORCE = 0x1; // 1

export const STAKE_ERROR__ALREADY_DEACTIVATED = 0x2; // 2

export const STAKE_ERROR__TOO_SOON_TO_REDELEGATE = 0x3; // 3

export const STAKE_ERROR__INSUFFICIENT_STAKE = 0x4; // 4

export const STAKE_ERROR__MERGE_TRANSIENT_STAKE = 0x5; // 5

export const STAKE_ERROR__MERGE_MISMATCH = 0x6; // 6

export const STAKE_ERROR__CUSTODIAN_MISSING = 0x7; // 7

export const STAKE_ERROR__CUSTODIAN_SIGNATURE_MISSING = 0x8; // 8

export const STAKE_ERROR__INSUFFICIENT_REFERENCE_VOTES = 0x9; // 9

export const STAKE_ERROR__VOTE_ADDRESS_MISMATCH = 0xa; // 10

export const STAKE_ERROR__MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION_NOT_MET = 0xb; // 11

export const STAKE_ERROR__INSUFFICIENT_DELEGATION = 0xc; // 12

export const STAKE_ERROR__REDELEGATE_TRANSIENT_OR_INACTIVE_STAKE = 0xd; // 13

export const STAKE_ERROR__REDELEGATE_TO_SAME_VOTE_ACCOUNT = 0xe; // 14

export const STAKE_ERROR__REDELEGATED_STAKE_MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED = 0xf; // 15

export const STAKE_ERROR__EPOCH_REWARDS_ACTIVE = 0x10; // 16

export type StakeError =
| typeof STAKE_ERROR__ALREADY_DEACTIVATED
| typeof STAKE_ERROR__CUSTODIAN_MISSING
| typeof STAKE_ERROR__CUSTODIAN_SIGNATURE_MISSING
| typeof STAKE_ERROR__EPOCH_REWARDS_ACTIVE
| typeof STAKE_ERROR__INSUFFICIENT_DELEGATION
| typeof STAKE_ERROR__INSUFFICIENT_REFERENCE_VOTES
| typeof STAKE_ERROR__INSUFFICIENT_STAKE
| typeof STAKE_ERROR__LOCKUP_IN_FORCE
| typeof STAKE_ERROR__MERGE_MISMATCH
| typeof STAKE_ERROR__MERGE_TRANSIENT_STAKE
| typeof STAKE_ERROR__MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION_NOT_MET
| typeof STAKE_ERROR__NO_CREDITS_TO_REDEEM
| typeof STAKE_ERROR__REDELEGATED_STAKE_MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED
| typeof STAKE_ERROR__REDELEGATE_TO_SAME_VOTE_ACCOUNT
| typeof STAKE_ERROR__REDELEGATE_TRANSIENT_OR_INACTIVE_STAKE
| typeof STAKE_ERROR__TOO_SOON_TO_REDELEGATE
| typeof STAKE_ERROR__VOTE_ADDRESS_MISMATCH;

let stakeErrorMessages: Record<StakeError, string> | undefined;
if (process.env.NODE_ENV !== 'production') {
stakeErrorMessages = {
[STAKE_ERROR__ALREADY_DEACTIVATED]: `Stake already deactivated`,
[STAKE_ERROR__CUSTODIAN_MISSING]: `Custodian address not present`,
[STAKE_ERROR__CUSTODIAN_SIGNATURE_MISSING]: `Custodian signature not present`,
[STAKE_ERROR__EPOCH_REWARDS_ACTIVE]: `Stake action is not permitted while the epoch rewards period is active`,
[STAKE_ERROR__INSUFFICIENT_DELEGATION]: `Delegation amount is less than the minimum`,
[STAKE_ERROR__INSUFFICIENT_REFERENCE_VOTES]: `Insufficient voting activity in the reference vote account`,
[STAKE_ERROR__INSUFFICIENT_STAKE]: `Split amount is more than is staked`,
[STAKE_ERROR__LOCKUP_IN_FORCE]: `Lockup has not yet expired`,
[STAKE_ERROR__MERGE_MISMATCH]: `Stake account merge failed due to different authority, lockups or state`,
[STAKE_ERROR__MERGE_TRANSIENT_STAKE]: `Stake account with transient stake cannot be merged`,
[STAKE_ERROR__MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION_NOT_MET]: `Stake account has not been delinquent for the minimum epochs required for deactivation`,
[STAKE_ERROR__NO_CREDITS_TO_REDEEM]: `Not enough credits to redeem`,
[STAKE_ERROR__REDELEGATED_STAKE_MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED]: `Redelegated stake must be fully activated before deactivation`,
[STAKE_ERROR__REDELEGATE_TO_SAME_VOTE_ACCOUNT]: `Stake redelegation to the same vote account is not permitted`,
[STAKE_ERROR__REDELEGATE_TRANSIENT_OR_INACTIVE_STAKE]: `Stake account with transient or inactive stake cannot be redelegated`,
[STAKE_ERROR__TOO_SOON_TO_REDELEGATE]: `One re-delegation permitted per epoch`,
[STAKE_ERROR__VOTE_ADDRESS_MISMATCH]: `Stake account is not delegated to the provided vote account`,
};
}

export function getStakeErrorMessage(code: StakeError): string {
if (process.env.NODE_ENV !== 'production') {
return (stakeErrorMessages as Record<StakeError, string>)[code];
}

return 'Error message not available in production bundles.';
}

export function isStakeError<TProgramErrorCode extends StakeError>(
error: unknown,
transactionMessage: {
instructions: Record<number, { programAddress: Address }>;
},
code?: TProgramErrorCode
): error is SolanaError<typeof SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM> &
Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> {
return isProgramError<TProgramErrorCode>(
error,
transactionMessage,
STAKE_PROGRAM_ADDRESS,
code
);
}
2 changes: 2 additions & 0 deletions clients/js/src/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @see https://github.com/codama-idl/codama
*/

export * from './accounts';
export * from './errors';
export * from './instructions';
export * from './programs';
export * from './types';
Loading

0 comments on commit e3f9f20

Please sign in to comment.