Skip to content

Commit

Permalink
Define IDL instructions for memo transfers extension (#12)
Browse files Browse the repository at this point in the history
* Add instructions

* Add tests

* Add tests
  • Loading branch information
lorisleiva authored Oct 9, 2024
1 parent 84c670b commit 61bc6d0
Show file tree
Hide file tree
Showing 7 changed files with 829 additions and 1 deletion.
216 changes: 216 additions & 0 deletions clients/js/src/generated/instructions/disableMemoTransfers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/**
* 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 {
AccountRole,
combineCodec,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IAccountSignerMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type ReadonlyAccount,
type ReadonlySignerAccount,
type TransactionSigner,
type WritableAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const DISABLE_MEMO_TRANSFERS_DISCRIMINATOR = 30;

export function getDisableMemoTransfersDiscriminatorBytes() {
return getU8Encoder().encode(DISABLE_MEMO_TRANSFERS_DISCRIMINATOR);
}

export const DISABLE_MEMO_TRANSFERS_MEMO_TRANSFERS_DISCRIMINATOR = 1;

export function getDisableMemoTransfersMemoTransfersDiscriminatorBytes() {
return getU8Encoder().encode(
DISABLE_MEMO_TRANSFERS_MEMO_TRANSFERS_DISCRIMINATOR
);
}

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

export type DisableMemoTransfersInstructionData = {
discriminator: number;
memoTransfersDiscriminator: number;
};

export type DisableMemoTransfersInstructionDataArgs = {};

export function getDisableMemoTransfersInstructionDataEncoder(): Encoder<DisableMemoTransfersInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['memoTransfersDiscriminator', getU8Encoder()],
]),
(value) => ({
...value,
discriminator: DISABLE_MEMO_TRANSFERS_DISCRIMINATOR,
memoTransfersDiscriminator:
DISABLE_MEMO_TRANSFERS_MEMO_TRANSFERS_DISCRIMINATOR,
})
);
}

export function getDisableMemoTransfersInstructionDataDecoder(): Decoder<DisableMemoTransfersInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['memoTransfersDiscriminator', getU8Decoder()],
]);
}

export function getDisableMemoTransfersInstructionDataCodec(): Codec<
DisableMemoTransfersInstructionDataArgs,
DisableMemoTransfersInstructionData
> {
return combineCodec(
getDisableMemoTransfersInstructionDataEncoder(),
getDisableMemoTransfersInstructionDataDecoder()
);
}

export type DisableMemoTransfersInput<
TAccountToken extends string = string,
TAccountOwner extends string = string,
> = {
/** The token account to update. */
token: Address<TAccountToken>;
/** The account's owner or its multisignature account. */
owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
multiSigners?: Array<TransactionSigner>;
};

export function getDisableMemoTransfersInstruction<
TAccountToken extends string,
TAccountOwner extends string,
>(
input: DisableMemoTransfersInput<TAccountToken, TAccountOwner>
): DisableMemoTransfersInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
(typeof input)['owner'] extends TransactionSigner<TAccountOwner>
? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
: TAccountOwner
> {
// Program address.
const programAddress = TOKEN_2022_PROGRAM_ADDRESS;

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

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

// Remaining accounts.
const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
(signer) => ({
address: signer.address,
role: AccountRole.READONLY_SIGNER,
signer,
})
);

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [
getAccountMeta(accounts.token),
getAccountMeta(accounts.owner),
...remainingAccounts,
],
programAddress,
data: getDisableMemoTransfersInstructionDataEncoder().encode({}),
} as DisableMemoTransfersInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
(typeof input)['owner'] extends TransactionSigner<TAccountOwner>
? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
: TAccountOwner
>;

return instruction;
}

export type ParsedDisableMemoTransfersInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
/** The token account to update. */
token: TAccountMetas[0];
/** The account's owner or its multisignature account. */
owner: TAccountMetas[1];
};
data: DisableMemoTransfersInstructionData;
};

export function parseDisableMemoTransfersInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedDisableMemoTransfersInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 2) {
// 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: {
token: getNextAccount(),
owner: getNextAccount(),
},
data: getDisableMemoTransfersInstructionDataDecoder().decode(
instruction.data
),
};
}
Loading

0 comments on commit 61bc6d0

Please sign in to comment.