forked from Dfunder/stellarAid-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdonation_verifier.rs
More file actions
91 lines (84 loc) · 2.44 KB
/
Copy pathdonation_verifier.rs
File metadata and controls
91 lines (84 loc) · 2.44 KB
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
use sdk::horizon::client::{HorizonClient, HorizonError};
use crate::models::donation_status::{DonationEvent, DonationStatus};
use tracing::{info, warn, error, Instrument, Span};
#[derive(Debug)]
pub struct VerificationResult {
pub status: DonationStatus,
pub ledger: Option<u64>,
pub created_at: Option<String>,
}
pub async fn cross_check_transaction(
horizon: &HorizonClient,
tx_hash: &str,
current_status: DonationStatus,
) -> Result<VerificationResult, HorizonError> {
let span = Span::current();
span.record("tx_hash", tx_hash);
span.record("current_status", %current_status);
info!(
tx_hash = tx_hash,
current_status = %current_status,
"starting donation verification"
);
let tx = match horizon.get_transaction(tx_hash).await {
Ok(tx) => {
info!(
tx_hash = tx_hash,
successful = tx.successful,
ledger = tx.ledger,
"horizon transaction fetched"
);
tx
}
Err(e) => {
error!(
tx_hash = tx_hash,
error = %e,
"failed to fetch transaction from horizon"
);
return Err(e);
}
};
let event = if tx.successful {
DonationEvent::Confirm
} else {
DonationEvent::Fail
};
let status = match current_status {
DonationStatus::Submitted => {
let new = DonationStatus::Confirming
.transition(event)
.unwrap_or(DonationStatus::Failed);
info!(
tx_hash = tx_hash,
from = %DonationStatus::Submitted,
to = %new,
"donation status transition"
);
new
}
DonationStatus::Confirming => {
let new = current_status.transition(event).unwrap_or(DonationStatus::Failed);
info!(
tx_hash = tx_hash,
from = %current_status,
to = %new,
"donation status transition"
);
new
}
other => {
warn!(
tx_hash = tx_hash,
status = %other,
"unexpected donation status, no transition applied"
);
other
}
};
Ok(VerificationResult {
status,
ledger: tx.ledger,
created_at: Some(tx.created_at),
})
}