Skip to main content

TokenRegistry

Struct TokenRegistry 

Source
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

Source

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.

Source

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.

Source

pub fn get(&self, asset: &str) -> Option<Address>

Look up the Address for a given asset symbol, e.g. "WETH".

§Arguments
  • asset — the ticker symbol to look up.
§Returns

Some(address) if the symbol is registered, None otherwise.

Source

pub fn get_decimals(&self, asset: &str) -> Option<u8>

Look up the decimal count for a given asset symbol.

§Arguments
  • asset — the ticker symbol to look up.
§Returns

Some(decimals) when the symbol is registered, None otherwise.

Source

pub fn insert(&mut self, symbol: impl Into<String>, address: Address)

Register a token with 18 decimals (or update an existing entry).

§Arguments
  • symbol — the ticker symbol to register.
  • address — the ERC-20 contract Address.
Source

pub fn insert_with_decimals( &mut self, symbol: impl Into<String>, address: Address, decimals: u8, )

Register a token with explicit decimals (or update an existing entry).

§Arguments
  • symbol — the ticker symbol to register.
  • address — the ERC-20 contract Address.
  • decimals — the token’s decimal precision.
Source

pub fn contains(&self, asset: &str) -> bool

Returns true if asset is registered in this registry.

§Arguments
  • asset — the ticker symbol to check.
§Returns

true when the symbol exists in the registry.

Source

pub fn len(&self) -> usize

Returns the number of registered tokens.

§Returns

The count of tokens in this registry.

Source

pub fn is_empty(&self) -> bool

Returns true if no tokens are registered.

§Returns

true when the registry contains zero tokens.

Source

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);
Source

pub fn remove(&mut self, asset: &str) -> Option<(Address, u8)>

Remove a token from the registry.

§Arguments
  • asset — the ticker symbol to remove.
§Returns

Some((address, decimals)) if the symbol was registered, None otherwise.

Trait Implementations§

Source§

impl Debug for TokenRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for TokenRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more