Skip to content

Commit bdc5747

Browse files
committed
finished skeleton of set fee function
1 parent 6fcfeba commit bdc5747

File tree

1 file changed

+34
-7
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+34
-7
lines changed

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ impl PythReceiver {
516516
price_feeds
517517
}
518518

519-
pub fn execute_governance_instruction(&mut self, data: Vec<u8>) -> Result<(), PythReceiverError> {
519+
pub fn execute_governance_instruction(
520+
&mut self,
521+
data: Vec<u8>,
522+
) -> Result<(), PythReceiverError> {
520523
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
521524
let config = Call::new();
522525
wormhole
@@ -531,13 +534,19 @@ impl PythReceiver {
531534
let instruction = governance_structs::parse_instruction(vm.body.payload.to_vec())
532535
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?;
533536

534-
if instruction.target_chain_id != 0 && instruction.target_chain_id != self.vm().chain_id() as u16 {
537+
if instruction.target_chain_id != 0
538+
&& instruction.target_chain_id != self.vm().chain_id() as u16
539+
{
535540
return Err(PythReceiverError::InvalidGovernanceTarget);
536541
}
537542

538543
match instruction.payload {
539-
GovernancePayload::SetFee(_payload) => {}
540-
GovernancePayload::SetFeeInToken(_payload) => {}
544+
GovernancePayload::SetFee(payload) => {
545+
self.set_fee(payload.value, payload.expo, self.fee_token_address.get());
546+
}
547+
GovernancePayload::SetFeeInToken(payload) => {
548+
self.set_fee(payload.value, payload.expo, payload.token);
549+
}
541550
GovernancePayload::SetDataSources(_payload) => {}
542551
GovernancePayload::SetWormholeAddress(_payload) => {}
543552
GovernancePayload::RequestGovernanceDataSourceTransfer(_) => {
@@ -561,7 +570,6 @@ impl PythReceiver {
561570
unimplemented!("Upgrade contract not yet implemented");
562571
}
563572

564-
565573
fn is_no_older_than(&self, publish_time: U64, max_age: u64) -> bool {
566574
self.get_current_timestamp()
567575
.saturating_sub(publish_time.to::<u64>())
@@ -580,14 +588,32 @@ impl PythReceiver {
580588
self.vm().block_timestamp()
581589
}
582590
}
591+
592+
fn set_fee(&mut self, value: u64, expo: u64, token_address: Address) {
593+
let new_fee = apply_decimal_expo(value, expo);
594+
let old_fee = self.single_update_fee_in_wei.get();
595+
596+
self.single_update_fee_in_wei.set(new_fee);
597+
598+
// TODO: HANDLE EVENT EMISSION
599+
}
600+
}
601+
602+
fn apply_decimal_expo(value: u64, expo: u64) -> U256 {
603+
U256::from(value) * U256::from(10).pow(expo as u32)
583604
}
584605

585606
fn verify_governance_vm(receiver: &mut PythReceiver, vm: Vaa) -> Result<(), PythReceiverError> {
586607
if vm.body.emitter_chain != receiver.governance_data_source_chain_id.get().to::<u16>() {
587608
return Err(PythReceiverError::InvalidGovernanceMessage);
588609
}
589610

590-
if vm.body.emitter_address.as_slice() != receiver.governance_data_source_emitter_address.get().as_slice() {
611+
if vm.body.emitter_address.as_slice()
612+
!= receiver
613+
.governance_data_source_emitter_address
614+
.get()
615+
.as_slice()
616+
{
591617
return Err(PythReceiverError::InvalidGovernanceMessage);
592618
}
593619

@@ -598,7 +624,8 @@ fn verify_governance_vm(receiver: &mut PythReceiver, vm: Vaa) -> Result<(), Pyth
598624
return Err(PythReceiverError::GovernanceMessageAlreadyExecuted);
599625
}
600626

601-
receiver.last_executed_governance_sequence
627+
receiver
628+
.last_executed_governance_sequence
602629
.set(U64::from(current_sequence));
603630

604631
Ok(())

0 commit comments

Comments
 (0)