Skip to content

Commit 21cda36

Browse files
authored
feat(target_chains/starknet): add all possible derives (#1667)
1 parent 139fb0c commit 21cda36

File tree

15 files changed

+210
-55
lines changed

15 files changed

+210
-55
lines changed

target_chains/starknet/contracts/src/byte_buffer.cairo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::util::one_shift_left_bytes_u256;
44

55
/// A byte array with storage format similar to `core::ByteArray`, but
66
/// suitable for reading data from it.
7-
#[derive(Drop, Clone, Serde)]
7+
#[derive(Drop, Clone, PartialEq, Serde)]
88
pub struct ByteBuffer {
99
// Number of bytes stored in the last item of `self.data` (or 0 if it's empty).
1010
num_last_bytes: u8,
@@ -27,6 +27,12 @@ impl DebugByteBuffer of Debug<ByteBuffer> {
2727
}
2828
}
2929

30+
impl DefaultByteBuffer of Default<ByteBuffer> {
31+
fn default() -> ByteBuffer {
32+
ByteBufferImpl::new(array![], 0)
33+
}
34+
}
35+
3036
#[generate_trait]
3137
pub impl ByteBufferImpl of ByteBufferTrait {
3238
/// Creates a byte array with the data.

target_chains/starknet/contracts/src/hash.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pyth::util::{
88

99
/// Allows to push data as big endian to a buffer and apply
1010
/// the keccak256 hash.
11-
#[derive(Drop, Debug)]
11+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
1212
pub struct Hasher {
1313
// Inputs in little endian.
1414
inputs_le: Array<u64>,

target_chains/starknet/contracts/src/merkle_tree.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const MERKLE_LEAF_PREFIX: u8 = 0;
99
const MERKLE_NODE_PREFIX: u8 = 1;
1010
const MERKLE_EMPTY_LEAF_PREFIX: u8 = 2;
1111

12-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
12+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
1313
pub enum MerkleVerificationError {
1414
Reader: super::reader::Error,
1515
DigestMismatch,

target_chains/starknet/contracts/src/pyth.cairo

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ mod pyth {
4343
use super::governance;
4444
use super::governance::GovernancePayload;
4545
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcherTrait, IERC20CamelDispatcher};
46-
use pyth::util::ResultMapErrInto;
46+
use pyth::util::{ResultMapErrInto, write_i64};
4747
use core::nullable::{NullableTrait, match_nullable, FromNullableResult};
48+
use core::fmt::{Debug, Formatter};
4849

4950
#[event]
50-
#[derive(Drop, PartialEq, starknet::Event)]
51+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
5152
pub enum Event {
5253
PriceFeedUpdated: PriceFeedUpdated,
5354
FeeSet: FeeSet,
@@ -57,41 +58,60 @@ mod pyth {
5758
ContractUpgraded: ContractUpgraded,
5859
}
5960

60-
#[derive(Drop, PartialEq, starknet::Event)]
61+
#[derive(Drop, Clone, PartialEq, Serde, starknet::Event)]
6162
pub struct PriceFeedUpdated {
6263
#[key]
6364
pub price_id: u256,
64-
pub publish_time: u64,
6565
pub price: i64,
6666
pub conf: u64,
67+
pub publish_time: u64,
68+
}
69+
70+
// TODO: use derives after upgrading cairo
71+
impl DebugPriceFeedUpdated of Debug<PriceFeedUpdated> {
72+
fn fmt(self: @PriceFeedUpdated, ref f: Formatter) -> Result<(), core::fmt::Error> {
73+
write!(f, "PriceFeedUpdated {{ price_id: {}, price: ", self.price_id)?;
74+
write_i64(ref f, *self.price)?;
75+
write!(f, ", conf: {}, publish_time: {} }}", self.conf, self.publish_time)
76+
}
77+
}
78+
79+
#[cfg(test)]
80+
#[test]
81+
fn test_debug_price_feed_updated() {
82+
let value = PriceFeedUpdated { price_id: 1, price: 2, conf: 3, publish_time: 5, };
83+
let expected = "PriceFeedUpdated { price_id: 1, price: 2, conf: 3, publish_time: 5 }";
84+
let actual = format!("{:?}", value);
85+
assert!(actual == expected);
6786
}
6887

69-
#[derive(Drop, PartialEq, starknet::Event)]
88+
89+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
7090
pub struct FeeSet {
7191
pub old_fee: u256,
7292
pub new_fee: u256,
7393
}
7494

75-
#[derive(Drop, PartialEq, starknet::Event)]
95+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
7696
pub struct DataSourcesSet {
7797
pub old_data_sources: Array<DataSource>,
7898
pub new_data_sources: Array<DataSource>,
7999
}
80100

81-
#[derive(Drop, PartialEq, starknet::Event)]
101+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
82102
pub struct WormholeAddressSet {
83103
pub old_address: ContractAddress,
84104
pub new_address: ContractAddress,
85105
}
86106

87-
#[derive(Drop, PartialEq, starknet::Event)]
107+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
88108
pub struct GovernanceDataSourceSet {
89109
pub old_data_source: DataSource,
90110
pub new_data_source: DataSource,
91111
pub last_executed_governance_sequence: u64,
92112
}
93113

94-
#[derive(Drop, PartialEq, starknet::Event)]
114+
#[derive(Drop, Clone, Debug, PartialEq, Serde, starknet::Event)]
95115
pub struct ContractUpgraded {
96116
pub new_class_hash: ClassHash,
97117
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
1+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
22
pub enum GetPriceUnsafeError {
33
PriceFeedNotFound,
44
}
@@ -11,7 +11,7 @@ impl GetPriceUnsafeErrorIntoFelt252 of Into<GetPriceUnsafeError, felt252> {
1111
}
1212
}
1313

14-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
14+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
1515
pub enum GetPriceNoOlderThanError {
1616
PriceFeedNotFound,
1717
StalePrice,
@@ -36,7 +36,7 @@ impl GetPriceUnsafeErrorIntoGetPriceNoOlderThanError of Into<
3636
}
3737
}
3838

39-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
39+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
4040
pub enum GovernanceActionError {
4141
Wormhole: pyth::wormhole::ParseAndVerifyVmError,
4242
InvalidGovernanceDataSource,
@@ -59,7 +59,7 @@ impl GovernanceActionErrorIntoFelt252 of Into<GovernanceActionError, felt252> {
5959
}
6060
}
6161

62-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
62+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
6363
pub enum UpdatePriceFeedsError {
6464
Reader: pyth::reader::Error,
6565
Wormhole: pyth::wormhole::ParseAndVerifyVmError,
@@ -80,7 +80,7 @@ impl UpdatePriceFeedsErrorIntoFelt252 of Into<UpdatePriceFeedsError, felt252> {
8080
}
8181
}
8282

83-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
83+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
8484
pub enum UpdatePriceFeedsIfNecessaryError {
8585
Update: UpdatePriceFeedsError,
8686
NoFreshUpdate,
@@ -97,7 +97,7 @@ impl UpdatePriceFeedsIfNecessaryErrorIntoFelt252 of Into<
9797
}
9898
}
9999

100-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
100+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
101101
pub enum ParsePriceFeedsError {
102102
Update: UpdatePriceFeedsError,
103103
PriceFeedNotFoundWithinRange,
@@ -112,7 +112,7 @@ impl ParsePriceFeedsErrorIntoFelt252 of Into<ParsePriceFeedsError, felt252> {
112112
}
113113
}
114114

115-
#[derive(Copy, Drop, Debug, Serde, PartialEq)]
115+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
116116
pub enum GetSingleUpdateFeeError {
117117
UnsupportedToken,
118118
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::DataSource;
1010
const MAGIC: u32 = 0x5054474d;
1111
const MODULE_TARGET: u8 = 1;
1212

13-
#[derive(Drop, Debug)]
13+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash)]
1414
pub enum GovernanceAction {
1515
UpgradeContract,
1616
AuthorizeGovernanceDataSourceTransfer,
@@ -37,13 +37,13 @@ impl U8TryIntoGovernanceAction of TryInto<u8, GovernanceAction> {
3737
}
3838
}
3939

40-
#[derive(Drop, Debug)]
40+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
4141
pub struct GovernanceInstruction {
4242
pub target_chain_id: u16,
4343
pub payload: GovernancePayload,
4444
}
4545

46-
#[derive(Drop, Debug)]
46+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
4747
pub enum GovernancePayload {
4848
UpgradeContract: UpgradeContract,
4949
AuthorizeGovernanceDataSourceTransfer: AuthorizeGovernanceDataSourceTransfer,
@@ -54,38 +54,38 @@ pub enum GovernancePayload {
5454
SetWormholeAddress: SetWormholeAddress,
5555
}
5656

57-
#[derive(Drop, Debug)]
57+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
5858
pub struct SetFee {
5959
pub value: u64,
6060
pub expo: u64,
6161
}
6262

63-
#[derive(Drop, Debug)]
63+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
6464
pub struct SetDataSources {
6565
pub sources: Array<DataSource>,
6666
}
6767

68-
#[derive(Drop, Debug)]
68+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
6969
pub struct SetWormholeAddress {
7070
pub address: ContractAddress,
7171
}
7272

73-
#[derive(Drop, Debug)]
73+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
7474
pub struct RequestGovernanceDataSourceTransfer {
7575
// Index is used to prevent replay attacks
7676
// So a claimVaa cannot be used twice.
7777
pub governance_data_source_index: u32,
7878
}
7979

80-
#[derive(Drop, Debug)]
80+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
8181
pub struct AuthorizeGovernanceDataSourceTransfer {
8282
// Transfer governance control over this contract to another data source.
8383
// The claim_vaa field is a VAA created by the new data source; using a VAA prevents mistakes
8484
// in the handoff by ensuring that the new data source can send VAAs (i.e., is not an invalid address).
8585
pub claim_vaa: ByteBuffer,
8686
}
8787

88-
#[derive(Drop, Debug)]
88+
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
8989
pub struct UpgradeContract {
9090
// Class hash of the new contract class. The contract class must already be deployed on the network
9191
// (e.g. with `starkli declare`). Class hash is a Poseidon hash of all properties

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use super::{GetPriceUnsafeError, GetPriceNoOlderThanError};
22
use pyth::byte_buffer::ByteBuffer;
33
use pyth::wormhole::VerifiedVM;
44
use core::starknet::ContractAddress;
5+
use core::fmt::{Debug, Formatter};
6+
use pyth::util::write_i64;
57

68
#[starknet::interface]
79
pub trait IPyth<T> {
@@ -55,7 +57,7 @@ pub trait IPyth<T> {
5557
fn pyth_upgradable_magic(self: @T) -> u32;
5658
}
5759

58-
#[derive(Drop, Debug, Clone, Copy, PartialEq, Hash, Default, Serde, starknet::Store)]
60+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash, Default, starknet::Store)]
5961
pub struct DataSource {
6062
pub emitter_chain_id: u16,
6163
pub emitter_address: u256,
@@ -73,22 +75,48 @@ impl GetDataSourceFromVerifiedVM of GetDataSource<VerifiedVM> {
7375
}
7476
}
7577

76-
#[derive(Drop, Copy, PartialEq, Serde)]
78+
#[derive(Drop, Copy, PartialEq, Serde, Hash, starknet::Store)]
7779
pub struct Price {
7880
pub price: i64,
7981
pub conf: u64,
8082
pub expo: i32,
8183
pub publish_time: u64,
8284
}
8385

84-
#[derive(Drop, Clone, Serde)]
86+
// TODO: use derives after upgrading cairo
87+
impl DebugPrice of Debug<Price> {
88+
fn fmt(self: @Price, ref f: Formatter) -> Result<(), core::fmt::Error> {
89+
write!(f, "Price {{ price: ")?;
90+
write_i64(ref f, *self.price)?;
91+
write!(f, ", conf: {}, expo: ", self.conf)?;
92+
write_i64(ref f, (*self.expo).into())?;
93+
write!(f, ", publish_time: {} }}", self.publish_time)
94+
}
95+
}
96+
97+
#[cfg(test)]
98+
#[test]
99+
fn test_debug_price() {
100+
let value = Price { price: 2, conf: 3, expo: -4, publish_time: 5, };
101+
let expected = "Price { price: 2, conf: 3, expo: -4, publish_time: 5 }";
102+
let actual = format!("{:?}", value);
103+
assert!(actual == expected);
104+
}
105+
106+
impl DefaultPrice of Default<Price> {
107+
fn default() -> Price {
108+
Price { price: 0, conf: 0, expo: 0, publish_time: 0 }
109+
}
110+
}
111+
112+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash, starknet::Store)]
85113
pub struct PriceFeedPublishTime {
86114
pub price_id: u256,
87115
pub publish_time: u64,
88116
}
89117

90118
// PriceFeed represents a current aggregate price from pyth publisher feeds.
91-
#[derive(Drop, Copy, PartialEq, Serde)]
119+
#[derive(Drop, Copy, Debug, PartialEq, Serde, Hash, starknet::Store)]
92120
pub struct PriceFeed {
93121
// The price ID.
94122
pub id: u256,

0 commit comments

Comments
 (0)