revoke.ts 5.1 KB

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