Class: Coinbase::StakingOperation

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

Overview

A representation of a staking operation (stake, unstake, claim rewards, etc). It may have multiple steps with some being transactions to sign, and others to wait.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ StakingOperation

Returns a new StakingOperation object.

Parameters:



14
15
16
# File 'lib/coinbase/staking_operation.rb', line 14

def initialize(model)
  from_model(model)
end

Instance Attribute Details

#statusSymbol (readonly)

Returns the status of the Staking Operation.

Returns:

  • (Symbol)

    The status



9
10
11
# File 'lib/coinbase/staking_operation.rb', line 9

def status
  @status
end

#transactionsArray<Coinbase::Transaction> (readonly)

The list of current transactions associated with the operation.

Returns:



9
10
11
# File 'lib/coinbase/staking_operation.rb', line 9

def transactions
  @transactions
end

Class Method Details

.fetch(network_id, address_id, id) ⇒ Coinbase::StakingOperation

Fetch the StakingOperation with the provided network, address and staking operation ID.

Parameters:

  • network_id (Symbol)

    The Network ID

  • address_id (Symbol)

    The Address ID

  • id (String)

    The ID of the StakingOperation

Returns:



70
71
72
73
74
75
76
# File 'lib/coinbase/staking_operation.rb', line 70

def self.fetch(network_id, address_id, id)
  staking_operation_model = Coinbase.call_api do
    stake_api.get_external_staking_operation(network_id, address_id, id)
  end

  from_model(staking_operation_model)
end

Instance Method Details

#address_idString

Returns the Address ID of the Staking Operation.

Returns:

  • (String)

    The Address ID



32
33
34
# File 'lib/coinbase/staking_operation.rb', line 32

def address_id
  @model.address_id
end

#idString

Returns the Staking Operation ID.

Returns:

  • (String)

    The Staking Operation ID



20
21
22
# File 'lib/coinbase/staking_operation.rb', line 20

def id
  @model.id
end

#network_idSymbol

Returns the Network ID of the Staking Operation.

Returns:

  • (Symbol)

    The Network ID



26
27
28
# File 'lib/coinbase/staking_operation.rb', line 26

def network_id
  Coinbase.to_sym(@model.network_id)
end

#reloadCoinbase::StakingOperation

Reloads the staking_operation from the service

Returns:



88
89
90
91
92
93
94
# File 'lib/coinbase/staking_operation.rb', line 88

def reload
  @model = Coinbase.call_api do
    stake_api.get_external_staking_operation(network_id, address_id, id)
  end

  from_model(@model)
end

#sign(key) ⇒ Object

Signs the Open Transactions with the provided key

Parameters:

  • key (Eth::Key)

    The key to sign the transactions with



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

def sign(key)
  transactions.each do |transaction|
    transaction.sign(key) unless transaction.signed?
  end
end

#signed_voluntary_exit_messagesArray<string>

Fetches the presigned_voluntary exit messages for the staking operation

Returns:

  • (Array<string>)

    The list of presigned exit transaction messages



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/coinbase/staking_operation.rb', line 98

def signed_voluntary_exit_messages
  return [] unless @model.

  signed_voluntary_exit_messages = []

  @model..each do ||
    decoded_string = Base64.decode64(.signed_voluntary_exit)
    signed_voluntary_exit_messages.push(decoded_string)
  end

  signed_voluntary_exit_messages
end

#wait!(interval_seconds = 5, timeout_seconds = 3600) ⇒ StakingOperation

Waits until the Staking Operation is completed or failed by polling its status at the given interval. Raises a Timeout::Error if the Staking Operation takes longer than the given timeout.

Parameters:

  • interval_seconds (Integer) (defaults to: 5)

    The interval at which to poll, in seconds

  • timeout_seconds (Integer) (defaults to: 3600)

    The maximum amount of time to wait for the StakingOperation to complete, in seconds

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/coinbase/staking_operation.rb', line 48

def wait!(interval_seconds = 5, timeout_seconds = 3600)
  start_time = Time.now

  loop do
    reload

    # Wait for the Staking Operation to be in a terminal state.
    break if status == 'complete'

    raise Timeout::Error, 'Staking Operation timed out' if Time.now - start_time > timeout_seconds

    self.sleep interval_seconds
  end

  self
end