Class: Coinbase::Trade
- Inherits:
-
Object
- Object
- Coinbase::Trade
- 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
-
#address_id ⇒ String
Returns the Address ID of the Trade.
- #approve_transaction ⇒ Object
-
#from_amount ⇒ BigDecimal
Returns the amount of the from asset for the Trade.
-
#from_asset_id ⇒ Symbol
Returns the From Asset ID of the Trade.
-
#id ⇒ String
Returns the Trade ID.
-
#initialize(model) ⇒ Trade
constructor
Returns a new Trade object.
-
#inspect ⇒ String
Same as to_s.
-
#network_id ⇒ Symbol
Returns the Network ID of the Trade.
-
#reload ⇒ Trade
Reloads the Trade model with the latest version from the server side.
-
#status ⇒ Symbol
Returns the status of the Trade.
-
#to_amount ⇒ BigDecimal
Returns the amount of the to asset for the Trade.
-
#to_asset_id ⇒ Symbol
Returns the To Asset ID of the Trade.
-
#to_s ⇒ String
Returns a String representation of the Trade.
-
#transaction ⇒ Coinbase::Transaction
Returns the Trade transaction.
-
#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.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Trade.
Constructor Details
#initialize(model) ⇒ Trade
Returns a new Trade object. Do not use this method directly. Instead, use Wallet#trade or Address#trade.
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_id ⇒ String
Returns the Address ID of the Trade.
41 42 43 |
# File 'lib/coinbase/trade.rb', line 41 def address_id @model.address_id end |
#approve_transaction ⇒ Object
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_amount ⇒ BigDecimal
Returns the amount of the from asset for the Trade.
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_id ⇒ Symbol
Returns the From Asset ID of the Trade.
47 48 49 |
# File 'lib/coinbase/trade.rb', line 47 def from_asset_id @model.from_asset.asset_id.to_sym end |
#id ⇒ String
Returns the Trade ID.
23 24 25 |
# File 'lib/coinbase/trade.rb', line 23 def id @model.trade_id end |
#inspect ⇒ String
Same as to_s.
137 138 139 |
# File 'lib/coinbase/trade.rb', line 137 def inspect to_s end |
#network_id ⇒ Symbol
Returns the Network ID of the Trade.
29 30 31 |
# File 'lib/coinbase/trade.rb', line 29 def network_id Coinbase.to_sym(@model.network_id) end |
#reload ⇒ Trade
Reloads the Trade model with the latest version from the server side.
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 |
#status ⇒ Symbol
Returns the status of the Trade.
81 82 83 |
# File 'lib/coinbase/trade.rb', line 81 def status transaction.status end |
#to_amount ⇒ BigDecimal
Returns the amount of the to asset for the Trade.
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_id ⇒ Symbol
Returns the To Asset ID of the Trade.
59 60 61 |
# File 'lib/coinbase/trade.rb', line 59 def to_asset_id @model.to_asset.asset_id.to_sym end |
#to_s ⇒ String
Returns 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 |
#transaction ⇒ Coinbase::Transaction
Returns the Trade transaction.
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.
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_id ⇒ String
Returns the Wallet ID of the Trade.
35 36 37 |
# File 'lib/coinbase/trade.rb', line 35 def wallet_id @model.wallet_id end |