pub struct AssetListBase<T: AddressLike>(/* private fields */);
Expand description
Represents a list of fungible tokens, each with a known amount
Implementations§
Source§impl AssetListBase<String>
impl AssetListBase<String>
Sourcepub fn check(
&self,
api: &dyn Api,
optional_whitelist: Option<&[&str]>,
) -> Result<AssetList, AssetError>
pub fn check( &self, api: &dyn Api, optional_whitelist: Option<&[&str]>, ) -> Result<AssetList, AssetError>
Validate data contained in an unchecked asset list instance, return a new checked asset list instance:
- For CW20 tokens, assert the contract address is valid
- For SDK coins, assert that the denom is included in a given whitelist; skip if the whitelist is not provided
use cosmwasm_std::{Addr, Api, StdResult};
use cw_asset::{Asset, AssetList, AssetListUnchecked, AssetUnchecked};
fn validate_assets(api: &dyn Api, list_unchecked: &AssetListUnchecked) {
match list_unchecked.check(api, Some(&["uatom", "uluna"])) {
Ok(list) => println!("asset list is valid: {}", list.to_string()),
Err(err) => println!("asset list is invalid! reason: {}", err),
}
}
Source§impl AssetListBase<Addr>
impl AssetListBase<Addr>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty asset list
use cw_asset::AssetList;
let list = AssetList::new();
let len = list.len(); // should be zero
Sourcepub fn to_vec(&self) -> Vec<Asset> ⓘ
pub fn to_vec(&self) -> Vec<Asset> ⓘ
Return a copy of the underlying vector
use cw_asset::{Asset, AssetList};
let list =
AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
let vec: Vec<Asset> = list.to_vec();
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return length of the asset list
use cw_asset::{Asset, AssetList};
let list =
AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
let len = list.len(); // should be two
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return whether the asset list is empty
use cw_asset::{Asset, AssetList};
let mut list = AssetList::from(vec![Asset::native("uluna", 12345u128)]);
let is_empty = list.is_empty(); // should be `false`
list.deduct(&Asset::native("uluna", 12345u128)).unwrap();
let is_empty = list.is_empty(); // should be `true`
Sourcepub fn find(&self, info: &AssetInfo) -> Option<&Asset>
pub fn find(&self, info: &AssetInfo) -> Option<&Asset>
Find an asset in the list that matches the provided asset info
Return Some(&asset)
if found, where &asset
is a reference to the
asset found; None
if not found.
A case where is method is useful is to find how much asset the user sent along with a message:
use cosmwasm_std::MessageInfo;
use cw_asset::{AssetInfo, AssetList};
fn find_uusd_received_amount(info: &MessageInfo) {
let list = AssetList::from(&info.funds);
match list.find(&AssetInfo::native("uusd")) {
Some(asset) => println!("received {} uusd", asset.amount),
None => println!("did not receive any uusd"),
}
}
Sourcepub fn apply<F: FnMut(&mut Asset)>(&mut self, f: F) -> &mut Self
pub fn apply<F: FnMut(&mut Asset)>(&mut self, f: F) -> &mut Self
Apply a mutation on each of the asset
An example case where this is useful is to scale the amount of each asset in the list by a certain factor:
use cw_asset::{Asset, AssetInfo, AssetList};
let mut list =
AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
let list_halved = list.apply(|a| a.amount = a.amount.multiply_ratio(1u128, 2u128));
Sourcepub fn purge(&mut self) -> &mut Self
pub fn purge(&mut self) -> &mut Self
Removes all assets in the list that has zero amount
use cw_asset::{Asset, AssetList};
let mut list =
AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 0u128)]);
let mut len = list.len(); // should be two
list.purge();
len = list.len(); // should be one
Sourcepub fn add(&mut self, asset_to_add: &Asset) -> Result<&mut Self, AssetError>
pub fn add(&mut self, asset_to_add: &Asset) -> Result<&mut Self, AssetError>
Add a new asset to the list
If asset of the same kind already exists in the list, then increment its amount; if not, append to the end of the list.
NOTE: purge
is automatically performed following the addition, so
adding an asset with zero amount has no effect.
use cw_asset::{Asset, AssetInfo, AssetList};
let mut list = AssetList::from(vec![
Asset::native("uluna", 12345u128),
]);
list.add(&Asset::native("uusd", 67890u128));
let mut len = list.len(); // should be two
list.add(&Asset::native("uluna", 11111u128));
len = list.len(); // should still be two
let uluna_amount = list
.find(&AssetInfo::native("uluna"))
.unwrap()
.amount; // should have increased to 23456
Sourcepub fn add_many(
&mut self,
assets_to_add: &AssetList,
) -> Result<&mut Self, AssetError>
pub fn add_many( &mut self, assets_to_add: &AssetList, ) -> Result<&mut Self, AssetError>
Add multiple new assets to the list
use cw_asset::{Asset, AssetInfo, AssetList};
let mut list = AssetList::from(vec![
Asset::native("uluna", 12345u128),
]);
list.add_many(&AssetList::from(vec![
Asset::native("uusd", 67890u128),
Asset::native("uluna", 11111u128),
]));
let uusd_amount = list
.find(&AssetInfo::native("uusd"))
.unwrap()
.amount; // should be 67890
let uluna_amount = list
.find(&AssetInfo::native("uluna"))
.unwrap()
.amount; // should have increased to 23456
Sourcepub fn deduct(
&mut self,
asset_to_deduct: &Asset,
) -> Result<&mut Self, AssetError>
pub fn deduct( &mut self, asset_to_deduct: &Asset, ) -> Result<&mut Self, AssetError>
Deduct an asset from the list
The asset of the same kind and equal or greater amount must already exist in the list. If so, deduct the amount from the asset; ifnot, throw an error.
NOTE: purge
is automatically performed following the addition.
Therefore, if an asset’s amount is reduced to zero, it will be removed
from the list.
use cw_asset::{Asset, AssetInfo, AssetList};
let mut list = AssetList::from(vec![
Asset::native("uluna", 12345u128),
]);
list.deduct(&Asset::native("uluna", 10000u128)).unwrap();
let uluna_amount = list
.find(&AssetInfo::native("uluna"))
.unwrap()
.amount; // should have reduced to 2345
list.deduct(&Asset::native("uluna", 2345u128));
let len = list.len(); // should be zero, as uluna is purged from the list
Sourcepub fn deduct_many(
&mut self,
assets_to_deduct: &AssetList,
) -> Result<&mut Self, AssetError>
pub fn deduct_many( &mut self, assets_to_deduct: &AssetList, ) -> Result<&mut Self, AssetError>
Deduct multiple assets from the list
use cw_asset::{Asset, AssetInfo, AssetList};
let mut list = AssetList::from(vec![
Asset::native("uluna", 12345u128),
Asset::native("uusd", 67890u128),
]);
list.deduct_many(&AssetList::from(vec![
Asset::native("uluna", 2345u128),
Asset::native("uusd", 67890u128),
])).unwrap();
let uluna_amount = list
.find(&AssetInfo::native("uluna"))
.unwrap()
.amount; // should have reduced to 2345
let len = list.len(); // should be zero, as uusd is purged from the list
Sourcepub fn transfer_msgs<A: Into<String> + Clone>(
&self,
to: A,
) -> Result<Vec<CosmosMsg>, AssetError>
pub fn transfer_msgs<A: Into<String> + Clone>( &self, to: A, ) -> Result<Vec<CosmosMsg>, AssetError>
Generate a transfer messages for every asset in the list
use cosmwasm_std::{Addr, Response};
use cw_asset::{AssetError, AssetList};
fn transfer_assets(list: &AssetList, recipient_addr: &Addr) -> Result<Response, AssetError> {
let msgs = list.transfer_msgs(recipient_addr)?;
Ok(Response::new().add_messages(msgs).add_attribute("assets_sent", list.to_string()))
}
Trait Implementations§
Source§impl<T: Clone + AddressLike> Clone for AssetListBase<T>
impl<T: Clone + AddressLike> Clone for AssetListBase<T>
Source§fn clone(&self) -> AssetListBase<T>
fn clone(&self) -> AssetListBase<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug + AddressLike> Debug for AssetListBase<T>
impl<T: Debug + AddressLike> Debug for AssetListBase<T>
Source§impl<T: AddressLike> Default for AssetListBase<T>
impl<T: AddressLike> Default for AssetListBase<T>
Source§impl<'de, T> Deserialize<'de> for AssetListBase<T>where
T: Deserialize<'de> + AddressLike,
impl<'de, T> Deserialize<'de> for AssetListBase<T>where
T: Deserialize<'de> + AddressLike,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<AssetListBase<Addr>> for AssetListUnchecked
impl From<AssetListBase<Addr>> for AssetListUnchecked
Source§impl<T: AddressLike + JsonSchema> JsonSchema for AssetListBase<T>
impl<T: AddressLike + JsonSchema> JsonSchema for AssetListBase<T>
Source§fn schema_name() -> String
fn schema_name() -> String
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moreSource§impl<T: PartialEq + AddressLike> PartialEq for AssetListBase<T>
impl<T: PartialEq + AddressLike> PartialEq for AssetListBase<T>
Source§impl<T> Serialize for AssetListBase<T>where
T: Serialize + AddressLike,
impl<T> Serialize for AssetListBase<T>where
T: Serialize + AddressLike,
impl<T: AddressLike> StructuralPartialEq for AssetListBase<T>
Auto Trait Implementations§
impl<T> Freeze for AssetListBase<T>
impl<T> RefUnwindSafe for AssetListBase<T>where
T: RefUnwindSafe,
impl<T> Send for AssetListBase<T>where
T: Send,
impl<T> Sync for AssetListBase<T>where
T: Sync,
impl<T> Unpin for AssetListBase<T>where
T: Unpin,
impl<T> UnwindSafe for AssetListBase<T>where
T: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more