Counter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 web3 from '@solana/web3.js'
  9. import * as beetSolana from '@metaplex-foundation/beet-solana'
  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(
  38. accountInfo: web3.AccountInfo<Buffer>,
  39. offset = 0
  40. ): [Counter, number] {
  41. return Counter.deserialize(accountInfo.data, offset)
  42. }
  43. /**
  44. * Retrieves the account info from the provided address and deserializes
  45. * the {@link Counter} from its data.
  46. *
  47. * @throws Error if no account info is found at the address or if deserialization fails
  48. */
  49. static async fromAccountAddress(
  50. connection: web3.Connection,
  51. address: web3.PublicKey
  52. ): Promise<Counter> {
  53. const accountInfo = await connection.getAccountInfo(address, { commitment: "confirmed" });
  54. if (accountInfo == null) {
  55. throw new Error(`Unable to find Counter account at ${address}`)
  56. }
  57. return Counter.fromAccountInfo(accountInfo, 0)[0]
  58. }
  59. /**
  60. * Provides a {@link web3.Connection.getProgramAccounts} config builder,
  61. * to fetch accounts matching filters that can be specified via that builder.
  62. *
  63. * @param programId - the program that owns the accounts we are filtering
  64. */
  65. static gpaBuilder(
  66. programId: web3.PublicKey = new web3.PublicKey(
  67. 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS'
  68. )
  69. ) {
  70. return beetSolana.GpaBuilder.fromStruct(programId, counterBeet)
  71. }
  72. /**
  73. * Deserializes the {@link Counter} from the provided data Buffer.
  74. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it.
  75. */
  76. static deserialize(buf: Buffer, offset = 0): [Counter, number] {
  77. return counterBeet.deserialize(buf, offset)
  78. }
  79. /**
  80. * Serializes the {@link Counter} into a Buffer.
  81. * @returns a tuple of the created Buffer and the offset up to which the buffer was written to store it.
  82. */
  83. serialize(): [Buffer, number] {
  84. return counterBeet.serialize(this)
  85. }
  86. /**
  87. * Returns the byteSize of a {@link Buffer} holding the serialized data of
  88. * {@link Counter}
  89. */
  90. static get byteSize() {
  91. return counterBeet.byteSize
  92. }
  93. /**
  94. * Fetches the minimum balance needed to exempt an account holding
  95. * {@link Counter} data from rent
  96. *
  97. * @param connection used to retrieve the rent exemption information
  98. */
  99. static async getMinimumBalanceForRentExemption(
  100. connection: web3.Connection,
  101. commitment?: web3.Commitment
  102. ): Promise<number> {
  103. return connection.getMinimumBalanceForRentExemption(
  104. Counter.byteSize,
  105. commitment
  106. )
  107. }
  108. /**
  109. * Determines if the provided {@link Buffer} has the correct byte size to
  110. * hold {@link Counter} data.
  111. */
  112. static hasCorrectByteSize(buf: Buffer, offset = 0) {
  113. return buf.byteLength - offset === Counter.byteSize
  114. }
  115. /**
  116. * Returns a readable version of {@link Counter} properties
  117. * and can be used to convert to JSON and/or logging
  118. */
  119. pretty() {
  120. return {
  121. count: (() => {
  122. const x = <{ toNumber: () => number }>this.count
  123. if (typeof x.toNumber === 'function') {
  124. try {
  125. return x.toNumber()
  126. } catch (_) {
  127. return x
  128. }
  129. }
  130. return x
  131. })(),
  132. }
  133. }
  134. }
  135. /**
  136. * @category Accounts
  137. * @category generated
  138. */
  139. export const counterBeet = new beet.BeetStruct<Counter, CounterArgs>(
  140. [['count', beet.u64]],
  141. Counter.fromArgs,
  142. 'Counter'
  143. )