setAuthority.ts 6.7 KB

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