RentalOrder.ts 5.5 KB

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