assign.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * This code was AUTOGENERATED using the codama library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun codama to update it.
  5. *
  6. * @see https://github.com/codama-idl/codama
  7. */
  8. import {
  9. combineCodec,
  10. getAddressDecoder,
  11. getAddressEncoder,
  12. getStructDecoder,
  13. getStructEncoder,
  14. getU32Decoder,
  15. getU32Encoder,
  16. transformEncoder,
  17. type AccountMeta,
  18. type AccountSignerMeta,
  19. type Address,
  20. type FixedSizeCodec,
  21. type FixedSizeDecoder,
  22. type FixedSizeEncoder,
  23. type Instruction,
  24. type InstructionWithAccounts,
  25. type InstructionWithData,
  26. type ReadonlyUint8Array,
  27. type TransactionSigner,
  28. type WritableSignerAccount,
  29. } from '@solana/kit';
  30. import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
  31. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  32. export const ASSIGN_DISCRIMINATOR = 1;
  33. export function getAssignDiscriminatorBytes() {
  34. return getU32Encoder().encode(ASSIGN_DISCRIMINATOR);
  35. }
  36. export type AssignInstruction<
  37. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  38. TAccountAccount extends string | AccountMeta<string> = string,
  39. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  40. > = Instruction<TProgram> &
  41. InstructionWithData<ReadonlyUint8Array> &
  42. InstructionWithAccounts<
  43. [
  44. TAccountAccount extends string
  45. ? WritableSignerAccount<TAccountAccount> &
  46. AccountSignerMeta<TAccountAccount>
  47. : TAccountAccount,
  48. ...TRemainingAccounts,
  49. ]
  50. >;
  51. export type AssignInstructionData = {
  52. discriminator: number;
  53. programAddress: Address;
  54. };
  55. export type AssignInstructionDataArgs = { programAddress: Address };
  56. export function getAssignInstructionDataEncoder(): FixedSizeEncoder<AssignInstructionDataArgs> {
  57. return transformEncoder(
  58. getStructEncoder([
  59. ['discriminator', getU32Encoder()],
  60. ['programAddress', getAddressEncoder()],
  61. ]),
  62. (value) => ({ ...value, discriminator: ASSIGN_DISCRIMINATOR })
  63. );
  64. }
  65. export function getAssignInstructionDataDecoder(): FixedSizeDecoder<AssignInstructionData> {
  66. return getStructDecoder([
  67. ['discriminator', getU32Decoder()],
  68. ['programAddress', getAddressDecoder()],
  69. ]);
  70. }
  71. export function getAssignInstructionDataCodec(): FixedSizeCodec<
  72. AssignInstructionDataArgs,
  73. AssignInstructionData
  74. > {
  75. return combineCodec(
  76. getAssignInstructionDataEncoder(),
  77. getAssignInstructionDataDecoder()
  78. );
  79. }
  80. export type AssignInput<TAccountAccount extends string = string> = {
  81. account: TransactionSigner<TAccountAccount>;
  82. programAddress: AssignInstructionDataArgs['programAddress'];
  83. };
  84. export function getAssignInstruction<
  85. TAccountAccount extends string,
  86. TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
  87. >(
  88. input: AssignInput<TAccountAccount>,
  89. config?: { programAddress?: TProgramAddress }
  90. ): AssignInstruction<TProgramAddress, TAccountAccount> {
  91. // Program address.
  92. const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
  93. // Original accounts.
  94. const originalAccounts = {
  95. account: { value: input.account ?? null, isWritable: true },
  96. };
  97. const accounts = originalAccounts as Record<
  98. keyof typeof originalAccounts,
  99. ResolvedAccount
  100. >;
  101. // Original args.
  102. const args = { ...input };
  103. const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted');
  104. const instruction = {
  105. accounts: [getAccountMeta(accounts.account)],
  106. programAddress,
  107. data: getAssignInstructionDataEncoder().encode(
  108. args as AssignInstructionDataArgs
  109. ),
  110. } as AssignInstruction<TProgramAddress, TAccountAccount>;
  111. return instruction;
  112. }
  113. export type ParsedAssignInstruction<
  114. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  115. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  116. > = {
  117. programAddress: Address<TProgram>;
  118. accounts: {
  119. account: TAccountMetas[0];
  120. };
  121. data: AssignInstructionData;
  122. };
  123. export function parseAssignInstruction<
  124. TProgram extends string,
  125. TAccountMetas extends readonly AccountMeta[],
  126. >(
  127. instruction: Instruction<TProgram> &
  128. InstructionWithAccounts<TAccountMetas> &
  129. InstructionWithData<ReadonlyUint8Array>
  130. ): ParsedAssignInstruction<TProgram, TAccountMetas> {
  131. if (instruction.accounts.length < 1) {
  132. // TODO: Coded error.
  133. throw new Error('Not enough accounts');
  134. }
  135. let accountIndex = 0;
  136. const getNextAccount = () => {
  137. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  138. accountIndex += 1;
  139. return accountMeta;
  140. };
  141. return {
  142. programAddress: instruction.programAddress,
  143. accounts: {
  144. account: getNextAccount(),
  145. },
  146. data: getAssignInstructionDataDecoder().decode(instruction.data),
  147. };
  148. }