index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. isProgramDerivedAddress,
  11. isTransactionSigner as web3JsIsTransactionSigner,
  12. type Address,
  13. type IAccountMeta,
  14. type IAccountSignerMeta,
  15. type ProgramDerivedAddress,
  16. type TransactionSigner,
  17. upgradeRoleToSigner,
  18. } from '@solana/web3.js';
  19. /**
  20. * Asserts that the given value is not null or undefined.
  21. * @internal
  22. */
  23. export function expectSome<T>(value: T | null | undefined): T {
  24. if (value == null) {
  25. throw new Error('Expected a value but received null or undefined.');
  26. }
  27. return value;
  28. }
  29. /**
  30. * Asserts that the given value is a PublicKey.
  31. * @internal
  32. */
  33. export function expectAddress<T extends string = string>(
  34. value:
  35. | Address<T>
  36. | ProgramDerivedAddress<T>
  37. | TransactionSigner<T>
  38. | null
  39. | undefined
  40. ): Address<T> {
  41. if (!value) {
  42. throw new Error('Expected a Address.');
  43. }
  44. if (typeof value === 'object' && 'address' in value) {
  45. return value.address;
  46. }
  47. if (Array.isArray(value)) {
  48. return value[0];
  49. }
  50. return value as Address<T>;
  51. }
  52. /**
  53. * Asserts that the given value is a PDA.
  54. * @internal
  55. */
  56. export function expectProgramDerivedAddress<T extends string = string>(
  57. value:
  58. | Address<T>
  59. | ProgramDerivedAddress<T>
  60. | TransactionSigner<T>
  61. | null
  62. | undefined
  63. ): ProgramDerivedAddress<T> {
  64. if (!value || !Array.isArray(value) || !isProgramDerivedAddress(value)) {
  65. throw new Error('Expected a ProgramDerivedAddress.');
  66. }
  67. return value;
  68. }
  69. /**
  70. * Asserts that the given value is a TransactionSigner.
  71. * @internal
  72. */
  73. export function expectTransactionSigner<T extends string = string>(
  74. value:
  75. | Address<T>
  76. | ProgramDerivedAddress<T>
  77. | TransactionSigner<T>
  78. | null
  79. | undefined
  80. ): TransactionSigner<T> {
  81. if (!value || !isTransactionSigner(value)) {
  82. throw new Error('Expected a TransactionSigner.');
  83. }
  84. return value;
  85. }
  86. /**
  87. * Defines an instruction account to resolve.
  88. * @internal
  89. */
  90. export type ResolvedAccount<
  91. T extends string = string,
  92. U extends
  93. | Address<T>
  94. | ProgramDerivedAddress<T>
  95. | TransactionSigner<T>
  96. | null =
  97. | Address<T>
  98. | ProgramDerivedAddress<T>
  99. | TransactionSigner<T>
  100. | null,
  101. > = {
  102. isWritable: boolean;
  103. value: U;
  104. };
  105. /**
  106. * Defines an instruction that stores additional bytes on-chain.
  107. * @internal
  108. */
  109. export type IInstructionWithByteDelta = {
  110. byteDelta: number;
  111. };
  112. /**
  113. * Get account metas and signers from resolved accounts.
  114. * @internal
  115. */
  116. export function getAccountMetaFactory(
  117. programAddress: Address,
  118. optionalAccountStrategy: 'omitted' | 'programId'
  119. ) {
  120. return (
  121. account: ResolvedAccount
  122. ): IAccountMeta | IAccountSignerMeta | undefined => {
  123. if (!account.value) {
  124. if (optionalAccountStrategy === 'omitted') return;
  125. return Object.freeze({
  126. address: programAddress,
  127. role: AccountRole.READONLY,
  128. });
  129. }
  130. const writableRole = account.isWritable
  131. ? AccountRole.WRITABLE
  132. : AccountRole.READONLY;
  133. return Object.freeze({
  134. address: expectAddress(account.value),
  135. role: isTransactionSigner(account.value)
  136. ? upgradeRoleToSigner(writableRole)
  137. : writableRole,
  138. ...(isTransactionSigner(account.value) ? { signer: account.value } : {}),
  139. });
  140. };
  141. }
  142. export function isTransactionSigner<TAddress extends string = string>(
  143. value:
  144. | Address<TAddress>
  145. | ProgramDerivedAddress<TAddress>
  146. | TransactionSigner<TAddress>
  147. ): value is TransactionSigner<TAddress> {
  148. return (
  149. !!value &&
  150. typeof value === 'object' &&
  151. 'address' in value &&
  152. web3JsIsTransactionSigner(value)
  153. );
  154. }