pub trait Usdc {
// Required method
fn usdc_address(&self) -> Result<Address, UsdcError>;
}Expand description
A trait for types that can provide a USDC contract address.
Required Methods§
Sourcefn usdc_address(&self) -> Result<Address, UsdcError>
fn usdc_address(&self) -> Result<Address, UsdcError>
Returns the USDC contract address for the implementing context.
§Errors
Returns UsdcError::UnsupportedChain if the address is not known for the
given context (e.g., an unsupported blockchain).
Returns UsdcError::AddressParseError if a known address string is malformed
and cannot be parsed.
Implementations on Foreign Types§
Source§impl Usdc for NamedChain
Implementation of the Usdc trait for the alloy_chains::NamedChain enum.
impl Usdc for NamedChain
Implementation of the Usdc trait for the alloy_chains::NamedChain enum.
This implementation provides USDC addresses for a predefined set of chains.
Source§fn usdc_address(&self) -> Result<Address, UsdcError>
fn usdc_address(&self) -> Result<Address, UsdcError>
Retrieves the USDC address for the given NamedChain.
§Supported Chains
Refer to the crate’s README for a list of currently supported chains.
§Examples
use usdshe::{Usdc, UsdcError};
use alloy_chains::NamedChain;
use alloy_primitives::Address;
use std::str::FromStr;
// Get Polygon USDC address
let polygon_address = NamedChain::Polygon.usdc_address().unwrap();
assert_eq!(
polygon_address,
// Replace with actual Polygon USDC address string from your constants
Address::from_str("0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359").unwrap()
);
// Handle an unsupported chain
// Assuming Gnosis is unsupported for this example.
let result = NamedChain::Gnosis.usdc_address();
match result {
Err(UsdcError::UnsupportedChain(chain)) => {
assert_eq!(chain, NamedChain::Gnosis);
// Handle the error appropriately
}
Ok(_) => panic!("Expected an error for Gnosis, but got an address."),
Err(e) => panic!("Unexpected error type: {}", e),
}§Errors
UsdcError::UnsupportedChain: If the USDC address for the specifiedNamedChainis not defined in this crate.UsdcError::AddressParseError: If the predefined address string for a supported chain is malformed (this should be a bug in the crate if it occurs).