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.
Class Method Summary collapse
-
.create(address_id:, from_asset_id:, to_asset_id:, amount:, network_id:, wallet_id:) ⇒ Send
Creates a new Trade object.
-
.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::Trade>
Enumerates the trades for a given address belonging to a wallet.
Instance Method Summary collapse
-
#address_id ⇒ String
Returns the Address ID of the Trade.
- #approve_transaction ⇒ Object
-
#broadcast! ⇒ Trade
Broadcasts the Trade to the Network.
-
#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.
-
#transactions ⇒ Object
Returns the list of Transactions for the Trade.
-
#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.
66 67 68 69 70 |
# File 'lib/coinbase/trade.rb', line 66 def initialize(model) raise unless model.is_a?(Coinbase::Client::Trade) @model = model end |
Class Method Details
.create(address_id:, from_asset_id:, to_asset_id:, amount:, network_id:, wallet_id:) ⇒ Send
Creates a new Trade object.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/coinbase/trade.rb', line 21 def create(address_id:, from_asset_id:, to_asset_id:, amount:, network_id:, wallet_id:) from_asset = Asset.fetch(network_id, from_asset_id) to_asset = Asset.fetch(network_id, to_asset_id) model = Coinbase.call_api do trades_api.create_trade( wallet_id, address_id, { amount: from_asset.to_atomic_amount(amount).to_i.to_s, from_asset_id: from_asset.primary_denomination.to_s, to_asset_id: to_asset.primary_denomination.to_s } ) end new(model) end |
.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::Trade>
Enumerates the trades for a given address belonging to a wallet. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…
44 45 46 47 48 49 50 |
# File 'lib/coinbase/trade.rb', line 44 def list(wallet_id:, address_id:) Coinbase::Pagination.enumerate( ->(page) { fetch_page(wallet_id, address_id, page) } ) do |trade| new(trade) end end |
Instance Method Details
#address_id ⇒ String
Returns the Address ID of the Trade.
92 93 94 |
# File 'lib/coinbase/trade.rb', line 92 def address_id @model.address_id end |
#approve_transaction ⇒ Object
126 127 128 |
# File 'lib/coinbase/trade.rb', line 126 def approve_transaction @approve_transaction ||= @model.approve_transaction ? Coinbase::Transaction.new(@model.approve_transaction) : nil end |
#broadcast! ⇒ Trade
Broadcasts the Trade to the Network. This raises an error if the Trade is not signed.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/coinbase/trade.rb', line 145 def broadcast! raise TransactionNotSignedError unless transactions.all?(&:signed?) payloads = { signed_payload: transaction.raw.hex } payloads[:approve_tx_signed_payload] = approve_transaction.raw.hex unless approve_transaction.nil? @model = Coinbase.call_api do trades_api.broadcast_trade(wallet_id, address_id, id, payloads) end update_transactions(@model) self end |
#from_amount ⇒ BigDecimal
Returns the amount of the from asset for the Trade.
104 105 106 |
# File 'lib/coinbase/trade.rb', line 104 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.
98 99 100 |
# File 'lib/coinbase/trade.rb', line 98 def from_asset_id @model.from_asset.asset_id.to_sym end |
#id ⇒ String
Returns the Trade ID.
74 75 76 |
# File 'lib/coinbase/trade.rb', line 74 def id @model.trade_id end |
#inspect ⇒ String
Same as to_s.
209 210 211 |
# File 'lib/coinbase/trade.rb', line 209 def inspect to_s end |
#network_id ⇒ Symbol
Returns the Network ID of the Trade.
80 81 82 |
# File 'lib/coinbase/trade.rb', line 80 def network_id Coinbase.to_sym(@model.network_id) end |
#reload ⇒ Trade
Reloads the Trade model with the latest version from the server side.
188 189 190 191 192 193 194 195 196 |
# File 'lib/coinbase/trade.rb', line 188 def reload @model = Coinbase.call_api do trades_api.get_trade(wallet_id, address_id, id) end update_transactions(@model) self end |
#status ⇒ Symbol
Returns the status of the Trade.
137 138 139 |
# File 'lib/coinbase/trade.rb', line 137 def status transaction.status end |
#to_amount ⇒ BigDecimal
Returns the amount of the to asset for the Trade.
116 117 118 |
# File 'lib/coinbase/trade.rb', line 116 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.
110 111 112 |
# File 'lib/coinbase/trade.rb', line 110 def to_asset_id @model.to_asset.asset_id.to_sym end |
#to_s ⇒ String
Returns a String representation of the Trade.
200 201 202 203 204 205 |
# File 'lib/coinbase/trade.rb', line 200 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.
122 123 124 |
# File 'lib/coinbase/trade.rb', line 122 def transaction @transaction ||= Coinbase::Transaction.new(@model.transaction) end |
#transactions ⇒ Object
Returns the list of Transactions for the Trade.
131 132 133 |
# File 'lib/coinbase/trade.rb', line 131 def transactions [approve_transaction, transaction].compact 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/coinbase/trade.rb', line 166 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.
86 87 88 |
# File 'lib/coinbase/trade.rb', line 86 def wallet_id @model.wallet_id end |