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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coinbase/staking_reward.rb', line 66

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



62
63
64
# File 'lib/coinbase/staking_reward.rb', line 62

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

Instance Method Details

#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



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

def inspect
  to_s
end

#to_sString

Returns a string representation of the StakingReward.

Returns:

  • (String)

    a string representation of the StakingReward



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

def to_s
  "Coinbase::StakingReward{amount: '#{amount}'}"
end