Struct cosmwasm_std::Coins
source · pub struct Coins(_);
Expand description
A collection of coins, similar to Cosmos SDK’s sdk.Coins
struct.
Differently from sdk.Coins
, which is a vector of sdk.Coin
, here we
implement Coins as a BTreeMap that maps from coin denoms to amounts.
This has a number of advantages:
- coins are naturally sorted alphabetically by denom
- duplicate denoms are automatically removed
- cheaper for searching/inserting/deleting: O(log(n)) compared to O(n)
Implementations§
source§impl Coins
impl Coins
sourcepub fn denoms(&self) -> Vec<String>
pub fn denoms(&self) -> Vec<String>
Returns the denoms as a vector of strings. The vector is guaranteed to not contain duplicates and sorted alphabetically.
sourcepub fn amount_of(&self, denom: &str) -> Uint128
pub fn amount_of(&self, denom: &str) -> Uint128
Returns the amount of the given denom or zero if the denom is not present.
sourcepub fn contains_only(&self, denom: &str) -> Option<Uint128>
pub fn contains_only(&self, denom: &str) -> Option<Uint128>
Returns the amount of the given denom if and only if this collection contains only
the given denom. Otherwise None
is returned.
Examples
use cosmwasm_std::{Coin, Coins, coin};
let coins: Coins = [coin(100, "uatom")].try_into().unwrap();
assert_eq!(coins.contains_only("uatom").unwrap().u128(), 100);
assert_eq!(coins.contains_only("uluna"), None);
use cosmwasm_std::{Coin, Coins, coin};
let coins: Coins = [coin(100, "uatom"), coin(200, "uusd")].try_into().unwrap();
assert_eq!(coins.contains_only("uatom"), None);
Trait Implementations§
source§impl PartialEq<Coins> for Coins
impl PartialEq<Coins> for Coins
source§impl TryFrom<Vec<Coin, Global>> for Coins
impl TryFrom<Vec<Coin, Global>> for Coins
Casting a VecCoins::default
and
use Coins::add
to add your coins.