-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define IDL instructions for the transfer hook extension (#42)
* Added transfer hook extension in idl * Used createMint helper * Used createMint helper * Reverted test change for createMint
- Loading branch information
1 parent
45ca918
commit 1cbdb4c
Showing
8 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
clients/js/src/generated/instructions/initializeTransferHook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/** | ||
* 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, | ||
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_TRANSFER_HOOK_DISCRIMINATOR = 36; | ||
|
||
export function getInitializeTransferHookDiscriminatorBytes() { | ||
return getU8Encoder().encode(INITIALIZE_TRANSFER_HOOK_DISCRIMINATOR); | ||
} | ||
|
||
export const INITIALIZE_TRANSFER_HOOK_TRANSFER_HOOK_DISCRIMINATOR = 0; | ||
|
||
export function getInitializeTransferHookTransferHookDiscriminatorBytes() { | ||
return getU8Encoder().encode( | ||
INITIALIZE_TRANSFER_HOOK_TRANSFER_HOOK_DISCRIMINATOR | ||
); | ||
} | ||
|
||
export type InitializeTransferHookInstruction< | ||
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 InitializeTransferHookInstructionData = { | ||
discriminator: number; | ||
transferHookDiscriminator: number; | ||
/** The public key for the account that can update the program id */ | ||
authority: Option<Address>; | ||
/** The program id that performs logic during transfers */ | ||
programId: Option<Address>; | ||
}; | ||
|
||
export type InitializeTransferHookInstructionDataArgs = { | ||
/** The public key for the account that can update the program id */ | ||
authority: OptionOrNullable<Address>; | ||
/** The program id that performs logic during transfers */ | ||
programId: OptionOrNullable<Address>; | ||
}; | ||
|
||
export function getInitializeTransferHookInstructionDataEncoder(): Encoder<InitializeTransferHookInstructionDataArgs> { | ||
return transformEncoder( | ||
getStructEncoder([ | ||
['discriminator', getU8Encoder()], | ||
['transferHookDiscriminator', getU8Encoder()], | ||
[ | ||
'authority', | ||
getOptionEncoder(getAddressEncoder(), { | ||
prefix: null, | ||
noneValue: 'zeroes', | ||
}), | ||
], | ||
[ | ||
'programId', | ||
getOptionEncoder(getAddressEncoder(), { | ||
prefix: null, | ||
noneValue: 'zeroes', | ||
}), | ||
], | ||
]), | ||
(value) => ({ | ||
...value, | ||
discriminator: INITIALIZE_TRANSFER_HOOK_DISCRIMINATOR, | ||
transferHookDiscriminator: | ||
INITIALIZE_TRANSFER_HOOK_TRANSFER_HOOK_DISCRIMINATOR, | ||
}) | ||
); | ||
} | ||
|
||
export function getInitializeTransferHookInstructionDataDecoder(): Decoder<InitializeTransferHookInstructionData> { | ||
return getStructDecoder([ | ||
['discriminator', getU8Decoder()], | ||
['transferHookDiscriminator', getU8Decoder()], | ||
[ | ||
'authority', | ||
getOptionDecoder(getAddressDecoder(), { | ||
prefix: null, | ||
noneValue: 'zeroes', | ||
}), | ||
], | ||
[ | ||
'programId', | ||
getOptionDecoder(getAddressDecoder(), { | ||
prefix: null, | ||
noneValue: 'zeroes', | ||
}), | ||
], | ||
]); | ||
} | ||
|
||
export function getInitializeTransferHookInstructionDataCodec(): Codec< | ||
InitializeTransferHookInstructionDataArgs, | ||
InitializeTransferHookInstructionData | ||
> { | ||
return combineCodec( | ||
getInitializeTransferHookInstructionDataEncoder(), | ||
getInitializeTransferHookInstructionDataDecoder() | ||
); | ||
} | ||
|
||
export type InitializeTransferHookInput<TAccountMint extends string = string> = | ||
{ | ||
/** The mint to initialize. */ | ||
mint: Address<TAccountMint>; | ||
authority: InitializeTransferHookInstructionDataArgs['authority']; | ||
programId: InitializeTransferHookInstructionDataArgs['programId']; | ||
}; | ||
|
||
export function getInitializeTransferHookInstruction< | ||
TAccountMint extends string, | ||
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
>( | ||
input: InitializeTransferHookInput<TAccountMint>, | ||
config?: { programAddress?: TProgramAddress } | ||
): InitializeTransferHookInstruction<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: getInitializeTransferHookInstructionDataEncoder().encode( | ||
args as InitializeTransferHookInstructionDataArgs | ||
), | ||
} as InitializeTransferHookInstruction<TProgramAddress, TAccountMint>; | ||
|
||
return instruction; | ||
} | ||
|
||
export type ParsedInitializeTransferHookInstruction< | ||
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: InitializeTransferHookInstructionData; | ||
}; | ||
|
||
export function parseInitializeTransferHookInstruction< | ||
TProgram extends string, | ||
TAccountMetas extends readonly IAccountMeta[], | ||
>( | ||
instruction: IInstruction<TProgram> & | ||
IInstructionWithAccounts<TAccountMetas> & | ||
IInstructionWithData<Uint8Array> | ||
): ParsedInitializeTransferHookInstruction<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: getInitializeTransferHookInstructionDataDecoder().decode( | ||
instruction.data | ||
), | ||
}; | ||
} |
Oops, something went wrong.