token.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. assertAccountExists,
  10. assertAccountsExist,
  11. combineCodec,
  12. decodeAccount,
  13. fetchEncodedAccount,
  14. fetchEncodedAccounts,
  15. getAddressDecoder,
  16. getAddressEncoder,
  17. getOptionDecoder,
  18. getOptionEncoder,
  19. getStructDecoder,
  20. getStructEncoder,
  21. getU32Decoder,
  22. getU32Encoder,
  23. getU64Decoder,
  24. getU64Encoder,
  25. type Account,
  26. type Address,
  27. type Codec,
  28. type Decoder,
  29. type EncodedAccount,
  30. type Encoder,
  31. type FetchAccountConfig,
  32. type FetchAccountsConfig,
  33. type MaybeAccount,
  34. type MaybeEncodedAccount,
  35. type Option,
  36. type OptionOrNullable,
  37. } from '@solana/kit';
  38. import {
  39. getAccountStateDecoder,
  40. getAccountStateEncoder,
  41. type AccountState,
  42. type AccountStateArgs,
  43. } from '../types';
  44. export type Token = {
  45. /** The mint associated with this account. */
  46. mint: Address;
  47. /** The owner of this account. */
  48. owner: Address;
  49. /** The amount of tokens this account holds. */
  50. amount: bigint;
  51. /**
  52. * If `delegate` is `Some` then `delegated_amount` represents
  53. * the amount authorized by the delegate.
  54. */
  55. delegate: Option<Address>;
  56. /** The account's state. */
  57. state: AccountState;
  58. /**
  59. * If is_native.is_some, this is a native token, and the value logs the
  60. * rent-exempt reserve. An Account is required to be rent-exempt, so
  61. * the value is used by the Processor to ensure that wrapped SOL
  62. * accounts do not drop below this threshold.
  63. */
  64. isNative: Option<bigint>;
  65. /** The amount delegated. */
  66. delegatedAmount: bigint;
  67. /** Optional authority to close the account. */
  68. closeAuthority: Option<Address>;
  69. };
  70. export type TokenArgs = {
  71. /** The mint associated with this account. */
  72. mint: Address;
  73. /** The owner of this account. */
  74. owner: Address;
  75. /** The amount of tokens this account holds. */
  76. amount: number | bigint;
  77. /**
  78. * If `delegate` is `Some` then `delegated_amount` represents
  79. * the amount authorized by the delegate.
  80. */
  81. delegate: OptionOrNullable<Address>;
  82. /** The account's state. */
  83. state: AccountStateArgs;
  84. /**
  85. * If is_native.is_some, this is a native token, and the value logs the
  86. * rent-exempt reserve. An Account is required to be rent-exempt, so
  87. * the value is used by the Processor to ensure that wrapped SOL
  88. * accounts do not drop below this threshold.
  89. */
  90. isNative: OptionOrNullable<number | bigint>;
  91. /** The amount delegated. */
  92. delegatedAmount: number | bigint;
  93. /** Optional authority to close the account. */
  94. closeAuthority: OptionOrNullable<Address>;
  95. };
  96. export function getTokenEncoder(): Encoder<TokenArgs> {
  97. return getStructEncoder([
  98. ['mint', getAddressEncoder()],
  99. ['owner', getAddressEncoder()],
  100. ['amount', getU64Encoder()],
  101. [
  102. 'delegate',
  103. getOptionEncoder(getAddressEncoder(), {
  104. prefix: getU32Encoder(),
  105. noneValue: 'zeroes',
  106. }),
  107. ],
  108. ['state', getAccountStateEncoder()],
  109. [
  110. 'isNative',
  111. getOptionEncoder(getU64Encoder(), {
  112. prefix: getU32Encoder(),
  113. noneValue: 'zeroes',
  114. }),
  115. ],
  116. ['delegatedAmount', getU64Encoder()],
  117. [
  118. 'closeAuthority',
  119. getOptionEncoder(getAddressEncoder(), {
  120. prefix: getU32Encoder(),
  121. noneValue: 'zeroes',
  122. }),
  123. ],
  124. ]);
  125. }
  126. export function getTokenDecoder(): Decoder<Token> {
  127. return getStructDecoder([
  128. ['mint', getAddressDecoder()],
  129. ['owner', getAddressDecoder()],
  130. ['amount', getU64Decoder()],
  131. [
  132. 'delegate',
  133. getOptionDecoder(getAddressDecoder(), {
  134. prefix: getU32Decoder(),
  135. noneValue: 'zeroes',
  136. }),
  137. ],
  138. ['state', getAccountStateDecoder()],
  139. [
  140. 'isNative',
  141. getOptionDecoder(getU64Decoder(), {
  142. prefix: getU32Decoder(),
  143. noneValue: 'zeroes',
  144. }),
  145. ],
  146. ['delegatedAmount', getU64Decoder()],
  147. [
  148. 'closeAuthority',
  149. getOptionDecoder(getAddressDecoder(), {
  150. prefix: getU32Decoder(),
  151. noneValue: 'zeroes',
  152. }),
  153. ],
  154. ]);
  155. }
  156. export function getTokenCodec(): Codec<TokenArgs, Token> {
  157. return combineCodec(getTokenEncoder(), getTokenDecoder());
  158. }
  159. export function decodeToken<TAddress extends string = string>(
  160. encodedAccount: EncodedAccount<TAddress>
  161. ): Account<Token, TAddress>;
  162. export function decodeToken<TAddress extends string = string>(
  163. encodedAccount: MaybeEncodedAccount<TAddress>
  164. ): MaybeAccount<Token, TAddress>;
  165. export function decodeToken<TAddress extends string = string>(
  166. encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>
  167. ): Account<Token, TAddress> | MaybeAccount<Token, TAddress> {
  168. return decodeAccount(
  169. encodedAccount as MaybeEncodedAccount<TAddress>,
  170. getTokenDecoder()
  171. );
  172. }
  173. export async function fetchToken<TAddress extends string = string>(
  174. rpc: Parameters<typeof fetchEncodedAccount>[0],
  175. address: Address<TAddress>,
  176. config?: FetchAccountConfig
  177. ): Promise<Account<Token, TAddress>> {
  178. const maybeAccount = await fetchMaybeToken(rpc, address, config);
  179. assertAccountExists(maybeAccount);
  180. return maybeAccount;
  181. }
  182. export async function fetchMaybeToken<TAddress extends string = string>(
  183. rpc: Parameters<typeof fetchEncodedAccount>[0],
  184. address: Address<TAddress>,
  185. config?: FetchAccountConfig
  186. ): Promise<MaybeAccount<Token, TAddress>> {
  187. const maybeAccount = await fetchEncodedAccount(rpc, address, config);
  188. return decodeToken(maybeAccount);
  189. }
  190. export async function fetchAllToken(
  191. rpc: Parameters<typeof fetchEncodedAccounts>[0],
  192. addresses: Array<Address>,
  193. config?: FetchAccountsConfig
  194. ): Promise<Account<Token>[]> {
  195. const maybeAccounts = await fetchAllMaybeToken(rpc, addresses, config);
  196. assertAccountsExist(maybeAccounts);
  197. return maybeAccounts;
  198. }
  199. export async function fetchAllMaybeToken(
  200. rpc: Parameters<typeof fetchEncodedAccounts>[0],
  201. addresses: Array<Address>,
  202. config?: FetchAccountsConfig
  203. ): Promise<MaybeAccount<Token>[]> {
  204. const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
  205. return maybeAccounts.map((maybeAccount) => decodeToken(maybeAccount));
  206. }
  207. export function getTokenSize(): number {
  208. return 165;
  209. }