Struct cosmwasm_std::Coins

source ·
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

source

pub fn to_vec(&self) -> Vec<Coin>

Conversion to Vec, while NOT consuming the original object.

This produces a vector of coins that is sorted alphabetically by denom with no duplicate denoms.

source

pub fn into_vec(self) -> Vec<Coin>

Conversion to Vec, consuming the original object.

This produces a vector of coins that is sorted alphabetically by denom with no duplicate denoms.

source

pub fn len(&self) -> usize

Returns the number of different denoms in this collection.

source

pub fn is_empty(&self) -> bool

Returns true if this collection contains no coins.

source

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.

source

pub fn amount_of(&self, denom: &str) -> Uint128

Returns the amount of the given denom or zero if the denom is not present.

source

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

pub fn add(&mut self, coin: Coin) -> StdResult<()>

Adds the given coin to this Coins instance. Errors in case of overflow.

source

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.

source

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 Clone for Coins

source§

fn clone(&self) -> Coins

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Coins

source§

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

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

impl Default for Coins

source§

fn default() -> Coins

Returns the “default value” for a type. Read more
source§

impl Display for Coins

source§

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

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

impl From<Coin> for Coins

source§

fn from(value: Coin) -> Self

Converts to this type from the input type.
source§

impl From<Coins> for Vec<Coin>

source§

fn from(value: Coins) -> Self

Converts to this type from the input type.
source§

impl FromStr for Coins

§

type Err = StdError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> StdResult<Self>

Parses a string s to return a value of this type. Read more
source§

impl<'a> IntoIterator for &'a Coins

§

type Item = &'a Coin

The type of the elements being iterated over.
§

type IntoIter = CoinsIter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for Coins

§

type Item = Coin

The type of the elements being iterated over.
§

type IntoIter = CoinsIntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for Coins

source§

fn eq(&self, other: &Coins) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<&[Coin]> for Coins

§

type Error = CoinsError

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

fn try_from(slice: &[Coin]) -> Result<Self, CoinsError>

Performs the conversion.
source§

impl<const N: usize> TryFrom<[Coin; N]> for Coins

§

type Error = CoinsError

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

fn try_from(slice: [Coin; N]) -> Result<Self, CoinsError>

Performs the conversion.
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.

§

type Error = CoinsError

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

fn try_from(vec: Vec<Coin>) -> Result<Self, CoinsError>

Performs the conversion.
source§

impl Eq for Coins

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<U> As for U

source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. 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> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

source§

default fn to_string(&self) -> String

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

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

§

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>,

§

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.