Class: Coinbase::Balance

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

Overview

A representation of an Balance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, asset_id:) ⇒ Balance

Returns a new Asset object. Do not use this method. Instead, use the Asset constants defined in the Coinbase module.

Parameters:

  • The ID of the Network to which the Asset belongs

  • The Asset ID

  • The Asset’s display name

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



34
35
36
37
# File 'lib/coinbase/balance.rb', line 34

def initialize(amount:, asset_id:)
  @amount = amount
  @asset_id = asset_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



39
40
41
# File 'lib/coinbase/balance.rb', line 39

def amount
  @amount
end

#asset_idObject (readonly)

Returns the value of attribute asset_id.



39
40
41
# File 'lib/coinbase/balance.rb', line 39

def asset_id
  @asset_id
end

Class Method Details

.from_model(balance_model) ⇒ Balance

Converts a Coinbase::Client::Balance model to a Coinbase::Balance

Parameters:

  • The balance fetched from the API.

Returns:

  • The converted Balance object.



9
10
11
12
13
# File 'lib/coinbase/balance.rb', line 9

def self.from_model(balance_model)
  asset_id = Coinbase.to_sym(balance_model.asset.asset_id.downcase)

  from_model_and_asset_id(balance_model, asset_id)
end

.from_model_and_asset_id(balance_model, asset_id) ⇒ Balance

Converts a Coinbase::Client::Balance model and asset ID to a Coinbase::Balance This can be used to specify a non-primary denomination that we want the balance to be converted to.

Parameters:

  • The balance fetched from the API.

  • The Asset ID of the denomination we want returned.

Returns:

  • The converted Balance object.



21
22
23
24
25
26
# File 'lib/coinbase/balance.rb', line 21

def self.from_model_and_asset_id(balance_model, asset_id)
  new(
    amount: Coinbase::Asset.from_atomic_amount(BigDecimal(balance_model.amount), asset_id),
    asset_id: asset_id
  )
end

Instance Method Details

#inspectString

Same as to_s.

Returns:

  • a string representation of the Balance



49
50
51
# File 'lib/coinbase/balance.rb', line 49

def inspect
  to_s
end

#to_sString

Returns a string representation of the Balance.

Returns:

  • a string representation of the Balance



43
44
45
# File 'lib/coinbase/balance.rb', line 43

def to_s
  "Coinbase::Balance{amount: '#{amount.to_i}', asset_id: '#{asset_id}'}"
end