Skip to main content

withdrawals_contract/
lib.rs

1pub use crate::error::Error;
2use num_enum::{IntoPrimitive, TryFromPrimitive};
3use platform_value::{Identifier, IdentifierBytes32};
4use platform_version::version::PlatformVersion;
5use serde_json::Value;
6use serde_repr::{Deserialize_repr, Serialize_repr};
7use std::fmt;
8
9mod error;
10pub mod v1;
11pub mod v2;
12
13pub const ID_BYTES: [u8; 32] = [
14    54, 98, 187, 97, 225, 127, 174, 62, 162, 148, 207, 96, 49, 151, 251, 10, 171, 109, 81, 24, 11,
15    216, 182, 16, 76, 73, 68, 166, 47, 226, 217, 127,
16];
17
18pub const OWNER_ID_BYTES: [u8; 32] = [0; 32];
19
20pub const ID: Identifier = Identifier(IdentifierBytes32(ID_BYTES));
21pub const OWNER_ID: Identifier = Identifier(IdentifierBytes32(OWNER_ID_BYTES));
22
23// @append_only
24#[repr(u8)]
25#[derive(
26    Serialize_repr,
27    Deserialize_repr,
28    PartialEq,
29    Eq,
30    Clone,
31    Copy,
32    Debug,
33    TryFromPrimitive,
34    IntoPrimitive,
35)]
36pub enum WithdrawalStatus {
37    /// The documents are in the state and waiting to be processed.
38    QUEUED = 0,
39    /// Pooled happens when we are waiting for signing.
40    POOLED = 1,
41    /// We have broadcasted the transaction to core.
42    BROADCASTED = 2,
43    /// The transaction is now complete.
44    COMPLETE = 3,
45    /// We broadcasted the transaction but core never saw it or rejected it.
46    EXPIRED = 4,
47    /// Terminal: the signed asset unlock transaction can never be mined by Core (its payout is
48    /// below Core's dust threshold), so Platform stops re-signing it. The withdrawn credits
49    /// are NOT returned: a quorum signature for the payout was released, so Platform can never
50    /// prove the transaction will not mine. They stay locked in Core's credit pool.
51    FAILED = 5,
52}
53
54impl fmt::Display for WithdrawalStatus {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let status_str = match self {
57            WithdrawalStatus::QUEUED => "Queued",
58            WithdrawalStatus::POOLED => "Pooled",
59            WithdrawalStatus::BROADCASTED => "Broadcasted",
60            WithdrawalStatus::COMPLETE => "Complete",
61            WithdrawalStatus::EXPIRED => "Expired",
62            WithdrawalStatus::FAILED => "Failed",
63        };
64        write!(f, "{}", status_str)
65    }
66}
67
68pub fn load_definitions(platform_version: &PlatformVersion) -> Result<Option<Value>, Error> {
69    match platform_version.system_data_contracts.withdrawals {
70        1 | 2 => Ok(None),
71        version => Err(Error::UnknownVersionMismatch {
72            method: "withdrawals_contract::load_definitions".to_string(),
73            known_versions: vec![1, 2],
74            received: version,
75        }),
76    }
77}
78pub fn load_documents_schemas(platform_version: &PlatformVersion) -> Result<Value, Error> {
79    match platform_version.system_data_contracts.withdrawals {
80        1 => v1::load_documents_schemas(),
81        2 => v2::load_documents_schemas(),
82        version => Err(Error::UnknownVersionMismatch {
83            method: "withdrawals_contract::load_documents_schemas".to_string(),
84            known_versions: vec![1, 2],
85            received: version,
86        }),
87    }
88}