TokenAmount

public class TokenAmount : Codable
extension TokenAmount: Comparable
extension TokenAmount: Equatable
extension TokenAmount: CustomStringConvertible
extension TokenAmount: Hashable

Class representing a numeric amount on the Tezos network. The network uses natural numbers inside strings, which technically have an infinite length. This class is used to encapsulate a BigInt and provide all the necessary init’s and formatting functions to work with the networks requirements.

  • Declaration

    Swift

    public enum TokenAmountError : Error
  • The number of decimal places that this token supports.

    Declaration

    Swift

    public var decimalPlaces: Int
  • Format the internal value to ensure it matches the format the RPC will expect

    Declaration

    Swift

    public var rpcRepresentation: String { get }
  • Basic formatting of a token to be human readable. For more advanced options, use the format function

    Declaration

    Swift

    public var normalisedRepresentation: String { get }

Init

  • Set the internal balance, using a RPC string (most likely directly from the RPC node response). e.g. “1 XTZ” to the user = “1000000” to the RPC, as there are no such thing as decimal places on the network

    Declaration

    Swift

    public init?(fromRpcAmount rpcAmount: String, decimalPlaces: Int)

    Parameters

    fromRpcAmount

    A string conforming to the RPC standard for the given token.

  • Set the internal balance, using a decimal version of an RPC amount. e.g. “1 XTZ” to the user = “1000000” to the RPC, as there are no such thing as decimal places on the network

    Declaration

    Swift

    public convenience init?(fromRpcAmount rpcAmount: Decimal, decimalPlaces: Int)

    Parameters

    fromRpcAmount

    A decimal conforming to the RPC standard for the given token. Decimal places will be ignored.

  • Set the internal balance, using a decimal version of a normalised amount. e.g. if the amount is 1.5 and the token is xtz, internally it will be stored as 1500000

    Declaration

    Swift

    public init(fromNormalisedAmount normalisedAmount: Decimal, decimalPlaces: Int)

    Parameters

    fromNormalisedAmount

    A decimal containing an amount for the given token. Anything over the given decimal places for the token will be ignored.

  • Set the internal balance, using a normalised amount string. e.g. if the amount is 1.5 and the token is xtz, internally it will be stored as 1500000

    Declaration

    Swift

    public convenience init?(fromNormalisedAmount normalisedAmount: String, decimalPlaces: Int)

    Parameters

    fromNormalisedAmount

    A string containing an amount for the given token. Anything over the given decimal places for the token will be ignored.

  • Quickly create a TokenAmount with zero balance and no decimal places. Warning: the decimal places attribute could be used by other code to determine precision. This should only be used in places where it is needed as a temporary, default value.

    Declaration

    Swift

    public class func zero() -> TokenAmount
  • Quickly create a TokenAmount with zero balance.

    Declaration

    Swift

    public class func zeroBalance(decimalPlaces: Int) -> TokenAmount

Codable

  • Token Amounts need an amount and to know the number of decimal places. When downloading from an API, the balance may be presented without the decimal info, where as when we encode, we have the info. This coder attempts to handle both states, first checking if its possible to extract both, if not, defaulting the decimal palces to zero, expecting the calling application to provide this information later on from another proptery or even another API call (such as a metadata query)

    Declaration

    Swift

    required public init(from decoder: Decoder) throws
  • Declaration

    Swift

    public func encode(to encoder: Encoder) throws

Display

  • Format the current value into a human readable string, using the given locale.

    Declaration

    Swift

    public func formatNormalisedRepresentation(locale: Locale) -> String?

    Parameters

    locale

    The locale to use to decide whether to use decimal or comma, comma or spaces etc, when formattting the number

  • Function to convert the underlying rpc value into a Decimal which can be useful in some situations for integrating with other tools and frameworks. Warning Decimal has a limited, lower treshold (163 digits). Its possible it can overrun, hence the optional return value.

    Declaration

    Swift

    public func toRpcDecimal() -> Decimal?
  • Function to convert the underlying normalised value into a Decimal which can be useful in some situations for integrating with other tools and frameworks. Warning Decimal has a limited, lower treshold (163 digits). Its possible it can overrun, hence the optional return value.

    Declaration

    Swift

    public func toNormalisedDecimal() -> Decimal?
  • Currently we are unable to cast directly from TokenAmount to XTZAmount. This function will create a new XTZAmount object from the TokenAmount. THis is useful in situations where an amount is passed in a generic manner as a TokenAmount, but its required to be an XTZAmount

    Declaration

    Swift

    public func toXTZAmount() -> XTZAmount

Arithmetic

  • Overload + operator to allow users to add two Token amounts of the same type, together.

    Declaration

    Swift

    public static func + (lhs: TokenAmount, rhs: TokenAmount) -> TokenAmount
  • Overload += operator to allow users to add two Token amounts of the same type, together in place.

    Declaration

    Swift

    public static func += (lhs: inout TokenAmount, rhs: TokenAmount)
  • Overload - operator to allow users to subtract two Token amounts of the same type.

    Declaration

    Swift

    public static func - (lhs: TokenAmount, rhs: TokenAmount) -> TokenAmount
  • Overload -= operator to allow users to subtract one Token amount of the same type from another, together in place.

    Declaration

    Swift

    public static func -= (lhs: inout TokenAmount, rhs: TokenAmount)
  • Overload multiplcation operator to allow users to multiple a token by a dollar value, and return the localCurrency value of the token.

    Declaration

    Swift

    public static func * (lhs: TokenAmount, rhs: Decimal) -> Decimal
  • Overload multiplcation operator to allow users to multiple a token by an Int. Useful for fee caluclation

    Declaration

    Swift

    public static func * (lhs: TokenAmount, rhs: Int) -> TokenAmount

Extensions

  • Conforming to Comparable

    Declaration

    Swift

    public static func < (lhs: TokenAmount, rhs: TokenAmount) -> Bool
  • Conforming to Equatable

    Declaration

    Swift

    public static func == (lhs: TokenAmount, rhs: TokenAmount) -> Bool
  • Conforming to CustomStringConvertible to print a number, giving the appearence of a numeric type

    Declaration

    Swift

    public var description: String { get }
  • Conforming to Hashable to enable working with UITableViewDiffableDataSource

    Declaration

    Swift

    public func hash(into hasher: inout Hasher)