chia_sdk_driver/actions/
fee.rs1use crate::{Deltas, DriverError, Id, SpendAction, SpendContext, Spends};
2
3#[derive(Debug, Clone, Copy)]
4pub struct FeeAction {
5 pub amount: u64,
6}
7
8impl FeeAction {
9 pub fn new(amount: u64) -> Self {
10 Self { amount }
11 }
12}
13
14impl SpendAction for FeeAction {
15 fn calculate_delta(&self, deltas: &mut Deltas, _index: usize) {
16 deltas.update(Id::Xch).output += self.amount;
17 }
18
19 fn spend(
20 &self,
21 _ctx: &mut SpendContext,
22 spends: &mut Spends,
23 _index: usize,
24 ) -> Result<(), DriverError> {
25 spends.outputs.fee += self.amount;
26
27 Ok(())
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use anyhow::Result;
34 use chia_puzzle_types::Memos;
35 use chia_sdk_test::Simulator;
36 use indexmap::indexmap;
37
38 use crate::{Action, Relation};
39
40 use super::*;
41
42 #[test]
43 fn test_action_send_with_fee() -> Result<()> {
44 let mut sim = Simulator::new();
45 let mut ctx = SpendContext::new();
46
47 let alice = sim.bls(2);
48
49 let mut spends = Spends::new(alice.puzzle_hash);
50 spends.add(alice.coin);
51
52 let deltas = spends.apply(
53 &mut ctx,
54 &[
55 Action::send(Id::Xch, alice.puzzle_hash, 1, Memos::None),
56 Action::fee(1),
57 ],
58 )?;
59
60 let outputs = spends.finish_with_keys(
61 &mut ctx,
62 &deltas,
63 Relation::None,
64 &indexmap! { alice.puzzle_hash => alice.pk },
65 )?;
66
67 sim.spend_coins(ctx.take(), &[alice.sk])?;
68
69 let coin = outputs.xch[0];
70 assert_eq!(outputs.xch.len(), 1);
71 assert_ne!(sim.coin_state(coin.coin_id()), None);
72 assert_eq!(coin.amount, 1);
73
74 Ok(())
75 }
76}