Counter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * This code was GENERATED using the solita package.
  3. * Please DO NOT EDIT THIS FILE, instead rerun solita to update it or write a wrapper to add functionality.
  4. *
  5. * See: https://github.com/metaplex-foundation/solita
  6. */
  7. import * as beet from '@metaplex-foundation/beet';
  8. import * as beetSolana from '@metaplex-foundation/beet-solana';
  9. import * as web3 from '@solana/web3.js';
  10. /**
  11. * Arguments used to create {@link Counter}
  12. * @category Accounts
  13. * @category generated
  14. */
  15. export type CounterArgs = {
  16. count: beet.bignum;
  17. };
  18. /**
  19. * Holds the data for the {@link Counter} Account and provides de/serialization
  20. * functionality for that data
  21. *
  22. * @category Accounts
  23. * @category generated
  24. */
  25. export class Counter implements CounterArgs {
  26. private constructor(readonly count: beet.bignum) {}
  27. /**
  28. * Creates a {@link Counter} instance from the provided args.
  29. */
  30. static fromArgs(args: CounterArgs) {
  31. return new Counter(args.count);
  32. }
  33. /**
  34. * Deserializes the {@link Counter} from the data of the provided {@link web3.AccountInfo}.
  35. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it.
  36. */
  37. static fromAccountInfo(accountInfo: web3.AccountInfo<Buffer>, offset = 0): [Counter, number] {
  38. return Counter.deserialize(accountInfo.data, offset);
  39. }
  40. /**
  41. * Retrieves the account info from the provided address and deserializes
  42. * the {@link Counter} from its data.
  43. *
  44. * @throws Error if no account info is found at the address or if deserialization fails
  45. */
  46. static async fromAccountAddress(connection: web3.Connection, address: web3.PublicKey): Promise<Counter> {
  47. const accountInfo = await connection.getAccountInfo(address, {
  48. commitment: 'confirmed',
  49. });
  50. if (accountInfo == null) {
  51. throw new Error(`Unable to find Counter account at ${address}`);
  52. }
  53. return Counter.fromAccountInfo(accountInfo, 0)[0];
  54. }
  55. /**
  56. * Provides a {@link web3.Connection.getProgramAccounts} config builder,
  57. * to fetch accounts matching filters that can be specified via that builder.
  58. *
  59. * @param programId - the program that owns the accounts we are filtering
  60. */
  61. static gpaBuilder(programId: web3.PublicKey = new web3.PublicKey('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')) {
  62. return beetSolana.GpaBuilder.fromStruct(programId, counterBeet);
  63. }
  64. /**
  65. * Deserializes the {@link Counter} from the provided data Buffer.
  66. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it.
  67. */
  68. static deserialize(buf: Buffer, offset = 0): [Counter, number] {
  69. return counterBeet.deserialize(buf, offset);
  70. }
  71. /**
  72. * Serializes the {@link Counter} into a Buffer.
  73. * @returns a tuple of the created Buffer and the offset up to which the buffer was written to store it.
  74. */
  75. serialize(): [Buffer, number] {
  76. return counterBeet.serialize(this);
  77. }
  78. /**
  79. * Returns the byteSize of a {@link Buffer} holding the serialized data of
  80. * {@link Counter}
  81. */
  82. static get byteSize() {
  83. return counterBeet.byteSize;
  84. }
  85. /**
  86. * Fetches the minimum balance needed to exempt an account holding
  87. * {@link Counter} data from rent
  88. *
  89. * @param connection used to retrieve the rent exemption information
  90. */
  91. static async getMinimumBalanceForRentExemption(connection: web3.Connection, commitment?: web3.Commitment): Promise<number> {
  92. return connection.getMinimumBalanceForRentExemption(Counter.byteSize, commitment);
  93. }
  94. /**
  95. * Determines if the provided {@link Buffer} has the correct byte size to
  96. * hold {@link Counter} data.
  97. */
  98. static hasCorrectByteSize(buf: Buffer, offset = 0) {
  99. return buf.byteLength - offset === Counter.byteSize;
  100. }
  101. /**
  102. * Returns a readable version of {@link Counter} properties
  103. * and can be used to convert to JSON and/or logging
  104. */
  105. pretty() {
  106. return {
  107. count: (() => {
  108. const x = <{ toNumber: () => number }>this.count;
  109. if (typeof x.toNumber === 'function') {
  110. try {
  111. return x.toNumber();
  112. } catch (_) {
  113. return x;
  114. }
  115. }
  116. return x;
  117. })(),
  118. };
  119. }
  120. }
  121. /**
  122. * @category Accounts
  123. * @category generated
  124. */
  125. export const counterBeet = new beet.BeetStruct<Counter, CounterArgs>([['count', beet.u64]], Counter.fromArgs, 'Counter');