Skip to content

Commit 9ff88bc

Browse files
authored
Use integer values for rewards input/output (#157)
Internal calculations are still done using decimal values for better precision but input, and mainly output, are now integer values to better support lovelaces calculation (or actually any other token).
1 parent 5ee0318 commit 9ff88bc

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

catalyst-toolbox/src/bin/cli/rewards/community_advisors.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
use catalyst_toolbox::rewards::community_advisors::{
2+
calculate_ca_rewards, ApprovedProposals, CommunityAdvisor, FundSetting, Funds,
3+
ProposalRewardSlots, ProposalsReviews, Rewards, Seed,
4+
};
5+
use catalyst_toolbox::utils;
16
use chain_crypto::digest::DigestOf;
2-
use color_eyre::eyre::bail;
7+
use color_eyre::eyre::{bail, eyre};
38
use color_eyre::Report;
9+
use rust_decimal::prelude::ToPrimitive;
410
use serde::Serialize;
511
use std::collections::{BTreeMap, BTreeSet};
612
use std::path::{Path, PathBuf};
713
use std::str::FromStr;
814

9-
use catalyst_toolbox::rewards::community_advisors::{
10-
calculate_ca_rewards, ApprovedProposals, CommunityAdvisor, FundSetting, Funds,
11-
ProposalRewardSlots, ProposalsReviews, Rewards, Seed,
12-
};
13-
use catalyst_toolbox::utils;
14-
1515
use catalyst_toolbox::community_advisors::models::{
1616
AdvisorReviewRow, ApprovedProposalRow, ProposalStatus,
1717
};
@@ -26,9 +26,9 @@ struct FundSettingOpt {
2626
/// % ratio, range in [0, 100]
2727
#[structopt(long = "bonus-ratio")]
2828
bonus_ratio: u8,
29-
/// total amount of funds to be rewarded
29+
/// total amount of funds to be rewarded (integer value)
3030
#[structopt(long = "funds")]
31-
total: Funds,
31+
total: u64,
3232
}
3333

3434
#[derive(StructOpt)]
@@ -171,7 +171,7 @@ impl From<FundSettingOpt> for FundSetting {
171171
Self {
172172
proposal_ratio: settings.proposal_ratio,
173173
bonus_ratio: settings.bonus_ratio,
174-
total: settings.total,
174+
total: Rewards::from(settings.total),
175175
}
176176
}
177177
}
@@ -187,34 +187,42 @@ impl From<ProposalRewardsSlotsOpt> for ProposalRewardSlots {
187187
}
188188
}
189189

190-
fn rewards_to_csv_data(rewards: &BTreeMap<CommunityAdvisor, Rewards>) -> Vec<impl Serialize> {
190+
fn rewards_to_csv_data(
191+
rewards: &BTreeMap<CommunityAdvisor, Rewards>,
192+
) -> Result<Vec<impl Serialize>, Report> {
191193
#[derive(Serialize)]
192194
struct Entry {
193195
id: String,
194-
rewards: Rewards,
196+
rewards: u64,
195197
}
196198

197199
rewards
198200
.iter()
199-
.map(|(id, rewards)| Entry {
200-
id: id.clone(),
201-
rewards: *rewards,
201+
.map(|(id, rewards)| {
202+
Ok(Entry {
203+
id: id.clone(),
204+
rewards: rewards.to_u64().ok_or_else(|| eyre!("Rewards overflow"))?,
205+
})
202206
})
203207
.collect()
204208
}
205209

206-
fn bonus_to_csv_data(rewards: BTreeMap<String, Rewards>) -> Vec<impl Serialize> {
210+
fn bonus_to_csv_data(rewards: BTreeMap<String, Rewards>) -> Result<Vec<impl Serialize>, Report> {
207211
#[derive(Serialize)]
208212
struct Entry {
209213
proposal_id: String,
210-
bonus_rewards: Rewards,
214+
bonus_rewards: u64,
211215
}
212216

213217
rewards
214218
.into_iter()
215-
.map(|(proposal_id, bonus_rewards)| Entry {
216-
proposal_id,
217-
bonus_rewards,
219+
.map(|(proposal_id, bonus_rewards)| {
220+
Ok(Entry {
221+
proposal_id,
222+
bonus_rewards: bonus_rewards
223+
.to_u64()
224+
.ok_or_else(|| eyre!("Rewards overflow"))?,
225+
})
218226
})
219227
.collect()
220228
}

catalyst-toolbox/src/bin/cli/rewards/veterans.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use catalyst_toolbox::community_advisors::models::VeteranRankingRow;
22
use catalyst_toolbox::rewards::veterans::{self, VcaRewards, VeteranAdvisorIncentive};
33
use catalyst_toolbox::rewards::Rewards;
44
use catalyst_toolbox::utils::csv;
5-
use color_eyre::eyre::bail;
5+
use color_eyre::eyre::{bail, eyre};
66
use color_eyre::Report;
7-
use rust_decimal::Decimal;
7+
use rust_decimal::{prelude::*, Decimal};
88
use serde::Serialize;
99
use std::path::PathBuf;
1010
use structopt::StructOpt;
@@ -18,9 +18,9 @@ pub struct VeteransRewards {
1818
/// Results file output path
1919
to: PathBuf,
2020

21-
/// Reward to be distributed
21+
/// Reward to be distributed (integer value)
2222
#[structopt(long = "total-rewards")]
23-
total_rewards: Rewards,
23+
total_rewards: u64,
2424

2525
/// Minimum number of rankings for each vca to be considered for reputation and rewards
2626
/// distribution
@@ -102,7 +102,7 @@ impl VeteransRewards {
102102

103103
let results = veterans::calculate_veteran_advisors_incentives(
104104
&reviews,
105-
total_rewards,
105+
Rewards::from(total_rewards),
106106
min_rankings..=max_rankings_rewards,
107107
min_rankings..=max_rankings_reputation,
108108
rewards_agreement_rate_cutoffs
@@ -115,17 +115,17 @@ impl VeteransRewards {
115115
.collect(),
116116
);
117117

118-
csv::dump_data_to_csv(rewards_to_csv_data(results).iter(), &to).unwrap();
118+
csv::dump_data_to_csv(rewards_to_csv_data(results)?.iter(), &to).unwrap();
119119

120120
Ok(())
121121
}
122122
}
123123

124-
fn rewards_to_csv_data(rewards: VcaRewards) -> Vec<impl Serialize> {
124+
fn rewards_to_csv_data(rewards: VcaRewards) -> Result<Vec<impl Serialize>, Report> {
125125
#[derive(Serialize)]
126126
struct Entry {
127127
id: String,
128-
rewards: Rewards,
128+
rewards: u64,
129129
reputation: u64,
130130
}
131131

@@ -138,10 +138,12 @@ fn rewards_to_csv_data(rewards: VcaRewards) -> Vec<impl Serialize> {
138138
rewards,
139139
reputation,
140140
},
141-
)| Entry {
142-
id,
143-
rewards,
144-
reputation,
141+
)| {
142+
Ok(Entry {
143+
id,
144+
rewards: rewards.to_u64().ok_or_else(|| eyre!("Rewards overflow"))?,
145+
reputation,
146+
})
145147
},
146148
)
147149
.collect()

0 commit comments

Comments
 (0)