Skip to main content

irys_sdk/token/
mod.rs

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    /// Gets the base unit name, such as "winston" for Arweave
64    fn get_min_unit_name(&self) -> String;
65
66    /// Gets token type
67    fn get_type(&self) -> TokenType;
68
69    /// Returns if the token needs fee for transacting
70    fn needs_fee(&self) -> bool;
71
72    /// Gets transaction based on transaction id
73    async fn get_tx(&self, tx_id: String) -> Result<Tx, BundlerError>;
74
75    /// Gets the transaction status, including height, included block's hash and height
76    async fn get_tx_status(
77        &self,
78        tx_id: String,
79    ) -> Result<(StatusCode, Option<TxStatus>), BundlerError>;
80
81    /// Gets public key
82    fn get_pub_key(&self) -> Result<Bytes, BundlerError>;
83
84    /// Gets wallet address, usually a hash from public key
85    fn wallet_address(&self) -> Result<String, BundlerError>;
86
87    /// Signs a given message
88    fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, BundlerError>;
89
90    /// Verifies if public key, message and signature matches
91    fn verify(&self, pub_key: &[u8], message: &[u8], signature: &[u8]) -> Result<(), BundlerError>;
92
93    /// Gets signer for more specific operations
94    fn get_signer(&self) -> Result<&dyn Signer, BundlerError>;
95
96    /// Gets token Id
97    async fn get_id(&self, item: ()) -> String;
98
99    /// Get price of token in USD
100    async fn price(&self) -> String;
101
102    /// Get given token network's block height
103    async fn get_current_height(&self) -> u128;
104
105    /// Get fee for transaction
106    async fn get_fee(&self, amount: u64, to: &str, multiplier: f64) -> Result<u64, BundlerError>;
107
108    /// Creates a new transaction
109    async fn create_tx(&self, amount: u64, to: &str, fee: u64) -> Result<Tx, BundlerError>;
110
111    /// Send a signed transaction
112    async fn send_tx(&self, data: Tx) -> Result<TxResponse, BundlerError>;
113}