Class: Coinbase::Asset
- Inherits:
-
Object
- Object
- Coinbase::Asset
- Defined in:
- lib/coinbase/asset.rb
Overview
A representation of an Asset.
Instance Attribute Summary collapse
-
#address_id ⇒ Object
readonly
Returns the value of attribute address_id.
-
#asset_id ⇒ Object
readonly
Returns the value of attribute asset_id.
-
#decimals ⇒ Object
readonly
Returns the value of attribute decimals.
-
#network_id ⇒ Object
readonly
Returns the value of attribute network_id.
Class Method Summary collapse
-
.fetch(network_id, asset_id) ⇒ Coinbase::Asset
Fetches the Asset with the provided Asset ID.
- .from_model(asset_model, asset_id: nil) ⇒ Object
-
.primary_denomination(asset_id) ⇒ Symbol
Returns the primary denomination for the provided Asset ID.
Instance Method Summary collapse
-
#from_atomic_amount(atomic_amount) ⇒ BigDecimal
Converts the amount of the Asset from atomic to whole units.
-
#initialize(network_id:, asset_id:, decimals:, address_id: nil) ⇒ Asset
constructor
Returns a new Asset object.
-
#inspect ⇒ String
Same as to_s.
-
#primary_denomination ⇒ Symbol
Returns the primary denomination for the Asset.
-
#to_atomic_amount(whole_amount) ⇒ BigDecimal
Converts the amount of the Asset from whole to atomic units.
-
#to_s ⇒ String
Returns a string representation of the Asset.
Constructor Details
#initialize(network_id:, asset_id:, decimals:, address_id: nil) ⇒ Asset
Returns a new Asset object. Do not use this method. Instead, use the Asset constants defined in the Coinbase module.
72 73 74 75 76 77 |
# File 'lib/coinbase/asset.rb', line 72 def initialize(network_id:, asset_id:, decimals:, address_id: nil) @network_id = network_id @asset_id = asset_id @address_id = address_id @decimals = decimals end |
Instance Attribute Details
#address_id ⇒ Object (readonly)
Returns the value of attribute address_id.
79 80 81 |
# File 'lib/coinbase/asset.rb', line 79 def address_id @address_id end |
#asset_id ⇒ Object (readonly)
Returns the value of attribute asset_id.
79 80 81 |
# File 'lib/coinbase/asset.rb', line 79 def asset_id @asset_id end |
#decimals ⇒ Object (readonly)
Returns the value of attribute decimals.
79 80 81 |
# File 'lib/coinbase/asset.rb', line 79 def decimals @decimals end |
#network_id ⇒ Object (readonly)
Returns the value of attribute network_id.
79 80 81 |
# File 'lib/coinbase/asset.rb', line 79 def network_id @network_id end |
Class Method Details
.fetch(network_id, asset_id) ⇒ Coinbase::Asset
Fetches the Asset with the provided Asset ID.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/coinbase/asset.rb', line 48 def fetch(network_id, asset_id) asset_model = Coinbase.call_api do assets_api.get_asset( Coinbase.normalize_network(network_id), primary_denomination(asset_id).to_s ) end from_model(asset_model, asset_id: asset_id) end |
.from_model(asset_model, asset_id: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/coinbase/asset.rb', line 19 def from_model(asset_model, asset_id: nil) raise unless asset_model.is_a?(Coinbase::Client::Asset) decimals = asset_model.decimals # Handle the non-primary denomination case at the asset level. # TODO: Push this logic down to the backend. if asset_id && Coinbase.to_sym(asset_id) != Coinbase.to_sym(asset_model.asset_id) case asset_id when :gwei decimals = GWEI_DECIMALS when :wei decimals = 0 else raise ArgumentError, "Unsupported asset ID: #{asset_id}" end end new( network_id: Coinbase.to_sym(asset_model.network_id), asset_id: asset_id || Coinbase.to_sym(asset_model.asset_id), address_id: asset_model.contract_address, decimals: decimals ) end |
.primary_denomination(asset_id) ⇒ Symbol
Returns the primary denomination for the provided Asset ID. For assets with multiple denominations, e.g. eth can also be denominated in wei and gwei, this method will return the primary denomination. e.g. eth.
13 14 15 16 17 |
# File 'lib/coinbase/asset.rb', line 13 def primary_denomination(asset_id) return :eth if %i[wei gwei].include?(asset_id) asset_id end |
Instance Method Details
#from_atomic_amount(atomic_amount) ⇒ BigDecimal
Converts the amount of the Asset from atomic to whole units.
84 85 86 |
# File 'lib/coinbase/asset.rb', line 84 def from_atomic_amount(atomic_amount) BigDecimal(atomic_amount) / BigDecimal(10).power(decimals) end |
#inspect ⇒ String
Same as to_s.
112 113 114 |
# File 'lib/coinbase/asset.rb', line 112 def inspect to_s end |
#primary_denomination ⇒ Symbol
Returns the primary denomination for the Asset. For gwei and wei the primary denomination is eth. For all other assets, the primary denomination is the same asset ID.
99 100 101 |
# File 'lib/coinbase/asset.rb', line 99 def primary_denomination self.class.primary_denomination(asset_id) end |
#to_atomic_amount(whole_amount) ⇒ BigDecimal
Converts the amount of the Asset from whole to atomic units.
91 92 93 |
# File 'lib/coinbase/asset.rb', line 91 def to_atomic_amount(whole_amount) whole_amount * BigDecimal(10).power(decimals) end |
#to_s ⇒ String
Returns a string representation of the Asset.
105 106 107 108 |
# File 'lib/coinbase/asset.rb', line 105 def to_s "Coinbase::Asset{network_id: '#{network_id}', asset_id: '#{asset_id}', decimals: '#{decimals}'" \ "#{address_id.nil? ? '' : ", address_id: '#{address_id}'"}}" end |