Skip to content

Commit

Permalink
update client generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Jun 24, 2024
1 parent 7492317 commit 88e82b1
Show file tree
Hide file tree
Showing 33 changed files with 4,367 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 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
*/

export * from './programAccount';
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* 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 {
assertAccountExists,
assertAccountsExist,
combineCodec,
decodeAccount,
fetchEncodedAccount,
fetchEncodedAccounts,
getAddressDecoder,
getAddressEncoder,
getStructDecoder,
getStructEncoder,
transformEncoder,
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 { ProgramAccountSeeds, findProgramAccountPda } from '../pdas';
import { Key, getKeyDecoder, getKeyEncoder } from '../types';

export type ProgramAccount = {
key: Key;
authority: Address;
productMint: Address;
};

export type ProgramAccountArgs = { authority: Address; productMint: Address };

export function getProgramAccountEncoder(): Encoder<ProgramAccountArgs> {
return transformEncoder(
getStructEncoder([
['key', getKeyEncoder()],
['authority', getAddressEncoder()],
['productMint', getAddressEncoder()],
]),
(value) => ({ ...value, key: Key.ProgramAccount })
);
}

export function getProgramAccountDecoder(): Decoder<ProgramAccount> {
return getStructDecoder([
['key', getKeyDecoder()],
['authority', getAddressDecoder()],
['productMint', getAddressDecoder()],
]);
}

export function getProgramAccountCodec(): Codec<
ProgramAccountArgs,
ProgramAccount
> {
return combineCodec(getProgramAccountEncoder(), getProgramAccountDecoder());
}

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

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

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

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

export async function fetchAllMaybeProgramAccount(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig
): Promise<MaybeAccount<ProgramAccount>[]> {
const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
return maybeAccounts.map((maybeAccount) =>
decodeProgramAccount(maybeAccount)
);
}

export function getProgramAccountSize(): number {
return 65;
}

export async function fetchProgramAccountFromSeeds(
rpc: Parameters<typeof fetchEncodedAccount>[0],
seeds: ProgramAccountSeeds,
config: FetchAccountConfig & { programAddress?: Address } = {}
): Promise<Account<ProgramAccount>> {
const maybeAccount = await fetchMaybeProgramAccountFromSeeds(
rpc,
seeds,
config
);
assertAccountExists(maybeAccount);
return maybeAccount;
}

export async function fetchMaybeProgramAccountFromSeeds(
rpc: Parameters<typeof fetchEncodedAccount>[0],
seeds: ProgramAccountSeeds,
config: FetchAccountConfig & { programAddress?: Address } = {}
): Promise<MaybeAccount<ProgramAccount>> {
const { programAddress, ...fetchConfig } = config;
const [address] = await findProgramAccountPda(seeds, { programAddress });
return await fetchMaybeProgramAccount(rpc, address, fetchConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 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
*/

export * from './productProgram';
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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
*/

/** DeserializationError: Error deserializing an account */
export const PRODUCT_PROGRAM_ERROR__DESERIALIZATION_ERROR = 0x0; // 0
/** SerializationError: Error serializing an account */
export const PRODUCT_PROGRAM_ERROR__SERIALIZATION_ERROR = 0x1; // 1
/** InvalidProgramOwner: Invalid program owner. This likely mean the provided account does not exist */
export const PRODUCT_PROGRAM_ERROR__INVALID_PROGRAM_OWNER = 0x2; // 2
/** InvalidPda: Invalid PDA derivation */
export const PRODUCT_PROGRAM_ERROR__INVALID_PDA = 0x3; // 3
/** ExpectedEmptyAccount: Expected empty account */
export const PRODUCT_PROGRAM_ERROR__EXPECTED_EMPTY_ACCOUNT = 0x4; // 4
/** ExpectedNonEmptyAccount: Expected non empty account */
export const PRODUCT_PROGRAM_ERROR__EXPECTED_NON_EMPTY_ACCOUNT = 0x5; // 5
/** ExpectedSignerAccount: Expected signer account */
export const PRODUCT_PROGRAM_ERROR__EXPECTED_SIGNER_ACCOUNT = 0x6; // 6
/** ExpectedWritableAccount: Expected writable account */
export const PRODUCT_PROGRAM_ERROR__EXPECTED_WRITABLE_ACCOUNT = 0x7; // 7
/** AccountMismatch: Account mismatch */
export const PRODUCT_PROGRAM_ERROR__ACCOUNT_MISMATCH = 0x8; // 8
/** InvalidAccountKey: Invalid account key */
export const PRODUCT_PROGRAM_ERROR__INVALID_ACCOUNT_KEY = 0x9; // 9
/** NumericalOverflow: Numerical overflow */
export const PRODUCT_PROGRAM_ERROR__NUMERICAL_OVERFLOW = 0xa; // 10

export type ProductProgramError =
| typeof PRODUCT_PROGRAM_ERROR__ACCOUNT_MISMATCH
| typeof PRODUCT_PROGRAM_ERROR__DESERIALIZATION_ERROR
| typeof PRODUCT_PROGRAM_ERROR__EXPECTED_EMPTY_ACCOUNT
| typeof PRODUCT_PROGRAM_ERROR__EXPECTED_NON_EMPTY_ACCOUNT
| typeof PRODUCT_PROGRAM_ERROR__EXPECTED_SIGNER_ACCOUNT
| typeof PRODUCT_PROGRAM_ERROR__EXPECTED_WRITABLE_ACCOUNT
| typeof PRODUCT_PROGRAM_ERROR__INVALID_ACCOUNT_KEY
| typeof PRODUCT_PROGRAM_ERROR__INVALID_PDA
| typeof PRODUCT_PROGRAM_ERROR__INVALID_PROGRAM_OWNER
| typeof PRODUCT_PROGRAM_ERROR__NUMERICAL_OVERFLOW
| typeof PRODUCT_PROGRAM_ERROR__SERIALIZATION_ERROR;

let productProgramErrorMessages:
| Record<ProductProgramError, string>
| undefined;
if (__DEV__) {
productProgramErrorMessages = {
[PRODUCT_PROGRAM_ERROR__ACCOUNT_MISMATCH]: `Account mismatch`,
[PRODUCT_PROGRAM_ERROR__DESERIALIZATION_ERROR]: `Error deserializing an account`,
[PRODUCT_PROGRAM_ERROR__EXPECTED_EMPTY_ACCOUNT]: `Expected empty account`,
[PRODUCT_PROGRAM_ERROR__EXPECTED_NON_EMPTY_ACCOUNT]: `Expected non empty account`,
[PRODUCT_PROGRAM_ERROR__EXPECTED_SIGNER_ACCOUNT]: `Expected signer account`,
[PRODUCT_PROGRAM_ERROR__EXPECTED_WRITABLE_ACCOUNT]: `Expected writable account`,
[PRODUCT_PROGRAM_ERROR__INVALID_ACCOUNT_KEY]: `Invalid account key`,
[PRODUCT_PROGRAM_ERROR__INVALID_PDA]: `Invalid PDA derivation`,
[PRODUCT_PROGRAM_ERROR__INVALID_PROGRAM_OWNER]: `Invalid program owner. This likely mean the provided account does not exist`,
[PRODUCT_PROGRAM_ERROR__NUMERICAL_OVERFLOW]: `Numerical overflow`,
[PRODUCT_PROGRAM_ERROR__SERIALIZATION_ERROR]: `Error serializing an account`,
};
}

export function getProductProgramErrorMessage(
code: ProductProgramError
): string {
if (__DEV__) {
return (productProgramErrorMessages as Record<ProductProgramError, string>)[
code
];
}

return 'Error message not available in production bundles. Compile with `__DEV__` set to true to see more information.';
}
11 changes: 11 additions & 0 deletions templates/product-program/clients/js/src/generated/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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
*/

/** Global variable provided by bundlers telling us if we are building for production or not. */
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const __DEV__: boolean;
14 changes: 14 additions & 0 deletions templates/product-program/clients/js/src/generated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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
*/

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

0 comments on commit 88e82b1

Please sign in to comment.