iota_bridge_indexer/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::fmt::{Display, Formatter};
6
7use iota_types::base_types::{IotaAddress, TransactionDigest};
8
9use crate::models::{
10    IotaErrorTransactions, TokenTransfer as DBTokenTransfer,
11    TokenTransferData as DBTokenTransferData,
12};
13
14pub mod config;
15pub mod iota_transaction_handler;
16pub mod iota_transaction_queries;
17pub mod metrics;
18pub mod models;
19pub mod postgres_manager;
20pub mod schema;
21pub mod types;
22
23pub mod eth_bridge_indexer;
24pub mod iota_bridge_indexer;
25
26#[derive(Clone)]
27pub enum ProcessedTxnData {
28    TokenTransfer(TokenTransfer),
29    Error(IotaTxnError),
30}
31
32#[derive(Clone)]
33pub struct IotaTxnError {
34    tx_digest: TransactionDigest,
35    sender: IotaAddress,
36    timestamp_ms: u64,
37    failure_status: String,
38    cmd_idx: Option<u64>,
39}
40
41#[derive(Clone)]
42pub struct TokenTransfer {
43    chain_id: u8,
44    nonce: u64,
45    block_height: u64,
46    timestamp_ms: u64,
47    txn_hash: Vec<u8>,
48    txn_sender: Vec<u8>,
49    status: TokenTransferStatus,
50    gas_usage: i64,
51    data_source: BridgeDataSource,
52    data: Option<TokenTransferData>,
53}
54
55#[derive(Clone)]
56pub struct TokenTransferData {
57    sender_address: Vec<u8>,
58    destination_chain: u8,
59    recipient_address: Vec<u8>,
60    token_id: u8,
61    amount: u64,
62}
63
64impl TokenTransfer {
65    fn to_db(&self) -> DBTokenTransfer {
66        DBTokenTransfer {
67            chain_id: self.chain_id as i32,
68            nonce: self.nonce as i64,
69            block_height: self.block_height as i64,
70            timestamp_ms: self.timestamp_ms as i64,
71            txn_hash: self.txn_hash.clone(),
72            txn_sender: self.txn_sender.clone(),
73            status: self.status.to_string(),
74            gas_usage: self.gas_usage,
75            data_source: self.data_source.to_string(),
76        }
77    }
78
79    fn to_data_maybe(&self) -> Option<DBTokenTransferData> {
80        self.data.as_ref().map(|data| DBTokenTransferData {
81            chain_id: self.chain_id as i32,
82            nonce: self.nonce as i64,
83            block_height: self.block_height as i64,
84            timestamp_ms: self.timestamp_ms as i64,
85            txn_hash: self.txn_hash.clone(),
86            sender_address: data.sender_address.clone(),
87            destination_chain: data.destination_chain as i32,
88            recipient_address: data.recipient_address.clone(),
89            token_id: data.token_id as i32,
90            amount: data.amount as i64,
91        })
92    }
93}
94
95impl IotaTxnError {
96    fn to_db(&self) -> IotaErrorTransactions {
97        IotaErrorTransactions {
98            txn_digest: self.tx_digest.inner().to_vec(),
99            sender_address: self.sender.to_vec(),
100            timestamp_ms: self.timestamp_ms as i64,
101            failure_status: self.failure_status.clone(),
102            cmd_idx: self.cmd_idx.map(|idx| idx as i64),
103        }
104    }
105}
106
107#[derive(Clone)]
108pub(crate) enum TokenTransferStatus {
109    Deposited,
110    Approved,
111    Claimed,
112}
113
114impl Display for TokenTransferStatus {
115    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116        let str = match self {
117            TokenTransferStatus::Deposited => "Deposited",
118            TokenTransferStatus::Approved => "Approved",
119            TokenTransferStatus::Claimed => "Claimed",
120        };
121        write!(f, "{str}")
122    }
123}
124
125#[derive(Clone)]
126enum BridgeDataSource {
127    Iota,
128    Eth,
129}
130
131impl Display for BridgeDataSource {
132    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
133        let str = match self {
134            BridgeDataSource::Eth => "ETH",
135            BridgeDataSource::Iota => "IOTA",
136        };
137        write!(f, "{str}")
138    }
139}