Car.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Car}
  12. * @category Accounts
  13. * @category generated
  14. */
  15. export type CarArgs = {
  16. year: number
  17. make: string
  18. model: string
  19. }
  20. /**
  21. * Holds the data for the {@link Car} Account and provides de/serialization
  22. * functionality for that data
  23. *
  24. * @category Accounts
  25. * @category generated
  26. */
  27. export class Car implements CarArgs {
  28. private constructor(
  29. readonly year: number,
  30. readonly make: string,
  31. readonly model: string
  32. ) {}
  33. /**
  34. * Creates a {@link Car} instance from the provided args.
  35. */
  36. static fromArgs(args: CarArgs) {
  37. return new Car(args.year, args.make, args.model)
  38. }
  39. /**
  40. * Deserializes the {@link Car} from the data of the provided {@link web3.AccountInfo}.
  41. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it.
  42. */
  43. static fromAccountInfo(
  44. accountInfo: web3.AccountInfo<Buffer>,
  45. offset = 0
  46. ): [Car, number] {
  47. return Car.deserialize(accountInfo.data, offset)
  48. }
  49. /**
  50. * Retrieves the account info from the provided address and deserializes
  51. * the {@link Car} from its data.
  52. *
  53. * @throws Error if no account info is found at the address or if deserialization fails
  54. */
  55. static async fromAccountAddress(
  56. connection: web3.Connection,
  57. address: web3.PublicKey,
  58. commitmentOrConfig?: web3.Commitment | web3.GetAccountInfoConfig
  59. ): Promise<Car> {
  60. const accountInfo = await connection.getAccountInfo(
  61. address,
  62. commitmentOrConfig
  63. )
  64. if (accountInfo == null) {
  65. throw new Error(`Unable to find Car account at ${address}`)
  66. }
  67. return Car.fromAccountInfo(accountInfo, 0)[0]
  68. }
  69. /**
  70. * Provides a {@link web3.Connection.getProgramAccounts} config builder,
  71. * to fetch accounts matching filters that can be specified via that builder.
  72. *
  73. * @param programId - the program that owns the accounts we are filtering
  74. */
  75. static gpaBuilder(
  76. programId: web3.PublicKey = new web3.PublicKey(
  77. '8avNGHVXDwsELJaWMSoUZ44CirQd4zyU9Ez4ZmP4jNjZ'
  78. )
  79. ) {
  80. return beetSolana.GpaBuilder.fromStruct(programId, carBeet)
  81. }
  82. /**
  83. * Deserializes the {@link Car} from the provided data Buffer.
  84. * @returns a tuple of the account data and the offset up to which the buffer was read to obtain it.
  85. */
  86. static deserialize(buf: Buffer, offset = 0): [Car, number] {
  87. return carBeet.deserialize(buf, offset)
  88. }
  89. /**
  90. * Serializes the {@link Car} into a Buffer.
  91. * @returns a tuple of the created Buffer and the offset up to which the buffer was written to store it.
  92. */
  93. serialize(): [Buffer, number] {
  94. return carBeet.serialize(this)
  95. }
  96. /**
  97. * Returns the byteSize of a {@link Buffer} holding the serialized data of
  98. * {@link Car} for the provided args.
  99. *
  100. * @param args need to be provided since the byte size for this account
  101. * depends on them
  102. */
  103. static byteSize(args: CarArgs) {
  104. const instance = Car.fromArgs(args)
  105. return carBeet.toFixedFromValue(instance).byteSize
  106. }
  107. /**
  108. * Fetches the minimum balance needed to exempt an account holding
  109. * {@link Car} data from rent
  110. *
  111. * @param args need to be provided since the byte size for this account
  112. * depends on them
  113. * @param connection used to retrieve the rent exemption information
  114. */
  115. static async getMinimumBalanceForRentExemption(
  116. args: CarArgs,
  117. connection: web3.Connection,
  118. commitment?: web3.Commitment
  119. ): Promise<number> {
  120. return connection.getMinimumBalanceForRentExemption(
  121. Car.byteSize(args),
  122. commitment
  123. )
  124. }
  125. /**
  126. * Returns a readable version of {@link Car} properties
  127. * and can be used to convert to JSON and/or logging
  128. */
  129. pretty() {
  130. return {
  131. year: this.year,
  132. make: this.make,
  133. model: this.model,
  134. }
  135. }
  136. }
  137. /**
  138. * @category Accounts
  139. * @category generated
  140. */
  141. export const carBeet = new beet.FixableBeetStruct<Car, CarArgs>(
  142. [
  143. ['year', beet.u16],
  144. ['make', beet.utf8String],
  145. ['model', beet.utf8String],
  146. ],
  147. Car.fromArgs,
  148. 'Car'
  149. )