Skip to content

Commit

Permalink
Define IDL instructions for token metadata extension (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Oct 11, 2024
1 parent e851b7b commit b1f2c8a
Show file tree
Hide file tree
Showing 32 changed files with 2,343 additions and 68 deletions.
185 changes: 185 additions & 0 deletions clients/js/src/generated/instructions/emitTokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/**
* 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 {
combineCodec,
getBytesDecoder,
getBytesEncoder,
getOptionDecoder,
getOptionEncoder,
getStructDecoder,
getStructEncoder,
getU64Decoder,
getU64Encoder,
none,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type Option,
type OptionOrNullable,
type ReadonlyAccount,
type ReadonlyUint8Array,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const EMIT_TOKEN_METADATA_DISCRIMINATOR = new Uint8Array([
250, 166, 180, 250, 13, 12, 184, 70,
]);

export function getEmitTokenMetadataDiscriminatorBytes() {
return getBytesEncoder().encode(EMIT_TOKEN_METADATA_DISCRIMINATOR);
}

export type EmitTokenMetadataInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetadata extends string | IAccountMeta<string> = string,
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountMetadata extends string
? ReadonlyAccount<TAccountMetadata>
: TAccountMetadata,
...TRemainingAccounts,
]
>;

export type EmitTokenMetadataInstructionData = {
discriminator: ReadonlyUint8Array;
/** Start of range of data to emit */
start: Option<bigint>;
/** End of range of data to emit */
end: Option<bigint>;
};

export type EmitTokenMetadataInstructionDataArgs = {
/** Start of range of data to emit */
start?: OptionOrNullable<number | bigint>;
/** End of range of data to emit */
end?: OptionOrNullable<number | bigint>;
};

export function getEmitTokenMetadataInstructionDataEncoder(): Encoder<EmitTokenMetadataInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getBytesEncoder()],
['start', getOptionEncoder(getU64Encoder())],
['end', getOptionEncoder(getU64Encoder())],
]),
(value) => ({
...value,
discriminator: EMIT_TOKEN_METADATA_DISCRIMINATOR,
start: value.start ?? none(),
end: value.end ?? none(),
})
);
}

export function getEmitTokenMetadataInstructionDataDecoder(): Decoder<EmitTokenMetadataInstructionData> {
return getStructDecoder([
['discriminator', getBytesDecoder()],
['start', getOptionDecoder(getU64Decoder())],
['end', getOptionDecoder(getU64Decoder())],
]);
}

export function getEmitTokenMetadataInstructionDataCodec(): Codec<
EmitTokenMetadataInstructionDataArgs,
EmitTokenMetadataInstructionData
> {
return combineCodec(
getEmitTokenMetadataInstructionDataEncoder(),
getEmitTokenMetadataInstructionDataDecoder()
);
}

export type EmitTokenMetadataInput<TAccountMetadata extends string = string> = {
metadata: Address<TAccountMetadata>;
start?: EmitTokenMetadataInstructionDataArgs['start'];
end?: EmitTokenMetadataInstructionDataArgs['end'];
};

export function getEmitTokenMetadataInstruction<
TAccountMetadata extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: EmitTokenMetadataInput<TAccountMetadata>,
config?: { programAddress?: TProgramAddress }
): EmitTokenMetadataInstruction<TProgramAddress, TAccountMetadata> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS;

// Original accounts.
const originalAccounts = {
metadata: { value: input.metadata ?? null, isWritable: false },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;

// Original args.
const args = { ...input };

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [getAccountMeta(accounts.metadata)],
programAddress,
data: getEmitTokenMetadataInstructionDataEncoder().encode(
args as EmitTokenMetadataInstructionDataArgs
),
} as EmitTokenMetadataInstruction<TProgramAddress, TAccountMetadata>;

return instruction;
}

export type ParsedEmitTokenMetadataInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
metadata: TAccountMetas[0];
};
data: EmitTokenMetadataInstructionData;
};

export function parseEmitTokenMetadataInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedEmitTokenMetadataInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 1) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = instruction.accounts![accountIndex]!;
accountIndex += 1;
return accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
metadata: getNextAccount(),
},
data: getEmitTokenMetadataInstructionDataDecoder().decode(instruction.data),
};
}
5 changes: 5 additions & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from './createAssociatedTokenIdempotent';
export * from './disableConfidentialCredits';
export * from './disableMemoTransfers';
export * from './disableNonConfidentialCredits';
export * from './emitTokenMetadata';
export * from './emptyConfidentialTransferAccount';
export * from './enableConfidentialCredits';
export * from './enableMemoTransfers';
Expand All @@ -45,11 +46,13 @@ export * from './initializeMint2';
export * from './initializeMintCloseAuthority';
export * from './initializeMultisig';
export * from './initializeMultisig2';
export * from './initializeTokenMetadata';
export * from './initializeTransferFeeConfig';
export * from './mintTo';
export * from './mintToChecked';
export * from './reallocate';
export * from './recoverNestedAssociatedToken';
export * from './removeTokenMetadataKey';
export * from './revoke';
export * from './setAuthority';
export * from './setTransferFee';
Expand All @@ -64,5 +67,7 @@ export * from './updateDefaultAccountState';
export * from './updateGroupMemberPointer';
export * from './updateGroupPointer';
export * from './updateMetadataPointer';
export * from './updateTokenMetadataField';
export * from './updateTokenMetadataUpdateAuthority';
export * from './withdrawWithheldTokensFromAccounts';
export * from './withdrawWithheldTokensFromMint';
Loading

0 comments on commit b1f2c8a

Please sign in to comment.