1
0

setAuthority.ts 6.7 KB

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