Class: Coinbase::CryptoAmount

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/crypto_amount.rb

Overview

A representation of a CryptoAmount that includes the amount and asset.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, asset:, asset_id: nil) ⇒ CryptoAmount

Returns a new CryptoAmount object. Do not use this method. Instead, use CryptoAmount.from_model or CryptoAmount.from_model_and_asset_id.

Parameters:

  • amount (BigDecimal)

    The amount of the Asset

  • asset (Coinbase::Asset)

    The Asset

  • asset_id (Symbol) (defaults to: nil)

    The Asset ID



36
37
38
39
40
# File 'lib/coinbase/crypto_amount.rb', line 36

def initialize(amount:, asset:, asset_id: nil)
  @amount = amount
  @asset = asset
  @asset_id = asset_id || asset.asset_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



42
43
44
# File 'lib/coinbase/crypto_amount.rb', line 42

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



42
43
44
# File 'lib/coinbase/crypto_amount.rb', line 42

def asset
  @asset
end

#asset_idObject (readonly)

Returns the value of attribute asset_id.



42
43
44
# File 'lib/coinbase/crypto_amount.rb', line 42

def asset_id
  @asset_id
end

Class Method Details

.from_model(amount_model) ⇒ CryptoAmount

Converts a Coinbase::Client::CryptoAmount model to a Coinbase::CryptoAmount

Parameters:

Returns:



9
10
11
12
13
# File 'lib/coinbase/crypto_amount.rb', line 9

def self.from_model(amount_model)
  asset = Coinbase::Asset.from_model(amount_model.asset)

  new(amount: asset.from_atomic_amount(amount_model.amount), asset: asset)
end

.from_model_and_asset_id(amount_model, asset_id) ⇒ CryptoAmount

Converts a Coinbase::Client::CryptoAmount model and asset ID to a Coinbase::CryptoAmount This can be used to specify a non-primary denomination that we want the amount to be converted to.

Parameters:

  • amount_model (Coinbase::Client::CryptoAmount)

    The crypto amount from the API.

  • asset_id (Symbol)

    The Asset ID of the denomination we want returned.

Returns:



21
22
23
24
25
26
27
28
29
# File 'lib/coinbase/crypto_amount.rb', line 21

def self.from_model_and_asset_id(amount_model, asset_id)
  asset = Coinbase::Asset.from_model(amount_model.asset, asset_id: asset_id)

  new(
    amount: asset.from_atomic_amount(amount_model.amount),
    asset: asset,
    asset_id: asset_id
  )
end

Instance Method Details

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the CryptoAmount



58
59
60
# File 'lib/coinbase/crypto_amount.rb', line 58

def inspect
  to_s
end

#to_atomic_amountBigDecimal

Returns the amount in atomic units.

Returns:

  • (BigDecimal)

    the amount in atomic units



46
47
48
# File 'lib/coinbase/crypto_amount.rb', line 46

def to_atomic_amount
  asset.to_atomic_amount(amount)
end

#to_sString

Returns a string representation of the CryptoAmount.

Returns:

  • (String)

    a string representation of the CryptoAmount



52
53
54
# File 'lib/coinbase/crypto_amount.rb', line 52

def to_s
  Coinbase.pretty_print_object(self.class, amount: amount.to_s('F'), asset_id: asset_id)
end