revoke.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 Address,
  17. type Codec,
  18. type Decoder,
  19. type Encoder,
  20. type IAccountMeta,
  21. type IAccountSignerMeta,
  22. type IInstruction,
  23. type IInstructionWithAccounts,
  24. type IInstructionWithData,
  25. type ReadonlyAccount,
  26. type ReadonlySignerAccount,
  27. type TransactionSigner,
  28. type WritableAccount,
  29. } from '@solana/kit';
  30. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  31. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  32. export const REVOKE_DISCRIMINATOR = 5;
  33. export function getRevokeDiscriminatorBytes() {
  34. return getU8Encoder().encode(REVOKE_DISCRIMINATOR);
  35. }
  36. export type RevokeInstruction<
  37. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  38. TAccountSource extends string | IAccountMeta<string> = string,
  39. TAccountOwner extends string | IAccountMeta<string> = string,
  40. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  41. > = IInstruction<TProgram> &
  42. IInstructionWithData<Uint8Array> &
  43. IInstructionWithAccounts<
  44. [
  45. TAccountSource extends string
  46. ? WritableAccount<TAccountSource>
  47. : TAccountSource,
  48. TAccountOwner extends string
  49. ? ReadonlyAccount<TAccountOwner>
  50. : TAccountOwner,
  51. ...TRemainingAccounts,
  52. ]
  53. >;
  54. export type RevokeInstructionData = { discriminator: number };
  55. export type RevokeInstructionDataArgs = {};
  56. export function getRevokeInstructionDataEncoder(): Encoder<RevokeInstructionDataArgs> {
  57. return transformEncoder(
  58. getStructEncoder([['discriminator', getU8Encoder()]]),
  59. (value) => ({ ...value, discriminator: REVOKE_DISCRIMINATOR })
  60. );
  61. }
  62. export function getRevokeInstructionDataDecoder(): Decoder<RevokeInstructionData> {
  63. return getStructDecoder([['discriminator', getU8Decoder()]]);
  64. }
  65. export function getRevokeInstructionDataCodec(): Codec<
  66. RevokeInstructionDataArgs,
  67. RevokeInstructionData
  68. > {
  69. return combineCodec(
  70. getRevokeInstructionDataEncoder(),
  71. getRevokeInstructionDataDecoder()
  72. );
  73. }
  74. export type RevokeInput<
  75. TAccountSource extends string = string,
  76. TAccountOwner extends string = string,
  77. > = {
  78. /** The source account. */
  79. source: Address<TAccountSource>;
  80. /** The source account owner or its multisignature. */
  81. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  82. multiSigners?: Array<TransactionSigner>;
  83. };
  84. export function getRevokeInstruction<
  85. TAccountSource extends string,
  86. TAccountOwner extends string,
  87. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  88. >(
  89. input: RevokeInput<TAccountSource, TAccountOwner>,
  90. config?: { programAddress?: TProgramAddress }
  91. ): RevokeInstruction<
  92. TProgramAddress,
  93. TAccountSource,
  94. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  95. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  96. : TAccountOwner
  97. > {
  98. // Program address.
  99. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  100. // Original accounts.
  101. const originalAccounts = {
  102. source: { value: input.source ?? null, isWritable: true },
  103. owner: { value: input.owner ?? null, isWritable: false },
  104. };
  105. const accounts = originalAccounts as Record<
  106. keyof typeof originalAccounts,
  107. ResolvedAccount
  108. >;
  109. // Original args.
  110. const args = { ...input };
  111. // Remaining accounts.
  112. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  113. (signer) => ({
  114. address: signer.address,
  115. role: AccountRole.READONLY_SIGNER,
  116. signer,
  117. })
  118. );
  119. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  120. const instruction = {
  121. accounts: [
  122. getAccountMeta(accounts.source),
  123. getAccountMeta(accounts.owner),
  124. ...remainingAccounts,
  125. ],
  126. programAddress,
  127. data: getRevokeInstructionDataEncoder().encode({}),
  128. } as RevokeInstruction<
  129. TProgramAddress,
  130. TAccountSource,
  131. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  132. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  133. : TAccountOwner
  134. >;
  135. return instruction;
  136. }
  137. export type ParsedRevokeInstruction<
  138. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  139. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  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 IAccountMeta[],
  153. >(
  154. instruction: IInstruction<TProgram> &
  155. IInstructionWithAccounts<TAccountMetas> &
  156. IInstructionWithData<Uint8Array>
  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![accountIndex]!;
  165. accountIndex += 1;
  166. return accountMeta;
  167. };
  168. return {
  169. programAddress: instruction.programAddress,
  170. accounts: {
  171. source: getNextAccount(),
  172. owner: getNextAccount(),
  173. },
  174. data: getRevokeInstructionDataDecoder().decode(instruction.data),
  175. };
  176. }