revoke.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. AccountRole,
  10. combineCodec,
  11. getStructDecoder,
  12. getStructEncoder,
  13. getU8Decoder,
  14. getU8Encoder,
  15. transformEncoder,
  16. type AccountMeta,
  17. type AccountSignerMeta,
  18. type Address,
  19. type FixedSizeCodec,
  20. type FixedSizeDecoder,
  21. type FixedSizeEncoder,
  22. type Instruction,
  23. type InstructionWithAccounts,
  24. type InstructionWithData,
  25. type ReadonlyAccount,
  26. type ReadonlySignerAccount,
  27. type ReadonlyUint8Array,
  28. type TransactionSigner,
  29. type WritableAccount,
  30. } from '@solana/kit';
  31. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  32. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  33. export const REVOKE_DISCRIMINATOR = 5;
  34. export function getRevokeDiscriminatorBytes() {
  35. return getU8Encoder().encode(REVOKE_DISCRIMINATOR);
  36. }
  37. export type RevokeInstruction<
  38. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  39. TAccountSource extends string | AccountMeta<string> = string,
  40. TAccountOwner extends string | AccountMeta<string> = string,
  41. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  42. > = Instruction<TProgram> &
  43. InstructionWithData<ReadonlyUint8Array> &
  44. InstructionWithAccounts<
  45. [
  46. TAccountSource extends string
  47. ? WritableAccount<TAccountSource>
  48. : TAccountSource,
  49. TAccountOwner extends string
  50. ? ReadonlyAccount<TAccountOwner>
  51. : TAccountOwner,
  52. ...TRemainingAccounts,
  53. ]
  54. >;
  55. export type RevokeInstructionData = { discriminator: number };
  56. export type RevokeInstructionDataArgs = {};
  57. export function getRevokeInstructionDataEncoder(): FixedSizeEncoder<RevokeInstructionDataArgs> {
  58. return transformEncoder(
  59. getStructEncoder([['discriminator', getU8Encoder()]]),
  60. (value) => ({ ...value, discriminator: REVOKE_DISCRIMINATOR })
  61. );
  62. }
  63. export function getRevokeInstructionDataDecoder(): FixedSizeDecoder<RevokeInstructionData> {
  64. return getStructDecoder([['discriminator', getU8Decoder()]]);
  65. }
  66. export function getRevokeInstructionDataCodec(): FixedSizeCodec<
  67. RevokeInstructionDataArgs,
  68. RevokeInstructionData
  69. > {
  70. return combineCodec(
  71. getRevokeInstructionDataEncoder(),
  72. getRevokeInstructionDataDecoder()
  73. );
  74. }
  75. export type RevokeInput<
  76. TAccountSource extends string = string,
  77. TAccountOwner extends string = string,
  78. > = {
  79. /** The source account. */
  80. source: Address<TAccountSource>;
  81. /** The source account owner or its multisignature. */
  82. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  83. multiSigners?: Array<TransactionSigner>;
  84. };
  85. export function getRevokeInstruction<
  86. TAccountSource extends string,
  87. TAccountOwner extends string,
  88. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  89. >(
  90. input: RevokeInput<TAccountSource, TAccountOwner>,
  91. config?: { programAddress?: TProgramAddress }
  92. ): RevokeInstruction<
  93. TProgramAddress,
  94. TAccountSource,
  95. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  96. ? ReadonlySignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner>
  97. : TAccountOwner
  98. > {
  99. // Program address.
  100. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  101. // Original accounts.
  102. const originalAccounts = {
  103. source: { value: input.source ?? null, isWritable: true },
  104. owner: { value: input.owner ?? null, isWritable: false },
  105. };
  106. const accounts = originalAccounts as Record<
  107. keyof typeof originalAccounts,
  108. ResolvedAccount
  109. >;
  110. // Original args.
  111. const args = { ...input };
  112. // Remaining accounts.
  113. const remainingAccounts: AccountMeta[] = (args.multiSigners ?? []).map(
  114. (signer) => ({
  115. address: signer.address,
  116. role: AccountRole.READONLY_SIGNER,
  117. signer,
  118. })
  119. );
  120. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  121. return Object.freeze({
  122. accounts: [
  123. getAccountMeta(accounts.source),
  124. getAccountMeta(accounts.owner),
  125. ...remainingAccounts,
  126. ],
  127. data: getRevokeInstructionDataEncoder().encode({}),
  128. programAddress,
  129. } as RevokeInstruction<
  130. TProgramAddress,
  131. TAccountSource,
  132. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  133. ? ReadonlySignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner>
  134. : TAccountOwner
  135. >);
  136. }
  137. export type ParsedRevokeInstruction<
  138. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  139. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  140. > = {
  141. programAddress: Address<TProgram>;
  142. accounts: {
  143. /** The source account. */
  144. source: TAccountMetas[0];
  145. /** The source account owner or its multisignature. */
  146. owner: TAccountMetas[1];
  147. };
  148. data: RevokeInstructionData;
  149. };
  150. export function parseRevokeInstruction<
  151. TProgram extends string,
  152. TAccountMetas extends readonly AccountMeta[],
  153. >(
  154. instruction: Instruction<TProgram> &
  155. InstructionWithAccounts<TAccountMetas> &
  156. InstructionWithData<ReadonlyUint8Array>
  157. ): ParsedRevokeInstruction<TProgram, TAccountMetas> {
  158. if (instruction.accounts.length < 2) {
  159. // TODO: Coded error.
  160. throw new Error('Not enough accounts');
  161. }
  162. let accountIndex = 0;
  163. const getNextAccount = () => {
  164. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  165. accountIndex += 1;
  166. return accountMeta;
  167. };
  168. return {
  169. programAddress: instruction.programAddress,
  170. accounts: { source: getNextAccount(), owner: getNextAccount() },
  171. data: getRevokeInstructionDataDecoder().decode(instruction.data),
  172. };
  173. }