setAuthority.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/kinobi-so/kinobi
  7. */
  8. import {
  9. AccountRole,
  10. combineCodec,
  11. getAddressDecoder,
  12. getAddressEncoder,
  13. getOptionDecoder,
  14. getOptionEncoder,
  15. getStructDecoder,
  16. getStructEncoder,
  17. getU8Decoder,
  18. getU8Encoder,
  19. transformEncoder,
  20. type Address,
  21. type Codec,
  22. type Decoder,
  23. type Encoder,
  24. type IAccountMeta,
  25. type IAccountSignerMeta,
  26. type IInstruction,
  27. type IInstructionWithAccounts,
  28. type IInstructionWithData,
  29. type Option,
  30. type OptionOrNullable,
  31. type ReadonlyAccount,
  32. type ReadonlySignerAccount,
  33. type TransactionSigner,
  34. type WritableAccount,
  35. } from '@solana/web3.js';
  36. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  37. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  38. import {
  39. getAuthorityTypeDecoder,
  40. getAuthorityTypeEncoder,
  41. type AuthorityType,
  42. type AuthorityTypeArgs,
  43. } from '../types';
  44. export type SetAuthorityInstruction<
  45. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  46. TAccountOwned extends string | IAccountMeta<string> = string,
  47. TAccountOwner extends string | IAccountMeta<string> = string,
  48. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  49. > = IInstruction<TProgram> &
  50. IInstructionWithData<Uint8Array> &
  51. IInstructionWithAccounts<
  52. [
  53. TAccountOwned extends string
  54. ? WritableAccount<TAccountOwned>
  55. : TAccountOwned,
  56. TAccountOwner extends string
  57. ? ReadonlyAccount<TAccountOwner>
  58. : TAccountOwner,
  59. ...TRemainingAccounts,
  60. ]
  61. >;
  62. export type SetAuthorityInstructionData = {
  63. discriminator: number;
  64. /** The type of authority to update. */
  65. authorityType: AuthorityType;
  66. /** The new authority */
  67. newAuthority: Option<Address>;
  68. };
  69. export type SetAuthorityInstructionDataArgs = {
  70. /** The type of authority to update. */
  71. authorityType: AuthorityTypeArgs;
  72. /** The new authority */
  73. newAuthority: OptionOrNullable<Address>;
  74. };
  75. export function getSetAuthorityInstructionDataEncoder(): Encoder<SetAuthorityInstructionDataArgs> {
  76. return transformEncoder(
  77. getStructEncoder([
  78. ['discriminator', getU8Encoder()],
  79. ['authorityType', getAuthorityTypeEncoder()],
  80. ['newAuthority', getOptionEncoder(getAddressEncoder())],
  81. ]),
  82. (value) => ({ ...value, discriminator: 6 })
  83. );
  84. }
  85. export function getSetAuthorityInstructionDataDecoder(): Decoder<SetAuthorityInstructionData> {
  86. return getStructDecoder([
  87. ['discriminator', getU8Decoder()],
  88. ['authorityType', getAuthorityTypeDecoder()],
  89. ['newAuthority', getOptionDecoder(getAddressDecoder())],
  90. ]);
  91. }
  92. export function getSetAuthorityInstructionDataCodec(): Codec<
  93. SetAuthorityInstructionDataArgs,
  94. SetAuthorityInstructionData
  95. > {
  96. return combineCodec(
  97. getSetAuthorityInstructionDataEncoder(),
  98. getSetAuthorityInstructionDataDecoder()
  99. );
  100. }
  101. export type SetAuthorityInput<
  102. TAccountOwned extends string = string,
  103. TAccountOwner extends string = string,
  104. > = {
  105. /** The mint or account to change the authority of. */
  106. owned: Address<TAccountOwned>;
  107. /** The current authority or the multisignature account of the mint or account to update. */
  108. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  109. authorityType: SetAuthorityInstructionDataArgs['authorityType'];
  110. newAuthority: SetAuthorityInstructionDataArgs['newAuthority'];
  111. multiSigners?: Array<TransactionSigner>;
  112. };
  113. export function getSetAuthorityInstruction<
  114. TAccountOwned extends string,
  115. TAccountOwner extends string,
  116. >(
  117. input: SetAuthorityInput<TAccountOwned, TAccountOwner>
  118. ): SetAuthorityInstruction<
  119. typeof TOKEN_PROGRAM_ADDRESS,
  120. TAccountOwned,
  121. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  122. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  123. : TAccountOwner
  124. > {
  125. // Program address.
  126. const programAddress = TOKEN_PROGRAM_ADDRESS;
  127. // Original accounts.
  128. const originalAccounts = {
  129. owned: { value: input.owned ?? null, isWritable: true },
  130. owner: { value: input.owner ?? null, isWritable: false },
  131. };
  132. const accounts = originalAccounts as Record<
  133. keyof typeof originalAccounts,
  134. ResolvedAccount
  135. >;
  136. // Original args.
  137. const args = { ...input };
  138. // Remaining accounts.
  139. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  140. (signer) => ({
  141. address: signer.address,
  142. role: AccountRole.READONLY_SIGNER,
  143. signer,
  144. })
  145. );
  146. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  147. const instruction = {
  148. accounts: [
  149. getAccountMeta(accounts.owned),
  150. getAccountMeta(accounts.owner),
  151. ...remainingAccounts,
  152. ],
  153. programAddress,
  154. data: getSetAuthorityInstructionDataEncoder().encode(
  155. args as SetAuthorityInstructionDataArgs
  156. ),
  157. } as SetAuthorityInstruction<
  158. typeof TOKEN_PROGRAM_ADDRESS,
  159. TAccountOwned,
  160. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  161. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  162. : TAccountOwner
  163. >;
  164. return instruction;
  165. }
  166. export type ParsedSetAuthorityInstruction<
  167. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  168. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  169. > = {
  170. programAddress: Address<TProgram>;
  171. accounts: {
  172. /** The mint or account to change the authority of. */
  173. owned: TAccountMetas[0];
  174. /** The current authority or the multisignature account of the mint or account to update. */
  175. owner: TAccountMetas[1];
  176. };
  177. data: SetAuthorityInstructionData;
  178. };
  179. export function parseSetAuthorityInstruction<
  180. TProgram extends string,
  181. TAccountMetas extends readonly IAccountMeta[],
  182. >(
  183. instruction: IInstruction<TProgram> &
  184. IInstructionWithAccounts<TAccountMetas> &
  185. IInstructionWithData<Uint8Array>
  186. ): ParsedSetAuthorityInstruction<TProgram, TAccountMetas> {
  187. if (instruction.accounts.length < 2) {
  188. // TODO: Coded error.
  189. throw new Error('Not enough accounts');
  190. }
  191. let accountIndex = 0;
  192. const getNextAccount = () => {
  193. const accountMeta = instruction.accounts![accountIndex]!;
  194. accountIndex += 1;
  195. return accountMeta;
  196. };
  197. return {
  198. programAddress: instruction.programAddress,
  199. accounts: {
  200. owned: getNextAccount(),
  201. owner: getNextAccount(),
  202. },
  203. data: getSetAuthorityInstructionDataDecoder().decode(instruction.data),
  204. };
  205. }