Class: Coinbase::Trade

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

Overview

A representation of a Trade, which trades an amount of an Asset to another Asset on a Network. The fee is assumed to be paid in the native Asset of the Network. Trades should be created through Wallet#trade or # Address#trade.

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Trade

Returns a new Trade object. Do not use this method directly. Instead, use Wallet#trade or Address#trade.

Parameters:



15
16
17
18
19
# File 'lib/coinbase/trade.rb', line 15

def initialize(model)
  raise unless model.is_a?(Coinbase::Client::Trade)

  @model = model
end

Instance Method Details

#address_idString

Returns the Address ID of the Trade.

Returns:

  • (String)

    The Address ID



41
42
43
# File 'lib/coinbase/trade.rb', line 41

def address_id
  @model.address_id
end

#approve_transactionObject



75
76
77
# File 'lib/coinbase/trade.rb', line 75

def approve_transaction
  @approve_transaction ||= @model.approve_transaction ? Coinbase::Transaction.new(@model.approve_transaction) : nil
end

#from_amountBigDecimal

Returns the amount of the from asset for the Trade.

Returns:

  • (BigDecimal)

    The amount of the from asset



53
54
55
# File 'lib/coinbase/trade.rb', line 53

def from_amount
  BigDecimal(@model.from_amount) / BigDecimal(10).power(@model.from_asset.decimals)
end

#from_asset_idSymbol

Returns the From Asset ID of the Trade.

Returns:

  • (Symbol)

    The From Asset ID



47
48
49
# File 'lib/coinbase/trade.rb', line 47

def from_asset_id
  @model.from_asset.asset_id.to_sym
end

#idString

Returns the Trade ID.

Returns:

  • (String)

    The Trade ID



23
24
25
# File 'lib/coinbase/trade.rb', line 23

def id
  @model.trade_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Trade



137
138
139
# File 'lib/coinbase/trade.rb', line 137

def inspect
  to_s
end

#network_idSymbol

Returns the Network ID of the Trade.

Returns:

  • (Symbol)

    The Network ID



29
30
31
# File 'lib/coinbase/trade.rb', line 29

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

#reloadTrade

Reloads the Trade model with the latest version from the server side.

Returns:

  • (Trade)

    The most recent version of Trade from the server.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/coinbase/trade.rb', line 112

def reload
  @model = Coinbase.call_api do
    trades_api.get_trade(wallet_id, address_id, id)
  end

  # Update the memoized transaction.
  @transaction = Coinbase::Transaction.new(@model.transaction)

  # Update the memoized approve transaction if it exists.
  @approve_transaction = @model.approve_transaction ? Coinbase::Transaction.new(@model.approve_transaction) : nil

  self
end

#statusSymbol

Returns the status of the Trade.

Returns:

  • (Symbol)

    The status



81
82
83
# File 'lib/coinbase/trade.rb', line 81

def status
  transaction.status
end

#to_amountBigDecimal

Returns the amount of the to asset for the Trade.

Returns:

  • (BigDecimal)

    The amount of the to asset



65
66
67
# File 'lib/coinbase/trade.rb', line 65

def to_amount
  BigDecimal(@model.to_amount) / BigDecimal(10).power(@model.to_asset.decimals)
end

#to_asset_idSymbol

Returns the To Asset ID of the Trade.

Returns:

  • (Symbol)

    The To Asset ID



59
60
61
# File 'lib/coinbase/trade.rb', line 59

def to_asset_id
  @model.to_asset.asset_id.to_sym
end

#to_sString

Returns a String representation of the Trade.

Returns:

  • (String)

    a String representation of the Trade



128
129
130
131
132
133
# File 'lib/coinbase/trade.rb', line 128

def to_s
  "Coinbase::Trade{transfer_id: '#{id}', network_id: '#{network_id}', " \
    "address_id: '#{address_id}', from_asset_id: '#{from_asset_id}', " \
    "to_asset_id: '#{to_asset_id}', from_amount: '#{from_amount}', " \
    "to_amount: '#{to_amount}' status: '#{status}'}"
end

#transactionCoinbase::Transaction

Returns the Trade transaction.

Returns:



71
72
73
# File 'lib/coinbase/trade.rb', line 71

def transaction
  @transaction ||= Coinbase::Transaction.new(@model.transaction)
end

#wait!(interval_seconds = 0.2, timeout_seconds = 10) ⇒ Trade

Waits until the Trade is completed or failed by polling the Network at the given interval. Raises a Timeout::Error if the Trade takes longer than the given timeout.

Parameters:

  • interval_seconds (Integer) (defaults to: 0.2)

    The interval at which to poll the Network, in seconds

  • timeout_seconds (Integer) (defaults to: 10)

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

Returns:

  • (Trade)

    The completed Trade object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/coinbase/trade.rb', line 90

def wait!(interval_seconds = 0.2, timeout_seconds = 10)
  start_time = Time.now

  loop do
    reload

    # Wait for the trade transaction to be in a terminal state.
    # The approve transaction is optional and must last first, so we don't need to wait for it.
    # We may want to handle a situation where the approve transaction fails and the
    # trade transaction does not ever get broadcast.
    break if transaction.terminal_state?

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

    self.sleep interval_seconds
  end

  self
end

#wallet_idString

Returns the Wallet ID of the Trade.

Returns:

  • (String)

    The Wallet ID



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

def wallet_id
  @model.wallet_id
end