-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.rs
60 lines (49 loc) · 1.45 KB
/
file.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
56
57
58
59
60
use std::str::FromStr;
use bundlr_contracts_shared::{
contract_utils::js_imports::{Contract, SmartWeave},
Address, Amount,
};
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsValue;
use crate::{
action::ActionResult, contract_utils::handler_result::HandlerResult, error::ContractError,
state::State,
};
#[derive(Serialize)]
struct Input {
function: String,
from: Address,
to: Address,
amount: Amount,
}
#[derive(Debug, Deserialize)]
struct Result {
#[serde(rename = "type")]
result_type: String,
}
pub async fn join(mut state: State) -> ActionResult {
let caller = SmartWeave::caller()
.parse::<Address>()
.map_err(|err| ContractError::ParseError(err.to_string()))?;
if !state.allowed_interactors.contains(&caller) {
return Err(ContractError::Forbidden);
}
let result = SmartWeave::write(
&state.token.to_string(),
JsValue::from_serde(&Input {
function: "transferFrom".to_string(),
from: caller.clone(),
to: Address::from_str(&Contract::id())
.map_err(|err| ContractError::ParseError(err.to_string()))?,
amount: state.stake,
})
.unwrap(),
)
.await;
let result: Result = result.into_serde().unwrap();
if result.result_type != "ok" {
return Err(ContractError::TransferFailed);
}
state.bundlers.insert(caller, None);
Ok(HandlerResult::NewState(state))
}