Skip to content

Commit 16ff912

Browse files
committed
Extended the example program to derive the concrete rewards obtained by users.
1 parent e04a2c6 commit 16ff912

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

examples/shielded_rewards.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::Write;
22

3-
use namada_core::dec::Dec;
43
use namada_core::token::DenominatedAmount;
4+
use namada_core::uint::Uint;
55

66
/// Default value for the native currency code
77
const DEFAULT_NATIVE_CODE: &str = "NAM";
@@ -68,6 +68,16 @@ pub fn main() -> std::io::Result<()> {
6868
.map_err(std::io::Error::other)?
6969
};
7070

71+
// Get the exchange rate for the native token
72+
print!("Exchange rate USD/{}: ", native_code);
73+
std::io::stdout().flush()?;
74+
let mut native_exchange_rate = String::new();
75+
stdin.read_line(&mut native_exchange_rate)?;
76+
let native_exchange_rate = native_exchange_rate
77+
.trim()
78+
.parse::<DenominatedAmount>()
79+
.map_err(std::io::Error::other)?;
80+
7181
// Let S be the total supply of NAM
7282
print!("Native token supply in {}: ", native_code);
7383
std::io::stdout().flush()?;
@@ -113,6 +123,16 @@ pub fn main() -> std::io::Result<()> {
113123
.map_err(std::io::Error::other)?
114124
};
115125

126+
// Get the exchange rate for the incentivised token
127+
print!("Exchange rate USD/{}: ", incent_code);
128+
std::io::stdout().flush()?;
129+
let mut incent_exchange_rate = String::new();
130+
stdin.read_line(&mut incent_exchange_rate)?;
131+
let incent_exchange_rate = incent_exchange_rate
132+
.trim()
133+
.parse::<DenominatedAmount>()
134+
.map_err(std::io::Error::other)?;
135+
116136
// Let X be the target amount of TOK locked in the MASP
117137
print!("Target locked amount in {}: ", incent_code);
118138
std::io::stdout().flush()?;
@@ -186,14 +206,46 @@ pub fn main() -> std::io::Result<()> {
186206
std::io::stdout().flush()?;
187207
let mut precision = String::new();
188208
stdin.read_line(&mut precision)?;
189-
let precision = precision
190-
.trim()
191-
.parse::<Dec>()
209+
let precision = Uint::from_str_radix(precision.trim(), 10)
192210
.map_err(std::io::Error::other)?;
211+
212+
// A reward of I*P/X uNAM is obtained for every P TOK locked in the pool
213+
let reward_per_precision = (inflation.amount().raw_amount() * precision)
214+
/ lock_target.amount().raw_amount();
215+
let precision =
216+
DenominatedAmount::new(precision.into(), incent_decimals.into());
217+
let reward_per_precision = DenominatedAmount::new(
218+
reward_per_precision.into(),
219+
native_decimals.into(),
220+
);
221+
let precision_usd = precision.checked_mul(incent_exchange_rate).ok_or(
222+
std::io::Error::other("unable to multiply precision by exchange rate"),
223+
)?;
224+
let reward_usd_per_precision = reward_per_precision
225+
.checked_mul(native_exchange_rate)
226+
.ok_or(std::io::Error::other(
227+
"unable to multiply minimum reward by exchange rate",
228+
))?;
229+
let exchanged_reward_rate = reward_usd_per_precision
230+
.checked_div(precision_usd)
231+
.ok_or(std::io::Error::other("unable to divide "))?;
232+
// Summarise the rewards from the perrspective of end-users
193233
println!(
194-
"Users holding at least {} {} in the shielded pool will receive \
195-
rewards.",
196-
precision, incent_code
234+
"For every {} {} held in the shielded pool, a shielded reward of {} \
235+
{} will be distributed every MASP epoch. Concretely this means that \
236+
for every {} USD worth of {} held in the shielded pool, a {} \
237+
shielded reward worth {} USD will be rewarded. Hence there's \
238+
effectively a reward rate of {} when all quantities are expressed in \
239+
the same units.",
240+
precision,
241+
incent_code,
242+
reward_per_precision,
243+
native_code,
244+
precision_usd,
245+
incent_code,
246+
native_code,
247+
reward_usd_per_precision,
248+
exchanged_reward_rate,
197249
);
198250

199251
// It must be that S*C/Y >= I, which implies that the reward rate

0 commit comments

Comments
 (0)