RentalOrder.ts 5.4 KB

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