Skip to content

Commit 9d2d673

Browse files
m30moptke3
andauthored
[WIP] Finalize SUI contracts and make it backward compatible (#935)
* move test accumulator message details to above the definition * camel case to snake case * deserialize and check header inside of parse_and_verify_accumulator_message * factor out cleanup / destruction function for worm / pyth states and clock * take_wormhole_and_pyth_states to simplify 9 tests * get_balance for price_info_object * Store fees as dynamic fields * Rename functions and modules back to the published version to remain bacward compatible * Remove withdrawal functionality for now This functionality can always be added via contract upgrades --------- Co-authored-by: optke3 <[email protected]> Co-authored-by: optke3 <[email protected]>
1 parent 3170e3f commit 9d2d673

File tree

4 files changed

+134
-99
lines changed

4 files changed

+134
-99
lines changed

target_chains/sui/contracts/sources/authenticated_vector.move

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/// This class represents a vector of objects wrapped
2+
/// inside of a hot potato struct.
3+
module pyth::hot_potato_vector {
4+
use std::vector;
5+
6+
const E_EMPTY_HOT_POTATO: u64 = 0;
7+
8+
friend pyth::pyth;
9+
10+
// A hot potato containing a vector of elements
11+
struct HotPotatoVector<T: copy + drop> {
12+
contents: vector<T>
13+
}
14+
15+
// A public destroy function.
16+
public fun destroy<T: copy + drop>(hot_potato_vector: HotPotatoVector<T>) {
17+
let HotPotatoVector { contents: _ } = hot_potato_vector;
18+
}
19+
20+
// Only certain on-chain functions are allowed to create a new hot potato vector.
21+
public(friend) fun new<T: copy + drop>(vec: vector<T>): HotPotatoVector<T> {
22+
HotPotatoVector {
23+
contents: vec
24+
}
25+
}
26+
27+
public fun length<T: copy + drop>(potato: &HotPotatoVector<T>): u64 {
28+
vector::length(&potato.contents)
29+
}
30+
31+
public fun is_empty<T: copy + drop>(potato: &HotPotatoVector<T>): bool {
32+
vector::is_empty(&potato.contents)
33+
}
34+
35+
public fun borrow<T: copy + drop>(potato: &HotPotatoVector<T>, i: u64): &T {
36+
vector::borrow<T>(&potato.contents, i)
37+
}
38+
39+
public(friend) fun pop_back<T: copy + drop>(hot_potato_vector: HotPotatoVector<T>): (T, HotPotatoVector<T>) {
40+
let elem = vector::pop_back<T>(&mut hot_potato_vector.contents);
41+
return (elem, hot_potato_vector)
42+
}
43+
44+
#[test_only]
45+
struct A has copy, drop {
46+
a: u64
47+
}
48+
49+
#[test]
50+
fun test_hot_potato_vector() {
51+
let vec_of_a = vector::empty<A>();
52+
vector::push_back(&mut vec_of_a, A { a: 5 });
53+
vector::push_back(&mut vec_of_a, A { a: 11 });
54+
vector::push_back(&mut vec_of_a, A { a: 23 });
55+
56+
let hot_potato = new<A>(vec_of_a);
57+
let (b, hot_potato) = pop_back<A>(hot_potato);
58+
assert!(b.a == 23, 0);
59+
(b, hot_potato) = pop_back<A>(hot_potato);
60+
assert!(b.a == 11, 0);
61+
let (b, hot_potato) = pop_back<A>(hot_potato);
62+
assert!(b.a == 5, 0);
63+
64+
destroy<A>(hot_potato);
65+
}
66+
}

target_chains/sui/contracts/sources/price_info.move

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ module pyth::price_info {
33
use sui::tx_context::{TxContext};
44
use sui::dynamic_object_field::{Self};
55
use sui::table::{Self};
6+
use sui::coin::{Self, Coin};
7+
use sui::sui::SUI;
68

79
use pyth::price_feed::{Self, PriceFeed};
810
use pyth::price_identifier::{PriceIdentifier};
911

1012
const KEY: vector<u8> = b"price_info";
13+
const FEE_STORAGE_KEY: vector<u8> = b"fee_storage";
1114
const E_PRICE_INFO_REGISTRY_ALREADY_EXISTS: u64 = 0;
1215
const E_PRICE_IDENTIFIER_ALREADY_REGISTERED: u64 = 1;
1316
const E_PRICE_IDENTIFIER_NOT_REGISTERED: u64 = 2;
@@ -91,6 +94,27 @@ module pyth::price_info {
9194
table::contains<PriceIdentifier, ID>(ref, price_identifier)
9295
}
9396

97+
public fun get_balance(price_info_object: &PriceInfoObject): u64 {
98+
if (!dynamic_object_field::exists_with_type<vector<u8>, Coin<SUI>>(&price_info_object.id, FEE_STORAGE_KEY)) {
99+
return 0
100+
};
101+
let fee = dynamic_object_field::borrow<vector<u8>, Coin<SUI>>(&price_info_object.id, FEE_STORAGE_KEY);
102+
coin::value(fee)
103+
}
104+
105+
public fun deposit_fee_coins(price_info_object: &mut PriceInfoObject, fee_coins: Coin<SUI>) {
106+
if (!dynamic_object_field::exists_with_type<vector<u8>, Coin<SUI>>(&price_info_object.id, FEE_STORAGE_KEY)) {
107+
dynamic_object_field::add(&mut price_info_object.id, FEE_STORAGE_KEY, fee_coins);
108+
}
109+
else {
110+
let current_fee = dynamic_object_field::borrow_mut<vector<u8>, Coin<SUI>>(
111+
&mut price_info_object.id,
112+
FEE_STORAGE_KEY
113+
);
114+
coin::join(current_fee, fee_coins);
115+
};
116+
}
117+
94118
public(friend) fun new_price_info_object(
95119
price_info: PriceInfo,
96120
ctx: &mut TxContext
@@ -143,7 +167,7 @@ module pyth::price_info {
143167
}
144168

145169
#[test_only]
146-
public fun destroy(price_info: PriceInfoObject){
170+
public fun destroy(price_info: PriceInfoObject) {
147171
let PriceInfoObject {
148172
id,
149173
price_info: _,

0 commit comments

Comments
 (0)