Skip to main content

chio_settle/
payments_approval.rs

1use chio_core::web3::settlement::Web3SettlementDispatchArtifact;
2
3use super::{ApprovalBinding, SettlementError};
4
5impl ApprovalBinding {
6    /// Assert that a settlement dispatch matches the approval-bound spend.
7    pub fn assert_dispatch(
8        &self,
9        lane: &str,
10        dispatch: &Web3SettlementDispatchArtifact,
11    ) -> Result<(), SettlementError> {
12        let expected_chain = format!("eip155:{}", self.chain_id);
13        if dispatch.chain_id != expected_chain {
14            return Err(SettlementError::InvalidBinding(format!(
15                "{lane} chain mismatch: dispatch chain {:?} is not the approval-bound chain {expected_chain:?}",
16                dispatch.chain_id
17            )));
18        }
19        if !dispatch
20            .beneficiary_address
21            .trim()
22            .eq_ignore_ascii_case(self.payee_address.trim())
23        {
24            return Err(SettlementError::InvalidBinding(format!(
25                "{lane} payee mismatch: dispatch payee {:?} is not the approval-bound payee {:?}",
26                dispatch.beneficiary_address, self.payee_address
27            )));
28        }
29        if u128::from(dispatch.settlement_amount.units) != self.amount_minor_units {
30            return Err(SettlementError::InvalidBinding(format!(
31                "{lane} amount mismatch: dispatch amount {} is not the approval-bound amount {}",
32                dispatch.settlement_amount.units, self.amount_minor_units
33            )));
34        }
35        self.assert_token_symbol(lane, &dispatch.settlement_amount.currency)
36    }
37
38    /// Assert that a lane's token symbol matches the approval-bound token.
39    pub fn assert_token_symbol(
40        &self,
41        lane: &str,
42        lane_token_symbol: &str,
43    ) -> Result<(), SettlementError> {
44        if !lane_token_symbol
45            .trim()
46            .eq_ignore_ascii_case(self.token_symbol.trim())
47        {
48            return Err(SettlementError::InvalidBinding(format!(
49                "{lane} token mismatch: lane token {lane_token_symbol:?} is not the \
50                 approval-bound token {:?}",
51                self.token_symbol
52            )));
53        }
54        Ok(())
55    }
56}