Skip to content

Commit e10d2d8

Browse files
committed
set up framework for contract initialization (i.e. constructor) and update all price feeds. some cargo build errors, but will fix in next commit
1 parent 9c43c5d commit e10d2d8

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

target_chains/stylus/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/stylus/contracts/pyth-receiver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ alloy-sol-types = "=0.8.20"
1414
stylus-sdk = "0.9.0"
1515
hex = { version = "0.4", default-features = false }
1616
pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk" }
17+
wormhole-contract = { path = "../../contracts/wormhole" }
1718

1819
[dev-dependencies]
1920
alloy-primitives = { version = "=0.8.20", features = ["sha3-keccak"] }

target_chains/stylus/contracts/pyth-receiver/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use stylus_sdk::{alloy_primitives::{U16, U32, U256, U64, I32, I64, FixedBytes, B
1414
storage::{StorageAddress, StorageVec, StorageMap, StorageUint, StorageBool, StorageU256, StorageU16, StorageFixedBytes},
1515
call::Call};
1616

17-
use structs::{PriceInfoReturn, PriceInfoStorage};
17+
use structs::{PriceInfoReturn, PriceInfoStorage, DataSourceStorage};
1818
use error::{PythReceiverError};
1919
use pythnet_sdk::{wire::{v1::{
2020
AccumulatorUpdateData, Proof,
@@ -32,8 +32,7 @@ sol_interface! {
3232
#[entrypoint]
3333
pub struct PythReceiver {
3434
pub wormhole: StorageAddress,
35-
pub valid_data_source_chain_ids: StorageVec<StorageU16>,
36-
pub valid_data_source_emitter_addresses: StorageVec<StorageFixedBytes<32>>,
35+
pub valid_data_sources: StorageVec<DataSourceStorage>,
3736
pub is_valid_data_source: StorageMap<FixedBytes<32>, StorageBool>,
3837
pub single_update_fee_in_wei: StorageU256,
3938
pub valid_time_period_seconds: StorageU256,
@@ -65,18 +64,15 @@ impl PythReceiver {
6564
for (i, chain_id) in data_source_emitter_chain_ids.iter().enumerate() {
6665
let emitter_address = FixedBytes::<32>::from(data_source_emitter_addresses[i]);
6766

68-
// Get a new storage slot in the vector and set its value
69-
let mut chain_id_storage = self.valid_data_source_chain_ids.grow();
70-
chain_id_storage.set(U16::from(*chain_id));
71-
72-
let mut emitter_address_storage = self.valid_data_source_emitter_addresses.grow();
73-
emitter_address_storage.set(emitter_address);
67+
// Create a new data source storage slot
68+
let mut data_source = self.valid_data_sources.grow();
69+
data_source.chain_id.set(U16::from(*chain_id));
70+
data_source.emitter_address.set(emitter_address);
7471

7572
self.is_valid_data_source
7673
.setter(emitter_address)
7774
.set(true);
7875
}
79-
8076
}
8177

8278
pub fn get_price_unsafe(&self, _id: [u8; 32]) -> Result<PriceInfoReturn, PythReceiverError> {
@@ -124,8 +120,17 @@ impl PythReceiver {
124120
let config = Call::new_in(self);
125121
let _parsed_vaa = wormhole.parse_and_verify_vm(config, Bytes::from(Vec::from(vaa))).map_err(|_| PythReceiverError::PriceUnavailable).unwrap();
126122

123+
if !self.is_valid_data_source.entry(_parsed_vaa.data_source()).read() {
124+
panic!("Update data source is not a valid data source.");
125+
}
126+
127127
for update in updates {
128-
// fill in update processign logic.
128+
// fill in update processing logic.
129+
// update is a merkle price update
130+
let message = update.message;
131+
let proof = update.proof;
132+
133+
129134
}
130135
}
131136
};

target_chains/stylus/contracts/pyth-receiver/src/structs.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
use alloc::vec::Vec;
2-
use stylus_sdk::{prelude::*, storage::{StorageU16, StorageU64, StorageI32, StorageI64, StorageFixedBytes}};
3-
use stylus_sdk::alloy_primitives::{U64, I32, I64};
2+
use stylus_sdk::{prelude::*, storage::{StorageU64, StorageI32, StorageI64, StorageU16, StorageFixedBytes}};
3+
use stylus_sdk::alloy_primitives::{U64, I32, I64, U16, FixedBytes};
4+
use wormhole_contract::types::VerifiedVM;
5+
6+
#[storage]
7+
pub struct DataSourceStorage {
8+
pub chain_id: StorageU16,
9+
pub emitter_address: StorageFixedBytes<32>,
10+
}
11+
12+
pub trait GetDataSource {
13+
fn data_source(&self) -> DataSourceStorage;
14+
}
15+
16+
impl GetDataSource for VerifiedVM {
17+
fn data_source(&self) -> DataSourceStorage {
18+
let mut ds = DataSourceStorage {
19+
chain_id: StorageU16::default(),
20+
emitter_address: StorageFixedBytes::<32>::default(),
21+
};
22+
ds.chain_id.set(U16::from(self.emitter_chain_id));
23+
ds.emitter_address.set(self.emitter_address);
24+
ds
25+
}
26+
}
427

528
// PriceInfo struct storing price information
629
#[storage]

target_chains/stylus/contracts/wormhole/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate alloc;
55
#[global_allocator]
66
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
77

8-
#[cfg(not(any(feature = "std", feature = "export-abi")))]
8+
#[cfg(all(not(any(feature = "std", feature = "export-abi")), feature = "main"))]
99
#[panic_handler]
1010
fn panic(_info: &core::panic::PanicInfo) -> ! {
1111
loop {}

0 commit comments

Comments
 (0)