pub struct TokenRegistry { /* private fields */ }Expand description
A registry mapping asset ticker symbols to their ERC-20 token metadata.
Stores both address and decimal precision so the executor can convert human-readable quantities to token atoms without assuming 18 decimals.
§Example
use alloy_primitives::Address;
use cow_chains::TokenRegistry;
let mut reg = TokenRegistry::new_with_decimals([
("USDC", Address::ZERO, 6u8),
("WETH", Address::ZERO, 18u8),
]);
assert_eq!(reg.get_decimals("USDC"), Some(6));
assert!(reg.contains("WETH"));
assert!(!reg.contains("DAI"));
reg.insert("DAI", Address::ZERO);
assert!(reg.contains("DAI"));
assert_eq!(reg.len(), 3);Implementations§
Source§impl TokenRegistry
impl TokenRegistry
Sourcepub fn new(
entries: impl IntoIterator<Item = (impl Into<String>, Address)>,
) -> Self
pub fn new( entries: impl IntoIterator<Item = (impl Into<String>, Address)>, ) -> Self
Create a new registry from (symbol, address) pairs.
All tokens registered this way are assumed to have 18 decimals.
Use new_with_decimals when tokens have
non-standard decimal counts (e.g. USDC = 6, WBTC = 8).
§Parameters
entries— an iterator of(symbol, address)pairs.
§Returns
A new TokenRegistry with all entries set to 18 decimals.
Sourcepub fn new_with_decimals(
entries: impl IntoIterator<Item = (impl Into<String>, Address, u8)>,
) -> Self
pub fn new_with_decimals( entries: impl IntoIterator<Item = (impl Into<String>, Address, u8)>, ) -> Self
Create a new registry from (symbol, address, decimals) tuples.
Use this when tokens have non-standard decimal counts (e.g. USDC = 6, WBTC = 8).
§Parameters
entries— an iterator of(symbol, address, decimals)tuples.
§Returns
A new TokenRegistry with explicit decimal counts per token.
Sourcepub fn get_decimals(&self, asset: &str) -> Option<u8>
pub fn get_decimals(&self, asset: &str) -> Option<u8>
Sourcepub fn insert_with_decimals(
&mut self,
symbol: impl Into<String>,
address: Address,
decimals: u8,
)
pub fn insert_with_decimals( &mut self, symbol: impl Into<String>, address: Address, decimals: u8, )
Sourcepub fn get_entry(&self, asset: &str) -> Option<(Address, u8)>
pub fn get_entry(&self, asset: &str) -> Option<(Address, u8)>
Look up both the address and decimal count for a given asset symbol.
Returns Some((address, decimals)) when registered, None otherwise.
use alloy_primitives::Address;
use cow_chains::TokenRegistry;
let reg = TokenRegistry::new_with_decimals([("USDC", Address::ZERO, 6u8)]);
assert_eq!(reg.get_entry("USDC"), Some((Address::ZERO, 6)));
assert_eq!(reg.get_entry("WETH"), None);Trait Implementations§
Source§impl Debug for TokenRegistry
impl Debug for TokenRegistry
Auto Trait Implementations§
impl Freeze for TokenRegistry
impl RefUnwindSafe for TokenRegistry
impl Send for TokenRegistry
impl Sync for TokenRegistry
impl Unpin for TokenRegistry
impl UnsafeUnpin for TokenRegistry
impl UnwindSafe for TokenRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.