penumbra_sdk_auction/component/action_handler/dutch/
withdraw.rs

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
use crate::auction::dutch::ActionDutchAuctionWithdraw;
use crate::component::AuctionStoreRead;
use crate::component::DutchAuctionManager;
use anyhow::{bail, ensure, Context, Result};
use ark_ff::Zero;
use async_trait::async_trait;
use cnidarium::StateWrite;
use cnidarium_component::ActionHandler;
use decaf377::Fr;

#[async_trait]
impl ActionHandler for ActionDutchAuctionWithdraw {
    type CheckStatelessContext = ();
    async fn check_stateless(&self, _context: ()) -> Result<()> {
        ensure!(
            self.seq >= 1,
            "the sequence number MUST be greater or equal to 1 (got: {})",
            self.seq
        );

        ensure!(
            self.seq < u64::MAX,
            "the sequence number maximum is `u64::MAX`"
        );

        Ok(())
    }

    async fn check_and_execute<S: StateWrite>(&self, mut state: S) -> Result<()> {
        let auction_id = self.auction_id;

        // Check that the auction exists and is a Dutch auction.
        let auction_state = state
            .get_dutch_auction_by_id(auction_id)
            .await
            .context("the auction associated with this id is not a dutch auction")?;

        let Some(auction_state) = auction_state else {
            bail!("no auction found for id {auction_id}")
        };

        // Check that sequence number is incremented by one.
        ensure!(
            self.seq == auction_state.state.sequence.saturating_add(1),
            "the action sequence number MUST be incremented by one (previous: {}, action: {})",
            self.seq,
            auction_state.state.sequence
        );

        // Execute the withdrawal, zero-ing out the auction state
        // and increasing its sequence number.
        let withdrawn_balance = state.withdraw_auction(auction_state).await?;

        // Check that the reported balance commitment, match the recorded reserves.
        let expected_reserve_commitment = withdrawn_balance.commit(Fr::zero());

        ensure!(
            self.reserves_commitment == expected_reserve_commitment,
            "the reported reserve commitment is incorrect"
        );

        Ok(())
    }
}