Skip to content

Commit 2891f4e

Browse files
authored
Merge pull request #53 from malik672/chore(doc)
chore(doc): Update comments in code and improve clarity
2 parents 61743f6 + da21c9f commit 2891f4e

15 files changed

+95
-60
lines changed

Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
[package]
22
name = "uniswap-sdk-core"
3-
version = "0.17.1"
3+
version = "0.18.0"
44
edition = "2021"
55
authors = ["malik <aremumalik05@gmail.com>", "Shuhui Luo <twitter.com/aureliano_law>"]
66
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
77
license = "MIT"
88

9-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10-
119
[dependencies]
1210
alloy-primitives = "0.6"
1311
bigdecimal = "0.4.2"

rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ imports_granularity = "Crate"
33
max_width = 100
44
unstable_features = true
55
use_field_init_shorthand = true
6+
wrap_comments = true
7+
comment_width = 100

src/addresses.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ pub struct ChainAddresses {
1818

1919
pub const DEFAULT_NETWORKS: [ChainId; 3] = [ChainId::MAINNET, ChainId::GOERLI, ChainId::SEPOLIA];
2020

21-
/// returns an hashmap of key pair input of chainid to address
21+
/// returns a hashmap of key pair input of chainid to address
2222
///
2323
/// # Arguments
2424
///
25-
/// * Address
26-
/// * additional networks a vector of chainid
25+
/// * `address`: Address
26+
/// * `additional networks`: a vector of chain ids
2727
///
2828
///
29-
/// returns: AdresssMap
29+
/// returns: [`AdresssMap`]
3030
pub fn construct_same_address_map(address: Address, additional_networks: &[ChainId]) -> AddressMap {
3131
let mut networks = DEFAULT_NETWORKS.to_vec();
3232
networks.extend_from_slice(additional_networks);
@@ -79,7 +79,8 @@ lazy_static! {
7979
}
8080

8181
impl Default for ChainAddresses {
82-
/// Networks that share most of the same addresses i.e. Mainnet, Goerli, Optimism, Arbitrum, Polygon
82+
/// Networks that share most of the same addresses i.e. Mainnet, Goerli, Optimism, Arbitrum,
83+
/// Polygon
8384
fn default() -> Self {
8485
Self {
8586
v3_core_factory_address: address!("1F98431c8aD98523631AE4a59f267346ea31F984"),

src/entities/base_currency.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub struct CurrencyLike<M> {
1111
pub meta: M,
1212
}
1313

14-
/// A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
14+
/// A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other
15+
/// chain-native currencies
1516
pub trait BaseCurrency: Clone {
1617
/// The chain ID on which this currency resides
1718
fn chain_id(&self) -> ChainId;
@@ -26,7 +27,6 @@ pub trait BaseCurrency: Clone {
2627
fn name(&self) -> Option<String>;
2728
}
2829

29-
// Implementation of methods for CurrencyLike
3030
impl<M: Clone> BaseCurrency for CurrencyLike<M> {
3131
fn chain_id(&self) -> ChainId {
3232
self.chain_id
@@ -45,6 +45,7 @@ impl<M: Clone> BaseCurrency for CurrencyLike<M> {
4545
}
4646
}
4747

48+
/// Implement [`Deref`] to allow direct access to the metadata of the currency
4849
impl<M> Deref for CurrencyLike<M> {
4950
type Target = M;
5051

src/entities/ether.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ lazy_static! {
66
}
77

88
/// Ether is the main usage of a 'native' currency, i.e., for Ethereum mainnet and all testnets.
9-
/// Represents the native currency with additional metadata.
9+
/// Represents the native currency of the blockchain.
1010
pub type Ether = CurrencyLike<()>;
1111

12-
/// Implementation of the `CurrencyTrait` for the `Ether` type.
1312
impl CurrencyTrait for Ether {
1413
/// Checks if the currency is native to the blockchain.
1514
fn is_native(&self) -> bool {
@@ -38,9 +37,8 @@ impl CurrencyTrait for Ether {
3837
}
3938
}
4039

41-
/// Implementation of additional methods for the `Ether` type.
4240
impl Ether {
43-
/// Creates a new instance of `Ether` with the specified chain ID.
41+
/// Creates a new instance of [`Ether`] with the specified chain ID.
4442
pub fn new(chain_id: u64) -> Self {
4543
Self {
4644
chain_id,
@@ -51,7 +49,7 @@ impl Ether {
5149
}
5250
}
5351

54-
/// Retrieves or creates an `Ether` instance for the specified chain ID.
52+
/// Retrieves or creates an [`Ether`] instance for the specified chain ID.
5553
pub fn on_chain(chain_id: u64) -> Self {
5654
let mut cache = ETHER_CACHE.lock().unwrap();
5755
match cache.get(&chain_id) {

src/entities/fractions/currency_amount.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/// External crate dependencies
22
use crate::prelude::*;
33

4-
/// Represents metadata about a currency.
5-
///
6-
/// This struct holds information about a currency, including its symbol and the number of decimals.
4+
/// Currency amount struct that represents a rational amount of a currency
75
pub type CurrencyAmount<T> = FractionLike<CurrencyMeta<T>>;
86

97
/// Struct representing metadata about a currency
@@ -13,7 +11,6 @@ pub struct CurrencyMeta<T: CurrencyTrait> {
1311
pub decimal_scale: BigUint,
1412
}
1513

16-
/// Implementation of methods for CurrencyAmount
1714
impl<T: CurrencyTrait> CurrencyAmount<T> {
1815
/// Constructor method for creating a new currency amount
1916
fn new(
@@ -43,7 +40,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
4340
Self::new(currency, raw_amount, 1)
4441
}
4542

46-
/// Construct a currency amount with a denominator that is not equal to 1
43+
/// Construct a currency amount with a denominator that is not equal to 0
4744
pub fn from_fractional_amount(
4845
currency: T,
4946
numerator: impl Into<BigInt>,
@@ -126,10 +123,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
126123
.to_fixed(decimal_places, rounding),
127124
)
128125
}
129-
}
130126

131-
/// Implementation for a specific type of CurrencyAmount (Token)
132-
impl<T: CurrencyTrait> CurrencyAmount<T> {
133127
/// Wrap the currency amount if the currency is not native
134128
pub fn wrapped(&self) -> Result<CurrencyAmount<Token>, Error> {
135129
CurrencyAmount::from_fractional_amount(

src/entities/fractions/fraction.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl<M: Default> Default for FractionLike<M> {
2020
}
2121
}
2222

23+
/// Implement [`Deref`] to allow direct access to the metadata of the fraction
2324
impl<M> Deref for FractionLike<M> {
2425
type Target = M;
2526

@@ -38,7 +39,7 @@ impl Fraction {
3839
}
3940
}
4041

41-
/// Function to convert the custom Rounding enum to `bigdecimal`'s `RoundingMode`
42+
/// Function to convert the custom Rounding enum to [`bigdecimal`]'s [`RoundingMode`]
4243
const fn to_rounding_strategy(rounding: Rounding) -> RoundingMode {
4344
match rounding {
4445
Rounding::RoundDown => RoundingMode::Down,
@@ -92,12 +93,13 @@ pub trait FractionBase<M>: Sized {
9293
Self::new(self.denominator(), self.numerator(), self.meta())
9394
}
9495

95-
/// Converts the fraction to a `bigdecimal::BigDecimal`
96+
/// Converts the fraction to a [`BigDecimal`]
9697
fn to_decimal(&self) -> BigDecimal {
9798
BigDecimal::from(self.numerator()).div(BigDecimal::from(self.denominator()))
9899
}
99100

100-
/// Converts the fraction to a string with a specified number of significant digits and rounding strategy
101+
/// Converts the fraction to a string with a specified number of significant digits and rounding
102+
/// strategy
101103
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result<String, Error> {
102104
if significant_digits == 0 {
103105
return Err(Error::Incorrect());
@@ -111,7 +113,8 @@ pub trait FractionBase<M>: Sized {
111113
Ok(quotient.normalized().to_string())
112114
}
113115

114-
/// Converts the fraction to a string with a fixed number of decimal places and rounding strategy
116+
/// Converts the fraction to a string with a fixed number of decimal places and rounding
117+
/// strategy
115118
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
116119
let rounding_strategy = to_rounding_strategy(rounding);
117120
let quotient = self
@@ -121,17 +124,16 @@ pub trait FractionBase<M>: Sized {
121124
format!("{:.1$}", quotient, decimal_places as usize)
122125
}
123126

124-
/// Helper method for converting any superclass back to a simple Fraction
127+
/// Helper method for converting any superclass back to a simple [`Fraction`]
125128
fn as_fraction(&self) -> Fraction {
126129
Fraction::new(self.numerator(), self.denominator())
127130
}
128131
}
129132

130-
/// Implementation of the FractionTrait for FractionLike
131133
impl<M: Clone + PartialEq> FractionTrait<M> for FractionLike<M> {}
132134

133135
impl<M: Clone> FractionBase<M> for FractionLike<M> {
134-
/// Constructor for creating a new Fraction with metadata
136+
/// Constructor for creating a new [`FractionLike`] with metadata
135137
fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>, meta: M) -> Self {
136138
let denominator = denominator.into();
137139
// Ensure the denominator is not zero
@@ -239,7 +241,6 @@ impl<M: Clone> Mul for FractionLike<M> {
239241

240242
/// Multiplies the current fraction by another fraction
241243
fn mul(self, other: Self) -> Self::Output {
242-
//There's little to no possibility of an error, so unwrap can be used
243244
FractionBase::new(
244245
self.numerator() * other.numerator(),
245246
self.denominator() * other.denominator(),

src/entities/fractions/percent.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,33 @@ lazy_static! {
1010
#[derive(Clone, Debug, Default, PartialEq)]
1111
pub struct IsPercent;
1212

13-
/// Type alias for a Percent, a Fraction with the IsPercent metadata
13+
/// Type alias for a Percent, a [`FractionLike`] with the [`IsPercent`] metadata
1414
pub type Percent = FractionLike<IsPercent>;
1515

1616
impl Percent {
17-
/// Constructor for creating a new Percent instance
17+
/// Constructor for creating a new [`Percent`] instance
1818
pub fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>) -> Self {
1919
FractionBase::new(numerator, denominator, IsPercent)
2020
}
2121

22-
/// Converts the Percent to a string with a specified number of significant digits and rounding strategy
22+
/// Converts the [`Percent`] to a string with a specified number of significant digits and
23+
/// rounding strategy
2324
pub fn to_significant(
2425
&self,
2526
significant_digits: u8,
2627
rounding: Rounding,
2728
) -> Result<String, Error> {
28-
// Convert the Percent to a simple Fraction, multiply by 100, and then call to_significant on the result
29+
// Convert the Percent to a simple Fraction, multiply by 100, and then call to_significant
30+
// on the result
2931
(self.as_fraction() * ONE_HUNDRED.as_fraction())
3032
.to_significant(significant_digits, rounding)
3133
}
3234

33-
/// Converts the Percent to a string with a fixed number of decimal places and rounding strategy
35+
/// Converts the [`Percent`] to a string with a fixed number of decimal places and rounding
36+
/// strategy
3437
pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
35-
// Convert the Percent to a simple Fraction, multiply by 100, and then call to_fixed on the result
38+
// Convert the Percent to a simple Fraction, multiply by 100, and then call to_fixed on the
39+
// result
3640
(self.as_fraction() * ONE_HUNDRED.as_fraction()).to_fixed(decimal_places, rounding)
3741
}
3842
}

src/entities/fractions/price.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// External crate dependencies
22
use crate::prelude::*;
33

4-
/// Type alias for a Price, a Fraction with metadata PriceMeta
4+
/// Type alias for a Price, a [`FractionLike`] with metadata [`PriceMeta`]
55
pub type Price<TBase, TQuote> = FractionLike<PriceMeta<TBase, TQuote>>;
66

7-
/// Struct representing metadata for a Price
7+
/// Struct representing metadata for a [`Price`]
88
#[derive(Clone, Debug, PartialEq)]
99
pub struct PriceMeta<TBase, TQuote>
1010
where
@@ -21,7 +21,7 @@ where
2121
TBase: CurrencyTrait,
2222
TQuote: CurrencyTrait,
2323
{
24-
/// Constructor for creating a new Price instance
24+
/// Constructor for creating a new [`Price`] instance
2525
pub fn new(
2626
base_currency: TBase,
2727
quote_currency: TQuote,
@@ -44,7 +44,7 @@ where
4444
)
4545
}
4646

47-
/// Create a Price instance from currency amounts of the base and quote currencies
47+
/// Create a [`Price`] instance from currency amounts of the base and quote currencies
4848
pub fn from_currency_amounts(
4949
base_amount: CurrencyAmount<TBase>,
5050
quote_amount: CurrencyAmount<TQuote>,
@@ -109,7 +109,8 @@ where
109109
self.as_fraction() * self.scalar.clone()
110110
}
111111

112-
/// Converts the adjusted price to a string with a specified number of significant digits and rounding strategy
112+
/// Converts the adjusted price to a string with a specified number of significant digits and
113+
/// rounding strategy
113114
pub fn to_significant(
114115
&self,
115116
significant_digits: u8,
@@ -119,7 +120,8 @@ where
119120
.to_significant(significant_digits, rounding)
120121
}
121122

122-
/// Converts the adjusted price to a string with a fixed number of decimal places and rounding strategy
123+
/// Converts the adjusted price to a string with a fixed number of decimal places and rounding
124+
/// strategy
123125
pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
124126
self.adjusted_for_decimals()
125127
.to_fixed(decimal_places, rounding)

src/entities/token.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,34 @@ impl Token {
8383
}
8484
}
8585

86-
/// Shorthand macro to create a token
86+
/// Shorthand macro to create a [`Token`] with the given chain id, address, decimals, optional
87+
/// symbol and name.
88+
///
89+
/// # Arguments
90+
///
91+
/// * `chain_id`: The chain id
92+
/// * `address`: The address of the token as a string, [`Address`] or a string literal without "0x"
93+
/// * `decimals`: The decimals of the token
94+
/// * `symbol`: The symbol of the token, optional
95+
/// * `name`: The name of the token, optional
96+
///
97+
/// returns: [`Token`]
98+
///
99+
/// # Example
100+
///
101+
/// ```
102+
/// use uniswap_sdk_core::{prelude::*, token};
103+
///
104+
/// const DAI_MAINNET: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
105+
/// let dai: Token = token!(1, DAI_MAINNET, 18, "DAI", "Dai Stablecoin");
106+
/// let dai: Token = token!(
107+
/// 1,
108+
/// "6B175474E89094C44Da98b954EedeAC495271d0F",
109+
/// 18,
110+
/// "DAI",
111+
/// "Dai Stablecoin"
112+
/// );
113+
/// ```
87114
#[macro_export]
88115
macro_rules! token {
89116
($chain_id:expr, $address:literal, $decimals:expr) => {
@@ -156,7 +183,8 @@ macro_rules! token {
156183

157184
#[cfg(test)]
158185
mod tests {
159-
///should test for neg chain_id or neg decimals or neg buy_fee or neg sell_fee, but the compiler will panic by itself, so no need
186+
///should test for neg chain_id or neg decimals or neg buy_fee or neg sell_fee, but the
187+
/// compiler will panic by itself, so no need
160188
use super::*;
161189

162190
const ADDRESS_ONE: &str = "0x0000000000000000000000000000000000000001";

src/entities/weth9.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
use crate::{prelude::*, token};
22

3-
/// `WETH9` represents the WETH9 contract and provides information about WETH tokens on different Ethereum chains.
4-
3+
/// Represents the WETH9 contract and provides information about WETH tokens on different Ethereum
4+
/// chains.
55
#[derive(Clone, PartialEq, Debug)]
66
pub struct WETH9 {
77
/// A mapping of chain IDs to corresponding WETH tokens.
88
tokens: HashMap<u64, Token>,
99
}
1010

11-
/// Default implementation for `WETH9`, creating an instance with predefined WETH tokens on various chains.
11+
/// Default implementation for [`WETH9`], creating an instance with predefined WETH tokens on
12+
/// various chains.
1213
impl Default for WETH9 {
1314
fn default() -> Self {
1415
Self::new()
1516
}
1617
}
1718

18-
/// Implementation for methods specific to the `WETH9` struct.
1919
impl WETH9 {
2020
pub fn new() -> Self {
2121
let mut tokens = HashMap::new();

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! # uniswap-sdk-core
22
//!
3-
//! The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange.
3+
//! The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap
4+
//! decentralized exchange.
45
56
pub mod addresses;
67
pub mod chains;

0 commit comments

Comments
 (0)