price_info.move 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. module pyth::price_info {
  2. use sui::object::{Self, UID, ID};
  3. use sui::tx_context::{TxContext};
  4. use sui::dynamic_object_field::{Self};
  5. use sui::table::{Self};
  6. use pyth::price_feed::{Self, PriceFeed};
  7. use pyth::price_identifier::{PriceIdentifier};
  8. const KEY: vector<u8> = b"price_info";
  9. const E_PRICE_INFO_REGISTRY_ALREADY_EXISTS: u64 = 0;
  10. const E_PRICE_IDENTIFIER_ALREADY_REGISTERED: u64 = 1;
  11. friend pyth::pyth;
  12. /// Sui Object version of PriceInfo.
  13. /// Has a key and lives in global store.
  14. struct PriceInfoObject has key, store {
  15. id: UID,
  16. price_info: PriceInfo
  17. }
  18. /// Copyable and droppable.
  19. struct PriceInfo has copy, drop, store {
  20. attestation_time: u64,
  21. arrival_time: u64,
  22. price_feed: PriceFeed,
  23. }
  24. /// Creates a table which maps a PriceIdentifier to the
  25. /// UID (in bytes) of the corresponding Sui PriceInfoObject.
  26. public fun new_price_info_registry(parent_id: &mut UID, ctx: &mut TxContext) {
  27. assert!(
  28. !dynamic_object_field::exists_(parent_id, KEY),
  29. E_PRICE_INFO_REGISTRY_ALREADY_EXISTS
  30. );
  31. dynamic_object_field::add(
  32. parent_id,
  33. KEY,
  34. table::new<PriceIdentifier, ID>(ctx)
  35. )
  36. }
  37. public fun add(parent_id: &mut UID, price_identifier: PriceIdentifier, id: ID) {
  38. assert!(
  39. !contains(parent_id, price_identifier),
  40. E_PRICE_IDENTIFIER_ALREADY_REGISTERED
  41. );
  42. table::add(
  43. dynamic_object_field::borrow_mut(parent_id, KEY),
  44. price_identifier,
  45. id
  46. )
  47. }
  48. public fun contains(parent_id: &UID, price_identifier: PriceIdentifier): bool {
  49. let ref = dynamic_object_field::borrow(parent_id, KEY);
  50. table::contains<PriceIdentifier, ID>(ref, price_identifier)
  51. }
  52. public fun new_price_info_object(
  53. price_info: PriceInfo,
  54. ctx: &mut TxContext
  55. ): PriceInfoObject {
  56. PriceInfoObject {
  57. id: object::new(ctx),
  58. price_info: price_info
  59. }
  60. }
  61. public fun new_price_info(
  62. attestation_time: u64,
  63. arrival_time: u64,
  64. price_feed: PriceFeed,
  65. ): PriceInfo {
  66. PriceInfo {
  67. attestation_time: attestation_time,
  68. arrival_time: arrival_time,
  69. price_feed: price_feed,
  70. }
  71. }
  72. #[test_only]
  73. public fun destroy(price_info: PriceInfoObject){
  74. let PriceInfoObject {
  75. id: id,
  76. price_info: _,
  77. } = price_info;
  78. object::delete(id);
  79. }
  80. public fun uid_to_inner(price_info: &PriceInfoObject): ID {
  81. object::uid_to_inner(&price_info.id)
  82. }
  83. public fun get_price_info_from_price_info_object(price_info: &PriceInfoObject): PriceInfo {
  84. price_info.price_info
  85. }
  86. public fun get_price_identifier(price_info: &PriceInfo): PriceIdentifier {
  87. price_feed::get_price_identifier(&price_info.price_feed)
  88. }
  89. public fun get_price_feed(price_info: &PriceInfo): &PriceFeed {
  90. &price_info.price_feed
  91. }
  92. public fun get_attestation_time(price_info: &PriceInfo): u64 {
  93. price_info.attestation_time
  94. }
  95. public fun get_arrival_time(price_info: &PriceInfo): u64 {
  96. price_info.arrival_time
  97. }
  98. public(friend) fun update_price_info_object(
  99. price_info_object: &mut PriceInfoObject,
  100. price_info: PriceInfo
  101. ) {
  102. price_info_object.price_info = price_info;
  103. }
  104. }