Skip to content

Commit 5fa402b

Browse files
committed
feat(lazer/sui): add setters, ctors
1 parent 00f8405 commit 5fa402b

File tree

7 files changed

+219
-93
lines changed

7 files changed

+219
-93
lines changed

lazer/contracts/sui/Move.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @generated by Move, please check-in and do not edit manually.
2+
3+
[move]
4+
version = 3
5+
manifest_digest = "BEA4B53A0FCA0BE323AA9CCD8FD8CB24392F7A7FF5C9FFF61181A37A7AEBD662"
6+
deps_digest = "F9B494B64F0615AED0E98FC12A85B85ECD2BC5185C22D30E7F67786BB52E507C"
7+
dependencies = [
8+
{ id = "Bridge", name = "Bridge" },
9+
{ id = "MoveStdlib", name = "MoveStdlib" },
10+
{ id = "Sui", name = "Sui" },
11+
{ id = "SuiSystem", name = "SuiSystem" },
12+
]
13+
14+
[[move.package]]
15+
id = "Bridge"
16+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "b448b1d971bd6c1aac8ef4eee4305943806d5d5b", subdir = "crates/sui-framework/packages/bridge" }
17+
18+
dependencies = [
19+
{ id = "MoveStdlib", name = "MoveStdlib" },
20+
{ id = "Sui", name = "Sui" },
21+
{ id = "SuiSystem", name = "SuiSystem" },
22+
]
23+
24+
[[move.package]]
25+
id = "MoveStdlib"
26+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "b448b1d971bd6c1aac8ef4eee4305943806d5d5b", subdir = "crates/sui-framework/packages/move-stdlib" }
27+
28+
[[move.package]]
29+
id = "Sui"
30+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "b448b1d971bd6c1aac8ef4eee4305943806d5d5b", subdir = "crates/sui-framework/packages/sui-framework" }
31+
32+
dependencies = [
33+
{ id = "MoveStdlib", name = "MoveStdlib" },
34+
]
35+
36+
[[move.package]]
37+
id = "SuiSystem"
38+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "b448b1d971bd6c1aac8ef4eee4305943806d5d5b", subdir = "crates/sui-framework/packages/sui-system" }
39+
40+
dependencies = [
41+
{ id = "MoveStdlib", name = "MoveStdlib" },
42+
{ id = "Sui", name = "Sui" },
43+
]
44+
45+
[move.toolchain-version]
46+
compiler-version = "1.53.2"
47+
edition = "2024.beta"
48+
flavor = "sui"

lazer/contracts/sui/Move.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth_lazer"
3-
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
3+
edition = "2024.beta"
44

55
[dependencies]
66

lazer/contracts/sui/sources/channel.move

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ public enum Channel has copy, drop {
77
FixedRate200ms,
88
}
99

10+
/// Create a new Invalid channel
11+
public fun new_invalid(): Channel {
12+
Channel::Invalid
13+
}
14+
15+
/// Create a new RealTime channel
16+
public fun new_real_time(): Channel {
17+
Channel::RealTime
18+
}
19+
20+
/// Create a new FixedRate50ms channel
21+
public fun new_fixed_rate_50ms(): Channel {
22+
Channel::FixedRate50ms
23+
}
24+
25+
/// Create a new FixedRate200ms channel
26+
public fun new_fixed_rate_200ms(): Channel {
27+
Channel::FixedRate200ms
28+
}
29+
1030
/// Check if the channel is Invalid
1131
public fun is_invalid(channel: &Channel): bool {
1232
match (channel) {

lazer/contracts/sui/sources/feed.move

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ public struct Feed has copy, drop {
3131
funding_timestamp: Option<Option<u64>>,
3232
}
3333

34+
/// Create a new Feed with the specified parameters
35+
public fun new(
36+
feed_id: u32,
37+
price: Option<Option<I64>>,
38+
best_bid_price: Option<Option<I64>>,
39+
best_ask_price: Option<Option<I64>>,
40+
publisher_count: Option<u16>,
41+
exponent: Option<I16>,
42+
confidence: Option<Option<I64>>,
43+
funding_rate: Option<Option<I64>>,
44+
funding_timestamp: Option<Option<u64>>,
45+
): Feed {
46+
Feed {
47+
feed_id,
48+
price,
49+
best_bid_price,
50+
best_ask_price,
51+
publisher_count,
52+
exponent,
53+
confidence,
54+
funding_rate,
55+
funding_timestamp,
56+
}
57+
}
58+
3459
/// Get the feed ID
3560
public fun feed_id(feed: &Feed): u32 {
3661
feed.feed_id
@@ -75,3 +100,48 @@ public fun funding_rate(feed: &Feed): Option<Option<I64>> {
75100
public fun funding_timestamp(feed: &Feed): Option<Option<u64>> {
76101
feed.funding_timestamp
77102
}
103+
104+
/// Set the feed ID
105+
public fun set_feed_id(feed: &mut Feed, feed_id: u32) {
106+
feed.feed_id = feed_id;
107+
}
108+
109+
/// Set the price
110+
public fun set_price(feed: &mut Feed, price: Option<Option<I64>>) {
111+
feed.price = price;
112+
}
113+
114+
/// Set the best bid price
115+
public fun set_best_bid_price(feed: &mut Feed, best_bid_price: Option<Option<I64>>) {
116+
feed.best_bid_price = best_bid_price;
117+
}
118+
119+
/// Set the best ask price
120+
public fun set_best_ask_price(feed: &mut Feed, best_ask_price: Option<Option<I64>>) {
121+
feed.best_ask_price = best_ask_price;
122+
}
123+
124+
/// Set the publisher count
125+
public fun set_publisher_count(feed: &mut Feed, publisher_count: Option<u16>) {
126+
feed.publisher_count = publisher_count;
127+
}
128+
129+
/// Set the exponent
130+
public fun set_exponent(feed: &mut Feed, exponent: Option<I16>) {
131+
feed.exponent = exponent;
132+
}
133+
134+
/// Set the confidence interval
135+
public fun set_confidence(feed: &mut Feed, confidence: Option<Option<I64>>) {
136+
feed.confidence = confidence;
137+
}
138+
139+
/// Set the funding rate
140+
public fun set_funding_rate(feed: &mut Feed, funding_rate: Option<Option<I64>>) {
141+
feed.funding_rate = funding_rate;
142+
}
143+
144+
/// Set the funding timestamp
145+
public fun set_funding_timestamp(feed: &mut Feed, funding_timestamp: Option<Option<u64>>) {
146+
feed.funding_timestamp = funding_timestamp;
147+
}

lazer/contracts/sui/sources/pyth_lazer.move

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module pyth_lazer::pyth_lazer;
22

3-
use pyth_lazer::i16::{Self, I16};
4-
use pyth_lazer::i64::{Self, I64};
5-
use pyth_lazer::update::Update;
3+
use pyth_lazer::i16::Self;
4+
use pyth_lazer::i64::Self;
5+
use pyth_lazer::update::{Self, Update};
6+
use pyth_lazer::feed::{Self, Feed};
7+
use pyth_lazer::channel::Self;
68
use sui::bcs;
79
use sui::ecdsa_k1::secp256k1_ecrecover;
810

@@ -47,15 +49,15 @@ public fun parse_and_validate_update(update: vector<u8>): Update {
4749
let timestamp = cursor.peel_u64();
4850
let channel_value = cursor.peel_u8();
4951
let channel = if (channel_value == 0) {
50-
Channel::Invalid
52+
channel::new_invalid()
5153
} else if (channel_value == 1) {
52-
Channel::RealTime
54+
channel::new_real_time()
5355
} else if (channel_value == 2) {
54-
Channel::FixedRate50ms
56+
channel::new_fixed_rate_50ms()
5557
} else if (channel_value == 3) {
56-
Channel::FixedRate200ms
58+
channel::new_fixed_rate_200ms()
5759
} else {
58-
Channel::Invalid // Default to Invalid for unknown values
60+
channel::new_invalid() // Default to Invalid for unknown values
5961
};
6062

6163
let mut feeds = vector::empty<Feed>();
@@ -65,17 +67,17 @@ public fun parse_and_validate_update(update: vector<u8>): Update {
6567

6668
while (feed_i < feed_count) {
6769
let feed_id = cursor.peel_u32();
68-
let mut feed = Feed {
69-
feed_id: feed_id,
70-
price: option::none(),
71-
best_bid_price: option::none(),
72-
best_ask_price: option::none(),
73-
publisher_count: option::none(),
74-
exponent: option::none(),
75-
confidence: option::none(),
76-
funding_rate: option::none(),
77-
funding_timestamp: option::none(),
78-
};
70+
let mut feed = feed::new(
71+
feed_id,
72+
option::none(),
73+
option::none(),
74+
option::none(),
75+
option::none(),
76+
option::none(),
77+
option::none(),
78+
option::none(),
79+
option::none()
80+
);
7981

8082
let properties_count = cursor.peel_u8();
8183
let mut properties_i = 0;
@@ -86,53 +88,53 @@ public fun parse_and_validate_update(update: vector<u8>): Update {
8688
if (property_id == 0) {
8789
let price = cursor.peel_u64();
8890
if (price != 0) {
89-
feed.price = option::some(option::some(i64::from_u64(price)));
91+
feed.set_price(option::some(option::some(i64::from_u64(price))));
9092
} else {
91-
feed.price = option::some(option::none());
93+
feed.set_price(option::some(option::none()));
9294
}
9395
} else if (property_id == 1) {
9496
let best_bid_price = cursor.peel_u64();
9597
if (best_bid_price != 0) {
96-
feed.best_bid_price = option::some(option::some(i64::from_u64(best_bid_price)));
98+
feed.set_best_bid_price(option::some(option::some(i64::from_u64(best_bid_price))));
9799
} else {
98-
feed.best_bid_price = option::some(option::none());
100+
feed.set_best_bid_price(option::some(option::none()));
99101
}
100102
} else if (property_id == 2) {
101103
let best_ask_price = cursor.peel_u64();
102104
if (best_ask_price != 0) {
103-
feed.best_ask_price = option::some(option::some(i64::from_u64(best_ask_price)));
105+
feed.set_best_ask_price(option::some(option::some(i64::from_u64(best_ask_price))));
104106
} else {
105-
feed.best_ask_price = option::some(option::none());
107+
feed.set_best_ask_price(option::some(option::none()));
106108
}
107109
} else if (property_id == 3) {
108110
let publisher_count = cursor.peel_u16();
109-
feed.publisher_count = option::some(publisher_count);
111+
feed.set_publisher_count(option::some(publisher_count));
110112
} else if (property_id == 4) {
111113
let exponent = cursor.peel_u16();
112-
feed.exponent = option::some(i16::from_u16(exponent));
114+
feed.set_exponent(option::some(i16::from_u16(exponent)));
113115
} else if (property_id == 5) {
114116
let confidence = cursor.peel_u64();
115117
if (confidence != 0) {
116-
feed.confidence = option::some(option::some(i64::from_u64(confidence)));
118+
feed.set_confidence(option::some(option::some(i64::from_u64(confidence))));
117119
} else {
118-
feed.confidence = option::some(option::none());
120+
feed.set_confidence(option::some(option::none()));
119121
}
120122
} else if (property_id == 6) {
121123
let exists = cursor.peel_u8();
122124
if (exists == 1) {
123125
let funding_rate = cursor.peel_u64();
124-
feed.funding_rate = option::some(option::some(i64::from_u64(funding_rate)));
126+
feed.set_funding_rate(option::some(option::some(i64::from_u64(funding_rate))));
125127
} else {
126-
feed.funding_rate = option::some(option::none());
128+
feed.set_funding_rate(option::some(option::none()));
127129
}
128130
} else if (property_id == 7) {
129131
let exists = cursor.peel_u8();
130132

131133
if (exists == 1) {
132134
let funding_timestamp = cursor.peel_u64();
133-
feed.funding_timestamp = option::some(option::some(funding_timestamp));
135+
feed.set_funding_timestamp(option::some(option::some(funding_timestamp)));
134136
} else {
135-
feed.funding_timestamp = option::some(option::none());
137+
feed.set_funding_timestamp(option::some(option::none()));
136138
}
137139
} else {
138140
// When we have an unknown property, we do not know its length, and therefore
@@ -151,9 +153,5 @@ public fun parse_and_validate_update(update: vector<u8>): Update {
151153
let remaining_bytes = cursor.into_remainder_bytes();
152154
assert!(remaining_bytes.length() == 0, 0);
153155

154-
Update {
155-
timestamp: timestamp,
156-
channel: channel,
157-
feeds: feeds,
158-
}
156+
update::new(timestamp, channel, feeds)
159157
}

lazer/contracts/sui/sources/update.move

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public struct Update has copy, drop {
1414
feeds: vector<Feed>,
1515
}
1616

17+
public fun new(
18+
timestamp: u64,
19+
channel: Channel,
20+
feeds: vector<Feed>
21+
): Update {
22+
Update { timestamp, channel, feeds }
23+
}
24+
1725
/// Get the timestamp of the update
1826
public fun timestamp(update: &Update): u64 {
1927
update.timestamp

0 commit comments

Comments
 (0)