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_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.

Parameters:

  • network_id (Symbol)

    The ID of the Network to which the Asset belongs

  • asset_id (Symbol)

    The Asset ID

  • display_name (String) (defaults to: nil)

    (Optional) The Asset’s display name

  • address_id (String) (defaults to: nil)

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

  • decimals (Integer) (defaults to: nil)

    (Optional) The number of decimal places the Asset uses



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_idObject (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_idObject (readonly)

Returns the value of attribute asset_id.



90
91
92
# File 'lib/coinbase/asset.rb', line 90

def asset_id
  @asset_id
end

#decimalsObject (readonly)

Returns the value of attribute decimals.



90
91
92
# File 'lib/coinbase/asset.rb', line 90

def decimals
  @decimals
end

#display_nameObject (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_idObject (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.

Parameters:

  • atomic_amount (BigDecimal)

    The amount in atomic units

  • asset_id (Symbol)

    The Asset ID

Returns:

  • (BigDecimal)

    The amount in 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.

Parameters:

  • asset_id (Symbol)

    The Asset ID

Returns:

  • (Symbol)

    The primary denomination for the Asset ID



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.

Parameters:

  • asset_id (Symbol)

    The Asset ID

Returns:

  • (Boolean)

    Whether the 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.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount to normalize

  • asset_id (Symbol)

    The ID of the Asset being transferred

Returns:

  • (BigDecimal)

    The normalized amount in atomic units



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

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the Balance



102
103
104
# File 'lib/coinbase/asset.rb', line 102

def inspect
  to_s
end

#to_sString

Returns a string representation of the Asset.

Returns:

  • (String)

    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