chia_sdk_bindings/puzzle/
streamed_asset.rs

1use bindy::Result;
2use chia_protocol::{Bytes, Bytes32, Coin};
3use chia_puzzle_types::LineageProof;
4use chia_sdk_driver::{StreamedAsset, StreamingPuzzleInfo};
5
6pub trait StreamedAssetExt {
7    fn new(
8        coin: Coin,
9        asset_id: Option<Bytes32>,
10        proof: Option<LineageProof>,
11        info: StreamingPuzzleInfo,
12    ) -> Self;
13    fn xch(coin: Coin, info: StreamingPuzzleInfo) -> Result<StreamedAsset>;
14    fn cat(
15        coin: Coin,
16        asset_id: Bytes32,
17        proof: LineageProof,
18        info: StreamingPuzzleInfo,
19    ) -> Result<StreamedAsset>;
20}
21
22impl StreamedAssetExt for StreamedAsset {
23    fn new(
24        coin: Coin,
25        asset_id: Option<Bytes32>,
26        proof: Option<LineageProof>,
27        info: StreamingPuzzleInfo,
28    ) -> Self {
29        StreamedAsset {
30            coin,
31            asset_id,
32            proof,
33            info,
34        }
35    }
36
37    fn xch(coin: Coin, info: StreamingPuzzleInfo) -> Result<StreamedAsset> {
38        Ok(StreamedAsset::xch(coin, info))
39    }
40
41    fn cat(
42        coin: Coin,
43        asset_id: Bytes32,
44        proof: LineageProof,
45        info: StreamingPuzzleInfo,
46    ) -> Result<StreamedAsset> {
47        Ok(StreamedAsset::cat(coin, asset_id, proof, info))
48    }
49}
50
51pub trait StreamingPuzzleInfoExt: Sized {
52    fn amount_to_be_paid(&self, my_coin_amount: u64, payment_time: u64) -> Result<u64>;
53    fn get_hint(recipient: Bytes32) -> Result<Bytes32>;
54    fn get_launch_hints(&self) -> Result<Vec<Bytes>>;
55    fn inner_puzzle_hash(&self) -> Result<Bytes32>;
56    fn from_memos(memos: Vec<Bytes>) -> Result<Option<Self>>;
57}
58
59impl StreamingPuzzleInfoExt for StreamingPuzzleInfo {
60    fn amount_to_be_paid(&self, my_coin_amount: u64, payment_time: u64) -> Result<u64> {
61        // LAST_PAYMENT_TIME + (to_pay * (END_TIME - LAST_PAYMENT_TIME) / my_amount) = payment_time
62        // to_pay = my_amount * (payment_time - LAST_PAYMENT_TIME) / (END_TIME - LAST_PAYMENT_TIME)
63        Ok(my_coin_amount * (payment_time - self.last_payment_time)
64            / (self.end_time - self.last_payment_time))
65    }
66
67    fn get_hint(recipient: Bytes32) -> Result<Bytes32> {
68        Ok(Self::get_hint(recipient))
69    }
70
71    fn get_launch_hints(&self) -> Result<Vec<Bytes>> {
72        Ok(self.get_launch_hints())
73    }
74
75    fn inner_puzzle_hash(&self) -> Result<Bytes32> {
76        Ok(self.inner_puzzle_hash().into())
77    }
78
79    fn from_memos(memos: Vec<Bytes>) -> Result<Option<Self>> {
80        Ok(Self::from_memos(&memos)?)
81    }
82}
83
84#[derive(Clone)]
85pub struct StreamedAssetParsingResult {
86    pub streamed_asset: Option<StreamedAsset>,
87    pub last_spend_was_clawback: bool,
88    pub last_payment_amount_if_clawback: u64,
89}