simperby_settlement/
lib.rs

1pub mod execution;
2pub mod tests;
3
4use eyre::Error;
5use merkle_tree::*;
6use rust_decimal::Decimal;
7use serde::{Deserialize, Serialize};
8use simperby_core::*;
9
10/// An abstract information about a block from a settlement chain.
11#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
12pub struct SettlementChainBlock {
13    /// The height of the block.
14    pub height: u64,
15    /// The UNIX timestamp of the block in seconds.
16    pub timestamp: u64,
17}
18
19/// An abstraction of a settlement chain with its treasury deployed on it.
20///
21/// One trivial implementation of this trait would carry the API endpoint of the full node and
22/// the relayer account used to submit message delivering transactions.
23#[async_trait::async_trait]
24pub trait SettlementChain: Send + Sync {
25    /// Returns the name of the chain.
26    async fn get_chain_name(&self) -> String;
27
28    /// Checks whether the chain is healthy and the full node is running.
29    async fn check_connection(&self) -> Result<(), Error>;
30
31    /// Gets the latest finalized block on the chain.
32    async fn get_last_block(&self) -> Result<SettlementChainBlock, Error>;
33
34    /// Returns the current sequence of the treasury contract.
35    async fn get_contract_sequence(&self) -> Result<u128, Error>;
36
37    /// Returns the address and the current balance (which is used to pay the gas fee) of the relayer account in this chain.
38    ///
39    /// The relayer account has no special authority; it is simply used to pay the gas fee for the transaction.
40    /// (i.e., there is no need for the contract to check the transaction submitter).
41    async fn get_relayer_account_info(&self) -> Result<(HexSerializedVec, Decimal), Error>;
42
43    /// Returns the latest header that the light client has verified.
44    async fn get_light_client_header(&self) -> Result<BlockHeader, Error>;
45
46    /// Returns the current balance of a particular fungible token in the treasury contract.
47    async fn get_treasury_fungible_token_balance(
48        &self,
49        address: HexSerializedVec,
50    ) -> Result<Decimal, Error>;
51
52    /// Returns the current balance of a particular non-fungible token collection in the treasury contract,
53    /// identified as their token indices.
54    async fn get_treasury_non_fungible_token_balance(
55        &self,
56        address: HexSerializedVec,
57    ) -> Result<Vec<HexSerializedVec>, Error>;
58
59    /// Updates the light client state in the treasury by providing the next, valid block header and its proof.
60    ///
61    /// This is one of the message delivery methods; a transaction that carries the given data will be submitted to the chain.
62    async fn update_treasury_light_client(
63        &self,
64        header: BlockHeader,
65        proof: FinalizationProof,
66    ) -> Result<(), Error>;
67
68    /// Delivers an execution transaction to the settlement chain with the commitment proof.
69    ///
70    /// - `transaction`: The transaction to deliver.
71    /// - `block_height`: The height of the block that the transaction is included in.
72    async fn execute(
73        &self,
74        transaction: Transaction,
75        block_height: u64,
76        proof: MerkleProof,
77    ) -> Result<(), Error>;
78
79    /// Returns the current sequence number of the given externally owned account.
80    async fn eoa_get_sequence(&self, address: HexSerializedVec) -> Result<u128, Error>;
81
82    /// Returns the current balance of a particular fungible token in the given externally owned account.
83    async fn eoa_get_fungible_token_balance(
84        &self,
85        address: HexSerializedVec,
86        token_address: HexSerializedVec,
87    ) -> Result<Decimal, Error>;
88
89    /// Submits a transaction to transfer a fungible token
90    /// from the given externally owned account to the given receiver address.
91    async fn eoa_transfer_fungible_token(
92        &self,
93        address: HexSerializedVec,
94        sender_private_key: HexSerializedVec,
95        token_address: HexSerializedVec,
96        receiver_address: HexSerializedVec,
97        amount: Decimal,
98    ) -> Result<(), Error>;
99}