Skip to content

Commit f25ac83

Browse files
fix: add missing SetTransactionFee and WithdrawFee governance actions
- Add SetTransactionFee (8) and WithdrawFee (9) to GovernanceAction enum - Implement parsing logic for both actions matching Solidity implementation - Add TODO comments for event emissions to match EVM contract behavior - Ensure instruction parsing compatibility between Stylus and Solidity Co-Authored-By: [email protected] <[email protected]>
1 parent 2cf7ef8 commit f25ac83

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub enum GovernanceAction {
1616
RequestGovernanceDataSourceTransfer,
1717
SetWormholeAddress,
1818
SetFeeInToken,
19+
SetTransactionFee,
20+
WithdrawFee,
1921
}
2022

2123
impl TryFrom<u8> for GovernanceAction {
@@ -31,6 +33,8 @@ impl TryFrom<u8> for GovernanceAction {
3133
5 => Ok(GovernanceAction::RequestGovernanceDataSourceTransfer),
3234
6 => Ok(GovernanceAction::SetWormholeAddress),
3335
7 => Ok(GovernanceAction::SetFeeInToken),
36+
8 => Ok(GovernanceAction::SetTransactionFee),
37+
9 => Ok(GovernanceAction::WithdrawFee),
3438
_ => Err(PythReceiverError::InvalidGovernanceAction),
3539
}
3640
}
@@ -313,6 +317,71 @@ pub fn parse_instruction(payload: Vec<u8>) -> Result<GovernanceInstruction, Pyth
313317
address: Address::from(address_bytes),
314318
})
315319
}
320+
GovernanceAction::SetTransactionFee => {
321+
if payload.len() < cursor + 16 {
322+
return Err(PythReceiverError::InvalidGovernanceMessage);
323+
}
324+
let fee_value_bytes = payload
325+
.get(cursor..cursor + 8)
326+
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;
327+
328+
let value = u64::from_be_bytes(
329+
fee_value_bytes
330+
.try_into()
331+
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?,
332+
);
333+
334+
cursor += 8;
335+
336+
let expo_bytes = payload
337+
.get(cursor..cursor + 8)
338+
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;
339+
let expo = u64::from_be_bytes(
340+
expo_bytes
341+
.try_into()
342+
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?,
343+
);
344+
345+
cursor += 8;
346+
GovernancePayload::SetTransactionFee(SetTransactionFee { value, expo })
347+
}
348+
GovernanceAction::WithdrawFee => {
349+
if payload.len() < cursor + 28 {
350+
return Err(PythReceiverError::InvalidGovernanceMessage);
351+
}
352+
353+
let mut target_address_bytes = [0u8; 20];
354+
target_address_bytes.copy_from_slice(&payload[cursor..cursor + 20]);
355+
cursor += 20;
356+
357+
let fee_value_bytes = payload
358+
.get(cursor..cursor + 8)
359+
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;
360+
361+
let value = u64::from_be_bytes(
362+
fee_value_bytes
363+
.try_into()
364+
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?,
365+
);
366+
367+
cursor += 8;
368+
369+
let expo_bytes = payload
370+
.get(cursor..cursor + 8)
371+
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;
372+
let expo = u64::from_be_bytes(
373+
expo_bytes
374+
.try_into()
375+
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?,
376+
);
377+
378+
cursor += 8;
379+
GovernancePayload::WithdrawFee(WithdrawFee {
380+
value,
381+
expo,
382+
target_address: Address::from(target_address_bytes),
383+
})
384+
}
316385
};
317386

318387
if cursor != payload.len() {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,15 @@ impl PythReceiver {
599599

600600
self.single_update_fee_in_wei.set(new_fee);
601601

602-
// TODO: HANDLE EVENT EMISSION
602+
// TODO: Emit FeeSet event with old_fee and new_fee
603603
}
604604

605605
fn set_valid_period(&mut self, valid_time_period_seconds: u64) {
606606
let _old_valid_period = self.valid_time_period_seconds.get();
607607
self.valid_time_period_seconds.set(U256::from(valid_time_period_seconds));
608608

609-
// TODO: HANDLE EVENT EMISSION
609+
// TODO: Emit ValidPeriodSet event with old_valid_period and new_valid_period
610+
// emit ValidPeriodSet(old_valid_period, valid_time_period_seconds);
610611
}
611612

612613
fn set_wormhole_address(
@@ -719,6 +720,8 @@ impl PythReceiver {
719720
let _old_fee = self.transaction_fee_in_wei.get();
720721

721722
self.transaction_fee_in_wei.set(new_fee);
723+
724+
// TODO: Emit TransactionFeeSet event with old_fee and new_fee
722725
}
723726

724727
fn withdraw_fee(&mut self, value: u64, expo: u64, target_address: Address) -> Result<(), PythReceiverError> {
@@ -733,6 +736,8 @@ impl PythReceiver {
733736
self.vm().transfer_eth(target_address, fee_to_withdraw)
734737
.map_err(|_| PythReceiverError::InsufficientFee)?;
735738

739+
// TODO: Emit FeeWithdrawn event with target_address and fee_to_withdraw
740+
736741
Ok(())
737742
}
738743
}

0 commit comments

Comments
 (0)