Class: Coinbase::CryptoAmount
- Inherits:
-
Object
- Object
- Coinbase::CryptoAmount
- Defined in:
- lib/coinbase/crypto_amount.rb
Overview
A representation of a CryptoAmount that includes the amount and asset.
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#asset_id ⇒ Object
readonly
Returns the value of attribute asset_id.
Class Method Summary collapse
-
.from_model(amount_model) ⇒ CryptoAmount
Converts a Coinbase::Client::CryptoAmount model to a Coinbase::CryptoAmount.
-
.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.
Instance Method Summary collapse
-
#initialize(amount:, asset:, asset_id: nil) ⇒ CryptoAmount
constructor
Returns a new CryptoAmount object.
-
#inspect ⇒ String
Same as to_s.
-
#to_atomic_amount ⇒ BigDecimal
Returns the amount in atomic units.
-
#to_s ⇒ String
Returns a string representation of the CryptoAmount.
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.
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
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
42 43 44 |
# File 'lib/coinbase/crypto_amount.rb', line 42 def amount @amount end |
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
42 43 44 |
# File 'lib/coinbase/crypto_amount.rb', line 42 def asset @asset end |
#asset_id ⇒ Object (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
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.
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
#inspect ⇒ String
Same as to_s.
58 59 60 |
# File 'lib/coinbase/crypto_amount.rb', line 58 def inspect to_s end |
#to_atomic_amount ⇒ BigDecimal
Returns 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_s ⇒ String
Returns 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 |