1#[cfg(feature = "arweave")]
2pub mod arweave;
3#[cfg(feature = "solana")]
4pub mod solana;
5
6#[cfg(feature = "ethereum")]
7pub mod ethereum;
8
9use core::fmt;
10
11use bytes::Bytes;
12use num_derive::FromPrimitive;
13use reqwest::StatusCode;
14use serde::{Deserialize, Serialize};
15use std::str::FromStr;
16
17#[cfg(feature = "build-binary")]
18use clap::ValueEnum;
19
20use crate::{
21 error::BundlerError,
22 transaction::{Tx, TxStatus},
23 Signer,
24};
25
26#[derive(FromPrimitive, Debug, Copy, Clone, Hash, Serialize, Deserialize, PartialEq, Eq)]
27#[cfg_attr(feature = "build-binary", derive(ValueEnum))]
28pub enum TokenType {
29 Arweave = 1,
30 Solana = 2,
31 Ethereum = 3,
32 Erc20 = 4,
33 Cosmos = 5,
34}
35
36#[derive(Deserialize)]
37pub struct TxResponse {
38 pub tx_id: String,
39}
40
41impl fmt::Display for TokenType {
42 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
43 write!(formatter, "{}", format!("{:?}", self).to_lowercase())
44 }
45}
46
47impl FromStr for TokenType {
48 type Err = anyhow::Error;
49 fn from_str(input: &str) -> Result<Self, Self::Err> {
50 match input {
51 "arweave" => Ok(TokenType::Arweave),
52 "solana" => Ok(TokenType::Solana),
53 "ethereum" => Ok(TokenType::Ethereum),
54 "erc20" => Ok(TokenType::Erc20),
55 "cosmos" => Ok(TokenType::Cosmos),
56 _ => Err(anyhow::Error::msg("Invalid or unsupported token")),
57 }
58 }
59}
60
61#[async_trait::async_trait]
62pub trait Token {
63 fn get_min_unit_name(&self) -> String;
65
66 fn get_type(&self) -> TokenType;
68
69 fn needs_fee(&self) -> bool;
71
72 async fn get_tx(&self, tx_id: String) -> Result<Tx, BundlerError>;
74
75 async fn get_tx_status(
77 &self,
78 tx_id: String,
79 ) -> Result<(StatusCode, Option<TxStatus>), BundlerError>;
80
81 fn get_pub_key(&self) -> Result<Bytes, BundlerError>;
83
84 fn wallet_address(&self) -> Result<String, BundlerError>;
86
87 fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, BundlerError>;
89
90 fn verify(&self, pub_key: &[u8], message: &[u8], signature: &[u8]) -> Result<(), BundlerError>;
92
93 fn get_signer(&self) -> Result<&dyn Signer, BundlerError>;
95
96 async fn get_id(&self, item: ()) -> String;
98
99 async fn price(&self) -> String;
101
102 async fn get_current_height(&self) -> u128;
104
105 async fn get_fee(&self, amount: u64, to: &str, multiplier: f64) -> Result<u64, BundlerError>;
107
108 async fn create_tx(&self, amount: u64, to: &str, fee: u64) -> Result<Tx, BundlerError>;
110
111 async fn send_tx(&self, data: Tx) -> Result<TxResponse, BundlerError>;
113}