forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.rs
More file actions
119 lines (111 loc) · 3.94 KB
/
Copy pathstellar.rs
File metadata and controls
119 lines (111 loc) · 3.94 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
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
119
use echomirror_stellar as stellar;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::client::PyEchoMirrorClient;
use crate::errors::to_py_err;
use crate::types::{
PyStellarBalance, PyStellarTransaction, PyTransactionHistoryPage, PyUnsignedTransaction,
};
/// Stellar balance lookups, ECHO transfers, transaction history, and testnet funding.
///
/// ```python
/// stellar = StellarClient(client)
/// balance = await stellar.get_balance(public_key)
/// ```
#[pyclass(name = "StellarClient")]
#[derive(Clone)]
pub struct PyStellarClient {
client: PyEchoMirrorClient,
}
#[pymethods]
impl PyStellarClient {
#[new]
pub fn new(client: PyEchoMirrorClient) -> Self {
Self { client }
}
/// Get the XLM and ECHO token balance for a Stellar public key.
fn get_balance<'py>(&self, py: Python<'py>, public_key: String) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let balance = stellar::get_balance(&inner, &public_key)
.await
.map_err(to_py_err)?;
Ok(PyStellarBalance::from(balance))
})
}
/// Build an unsigned ECHO transfer. Sign the returned `xdr` (e.g. with a
/// wallet or a secret key), then pass it to `submit_transaction`.
#[pyo3(signature = (from_address, to_address, amount, memo=None))]
fn build_transfer<'py>(
&self,
py: Python<'py>,
from_address: String,
to_address: String,
amount: f64,
memo: Option<String>,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let params = stellar::EchoTransferParams {
from: from_address,
to: to_address,
amount,
memo,
};
let unsigned = stellar::build_echo_transfer(&inner, params)
.await
.map_err(to_py_err)?;
Ok(PyUnsignedTransaction::from(unsigned))
})
}
/// Submit a pre-signed XDR transaction envelope.
fn submit_transaction<'py>(
&self,
py: Python<'py>,
signed_xdr: String,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let tx = stellar::submit_transaction(&inner, &signed_xdr)
.await
.map_err(to_py_err)?;
Ok(PyStellarTransaction::from(tx))
})
}
/// Get paginated Stellar transaction history for a public key.
#[pyo3(signature = (public_key, limit=20, cursor=None))]
fn get_transaction_history<'py>(
&self,
py: Python<'py>,
public_key: String,
limit: u32,
cursor: Option<String>,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let page =
stellar::get_transaction_history(&inner, &public_key, limit, cursor.as_deref())
.await
.map_err(to_py_err)?;
Ok(PyTransactionHistoryPage {
transactions: page.transactions.into_iter().map(Into::into).collect(),
cursor: page.cursor,
})
})
}
/// Fund a testnet account using Stellar Friendbot (gives 10,000 XLM).
/// Raises `ConfigError` if the client is configured for mainnet.
fn fund_testnet_account<'py>(
&self,
py: Python<'py>,
public_key: String,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
stellar::fund_testnet_account(&inner, &public_key)
.await
.map_err(to_py_err)?;
Ok(())
})
}
}