ree_types/
exchange_interfaces.rs

1use crate::{CoinBalance, IntentionSet, Pubkey, Txid, Utxo};
2use alloc::borrow::Cow;
3use candid::CandidType;
4use ic_stable_structures::{Storable, storable::Bound};
5use serde::{Deserialize, Serialize};
6
7#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
8pub struct PoolBasic {
9    pub name: String,
10    pub address: String,
11}
12
13#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
14pub struct PoolInfo {
15    pub key: Pubkey,
16    pub key_derivation_path: Vec<Vec<u8>>,
17    pub name: String,
18    pub address: String,
19    pub nonce: u64,
20    pub coin_reserved: Vec<CoinBalance>,
21    pub btc_reserved: u64,
22    pub utxos: Vec<Utxo>,
23    pub attributes: String,
24}
25
26pub type GetPoolListResponse = Vec<PoolBasic>;
27
28#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
29pub struct GetPoolInfoArgs {
30    pub pool_address: String,
31}
32
33pub type GetPoolInfoResponse = Option<PoolInfo>;
34
35#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
36pub struct ExecuteTxArgs {
37    pub psbt_hex: String,
38    pub txid: Txid,
39    pub intention_set: IntentionSet,
40    pub intention_index: u32,
41    pub zero_confirmed_tx_queue_length: u32,
42    pub is_reapply: Option<bool>,
43}
44
45impl ExecuteTxArgs {
46    pub fn psbt(&self) -> Result<bitcoin::Psbt, String> {
47        let raw = hex::decode(&self.psbt_hex).map_err(|_| "invalid psbt".to_string())?;
48        bitcoin::Psbt::deserialize(raw.as_slice()).map_err(|_| "invalid psbt".to_string())
49    }
50}
51
52pub type ExecuteTxResponse = Result<String, String>;
53
54#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
55pub struct RollbackTxArgs {
56    pub txid: Txid,
57    pub reason_code: String,
58}
59
60pub type RollbackTxResponse = Result<(), String>;
61
62/// The parameters for the hook `on_block_received` and `on_block_processed`
63#[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
64pub struct NewBlockInfo {
65    pub block_height: u32,
66    pub block_hash: String,
67    /// The block timestamp in seconds since the Unix epoch.
68    pub block_timestamp: u64,
69    pub confirmed_txids: Vec<Txid>,
70}
71
72pub type NewBlockArgs = NewBlockInfo;
73
74pub type NewBlockResponse = Result<(), String>;
75
76impl Storable for NewBlockInfo {
77    fn to_bytes(&self) -> Cow<'_, [u8]> {
78        let bytes = bincode::serialize(self).unwrap();
79        Cow::Owned(bytes)
80    }
81
82    fn into_bytes(self) -> Vec<u8> {
83        bincode::serialize(&self).unwrap()
84    }
85
86    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
87        bincode::deserialize(bytes.as_ref()).unwrap()
88    }
89
90    const BOUND: Bound = Bound::Unbounded;
91}