Skip to content

Commit 1aa3f5d

Browse files
committed
better version
1 parent c0992c1 commit 1aa3f5d

File tree

4 files changed

+45
-57
lines changed

4 files changed

+45
-57
lines changed

apps/fortuna/src/chain/ethereum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<T: JsonRpcClient + 'static + Clone> SignablePythContractInner<T> {
181181
let chain_id = provider.get_chainid().await?;
182182
let gas_oracle = EthProviderOracle::new(
183183
provider.clone(),
184-
chain_config.priority_fee_multiplier_pct.multiplier(),
184+
chain_config.priority_fee_multiplier_pct.value(),
185185
);
186186
let wallet__ = private_key
187187
.parse::<LocalWallet>()?

apps/fortuna/src/config.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ impl Config {
8787

8888
// Run correctness checks for the config and fail if there are any issues.
8989
for (chain_id, config) in config.chains.iter() {
90-
if !(config.min_profit_pct.value() <= config.target_profit_pct.value()
91-
&& config.target_profit_pct.value() <= config.max_profit_pct.value())
90+
if !(config.min_profit_multiplier_pct.value()
91+
<= config.target_profit_multiplier_pct.value()
92+
&& config.target_profit_multiplier_pct.value()
93+
<= config.max_profit_multiplier_pct.value())
9294
{
93-
return Err(anyhow!("chain id {:?} configuration is invalid. Config must satisfy min_profit_pct <= target_profit_pct <= max_profit_pct.", chain_id));
95+
return Err(anyhow!("chain id {:?} configuration is invalid. Config must satisfy min_profit_multiplier_pct <= target_profit_multiplier_pct <= max_profit_multiplier_pct.", chain_id));
9496
}
9597
}
9698

@@ -145,23 +147,26 @@ pub struct EthereumConfig {
145147
pub escalation_policy: EscalationPolicyConfig,
146148

147149
/// The minimum percentage profit to earn as a function of the callback cost.
148-
/// For example, 20 means a profit of 20% over the cost of a callback that uses the full gas limit.
150+
/// A callback using the full gas limit has a cost of 100.
151+
/// Thus, 120 means a profit of 20% over the cost of a callback that uses the full gas limit.
149152
/// The fee will be raised if the profit is less than this number.
150-
/// The minimum value for this is -100. If set to < 0, it means the keeper may lose money on callbacks that use the full gas limit.
151-
pub min_profit_pct: Percentage,
153+
/// The minimum value for this is 0. If set to < 100, it means the keeper may lose money on callbacks that use the full gas limit.
154+
pub min_profit_multiplier_pct: Percentage,
152155

153156
/// The target percentage profit to earn as a function of the callback cost.
154-
/// For example, 20 means a profit of 20% over the cost of a callback that uses the full gas limit.
157+
/// A callback using the full gas limit has a cost of 100.
158+
/// Thus, 120 means a profit of 20% over the cost of a callback that uses the full gas limit.
155159
/// The fee will be set to this target whenever it falls outside the min/max bounds.
156-
/// The minimum value for this is -100. If set to < 0, it means the keeper may lose money on callbacks that use the full gas limit.
157-
pub target_profit_pct: Percentage,
160+
/// The minimum value for this is 0. If set to < 100, it means the keeper may lose money on callbacks that use the full gas limit.
161+
pub target_profit_multiplier_pct: Percentage,
158162

159163
/// The maximum percentage profit to earn as a function of the callback cost.
160-
/// For example, 100 means a profit of 100% over the cost of a callback that uses the full gas limit.
164+
/// A callback using the full gas limit has a cost of 100.
165+
/// Thus, 200 means a profit of 100% over the cost of a callback that uses the full gas limit.
161166
/// The fee will be lowered if it is more profitable than specified here.
162-
/// Must be larger than min_profit_pct.
163-
/// The minimum value for this is -100. If set to < 0, it means the keeper may lose money on callbacks that use the full gas limit.
164-
pub max_profit_pct: Percentage,
167+
/// Must be larger than min_profit_multiplier_pct.
168+
/// The minimum value for this is 0. If set to < 100, it means the keeper may lose money on callbacks that use the full gas limit.
169+
pub max_profit_multiplier_pct: Percentage,
165170

166171
/// Minimum wallet balance for the keeper. If the balance falls below this level, the keeper will
167172
/// withdraw fees from the contract to top up. This functionality requires the keeper to be the fee
@@ -192,7 +197,7 @@ fn default_block_delays() -> Vec<u64> {
192197
}
193198

194199
fn default_priority_fee_multiplier_pct() -> Percentage {
195-
Percentage::from_u32(100)
200+
Percentage::new(100)
196201
}
197202

198203
fn default_backlog_range() -> u64 {
@@ -228,27 +233,27 @@ pub struct EscalationPolicyConfig {
228233
}
229234

230235
fn default_gas_limit_tolerance_pct() -> Percentage {
231-
Percentage::from_u32(110)
236+
Percentage::new(110)
232237
}
233238

234239
fn default_initial_gas_multiplier_pct() -> Percentage {
235-
Percentage::from_u32(125)
240+
Percentage::new(125)
236241
}
237242

238243
fn default_gas_multiplier_pct() -> Percentage {
239-
Percentage::from_u32(110)
244+
Percentage::new(110)
240245
}
241246

242247
fn default_gas_multiplier_cap_pct() -> Percentage {
243-
Percentage::from_u32(600)
248+
Percentage::new(600)
244249
}
245250

246251
fn default_fee_multiplier_pct() -> Percentage {
247-
Percentage::from_u32(110)
252+
Percentage::new(110)
248253
}
249254

250255
fn default_fee_multiplier_cap_pct() -> Percentage {
251-
Percentage::from_u32(200)
256+
Percentage::new(200)
252257
}
253258

254259
impl Default for EscalationPolicyConfig {
@@ -267,12 +272,12 @@ impl Default for EscalationPolicyConfig {
267272
impl EscalationPolicyConfig {
268273
pub fn to_policy(&self) -> EscalationPolicy {
269274
EscalationPolicy {
270-
gas_limit_tolerance_pct: self.gas_limit_tolerance_pct.value() as u64,
271-
initial_gas_multiplier_pct: self.initial_gas_multiplier_pct.value() as u64,
272-
gas_multiplier_pct: self.gas_multiplier_pct.value() as u64,
273-
gas_multiplier_cap_pct: self.gas_multiplier_cap_pct.value() as u64,
274-
fee_multiplier_pct: self.fee_multiplier_pct.value() as u64,
275-
fee_multiplier_cap_pct: self.fee_multiplier_cap_pct.value() as u64,
275+
gas_limit_tolerance_pct: self.gas_limit_tolerance_pct.value(),
276+
initial_gas_multiplier_pct: self.initial_gas_multiplier_pct.value(),
277+
gas_multiplier_pct: self.gas_multiplier_pct.value(),
278+
gas_multiplier_cap_pct: self.gas_multiplier_cap_pct.value(),
279+
fee_multiplier_pct: self.fee_multiplier_pct.value(),
280+
fee_multiplier_cap_pct: self.fee_multiplier_cap_pct.value(),
276281
}
277282
}
278283
}

apps/fortuna/src/keeper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ pub async fn run_keeper_threads(
143143
// In the unlikely event that the keeper fees aren't sufficient, the solution to this is to configure the target
144144
// fee percentage to be higher on that specific chain.
145145
chain_eth_config.gas_limit,
146-
chain_eth_config.min_profit_pct.multiplier(),
147-
chain_eth_config.target_profit_pct.multiplier(),
148-
chain_eth_config.max_profit_pct.multiplier(),
146+
chain_eth_config.min_profit_multiplier_pct.value(),
147+
chain_eth_config.target_profit_multiplier_pct.value(),
148+
chain_eth_config.max_profit_multiplier_pct.value(),
149149
chain_eth_config.fee,
150150
metrics.clone(),
151151
)
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
use anyhow::{anyhow, Result};
1+
use anyhow::Result;
22

3-
/// A type representing a percentage value that must be >= -100
3+
/// A type representing a percentage value that must be >= 0
44
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
5-
pub struct Percentage(i64);
5+
pub struct Percentage(u64);
66

77
impl Percentage {
8-
pub fn new(value: i64) -> Result<Self> {
9-
if value < -100 {
10-
return Err(anyhow!("Percentage value must be >= -100"));
11-
}
12-
Ok(Self(value))
13-
}
14-
15-
pub fn from_u32(value: u32) -> Self {
16-
Self(value as i64)
8+
pub fn new(value: u64) -> Self {
9+
Self(value)
1710
}
1811

19-
pub fn value(&self) -> i64 {
12+
pub fn value(&self) -> u64 {
2013
self.0
2114
}
22-
23-
pub fn multiplier(&self) -> u64 {
24-
u64::try_from(100 + self.0).unwrap_or(0)
25-
}
2615
}
2716

2817
impl serde::Serialize for Percentage {
2918
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
3019
where
3120
S: serde::Serializer,
3221
{
33-
serializer.serialize_i64(self.0)
22+
serializer.serialize_u64(self.0)
3423
}
3524
}
3625

@@ -39,19 +28,13 @@ impl<'de> serde::Deserialize<'de> for Percentage {
3928
where
4029
D: serde::Deserializer<'de>,
4130
{
42-
let value = i64::deserialize(deserializer)?;
43-
Self::new(value).map_err(serde::de::Error::custom)
44-
}
45-
}
46-
47-
impl From<Percentage> for i64 {
48-
fn from(p: Percentage) -> Self {
49-
p.0
31+
let value = u64::deserialize(deserializer)?;
32+
Ok(Self(value))
5033
}
5134
}
5235

5336
impl From<Percentage> for u64 {
5437
fn from(p: Percentage) -> Self {
55-
p.0 as u64
38+
p.0
5639
}
5740
}

0 commit comments

Comments
 (0)