Skip to content

Commit

Permalink
[wip]: Write IDL for accounts and initialize instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Jan 14, 2025
1 parent 44eb08f commit 09603b6
Show file tree
Hide file tree
Showing 20 changed files with 1,561 additions and 166 deletions.
173 changes: 173 additions & 0 deletions clients/js/src/generated/accounts/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* 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,
getAddressDecoder,
getAddressEncoder,
getBooleanDecoder,
getBooleanEncoder,
getBytesDecoder,
getBytesEncoder,
getOptionDecoder,
getOptionEncoder,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
padRightDecoder,
padRightEncoder,
transformEncoder,
type Account,
type Address,
type Codec,
type Decoder,
type EncodedAccount,
type Encoder,
type FetchAccountConfig,
type FetchAccountsConfig,
type MaybeAccount,
type MaybeEncodedAccount,
type Option,
type OptionOrNullable,
type ReadonlyUint8Array,
} from '@solana/web3.js';
import {
getSeedDecoder,
getSeedEncoder,
type Seed,
type SeedArgs,
} from '../types';

export type Buffer = {
discriminator: number;
program: Option<Address>;
authority: Option<Address>;
canonical: boolean;
seed: Seed;
data: ReadonlyUint8Array;
};

export type BufferArgs = {
program: OptionOrNullable<Address>;
authority: OptionOrNullable<Address>;
canonical: boolean;
seed: SeedArgs;
data: ReadonlyUint8Array;
};

export function getBufferEncoder(): Encoder<BufferArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
[
'program',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
[
'authority',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['canonical', getBooleanEncoder()],
['seed', padRightEncoder(getSeedEncoder(), 14)],
['data', getBytesEncoder()],
]),
(value) => ({ ...value, discriminator: 1 })
);
}

export function getBufferDecoder(): Decoder<Buffer> {
return getStructDecoder([
['discriminator', getU8Decoder()],
[
'program',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
[
'authority',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['canonical', getBooleanDecoder()],
['seed', padRightDecoder(getSeedDecoder(), 14)],
['data', getBytesDecoder()],
]);
}

export function getBufferCodec(): Codec<BufferArgs, Buffer> {
return combineCodec(getBufferEncoder(), getBufferDecoder());
}

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

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

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

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

export async function fetchAllMaybeBuffer(
rpc: Parameters<typeof fetchEncodedAccounts>[0],
addresses: Array<Address>,
config?: FetchAccountsConfig
): Promise<MaybeAccount<Buffer>[]> {
const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
return maybeAccounts.map((maybeAccount) => decodeBuffer(maybeAccount));
}
10 changes: 10 additions & 0 deletions clients/js/src/generated/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 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 './buffer';
export * from './metadata';
203 changes: 203 additions & 0 deletions clients/js/src/generated/accounts/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* 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,
getAddressDecoder,
getAddressEncoder,
getBooleanDecoder,
getBooleanEncoder,
getBytesDecoder,
getBytesEncoder,
getOptionDecoder,
getOptionEncoder,
getStructDecoder,
getStructEncoder,
getU32Decoder,
getU32Encoder,
getU8Decoder,
getU8Encoder,
padRightDecoder,
padRightEncoder,
transformEncoder,
type Account,
type Address,
type Codec,
type Decoder,
type EncodedAccount,
type Encoder,
type FetchAccountConfig,
type FetchAccountsConfig,
type MaybeAccount,
type MaybeEncodedAccount,
type Option,
type OptionOrNullable,
type ReadonlyUint8Array,
} from '@solana/web3.js';
import {
getCompressionDecoder,
getCompressionEncoder,
getDataSourceDecoder,
getDataSourceEncoder,
getEncodingDecoder,
getEncodingEncoder,
getFormatDecoder,
getFormatEncoder,
getSeedDecoder,
getSeedEncoder,
type Compression,
type CompressionArgs,
type DataSource,
type DataSourceArgs,
type Encoding,
type EncodingArgs,
type Format,
type FormatArgs,
type Seed,
type SeedArgs,
} from '../types';

export type Metadata = {
discriminator: number;
program: Address;
authority: Option<Address>;
mutable: boolean;
canonical: boolean;
seed: Seed;
encoding: Encoding;
compression: Compression;
format: Format;
dataSource: DataSource;
dataLength: number;
data: ReadonlyUint8Array;
};

export type MetadataArgs = {
program: Address;
authority: OptionOrNullable<Address>;
mutable: boolean;
canonical: boolean;
seed: SeedArgs;
encoding: EncodingArgs;
compression: CompressionArgs;
format: FormatArgs;
dataSource: DataSourceArgs;
dataLength: number;
data: ReadonlyUint8Array;
};

export function getMetadataEncoder(): Encoder<MetadataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['program', getAddressEncoder()],
[
'authority',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['mutable', getBooleanEncoder()],
['canonical', getBooleanEncoder()],
['seed', getSeedEncoder()],
['encoding', getEncodingEncoder()],
['compression', getCompressionEncoder()],
['format', getFormatEncoder()],
['dataSource', getDataSourceEncoder()],
['dataLength', padRightEncoder(getU32Encoder(), 5)],
['data', getBytesEncoder()],
]),
(value) => ({ ...value, discriminator: 2 })
);
}

export function getMetadataDecoder(): Decoder<Metadata> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['program', getAddressDecoder()],
[
'authority',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['mutable', getBooleanDecoder()],
['canonical', getBooleanDecoder()],
['seed', getSeedDecoder()],
['encoding', getEncodingDecoder()],
['compression', getCompressionDecoder()],
['format', getFormatDecoder()],
['dataSource', getDataSourceDecoder()],
['dataLength', padRightDecoder(getU32Decoder(), 5)],
['data', getBytesDecoder()],
]);
}

export function getMetadataCodec(): Codec<MetadataArgs, Metadata> {
return combineCodec(getMetadataEncoder(), getMetadataDecoder());
}

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

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

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

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

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

0 comments on commit 09603b6

Please sign in to comment.