nonce.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. getStructDecoder,
  18. getStructEncoder,
  19. getU64Decoder,
  20. getU64Encoder,
  21. type Account,
  22. type Address,
  23. type EncodedAccount,
  24. type FetchAccountConfig,
  25. type FetchAccountsConfig,
  26. type FixedSizeCodec,
  27. type FixedSizeDecoder,
  28. type FixedSizeEncoder,
  29. type MaybeAccount,
  30. type MaybeEncodedAccount,
  31. } from '@solana/kit';
  32. import {
  33. getNonceStateDecoder,
  34. getNonceStateEncoder,
  35. getNonceVersionDecoder,
  36. getNonceVersionEncoder,
  37. type NonceState,
  38. type NonceStateArgs,
  39. type NonceVersion,
  40. type NonceVersionArgs,
  41. } from '../types';
  42. export type Nonce = {
  43. version: NonceVersion;
  44. state: NonceState;
  45. authority: Address;
  46. blockhash: Address;
  47. lamportsPerSignature: bigint;
  48. };
  49. export type NonceArgs = {
  50. version: NonceVersionArgs;
  51. state: NonceStateArgs;
  52. authority: Address;
  53. blockhash: Address;
  54. lamportsPerSignature: number | bigint;
  55. };
  56. export function getNonceEncoder(): FixedSizeEncoder<NonceArgs> {
  57. return getStructEncoder([
  58. ['version', getNonceVersionEncoder()],
  59. ['state', getNonceStateEncoder()],
  60. ['authority', getAddressEncoder()],
  61. ['blockhash', getAddressEncoder()],
  62. ['lamportsPerSignature', getU64Encoder()],
  63. ]);
  64. }
  65. export function getNonceDecoder(): FixedSizeDecoder<Nonce> {
  66. return getStructDecoder([
  67. ['version', getNonceVersionDecoder()],
  68. ['state', getNonceStateDecoder()],
  69. ['authority', getAddressDecoder()],
  70. ['blockhash', getAddressDecoder()],
  71. ['lamportsPerSignature', getU64Decoder()],
  72. ]);
  73. }
  74. export function getNonceCodec(): FixedSizeCodec<NonceArgs, Nonce> {
  75. return combineCodec(getNonceEncoder(), getNonceDecoder());
  76. }
  77. export function decodeNonce<TAddress extends string = string>(
  78. encodedAccount: EncodedAccount<TAddress>
  79. ): Account<Nonce, TAddress>;
  80. export function decodeNonce<TAddress extends string = string>(
  81. encodedAccount: MaybeEncodedAccount<TAddress>
  82. ): MaybeAccount<Nonce, TAddress>;
  83. export function decodeNonce<TAddress extends string = string>(
  84. encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>
  85. ): Account<Nonce, TAddress> | MaybeAccount<Nonce, TAddress> {
  86. return decodeAccount(
  87. encodedAccount as MaybeEncodedAccount<TAddress>,
  88. getNonceDecoder()
  89. );
  90. }
  91. export async function fetchNonce<TAddress extends string = string>(
  92. rpc: Parameters<typeof fetchEncodedAccount>[0],
  93. address: Address<TAddress>,
  94. config?: FetchAccountConfig
  95. ): Promise<Account<Nonce, TAddress>> {
  96. const maybeAccount = await fetchMaybeNonce(rpc, address, config);
  97. assertAccountExists(maybeAccount);
  98. return maybeAccount;
  99. }
  100. export async function fetchMaybeNonce<TAddress extends string = string>(
  101. rpc: Parameters<typeof fetchEncodedAccount>[0],
  102. address: Address<TAddress>,
  103. config?: FetchAccountConfig
  104. ): Promise<MaybeAccount<Nonce, TAddress>> {
  105. const maybeAccount = await fetchEncodedAccount(rpc, address, config);
  106. return decodeNonce(maybeAccount);
  107. }
  108. export async function fetchAllNonce(
  109. rpc: Parameters<typeof fetchEncodedAccounts>[0],
  110. addresses: Array<Address>,
  111. config?: FetchAccountsConfig
  112. ): Promise<Account<Nonce>[]> {
  113. const maybeAccounts = await fetchAllMaybeNonce(rpc, addresses, config);
  114. assertAccountsExist(maybeAccounts);
  115. return maybeAccounts;
  116. }
  117. export async function fetchAllMaybeNonce(
  118. rpc: Parameters<typeof fetchEncodedAccounts>[0],
  119. addresses: Array<Address>,
  120. config?: FetchAccountsConfig
  121. ): Promise<MaybeAccount<Nonce>[]> {
  122. const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
  123. return maybeAccounts.map((maybeAccount) => decodeNonce(maybeAccount));
  124. }
  125. export function getNonceSize(): number {
  126. return 80;
  127. }