Skip to content

Commit

Permalink
Define IDL instructions for the interest bearing mint extension (#45)
Browse files Browse the repository at this point in the history
* feat(interestBearningMint): add interestBearingMint idl

* feat: add InterestBearingMint initiailze function

* test(interestBearingMint): add tests for initilize and update

* fix: some merge conflict with main

* fix: as review

- change `isSigner` from `true` to `either` in account rateAuthority in
updateRateInterestBearingMint
- use `createMint` function instead then update instruction in
updateInterestBearingMint test

* style: format updateInterestBearingMint.test.ts
  • Loading branch information
qapquiz authored Nov 14, 2024
1 parent 8947647 commit 060f7cc
Show file tree
Hide file tree
Showing 8 changed files with 819 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from './initializeDefaultAccountState';
export * from './initializeGroupMemberPointer';
export * from './initializeGroupPointer';
export * from './initializeImmutableOwner';
export * from './initializeInterestBearingMint';
export * from './initializeMetadataPointer';
export * from './initializeMint';
export * from './initializeMint2';
Expand Down Expand Up @@ -79,6 +80,7 @@ export * from './updateDefaultAccountState';
export * from './updateGroupMemberPointer';
export * from './updateGroupPointer';
export * from './updateMetadataPointer';
export * from './updateRateInterestBearingMint';
export * from './updateTokenGroupMaxSize';
export * from './updateTokenGroupUpdateAuthority';
export * from './updateTokenMetadataField';
Expand Down
212 changes: 212 additions & 0 deletions clients/js/src/generated/instructions/initializeInterestBearingMint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/**
* 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,
getAddressDecoder,
getAddressEncoder,
getI16Decoder,
getI16Encoder,
getOptionDecoder,
getOptionEncoder,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type Option,
type OptionOrNullable,
type WritableAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const INITIALIZE_INTEREST_BEARING_MINT_DISCRIMINATOR = 33;

export function getInitializeInterestBearingMintDiscriminatorBytes() {
return getU8Encoder().encode(INITIALIZE_INTEREST_BEARING_MINT_DISCRIMINATOR);
}

export const INITIALIZE_INTEREST_BEARING_MINT_INTEREST_BEARING_MINT_DISCRIMINATOR = 0;

export function getInitializeInterestBearingMintInterestBearingMintDiscriminatorBytes() {
return getU8Encoder().encode(
INITIALIZE_INTEREST_BEARING_MINT_INTEREST_BEARING_MINT_DISCRIMINATOR
);
}

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

export type InitializeInterestBearingMintInstructionData = {
discriminator: number;
interestBearingMintDiscriminator: number;
/** The public key for the account that can update the rate */
rateAuthority: Option<Address>;
/** The initial interest rate */
rate: number;
};

export type InitializeInterestBearingMintInstructionDataArgs = {
/** The public key for the account that can update the rate */
rateAuthority: OptionOrNullable<Address>;
/** The initial interest rate */
rate: number;
};

export function getInitializeInterestBearingMintInstructionDataEncoder(): Encoder<InitializeInterestBearingMintInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['interestBearingMintDiscriminator', getU8Encoder()],
[
'rateAuthority',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['rate', getI16Encoder()],
]),
(value) => ({
...value,
discriminator: INITIALIZE_INTEREST_BEARING_MINT_DISCRIMINATOR,
interestBearingMintDiscriminator:
INITIALIZE_INTEREST_BEARING_MINT_INTEREST_BEARING_MINT_DISCRIMINATOR,
})
);
}

export function getInitializeInterestBearingMintInstructionDataDecoder(): Decoder<InitializeInterestBearingMintInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['interestBearingMintDiscriminator', getU8Decoder()],
[
'rateAuthority',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
['rate', getI16Decoder()],
]);
}

export function getInitializeInterestBearingMintInstructionDataCodec(): Codec<
InitializeInterestBearingMintInstructionDataArgs,
InitializeInterestBearingMintInstructionData
> {
return combineCodec(
getInitializeInterestBearingMintInstructionDataEncoder(),
getInitializeInterestBearingMintInstructionDataDecoder()
);
}

export type InitializeInterestBearingMintInput<
TAccountMint extends string = string,
> = {
/** The mint to initialize. */
mint: Address<TAccountMint>;
rateAuthority: InitializeInterestBearingMintInstructionDataArgs['rateAuthority'];
rate: InitializeInterestBearingMintInstructionDataArgs['rate'];
};

export function getInitializeInterestBearingMintInstruction<
TAccountMint extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: InitializeInterestBearingMintInput<TAccountMint>,
config?: { programAddress?: TProgramAddress }
): InitializeInterestBearingMintInstruction<TProgramAddress, TAccountMint> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS;

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

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

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [getAccountMeta(accounts.mint)],
programAddress,
data: getInitializeInterestBearingMintInstructionDataEncoder().encode(
args as InitializeInterestBearingMintInstructionDataArgs
),
} as InitializeInterestBearingMintInstruction<TProgramAddress, TAccountMint>;

return instruction;
}

export type ParsedInitializeInterestBearingMintInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
/** The mint to initialize. */
mint: TAccountMetas[0];
};
data: InitializeInterestBearingMintInstructionData;
};

export function parseInitializeInterestBearingMintInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedInitializeInterestBearingMintInstruction<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: {
mint: getNextAccount(),
},
data: getInitializeInterestBearingMintInstructionDataDecoder().decode(
instruction.data
),
};
}
Loading

0 comments on commit 060f7cc

Please sign in to comment.