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.
-
#display_name ⇒ Object
readonly
Returns the value of attribute display_name.
-
#network_id ⇒ Object
readonly
Returns the value of attribute network_id.
Class Method Summary collapse
-
.from_atomic_amount(atomic_amount, asset_id) ⇒ BigDecimal
Converts an amount from the atomic value of the primary denomination of the provided Asset ID to whole units of the specified asset ID.
- .from_model(asset_model) ⇒ Object
-
.primary_denomination(asset_id) ⇒ Symbol
Returns the primary denomination for the provided Asset ID.
-
.supported?(asset_id) ⇒ Boolean
Retuns whether the provided asset ID is supported.
-
.to_atomic_amount(amount, asset_id) ⇒ BigDecimal
Converts the amount of the Asset to the atomic units of the primary denomination of the Asset.
Instance Method Summary collapse
-
#initialize(network_id:, asset_id:, display_name: nil, address_id: nil, decimals: nil) ⇒ Asset
constructor
Returns a new Asset object.
-
#inspect ⇒ String
Same as to_s.
-
#to_s ⇒ String
Returns a string representation of the Asset.
Constructor Details
#initialize(network_id:, asset_id:, display_name: nil, address_id: nil, decimals: nil) ⇒ Asset
Returns a new Asset object. Do not use this method. Instead, use the Asset constants defined in the Coinbase module.
82 83 84 85 86 87 88 |
# File 'lib/coinbase/asset.rb', line 82 def initialize(network_id:, asset_id:, display_name: nil, address_id: nil, decimals: nil) @network_id = network_id @asset_id = asset_id @display_name = display_name @address_id = address_id @decimals = decimals end |
Instance Attribute Details
#address_id ⇒ Object (readonly)
Returns the value of attribute address_id.
90 91 92 |
# File 'lib/coinbase/asset.rb', line 90 def address_id @address_id end |
#asset_id ⇒ Object (readonly)
Returns the value of attribute asset_id.
90 91 92 |
# File 'lib/coinbase/asset.rb', line 90 def asset_id @asset_id end |
#decimals ⇒ Object (readonly)
Returns the value of attribute decimals.
90 91 92 |
# File 'lib/coinbase/asset.rb', line 90 def decimals @decimals end |
#display_name ⇒ Object (readonly)
Returns the value of attribute display_name.
90 91 92 |
# File 'lib/coinbase/asset.rb', line 90 def display_name @display_name end |
#network_id ⇒ Object (readonly)
Returns the value of attribute network_id.
90 91 92 |
# File 'lib/coinbase/asset.rb', line 90 def network_id @network_id end |
Class Method Details
.from_atomic_amount(atomic_amount, asset_id) ⇒ BigDecimal
Converts an amount from the atomic value of the primary denomination of the provided Asset ID to whole units of the specified asset ID.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/coinbase/asset.rb', line 37 def self.from_atomic_amount(atomic_amount, asset_id) case asset_id when :eth atomic_amount / BigDecimal(Coinbase::WEI_PER_ETHER.to_s) when :gwei atomic_amount / BigDecimal(Coinbase::WEI_PER_GWEI.to_s) when :usdc atomic_amount / BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC.to_s) when :weth atomic_amount / BigDecimal(Coinbase::WEI_PER_ETHER) else atomic_amount end end |
.from_model(asset_model) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coinbase/asset.rb', line 64 def self.from_model(asset_model) raise unless asset_model.is_a?(Coinbase::Client::Asset) new( network_id: Coinbase.to_sym(asset_model.network_id), asset_id: Coinbase.to_sym(asset_model.asset_id), address_id: asset_model.contract_address, decimals: asset_model.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.
58 59 60 61 62 |
# File 'lib/coinbase/asset.rb', line 58 def self.primary_denomination(asset_id) return :eth if %i[wei gwei].include?(asset_id) asset_id end |
.supported?(asset_id) ⇒ Boolean
Retuns whether the provided asset ID is supported.
9 10 11 |
# File 'lib/coinbase/asset.rb', line 9 def self.supported?(asset_id) !!Coinbase::SUPPORTED_ASSET_IDS[asset_id] end |
.to_atomic_amount(amount, asset_id) ⇒ BigDecimal
Converts the amount of the Asset to the atomic units of the primary denomination of the Asset.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/coinbase/asset.rb', line 17 def self.to_atomic_amount(amount, asset_id) case asset_id when :eth amount * BigDecimal(Coinbase::WEI_PER_ETHER.to_s) when :gwei amount * BigDecimal(Coinbase::WEI_PER_GWEI.to_s) when :usdc amount * BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC.to_s) when :weth amount * BigDecimal(Coinbase::WEI_PER_ETHER) else amount end end |
Instance Method Details
#inspect ⇒ String
Same as to_s.
102 103 104 |
# File 'lib/coinbase/asset.rb', line 102 def inspect to_s end |
#to_s ⇒ String
Returns a string representation of the Asset.
94 95 96 97 98 |
# File 'lib/coinbase/asset.rb', line 94 def to_s "Coinbase::Asset{network_id: '#{network_id}', asset_id: '#{asset_id}', display_name: '#{display_name}'" + (address_id.nil? ? '}' : ", address_id: '#{address_id}'}") + (decimals.nil? ? '}' : ", decimals: '#{decimals}'}") end |