Skip to content

Commit

Permalink
Update Kinobi
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Dec 11, 2023
1 parent 5489666 commit 5777014
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 113 deletions.
1 change: 1 addition & 0 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"author": "Metaplex Maintainers <[email protected]>",
"license": "Apache-2.0",
"dependencies": {
"@solana/accounts": "2.0.0-experimental.9741939",
"@solana/addresses": "2.0.0-experimental.9741939",
"@solana/codecs-core": "2.0.0-experimental.9741939",
"@solana/codecs-data-structures": "2.0.0-experimental.9741939",
Expand Down
21 changes: 17 additions & 4 deletions clients/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions clients/js/src/generated/accounts/addressLookupTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Account,
EncodedAccount,
FetchAccountConfig,
FetchAccountsConfig,
assertAccountExists,
decodeAccount,
} from '@solana/accounts';
import {
Address,
ProgramDerivedAddress,
Expand Down Expand Up @@ -42,13 +50,7 @@ import {
getOptionEncoder,
} from '@solana/options';
import {
Account,
Context,
EncodedAccount,
FetchEncodedAccountOptions,
FetchEncodedAccountsOptions,
assertAccountExists,
decodeAccount,
getProgramAddress,
getProgramDerivedAddress,
} from '../shared';
Expand Down Expand Up @@ -137,7 +139,7 @@ export function decodeAddressLookupTable<TAddress extends string = string>(
export async function fetchAddressLookupTable<TAddress extends string = string>(
context: Pick<Context, 'fetchEncodedAccount'>,
address: Address<TAddress>,
options?: FetchEncodedAccountOptions
options?: FetchAccountConfig
): Promise<AddressLookupTable<TAddress>> {
const maybeAccount = await context.fetchEncodedAccount(address, options);
assertAccountExists(maybeAccount);
Expand All @@ -149,7 +151,7 @@ export async function safeFetchAddressLookupTable<
>(
context: Pick<Context, 'fetchEncodedAccount'>,
address: Address<TAddress>,
options?: FetchEncodedAccountOptions
options?: FetchAccountConfig
): Promise<AddressLookupTable<TAddress> | null> {
const maybeAccount = await context.fetchEncodedAccount(address, options);
return maybeAccount.exists ? decodeAddressLookupTable(maybeAccount) : null;
Expand All @@ -158,7 +160,7 @@ export async function safeFetchAddressLookupTable<
export async function fetchAllAddressLookupTable(
context: Pick<Context, 'fetchEncodedAccounts'>,
addresses: Array<Address>,
options?: FetchEncodedAccountsOptions
options?: FetchAccountsConfig
): Promise<AddressLookupTable[]> {
const maybeAccounts = await context.fetchEncodedAccounts(addresses, options);
return maybeAccounts.map((maybeAccount) => {
Expand All @@ -170,7 +172,7 @@ export async function fetchAllAddressLookupTable(
export async function safeFetchAllAddressLookupTable(
context: Pick<Context, 'fetchEncodedAccounts'>,
addresses: Array<Address>,
options?: FetchEncodedAccountsOptions
options?: FetchAccountsConfig
): Promise<AddressLookupTable[]> {
const maybeAccounts = await context.fetchEncodedAccounts(addresses, options);
return maybeAccounts
Expand Down Expand Up @@ -206,7 +208,7 @@ export async function fetchAddressLookupTableFromSeeds(
'fetchEncodedAccount' | 'getProgramAddress' | 'getProgramDerivedAddress'
>,
seeds: Parameters<typeof findAddressLookupTablePda>[1],
options?: FetchEncodedAccountOptions
options?: FetchAccountConfig
): Promise<AddressLookupTable> {
const [address] = await findAddressLookupTablePda(context, seeds);
return fetchAddressLookupTable(context, address, options);
Expand All @@ -218,7 +220,7 @@ export async function safeFetchAddressLookupTableFromSeeds(
'fetchEncodedAccount' | 'getProgramAddress' | 'getProgramDerivedAddress'
>,
seeds: Parameters<typeof findAddressLookupTablePda>[1],
options?: FetchEncodedAccountOptions
options?: FetchAccountConfig
): Promise<AddressLookupTable | null> {
const [address] = await findAddressLookupTablePda(context, seeds);
return safeFetchAddressLookupTable(context, address, options);
Expand Down
88 changes: 7 additions & 81 deletions clients/js/src/generated/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
FetchAccountConfig,
FetchAccountsConfig,
MaybeEncodedAccount,
} from '@solana/accounts';
import {
Address,
isProgramDerivedAddress,
getProgramDerivedAddress as web3JsGetProgramDerivedAddress,
ProgramDerivedAddress,
} from '@solana/addresses';
import { Decoder } from '@solana/codecs-core';
import {
AccountRole,
IAccountMeta,
IInstruction,
upgradeRoleToSigner,
} from '@solana/instructions';
import {
Expand Down Expand Up @@ -187,21 +190,14 @@ export function isTransactionSigner<TAddress extends string = string>(
);
}

export type CustomGeneratedInstruction<
TInstruction extends IInstruction,
TReturn
> = {
getGeneratedInstruction: (instruction: TInstruction) => TReturn;
};

export type Context = {
fetchEncodedAccount: <TAddress extends string = string>(
address: Address<TAddress>,
options?: FetchEncodedAccountOptions
options?: FetchAccountConfig
) => Promise<MaybeEncodedAccount<TAddress>>;
fetchEncodedAccounts: (
addresses: Address[],
options?: FetchEncodedAccountsOptions
options?: FetchAccountsConfig
) => Promise<MaybeEncodedAccount[]>;
getProgramAddress?: (program: { name: string; address: Address }) => Address;
getProgramDerivedAddress?: (
Expand Down Expand Up @@ -235,76 +231,6 @@ export async function getProgramDerivedAddress(
});
}

export const ACCOUNT_HEADER_SIZE = 128;

export type AccountHeader = {
programAddress: Address;
executable: boolean;
lamports: bigint;
rentEpoch?: bigint;
};

export type Account<
TData extends object | Uint8Array,
TAddress extends string = string
> = AccountHeader & {
address: Address<TAddress>;
data: TData;
};

export type MaybeAccount<
TData extends object | Uint8Array,
TAddress extends string = string
> =
| ({ exists: true } & Account<TData, TAddress>)
| { exists: false; address: Address<TAddress> };

export type EncodedAccount<TAddress extends string = string> = Account<
Uint8Array,
TAddress
>;
export type MaybeEncodedAccount<TAddress extends string = string> =
MaybeAccount<Uint8Array, TAddress>;

export function decodeAccount<
TData extends object,
TAddress extends string = string
>(
encodedAccount: EncodedAccount<TAddress>,
decoder: Decoder<TData>
): Account<TData, TAddress> {
try {
return { ...encodedAccount, data: decoder.decode(encodedAccount.data) };
} catch (error: any) {
// TODO: Coded error.
throw new Error(`Failed to decode account [${encodedAccount.address}].`);
}
}

export function assertAccountExists<
TData extends object | Uint8Array,
TAddress extends string = string
>(
account: MaybeAccount<TData, TAddress>
): asserts account is Account<TData, TAddress> & { exists: true } {
if (!account.exists) {
// TODO: Coded error.
throw new Error(`Expected account [${account.address}] to exist.`);
}
}

export type Commitment = 'confirmed' | 'finalized' | 'processed';

export type FetchEncodedAccountOptions = {
commitment?: Commitment;
abortSignal?: AbortSignal;
};

export type FetchEncodedAccountsOptions = {
commitment?: Commitment;
abortSignal?: AbortSignal;
};

export type Program<TAddress extends string = string> = {
name: string;
address: Address<TAddress>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"validator:stop": "amman stop"
},
"devDependencies": {
"@metaplex-foundation/kinobi": "file:../@metaplex/kinobi",
"@metaplex-foundation/kinobi": "^0.16.10",
"@metaplex-foundation/amman": "^0.12.1",
"typescript": "^4.9.4"
},
Expand Down
28 changes: 13 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5777014

Please sign in to comment.