melwalletd_prot/
types.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use melstructs::{
6    Address, BlockHeight, CoinData, CoinID, CoinValue, Denom, NetID, Transaction, TxKind,
7};
8use thiserror::Error;
9
10use std::error::Error as StdError;
11
12#[derive(Error, Debug, Serialize, Deserialize)]
13/// Indicates a problem with accessing the wallet
14pub enum WalletAccessError {
15    #[error("wallet not found")]
16    NotFound,
17    #[error("wallet needs to be unlocked")]
18    Locked,
19    #[error("other problem accessing wallet: {0}")]
20    Other(String),
21}
22
23#[derive(Error, Debug, Serialize, Deserialize)]
24pub enum CreateWalletError {
25    #[error("error parsing secret key: {0}")]
26    SecretKey(String),
27    #[error("wallet already exists")]
28    WalletExists,
29    #[error("other error creating wallet: {0}")]
30    Other(String),
31}
32
33#[derive(Error, Debug, Serialize, Deserialize)]
34/// The error type returned by [crate::MelwalletdProtocol::prepare_tx].
35pub enum PrepareTxError {
36    #[error("not enough money ({0} more of {1} needed)")]
37    InsufficientFunds(CoinValue, Denom),
38
39    #[error("cannot access external input coin {0}")]
40    BadExternalInput(CoinID),
41
42    #[error("could not access network: {0}")]
43    Network(NetworkError),
44}
45
46#[derive(Error, Debug, Serialize, Deserialize)]
47/// The error type returned by [crate::MelwalletdProtocol::send_tx].
48pub enum SendTxError {
49    #[error("could not access wallet: {0}")]
50    Wallet(WalletAccessError),
51
52    #[error("could not access network: {0}")]
53    Network(NetworkError),
54}
55
56/// Either a wallet error, or some other kind of error. The JSON representation is ["untagged"](https://serde.rs/enum-representations.html#untagged).
57#[derive(Error, Debug, Serialize, Deserialize)]
58#[serde(untagged)]
59pub enum NeedWallet<T: StdError> {
60    #[error("wallet access error: {0}")]
61    Wallet(WalletAccessError),
62    #[error(transparent)]
63    Other(#[from] T),
64}
65
66/// A network-caused, possibly transient state-access error.
67#[derive(Error, Debug, Serialize, Deserialize)]
68pub enum NetworkError {
69    #[error("transient network error: {0}")]
70    Transient(String),
71    #[error("fatal network error: {0}")]
72    Fatal(String),
73}
74
75#[derive(Clone, Debug, Serialize, Deserialize)]
76/// A JSON-serializable summary of the entire wallet, generally corresponding to whatever is displayed on the "front page" of the wallet.
77pub struct WalletSummary {
78    /// Total MEL balance. JSON-serialized as micromels.
79    pub total_micromel: CoinValue,
80    /// Detailed balance. Keys are the standard (Display/FromStr) string representation of a [Denom].
81    pub detailed_balance: BTreeMap<String, CoinValue>,
82    /// SYM that is inaccessible due to being staked.
83    pub staked_microsym: CoinValue,
84    /// Network ID (mainnet, testnet, etc). JSON-serialized as the corresponding integer.
85    pub network: NetID,
86    /// Address of this wallet. JSON-serialized as the standard `t.....` address format.
87    #[serde(with = "stdcode::asstr")]
88    pub address: Address,
89    /// Whether this wallet is locked. Locked wallets generally cannot be interacted with.
90    pub locked: bool,
91}
92
93#[derive(Debug, Serialize, Deserialize)]
94/// Arguments passed to [crate::MelwalletdProtocol::prepare_tx]. Configures what sort of transaction to construct.
95pub struct PrepareTxArgs {
96    #[serde(default = "txkind_normal")]
97    /// "Kind" of the transaction. Optional in JSON, defaulting to [TxKind::Normal].
98    pub kind: TxKind,
99    /// **Additional** inputs of the transaction. Normally, this field can be left as an empty vector, in which case UTXOs locked by the wallet's own address are picked automatically.
100    ///
101    /// Use this field to specify "out of wallet" coins from dapps, multisig vaults, and such, which do not have their `covhash` field equal to the [Address] of the wallet, yet the wallet is able to spend, possibly in combination with other fields of [PrepareTxArgs]. For example, a multisig coin would not have the [Address] of any single-key wallet, and spending it must require explicitly specifying its [CoinID] and explicitly passing unlock arguments.
102    ///
103    /// Optional in JSON, in which case it defaults to an empty list.
104    #[serde(default)]
105    pub inputs: Vec<CoinID>,
106    /// **Required** outputs of the transaction. This generally specifies the "recipients" of the transaction. Note that this only specifies the first outputs of the transaction; more outputs may be created as "change" outputs.
107    pub outputs: Vec<CoinData>,
108    /// **Additional** covenants that must be included in the transaction. This is needed when spending out-of-wallet coins. Optional in JSON, defaulting to an empty list.
109    #[serde(default, with = "stdcode::hexvec")]
110    pub covenants: Vec<Vec<u8>>,
111    /// The "data" field of the transaction. Optional and hex-encoded in JSON, defaulting to an empty string.
112    #[serde(default, with = "stdcode::hex")]
113    pub data: Vec<u8>,
114    #[serde(default)]
115    /// The "data" field of the transaction. Optional and hex-encoded in JSON, defaulting to an empty string.
116    pub nobalance: Vec<Denom>,
117    #[serde(default)]
118    /// Pretend like the transaction has this many more bytes when calculating the correct fee level. Useful in niche situations where you want to intentionally pay more fees than necessary.
119    pub fee_ballast: usize,
120}
121
122fn txkind_normal() -> TxKind {
123    TxKind::Normal
124}
125
126#[derive(Serialize, Deserialize)]
127/// Returned from [crate::MelwalletdProtocol::simulate_swap], encoding information about a simulated Melswap swap.
128pub struct SwapInfo {
129    /// How many units of the "other" token will the swap obtain
130    pub result: u128,
131    /// Impact to the price, as a fraction
132    pub price_impact: f64,
133    /// String representation of the poolkey
134    pub poolkey: String,
135}
136#[derive(Serialize, Deserialize)]
137/// A tuple including:
138/// - whether the transaction is spent
139/// - the kind of the transaction
140/// - a mapping between string-represented [Denom]s and how much the balance of that [Denom] changed in this transaction.
141pub struct TxBalance(pub bool, pub TxKind, pub BTreeMap<String, i128>);
142
143#[derive(Serialize, Deserialize, Clone, Debug)]
144/// The status of a transaction that is currently in progress.
145pub struct TransactionStatus {
146    pub raw: Transaction,
147    pub confirmed_height: Option<BlockHeight>,
148    pub outputs: Vec<AnnCoinID>,
149}
150
151#[derive(Serialize, Deserialize, Clone, Debug)]
152/// An "annotated" CoinID that marks whether or not this coin is a change output or not.
153pub struct AnnCoinID {
154    pub coin_data: CoinData,
155    pub is_change: bool,
156    pub coin_id: String,
157}