-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from shuhuiluo/oop-the-rust-way
impl, refactor and test fractions and currencies
- Loading branch information
Showing
14 changed files
with
1,144 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Rust | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
name: Rust Tests | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
|
||
- name: Cache Cargo registry | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cargo/registry | ||
~/.cargo/git | ||
target | ||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-cargo-registry- | ||
- name: Build | ||
run: cargo build --verbose | ||
|
||
- name: Run tests | ||
run: cargo test --verbose |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
use super::{currency::CurrencyTrait, token::Token}; | ||
|
||
/// A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies | ||
#[derive(Clone, PartialEq)] | ||
pub struct BaseCurrency { | ||
pub chain_id: u32, | ||
pub decimals: u32, | ||
pub name: Option<String>, | ||
pub symbol: Option<String>, | ||
pub is_native: bool, | ||
} | ||
pub trait BaseCurrency: Clone { | ||
/// The chain ID on which this currency resides | ||
fn chain_id(&self) -> u32; | ||
|
||
/// The decimals used in representing currency amounts | ||
fn decimals(&self) -> u32; | ||
|
||
/// The symbol of the currency, i.e. a short textual non-unique identifier | ||
fn symbol(&self) -> Option<String>; | ||
|
||
impl BaseCurrency { | ||
pub fn new(chain_id: u32, decimals: u32, name: Option<String>, symbol: Option<String>) -> Self { | ||
assert!(chain_id > 0, "CHAIN_ID"); | ||
assert!(decimals < 255, "DECIMALS"); | ||
/// The name of the currency, i.e. a descriptive textual non-unique identifier | ||
fn name(&self) -> Option<String>; | ||
|
||
Self { | ||
chain_id, | ||
decimals, | ||
name, | ||
symbol, | ||
is_native: Self::is_native(), | ||
} | ||
} | ||
/// Returns whether this currency is functionally equivalent to the other currency | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `other`: the other currency | ||
/// | ||
fn equals(&self, other: &impl CurrencyTrait) -> bool; | ||
|
||
/// Returns whether the currency is native to the chain and must be wrapped (e.g. Ether) | ||
pub fn is_native() -> bool { | ||
true | ||
} | ||
/// Return the wrapped version of this currency that can be used with the Uniswap contracts. | ||
/// Currencies must implement this to be used in Uniswap | ||
fn wrapped(&self) -> Token; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,74 @@ | ||
use super::{native_currency::NativeCurrency, token::Token}; | ||
use super::{base_currency::BaseCurrency, ether::Ether, token::Token}; | ||
|
||
#[derive(Clone, PartialEq)] | ||
pub enum Currency { | ||
NativeCurrency(NativeCurrency), | ||
NativeCurrency(Ether), | ||
Token(Token), | ||
} | ||
|
||
pub trait CurrencyTrait: BaseCurrency { | ||
/// Returns whether the currency is native to the chain and must be wrapped (e.g. Ether) | ||
fn is_native(&self) -> bool; | ||
|
||
fn address(&self) -> String; | ||
} | ||
|
||
impl CurrencyTrait for Currency { | ||
fn is_native(&self) -> bool { | ||
match self { | ||
Currency::NativeCurrency(_) => true, | ||
Currency::Token(_) => false, | ||
} | ||
} | ||
|
||
fn address(&self) -> String { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.address(), | ||
Currency::Token(token) => token.address(), | ||
} | ||
} | ||
} | ||
|
||
impl BaseCurrency for Currency { | ||
fn chain_id(&self) -> u32 { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.chain_id(), | ||
Currency::Token(token) => token.chain_id(), | ||
} | ||
} | ||
|
||
fn decimals(&self) -> u32 { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.decimals(), | ||
Currency::Token(token) => token.decimals(), | ||
} | ||
} | ||
|
||
fn symbol(&self) -> Option<String> { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.symbol(), | ||
Currency::Token(token) => token.symbol(), | ||
} | ||
} | ||
|
||
fn name(&self) -> Option<String> { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.name(), | ||
Currency::Token(token) => token.name(), | ||
} | ||
} | ||
|
||
fn equals(&self, other: &impl CurrencyTrait) -> bool { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.equals(other), | ||
Currency::Token(token) => token.equals(other), | ||
} | ||
} | ||
|
||
fn wrapped(&self) -> Token { | ||
match self { | ||
Currency::NativeCurrency(native_currency) => native_currency.wrapped(), | ||
Currency::Token(token) => token.clone(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.