Car.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 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(accountInfo: web3.AccountInfo<Buffer>, offset = 0): [Car, number] {
  44. return Car.deserialize(accountInfo.data, offset);
  45. }
  46. /**
  47. * Retrieves the account info from the provided address and deserializes
  48. * the {@link Car} from its data.
  49. *
  50. * @throws Error if no account info is found at the address or if deserialization fails
  51. */
  52. static async fromAccountAddress(
  53. connection: web3.Connection,
  54. address: web3.PublicKey,
  55. commitmentOrConfig?: web3.Commitment | web3.GetAccountInfoConfig,
  56. ): Promise<Car> {
  57. const accountInfo = await connection.getAccountInfo(address, commitmentOrConfig);
  58. if (accountInfo == null) {
  59. throw new Error(`Unable to find Car account at ${address}`);
  60. }
  61. return Car.fromAccountInfo(accountInfo, 0)[0];
  62. }
  63. /**
  64. * Provides a {@link web3.Connection.getProgramAccounts} config builder,
  65. * to fetch accounts matching filters that can be specified via that builder.
  66. *
  67. * @param programId - the program that owns the accounts we are filtering
  68. */
  69. static gpaBuilder(programId: web3.PublicKey = new web3.PublicKey('8avNGHVXDwsELJaWMSoUZ44CirQd4zyU9Ez4ZmP4jNjZ')) {
  70. return beetSolana.GpaBuilder.fromStruct(programId, carBeet);
  71. }
  72. /**
  73. * Deserializes the {@link Car} 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): [Car, number] {
  77. return carBeet.deserialize(buf, offset);
  78. }
  79. /**
  80. * Serializes the {@link Car} 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 carBeet.serialize(this);
  85. }
  86. /**
  87. * Returns the byteSize of a {@link Buffer} holding the serialized data of
  88. * {@link Car} for the provided args.
  89. *
  90. * @param args need to be provided since the byte size for this account
  91. * depends on them
  92. */
  93. static byteSize(args: CarArgs) {
  94. const instance = Car.fromArgs(args);
  95. return carBeet.toFixedFromValue(instance).byteSize;
  96. }
  97. /**
  98. * Fetches the minimum balance needed to exempt an account holding
  99. * {@link Car} data from rent
  100. *
  101. * @param args need to be provided since the byte size for this account
  102. * depends on them
  103. * @param connection used to retrieve the rent exemption information
  104. */
  105. static async getMinimumBalanceForRentExemption(args: CarArgs, connection: web3.Connection, commitment?: web3.Commitment): Promise<number> {
  106. return connection.getMinimumBalanceForRentExemption(Car.byteSize(args), commitment);
  107. }
  108. /**
  109. * Returns a readable version of {@link Car} properties
  110. * and can be used to convert to JSON and/or logging
  111. */
  112. pretty() {
  113. return {
  114. year: this.year,
  115. make: this.make,
  116. model: this.model,
  117. };
  118. }
  119. }
  120. /**
  121. * @category Accounts
  122. * @category generated
  123. */
  124. export const carBeet = new beet.FixableBeetStruct<Car, CarArgs>(
  125. [
  126. ['year', beet.u16],
  127. ['make', beet.utf8String],
  128. ['model', beet.utf8String],
  129. ],
  130. Car.fromArgs,
  131. 'Car',
  132. );