pub struct Coins(/* private fields */);
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 Coin
.
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 to_vec(&self) -> Vec<Coin>
pub fn to_vec(&self) -> Vec<Coin>
Conversion to Vec
This produces a vector of coins that is sorted alphabetically by denom with no duplicate denoms.
Sourcepub fn into_vec(self) -> Vec<Coin>
pub fn into_vec(self) -> Vec<Coin>
Conversion to Vec
This produces a vector of coins that is sorted alphabetically by denom with no duplicate denoms.
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);
Sourcepub fn add(&mut self, coin: Coin) -> StdResult<()>
pub fn add(&mut self, coin: Coin) -> StdResult<()>
Adds the given coin to this Coins
instance.
Errors in case of overflow.
Sourcepub fn sub(&mut self, coin: Coin) -> StdResult<()>
pub fn sub(&mut self, coin: Coin) -> StdResult<()>
Subtracts the given coin from this Coins
instance.
Errors in case of overflow or if the denom is not present.
Sourcepub fn iter(&self) -> CoinsIter<'_>
pub fn iter(&self) -> CoinsIter<'_>
Returns an iterator over the coins.
§Examples
let mut coins = Coins::default();
coins.add(coin(500, "uluna")).unwrap();
coins.add(coin(1000, "uatom")).unwrap();
let mut iterator = coins.iter();
let uatom = iterator.next().unwrap();
assert_eq!(uatom.denom, "uatom");
assert_eq!(uatom.amount.u128(), 1000);
let uluna = iterator.next().unwrap();
assert_eq!(uluna.denom, "uluna");
assert_eq!(uluna.amount.u128(), 500);
assert_eq!(iterator.next(), None);
Trait Implementations§
Source§impl<'a> IntoIterator for &'a Coins
impl<'a> IntoIterator for &'a Coins
Source§impl IntoIterator for Coins
impl IntoIterator for Coins
Source§impl TryFrom<&[Coin]> for Coins
impl TryFrom<&[Coin]> for Coins
Source§type Error = CoinsError
type Error = CoinsError
Source§impl TryFrom<Vec<Coin>> for Coins
Casting a Vec to Coins.
The Vec can be out of order, but must not contain duplicate denoms.
If you want to sum up duplicates, create an empty instance using Coins::default
and
use Coins::add
to add your coins.
impl TryFrom<Vec<Coin>> for Coins
Casting a VecCoins::default
and
use Coins::add
to add your coins.
impl Eq for Coins
impl StructuralPartialEq for Coins
Auto Trait Implementations§
impl Freeze for Coins
impl RefUnwindSafe for Coins
impl Send for Coins
impl Sync for Coins
impl Unpin for Coins
impl UnwindSafe for Coins
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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