Skip to content

Commit 94191a5

Browse files
authored
refactor(target_chains/starknet): cleanup and GetDataSources trait (#1664)
1 parent 84e5ae6 commit 94191a5

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

target_chains/starknet/contracts/src/pyth.cairo

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub use errors::{
1414
UpdatePriceFeedsIfNecessaryError, ParsePriceFeedsError, GetSingleUpdateFeeError,
1515
};
1616
pub use interface::{
17-
IPyth, IPythDispatcher, IPythDispatcherTrait, DataSource, Price, PriceFeedPublishTime, PriceFeed
17+
IPyth, IPythDispatcher, IPythDispatcherTrait, DataSource, GetDataSource, Price,
18+
PriceFeedPublishTime, PriceFeed
1819
};
1920

2021
#[starknet::contract]
@@ -34,9 +35,10 @@ mod pyth {
3435
use core::starknet::syscalls::replace_class_syscall;
3536
use pyth::wormhole::{IWormholeDispatcher, IWormholeDispatcherTrait, VerifiedVM};
3637
use super::{
37-
DataSource, UpdatePriceFeedsError, GovernanceActionError, Price, GetPriceUnsafeError,
38-
IPythDispatcher, IPythDispatcherTrait, PriceFeedPublishTime, GetPriceNoOlderThanError,
39-
UpdatePriceFeedsIfNecessaryError, PriceFeed, ParsePriceFeedsError, GetSingleUpdateFeeError,
38+
DataSource, GetDataSource, UpdatePriceFeedsError, GovernanceActionError, Price,
39+
GetPriceUnsafeError, IPythDispatcher, IPythDispatcherTrait, PriceFeedPublishTime,
40+
GetPriceNoOlderThanError, UpdatePriceFeedsIfNecessaryError, PriceFeed, ParsePriceFeedsError,
41+
GetSingleUpdateFeeError,
4042
};
4143
use super::governance;
4244
use super::governance::GovernancePayload;
@@ -547,7 +549,7 @@ mod pyth {
547549
let wormhole = IWormholeDispatcher { contract_address: self.wormhole_address.read() };
548550
let claim_vm = wormhole.parse_and_verify_vm(claim_vaa.clone());
549551
// Note: no verify_governance_vm() because claim_vaa is signed by the new data source
550-
let instruction = governance::parse_instruction(claim_vm.payload);
552+
let instruction = governance::parse_instruction(claim_vm.payload.clone());
551553
if instruction.target_chain_id != 0
552554
&& instruction.target_chain_id != wormhole.chain_id() {
553555
panic_with_felt252(GovernanceActionError::InvalidGovernanceTarget.into());
@@ -565,10 +567,7 @@ mod pyth {
565567
}
566568
self.governance_data_source_index.write(request_payload.governance_data_source_index);
567569
let old_data_source = self.governance_data_source.read();
568-
let new_data_source = DataSource {
569-
emitter_chain_id: claim_vm.emitter_chain_id,
570-
emitter_address: claim_vm.emitter_address,
571-
};
570+
let new_data_source = claim_vm.data_source();
572571
self.governance_data_source.write(new_data_source);
573572
// Setting the last executed governance to the claimVaa sequence to avoid
574573
// using older sequences.
@@ -620,10 +619,7 @@ mod pyth {
620619
let wormhole = IWormholeDispatcher { contract_address: self.wormhole_address.read() };
621620
let vm = wormhole.parse_and_verify_vm(wormhole_proof);
622621

623-
let source = DataSource {
624-
emitter_chain_id: vm.emitter_chain_id, emitter_address: vm.emitter_address
625-
};
626-
if !self.is_valid_data_source.read(source) {
622+
if !self.is_valid_data_source.read(vm.data_source()) {
627623
panic_with_felt252(UpdatePriceFeedsError::InvalidUpdateDataSource.into());
628624
}
629625

target_chains/starknet/contracts/src/pyth/errors.cairo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl GetPriceUnsafeErrorIntoGetPriceNoOlderThanError of Into<
3838

3939
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
4040
pub enum GovernanceActionError {
41-
AccessDenied,
4241
Wormhole: pyth::wormhole::ParseAndVerifyVmError,
4342
InvalidGovernanceDataSource,
4443
OldGovernanceMessage,
@@ -50,7 +49,6 @@ pub enum GovernanceActionError {
5049
impl GovernanceActionErrorIntoFelt252 of Into<GovernanceActionError, felt252> {
5150
fn into(self: GovernanceActionError) -> felt252 {
5251
match self {
53-
GovernanceActionError::AccessDenied => 'access denied',
5452
GovernanceActionError::Wormhole(err) => err.into(),
5553
GovernanceActionError::InvalidGovernanceDataSource => 'invalid governance data source',
5654
GovernanceActionError::OldGovernanceMessage => 'old governance message',

target_chains/starknet/contracts/src/pyth/interface.cairo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{GetPriceUnsafeError, GetPriceNoOlderThanError};
22
use pyth::byte_array::ByteArray;
3+
use pyth::wormhole::VerifiedVM;
34
use core::starknet::ContractAddress;
45

56
#[starknet::interface]
@@ -56,6 +57,18 @@ pub struct DataSource {
5657
pub emitter_address: u256,
5758
}
5859

60+
pub trait GetDataSource<T> {
61+
fn data_source(self: @T) -> DataSource;
62+
}
63+
64+
impl GetDataSourceFromVerifiedVM of GetDataSource<VerifiedVM> {
65+
fn data_source(self: @VerifiedVM) -> DataSource {
66+
DataSource {
67+
emitter_chain_id: *self.emitter_chain_id, emitter_address: *self.emitter_address
68+
}
69+
}
70+
}
71+
5972
#[derive(Drop, Copy, PartialEq, Serde)]
6073
pub struct Price {
6174
pub price: i64,

target_chains/starknet/contracts/tests/pyth.cairo

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,7 @@ fn deploy_pyth(
11201120
let contract = declare("pyth");
11211121
let contract_address = match contract.deploy(@args) {
11221122
Result::Ok(v) => { v },
1123-
Result::Err(err) => {
1124-
panic(err.panic_data);
1125-
0.try_into().unwrap()
1126-
},
1123+
Result::Err(err) => { panic(err.panic_data) },
11271124
};
11281125
IPythDispatcher { contract_address }
11291126
}
@@ -1135,10 +1132,7 @@ fn deploy_fee_contract(class: ContractClass, recipient: ContractAddress) -> IERC
11351132
(name, symbol, 100000_u256, recipient).serialize(ref args);
11361133
let contract_address = match class.deploy(@args) {
11371134
Result::Ok(v) => { v },
1138-
Result::Err(err) => {
1139-
panic(err.panic_data);
1140-
0.try_into().unwrap()
1141-
},
1135+
Result::Err(err) => { panic(err.panic_data) },
11421136
};
11431137
IERC20CamelDispatcher { contract_address }
11441138
}

target_chains/starknet/contracts/tests/wormhole.cairo

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ pub fn deploy_declared_at(
261261
};
262262
let contract_address = match result {
263263
Result::Ok(v) => { v },
264-
Result::Err(err) => {
265-
panic(err.panic_data);
266-
0.try_into().unwrap()
267-
},
264+
Result::Err(err) => { panic(err.panic_data) },
268265
};
269266
IWormholeDispatcher { contract_address }
270267
}

0 commit comments

Comments
 (0)