Class: Coinbase::Asset

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

Overview

A representation of an Asset.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network:, 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.

Parameters:

  • network (Symbol)

    The Network or Network ID to which the Asset belongs

  • asset_id (Symbol)

    The Asset ID

  • address_id (String) (defaults to: nil)

    (Optional) The Asset’s address ID, if one exists

  • decimals (Integer)

    (Optional) The number of decimal places the Asset uses



80
81
82
83
84
85
# File 'lib/coinbase/asset.rb', line 80

def initialize(network:, asset_id:, decimals:, address_id: nil)
  @network = Coinbase::Network.from_id(network)
  @asset_id = asset_id
  @address_id = address_id
  @decimals = decimals
end

Instance Attribute Details

#address_idObject (readonly)

Returns the value of attribute address_id.



87
88
89
# File 'lib/coinbase/asset.rb', line 87

def address_id
  @address_id
end

#asset_idObject (readonly)

Returns the value of attribute asset_id.



87
88
89
# File 'lib/coinbase/asset.rb', line 87

def asset_id
  @asset_id
end

#decimalsObject (readonly)

Returns the value of attribute decimals.



87
88
89
# File 'lib/coinbase/asset.rb', line 87

def decimals
  @decimals
end

#networkObject (readonly)

Returns the value of attribute network.



87
88
89
# File 'lib/coinbase/asset.rb', line 87

def network
  @network
end

Class Method Details

.fetch(network, asset_id) ⇒ Coinbase::Asset

Fetches the Asset with the provided Asset ID.

Parameters:

  • network (Coinbase::Network, Symbol)

    The Network or Network ID

  • asset_id (Symbol)

    The Asset ID

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/coinbase/asset.rb', line 54

def fetch(network, asset_id)
  network = Coinbase::Network.from_id(network)

  asset_model = Coinbase.call_api do
    assets_api.get_asset(
      network.normalized_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
44
45
46
47
48
# 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 && asset_model.asset_id
    normalized_asset_id = asset_id.downcase
    normalized_asset_model_id = asset_model.asset_id.downcase

    if Coinbase.to_sym(normalized_asset_id) != Coinbase.to_sym(normalized_asset_model_id)
      case normalized_asset_id
      when :gwei
        decimals = GWEI_DECIMALS
      when :wei
        decimals = 0
      else
        raise ArgumentError, "Unsupported asset ID: #{asset_id}"
      end
    end
  end

  new(
    network: 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.

Parameters:

  • asset_id (Symbol)

    The Asset ID

Returns:

  • (Symbol)

    The primary denomination for the Asset ID



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.

Parameters:

  • atomic_amount (Integer, Float, BigDecimal)

    The atomic amount to convert to whole units.

Returns:

  • (BigDecimal)

    The amount in whole units



92
93
94
# File 'lib/coinbase/asset.rb', line 92

def from_atomic_amount(atomic_amount)
  BigDecimal(atomic_amount) / BigDecimal(10).power(decimals)
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the Balance



120
121
122
# File 'lib/coinbase/asset.rb', line 120

def inspect
  to_s
end

#primary_denominationSymbol

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.

Returns:

  • (Symbol)

    The primary denomination for the Asset



107
108
109
# File 'lib/coinbase/asset.rb', line 107

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.

Parameters:

  • whole_amount (Integer, Float, BigDecimal)

    The whole amount to convert to atomic units.

Returns:

  • (BigDecimal)

    The amount in atomic units



99
100
101
# File 'lib/coinbase/asset.rb', line 99

def to_atomic_amount(whole_amount)
  whole_amount * BigDecimal(10).power(decimals)
end

#to_sString

Returns a string representation of the Asset.

Returns:

  • (String)

    a string representation of the Asset



113
114
115
116
# File 'lib/coinbase/asset.rb', line 113

def to_s
  "Coinbase::Asset{network_id: '#{network.id}', asset_id: '#{asset_id}', decimals: '#{decimals}'" \
    "#{address_id.nil? ? '' : ", address_id: '#{address_id}'"}}"
end