Class: Coinbase::StakingReward

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

Overview

A representation of a staking reward earned on a network for a given asset.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, asset, format) ⇒ StakingReward

Returns a new StakingReward object.

Parameters:



30
31
32
33
34
# File 'lib/coinbase/staking_reward.rb', line 30

def initialize(model, asset, format)
  @model = model
  @asset = asset
  @format = format
end

Class Method Details

.list(network_id, asset_id, address_ids, start_time: DateTime.now.prev_month(1), end_time: DateTime.now, format: :usd) ⇒ Enumerable<Coinbase::StakingReward>

Returns a list of StakingRewards for the provided network, asset, and addresses.

Parameters:

  • network_id (Symbol)

    The network ID

  • asset_id (Symbol)

    The asset ID

  • address_ids (Array<String>)

    The address IDs

  • start_time (Time) (defaults to: DateTime.now.prev_month(1))

    The start time. Defaults to one month ago.

  • end_time (Time) (defaults to: DateTime.now)

    The end time. Defaults to the current time.

  • format (Symbol) (defaults to: :usd)

    The format to return the rewards in. (:usd, :native) Defaults to :usd.

Returns:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/coinbase/staking_reward.rb', line 16

def self.list(network_id, asset_id, address_ids, start_time: DateTime.now.prev_month(1), end_time: DateTime.now,
              format: :usd)
  asset = Coinbase.call_api do
    Asset.fetch(network_id, asset_id)
  end
  Coinbase::Pagination.enumerate(
    ->(page) { list_page(network_id, asset_id, address_ids, start_time, end_time, page, format) }
  ) do |staking_reward|
    new(staking_reward, asset, format)
  end
end

.list_page(network_id, asset_id, address_ids, start_time, end_time, page, format) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/coinbase/staking_reward.rb', line 90

def self.list_page(network_id, asset_id, address_ids, start_time, end_time, page, format)
  req = {
    network_id: Coinbase.normalize_network(network_id),
    asset_id: asset_id,
    address_ids: address_ids,
    start_time: start_time.iso8601,
    end_time: end_time.iso8601,
    format: format,
    next_page: page
  }
  stake_api.fetch_staking_rewards(req)
end

.stake_apiObject



86
87
88
# File 'lib/coinbase/staking_reward.rb', line 86

def self.stake_api
  Coinbase::Client::StakeApi.new(Coinbase.configuration.api_client)
end

Instance Method Details

#address_idTime

Returns the onchain address of the StakingReward.

Returns:

  • (Time)

    The onchain address



52
53
54
# File 'lib/coinbase/staking_reward.rb', line 52

def address_id
  @model.address_id
end

#amountBigDecimal

Returns the amount of the StakingReward.

Returns:

  • (BigDecimal)

    The amount



38
39
40
41
42
# File 'lib/coinbase/staking_reward.rb', line 38

def amount
  return BigDecimal(@model.amount.to_i) / BigDecimal(100) if @format == :usd

  @asset.from_atomic_amount(@model.amount.to_i)
end

#dateTime

Returns the date of the StakingReward.

Returns:

  • (Time)

    The date



46
47
48
# File 'lib/coinbase/staking_reward.rb', line 46

def date
  @model.date
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the StakingReward



82
83
84
# File 'lib/coinbase/staking_reward.rb', line 82

def inspect
  to_s
end

#to_sString

Returns a string representation of the StakingReward.

Returns:

  • (String)

    a string representation of the StakingReward



76
77
78
# File 'lib/coinbase/staking_reward.rb', line 76

def to_s
  "Coinbase::StakingReward{date: '#{date}' address_id: '#{address_id}' amount: '#{amount.to_f}'}"
end

#usd_conversion_priceBigDecimal

Returns the USD conversion price of the StakingReward.

Returns:

  • (BigDecimal)

    The USD conversion price



64
65
66
# File 'lib/coinbase/staking_reward.rb', line 64

def usd_conversion_price
  BigDecimal(@model.usd_value.conversion_price.to_i)
end

#usd_conversion_timeTime

Returns the USD conversion time of the StakingReward.

Returns:

  • (Time)

    The USD conversion time



70
71
72
# File 'lib/coinbase/staking_reward.rb', line 70

def usd_conversion_time
  @model.usd_value.conversion_time
end

#usd_valueBigDecimal

Returns the USD value of the StakingReward.

Returns:

  • (BigDecimal)

    The USD value



58
59
60
# File 'lib/coinbase/staking_reward.rb', line 58

def usd_value
  BigDecimal(@model.usd_value.amount.to_i) / BigDecimal(100)
end