Coin & Coins

Coin

class terra_sdk.core.coin.Coin(denom, amount)[source]

Represents a (denom, amount) pairing, analagous to sdk.Coin and sdk.DecCoin in Cosmos SDK. Used for representing Terra native assets.

add(addend)[source]

Creates a new Coin with the sum as amount. If the addend is a Coin, its denom must match.

Parameters

addend (Union[Numeric.Input, Coin]) – addend

Raises

ArithmeticError – if addedend has different denom

Returns

sum

Return type

Coin

amount: Union[int, terra_sdk.core.numeric.Dec]

Coin’s amount – can be a int or Dec

denom: str

Coin’s denomination, ex uusd, uluna, etc.

div(divisor)[source]

Creates a new Coin with the quotient as amount. The divisor argument is first coerced to either an int or Dec.

Parameters

divisor (Numeric.Input) – divisor

Returns

quotient

Return type

Coin

classmethod from_amino(data)[source]

Deserializes a Coin object from its amino-codec representation.

Parameters

data (dict) – data object

Return type

Coin

classmethod from_data(data)[source]

Deserializes a Coin object from its JSON data representation.

Parameters

data (dict) – data object

Return type

Coin

classmethod from_str(string)[source]

Creates a new Coin from a coin-format string. Must match the format: 283923uusd (int-Coin) or 23920.23020uusd (Dec-Coin).

>>> int_coin = Coin.from_str("230920uusd")
>>> int_coin.denom
'uusd'
>>> int_coin.amount
230920
>>> dec_coin = Coin.from_str("203922.223uluna")
>>> dec_coin.denom
'uluna'
>>> dec_coin.amount
Dec('203922.223')
Parameters

string (str) – string to convert

Raises

ValueError – if string is in wrong format

Returns

converted string

Return type

Coin

is_dec_coin()[source]

Checks whether the coin’s amount is of type Dec.

Return type

bool

is_int_coin()[source]

Checks whether the coin’s amount is of type int.

Return type

bool

mod(modulo)[source]

Creates a new Coin with the modulus as amount.

Parameters

modulo (Numeric.Input) – modulo

Returns

modulo

Return type

Coin

mul(multiplier)[source]

Creates a new Coin with the product as amount. The multiplier argument is first coerced to either an int or Dec.

Parameters

multiplier (Numeric.Input) – multiplier

Returns

product

Return type

Coin

static parse(arg)[source]

Converts the argument into a coin.

Parameters

arg (Union[Coin, str, dict]) – value to be converted to coin

Return type

Coin

sub(subtrahend)[source]

Creates a new Coin with the difference as amount. If the subtrahend is a Coin, its denom must match.

Parameters

subtrahend (Union[Numeric.Input, Coin]) – subtrahend

Returns

difference

Return type

Coin

to_data()[source]

Converts the object to its JSON-serializable Python data representation.

Return type

dict

to_dec_coin()[source]

Creates a new Coin with a Dec amount.

Return type

Coin

to_int_ceil_coin()[source]

Turns the coin into an int coin with ceiling the amount.

Return type

Coin

to_int_coin()[source]

Creates a new Coin with an int amount.

Return type

Coin

Coins

class terra_sdk.core.coins.Coins(arg={}, **denoms)[source]

Represents an unordered collection of Coin objects – analagous to sdk.Coins and sdk.DecCoins in Cosmos SDK. If one of the input coins would be Dec-amount type coin, the resultant Coins is converted to Dec-amount coins.

Parameters

arg (Optional[Coins.Input], optional) – argument to convert. Defaults to {}.

Raises

TypeError – if arg is not an Iterable

Input

Types which can be converted into a Coins object.

alias of Union[Iterable[terra_sdk.core.coin.Coin], str, Dict[str, Union[str, int, float, decimal.Decimal, terra_sdk.core.numeric.Dec]], Dict[str, terra_sdk.core.coin.Coin]]

add(addend)[source]

Performs addition, which combines the sets of Coin objects. Coins of similar denoms will be merged into one Coin representing the denom.

Parameters

addend (Union[Coin, Coins]) – addend

Return type

Coins

denoms()[source]

Get the list of denoms for all Coin objects contained.

Return type

List[str]

div(divisor)[source]

Performs division, which divides all the Coin objects in the set by a divisor.

Parameters

divisor (Numeric.Input) – divisor

Return type

Coins

filter(predicate)[source]

Creates a new Coins collection which filters out all Coin objects that do not meet the predicate.

Parameters

predicate (Callable[[Coin], bool]) – predicate for filtering

Return type

Coins

classmethod from_amino(amino)[source]

Converts list of Coin-amino objects to Coins.

Parameters

amino (list) – list of Coin-data objects

Return type

Coins

classmethod from_data(data)[source]

Converts list of Coin-data objects to Coins.

Parameters

data (list) – list of Coin-data objects

Return type

Coins

classmethod from_proto(proto)[source]

Converts list of Coin-data objects to Coins.

Parameters

data (list) – list of Coin-data objects

Return type

Coins

classmethod from_str(s)[source]

Converts a comma-separated list of Coin-format strings to Coins.

>>> Coins.from_str('1000uluna,1234ukrw')
Coins("1000uluna,1234ukrw")
Parameters

s (str) – string to convert

Return type

Coins

get(denom)[source]

Get the Coin with the denom contained in the Coins set.

Parameters

denom (str) – denom

Returns

result (can be None)

Return type

Optional[Coin]

map(fn)[source]

Creates an iterable which applies the function to all coins in the set, ordered by denomination.

Parameters

fn (Callable[[Coin], Any]) – function to apply

Returns

coin map

Return type

Iterator[Any]

Yields

Iterator[Any] – coin map

mul(multiplier)[source]

Performs multiplicaiton, which multiplies all the Coin objects in the set by a multiplier.

Parameters

multiplier (Numeric.Input) – multiplier

Return type

Coins

sub(subtrahend)[source]

Performs subtraction, which combines the sets of Coin objects. Coins of similar denoms will be merged into one Coin representing the denom.

Parameters

subtrahend (Union[Coin, Coins]) – subtrahend

Return type

Coins

to_data()[source]

Converts the object to its JSON-serializable Python data representation.

Return type

List[dict]

to_dec_coins()[source]

Creates new set of Coins that have :class`Dec` amounts.

Return type

Coins

to_int_ceil_coins()[source]

Creates a new Coins object with all int coins with ceiling the amount

Return type

Coins

to_int_coins()[source]

Creates new set of Coins that have int amounts.

Return type

Coins

to_list()[source]

Converts the set of Coin objects contained into a sorted list by denom.

Returns

list, sorted by denom

Return type

List[Coin]