Skip to content

Commit 2cf7ef8

Browse files
committed
st
1 parent cf1c462 commit 2cf7ef8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub struct SetTransactionFee {
7777
pub struct WithdrawFee {
7878
pub value: u64,
7979
pub expo: u64,
80+
pub target_address: Address,
8081
}
8182

8283
#[derive(Clone, Debug, PartialEq)]

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,12 @@ impl PythReceiver {
559559
GovernancePayload::SetValidPeriod(payload) => {
560560
self.set_valid_period(payload.valid_time_period_seconds);
561561
}
562-
GovernancePayload::SetTransactionFee(_payload) => todo!(),
563-
GovernancePayload::WithdrawFee(_payload) => todo!(),
562+
GovernancePayload::SetTransactionFee(payload) => {
563+
self.set_transaction_fee(payload.value, payload.expo);
564+
},
565+
GovernancePayload::WithdrawFee(payload) => {
566+
self.withdraw_fee(payload.value, payload.expo, payload.target_address)?;
567+
},
564568
}
565569

566570
Ok(())
@@ -709,6 +713,28 @@ impl PythReceiver {
709713

710714
Ok(())
711715
}
716+
717+
fn set_transaction_fee(&mut self, value: u64, expo: u64) {
718+
let new_fee = U256::from(value) * U256::from(10).pow(U256::from(expo));
719+
let _old_fee = self.transaction_fee_in_wei.get();
720+
721+
self.transaction_fee_in_wei.set(new_fee);
722+
}
723+
724+
fn withdraw_fee(&mut self, value: u64, expo: u64, target_address: Address) -> Result<(), PythReceiverError> {
725+
let fee_to_withdraw = U256::from(value) * U256::from(10).pow(U256::from(expo));
726+
let current_balance = self.vm().balance(self.vm().contract_address());
727+
728+
729+
if current_balance < fee_to_withdraw {
730+
return Err(PythReceiverError::InsufficientFee);
731+
}
732+
733+
self.vm().transfer_eth(target_address, fee_to_withdraw)
734+
.map_err(|_| PythReceiverError::InsufficientFee)?;
735+
736+
Ok(())
737+
}
712738
}
713739

714740
fn verify_governance_vm(receiver: &mut PythReceiver, vm: Vaa) -> Result<(), PythReceiverError> {

0 commit comments

Comments
 (0)