forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontract.rs
55 lines (46 loc) · 1.4 KB
/
contract.rs
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
use crate::{get_state, set_state, CosmosMsg, InitParams, SendAmount, SendParams};
use failure::{bail, Error};
use serde::{Deserialize, Serialize};
use serde_json::{from_slice, from_str, to_vec};
#[derive(Serialize, Deserialize)]
struct RegenInitMsg {
verifier: String,
beneficiary: String,
}
#[derive(Serialize, Deserialize)]
struct RegenState {
verifier: String,
beneficiary: String,
payout: u64,
funder: String,
}
#[derive(Serialize, Deserialize)]
struct RegenSendMsg {}
pub fn init(params: InitParams) -> Result<Vec<CosmosMsg>, Error> {
let msg: RegenInitMsg = from_str(params.msg.get())?;
set_state(to_vec(&RegenState {
verifier: msg.verifier,
beneficiary: msg.beneficiary,
payout: params.sent_funds,
funder: params.sender
})?);
Ok(Vec::new())
}
pub fn send(params: SendParams) -> Result<Vec<CosmosMsg>, Error> {
let mut state: RegenState = from_slice(&get_state())?;
let funds = state.payout + params.sent_funds;
state.payout = 0;
set_state(to_vec(&state)?);
if params.sender == state.verifier {
Ok(vec![CosmosMsg::SendTx {
from_address: params.contract_address,
to_address: state.beneficiary,
amount: vec![SendAmount {
denom: "earth".into(),
amount: funds.to_string(),
}],
}])
} else {
bail!("Unauthorized")
}
}