1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#[cfg(feature = "simulated-payouts")]
use sn_data_types::Transfer;
#[cfg(feature = "simulated-payouts")]
use sn_messaging::client::{Cmd, TransferCmd};
#[cfg(feature = "simulated-payouts")]
use log::info;
use crate::{Client, Error};
use sn_data_types::Token;
impl Client {
#[cfg(not(feature = "simulated-payouts"))]
pub async fn trigger_simulated_farming_payout(&mut self, _amount: Token) -> Result<(), Error> {
Err(Error::NotBuiltWithSimulatedPayouts)
}
#[cfg(feature = "simulated-payouts")]
pub async fn trigger_simulated_farming_payout(&mut self, amount: Token) -> Result<(), Error> {
let pk = self.public_key().await;
info!("Triggering a simulated farming payout to: {:?}", pk);
self.simulated_farming_payout_dot.apply_inc();
let simulated_transfer = Transfer {
to: pk,
amount,
debit_id: self.simulated_farming_payout_dot,
msg: "asdf".to_string(),
};
let simluated_farming_cmd =
Cmd::Transfer(TransferCmd::SimulatePayout(simulated_transfer.clone()));
let message = self.create_cmd_message(simluated_farming_cmd).await?;
let _ = self.session.send_cmd(&message).await?;
info!("Applying simulated payout locally, via query for history...");
self.get_history().await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::test_utils::create_test_client;
use anyhow::Result;
use std::str::FromStr;
use tokio::time::{sleep, Duration};
#[tokio::test]
#[cfg(feature = "simulated-payouts")]
async fn transfer_actor_can_receive_simulated_farming_payout() -> Result<()> {
let mut client = create_test_client().await?;
let _ = client
.trigger_simulated_farming_payout(Token::from_str("100")?)
.await?;
let mut tokens = client.get_balance_from_network(None).await?;
while tokens != Token::from_str("110")? {
sleep(Duration::from_millis(200)).await;
tokens = client.get_balance_from_network(None).await?;
}
Ok(())
}
}