Skip to content

Commit

Permalink
Make chain model public
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Aug 3, 2023
1 parent a48a11a commit d31019d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 26 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ default-members = [

[workspace.dependencies]
typeshare = "1.0.1"
serde = { version = "1.0.180", features = ["derive"] }
serde_json = { version = "1.0.104" }
4 changes: 3 additions & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "primitives"
edition = { workspace = true }
version = { workspace = true }

[dependencies]
typeshare = { workspace = true }
typeshare = { workspace = true }
serde = { workspace = true }
87 changes: 74 additions & 13 deletions primitives/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,77 @@
use serde::{Serializer, Serialize};
use typeshare::typeshare;

#[derive(Copy, Clone, Debug)]
#[typeshare(swift = "Equatable, Codable, CaseIterable")]
pub enum Chain {
bitcoin,
ethereum,
binance,
smartchain,
solana,
polygon,
thorchain,
cosmos,
osmosis,
arbitrum,
ton,
tron,
doge,
Bitcoin,
Ethereum,
Binance,
SmartChain,
Solana,
Polygon,
Thorchain,
Cosmos,
Osmosis,
Arbitrum,
Ton,
Tron,
Doge,
Optimism,
}

impl PartialEq for Chain {
fn eq(&self, other: &Self) -> bool {
return self.as_str() == other.as_str()
}
}

impl Serialize for Chain {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}

impl Chain {
pub fn new(chain: &str) -> Option<Self> {
match chain {
"bitcoin" => Some(Self::Bitcoin),
"binance" => Some(Self::Binance),
"ethereum" => Some(Self::Ethereum),
"smartchain" => Some(Self::SmartChain),
"polygon" => Some(Self::Polygon),
"solana" => Some(Self::Solana),
"arbitrum" => Some(Self::Arbitrum),
"optimism" => Some(Self::Optimism),
"thorchain" => Some(Self::Thorchain),
"cosmos" => Some(Self::Cosmos),
"osmosis" => Some(Self::Osmosis),
"ton" => Some(Self::Ton),
"tron" => Some(Self::Tron),
"doge" => Some(Self::Doge),
_ => None,
}
}

pub fn as_str(&self) -> &'static str {
match self {
Self::Binance => "binance",
Self::Bitcoin => "bitcoin",
Self::Ethereum => "ethereum",
Self::SmartChain => "smartchain",
Self::Polygon => "polygon",
Self::Solana => "solana",
Self::Arbitrum => "arbitrum",
Self::Optimism => "optimism",
Self::Thorchain => "thorchain",
Self::Cosmos => "cosmos",
Self::Osmosis => "osmosis",
Self::Ton => "ton",
Self::Tron => "tron",
Self::Doge => "doge",
}
}
}
4 changes: 3 additions & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// lib.rs
// lib.rs

pub mod chain;

0 comments on commit d31019d

Please sign in to comment.