coinbase-sdk 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,6 +40,8 @@ module Coinbase::Client
40
40
 
41
41
  attr_accessor :transaction
42
42
 
43
+ attr_accessor :approve_transaction
44
+
43
45
  # Attribute mapping from ruby-style variable name to JSON key.
44
46
  def self.attribute_map
45
47
  {
@@ -51,7 +53,8 @@ module Coinbase::Client
51
53
  :'from_asset' => :'from_asset',
52
54
  :'to_amount' => :'to_amount',
53
55
  :'to_asset' => :'to_asset',
54
- :'transaction' => :'transaction'
56
+ :'transaction' => :'transaction',
57
+ :'approve_transaction' => :'approve_transaction'
55
58
  }
56
59
  end
57
60
 
@@ -71,7 +74,8 @@ module Coinbase::Client
71
74
  :'from_asset' => :'Asset',
72
75
  :'to_amount' => :'String',
73
76
  :'to_asset' => :'Asset',
74
- :'transaction' => :'Transaction'
77
+ :'transaction' => :'Transaction',
78
+ :'approve_transaction' => :'Transaction'
75
79
  }
76
80
  end
77
81
 
@@ -149,6 +153,10 @@ module Coinbase::Client
149
153
  else
150
154
  self.transaction = nil
151
155
  end
156
+
157
+ if attributes.key?(:'approve_transaction')
158
+ self.approve_transaction = attributes[:'approve_transaction']
159
+ end
152
160
  end
153
161
 
154
162
  # Show invalid properties with the reasons. Usually used together with valid?
@@ -224,7 +232,8 @@ module Coinbase::Client
224
232
  from_asset == o.from_asset &&
225
233
  to_amount == o.to_amount &&
226
234
  to_asset == o.to_asset &&
227
- transaction == o.transaction
235
+ transaction == o.transaction &&
236
+ approve_transaction == o.approve_transaction
228
237
  end
229
238
 
230
239
  # @see the `==` method
@@ -236,7 +245,7 @@ module Coinbase::Client
236
245
  # Calculates hash code according to all attributes.
237
246
  # @return [Integer] Hash code
238
247
  def hash
239
- [network_id, wallet_id, address_id, trade_id, from_amount, from_asset, to_amount, to_asset, transaction].hash
248
+ [network_id, wallet_id, address_id, trade_id, from_amount, from_asset, to_amount, to_asset, transaction, approve_transaction].hash
240
249
  end
241
250
 
242
251
  # Builds the object from hash
@@ -16,6 +16,12 @@ require 'time'
16
16
  module Coinbase::Client
17
17
  # An onchain transaction.
18
18
  class Transaction
19
+ # The ID of the blockchain network
20
+ attr_accessor :network_id
21
+
22
+ # The onchain address of the sender
23
+ attr_accessor :from_address_id
24
+
19
25
  # The unsigned payload of the transaction. This is the payload that needs to be signed by the sender.
20
26
  attr_accessor :unsigned_payload
21
27
 
@@ -25,6 +31,9 @@ module Coinbase::Client
25
31
  # The hash of the transaction
26
32
  attr_accessor :transaction_hash
27
33
 
34
+ # The link to view the transaction on a block explorer. This is optional and may not be present for all transactions.
35
+ attr_accessor :transaction_link
36
+
28
37
  # The status of the transaction
29
38
  attr_accessor :status
30
39
 
@@ -53,9 +62,12 @@ module Coinbase::Client
53
62
  # Attribute mapping from ruby-style variable name to JSON key.
54
63
  def self.attribute_map
55
64
  {
65
+ :'network_id' => :'network_id',
66
+ :'from_address_id' => :'from_address_id',
56
67
  :'unsigned_payload' => :'unsigned_payload',
57
68
  :'signed_payload' => :'signed_payload',
58
69
  :'transaction_hash' => :'transaction_hash',
70
+ :'transaction_link' => :'transaction_link',
59
71
  :'status' => :'status'
60
72
  }
61
73
  end
@@ -68,9 +80,12 @@ module Coinbase::Client
68
80
  # Attribute type mapping.
69
81
  def self.openapi_types
70
82
  {
83
+ :'network_id' => :'String',
84
+ :'from_address_id' => :'String',
71
85
  :'unsigned_payload' => :'String',
72
86
  :'signed_payload' => :'String',
73
87
  :'transaction_hash' => :'String',
88
+ :'transaction_link' => :'String',
74
89
  :'status' => :'String'
75
90
  }
76
91
  end
@@ -96,6 +111,18 @@ module Coinbase::Client
96
111
  h[k.to_sym] = v
97
112
  }
98
113
 
114
+ if attributes.key?(:'network_id')
115
+ self.network_id = attributes[:'network_id']
116
+ else
117
+ self.network_id = nil
118
+ end
119
+
120
+ if attributes.key?(:'from_address_id')
121
+ self.from_address_id = attributes[:'from_address_id']
122
+ else
123
+ self.from_address_id = nil
124
+ end
125
+
99
126
  if attributes.key?(:'unsigned_payload')
100
127
  self.unsigned_payload = attributes[:'unsigned_payload']
101
128
  else
@@ -110,6 +137,10 @@ module Coinbase::Client
110
137
  self.transaction_hash = attributes[:'transaction_hash']
111
138
  end
112
139
 
140
+ if attributes.key?(:'transaction_link')
141
+ self.transaction_link = attributes[:'transaction_link']
142
+ end
143
+
113
144
  if attributes.key?(:'status')
114
145
  self.status = attributes[:'status']
115
146
  else
@@ -122,6 +153,14 @@ module Coinbase::Client
122
153
  def list_invalid_properties
123
154
  warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
124
155
  invalid_properties = Array.new
156
+ if @network_id.nil?
157
+ invalid_properties.push('invalid value for "network_id", network_id cannot be nil.')
158
+ end
159
+
160
+ if @from_address_id.nil?
161
+ invalid_properties.push('invalid value for "from_address_id", from_address_id cannot be nil.')
162
+ end
163
+
125
164
  if @unsigned_payload.nil?
126
165
  invalid_properties.push('invalid value for "unsigned_payload", unsigned_payload cannot be nil.')
127
166
  end
@@ -137,6 +176,8 @@ module Coinbase::Client
137
176
  # @return true if the model is valid
138
177
  def valid?
139
178
  warn '[DEPRECATED] the `valid?` method is obsolete'
179
+ return false if @network_id.nil?
180
+ return false if @from_address_id.nil?
140
181
  return false if @unsigned_payload.nil?
141
182
  return false if @status.nil?
142
183
  status_validator = EnumAttributeValidator.new('String', ["pending", "broadcast", "complete", "failed"])
@@ -159,9 +200,12 @@ module Coinbase::Client
159
200
  def ==(o)
160
201
  return true if self.equal?(o)
161
202
  self.class == o.class &&
203
+ network_id == o.network_id &&
204
+ from_address_id == o.from_address_id &&
162
205
  unsigned_payload == o.unsigned_payload &&
163
206
  signed_payload == o.signed_payload &&
164
207
  transaction_hash == o.transaction_hash &&
208
+ transaction_link == o.transaction_link &&
165
209
  status == o.status
166
210
  end
167
211
 
@@ -174,7 +218,7 @@ module Coinbase::Client
174
218
  # Calculates hash code according to all attributes.
175
219
  # @return [Integer] Hash code
176
220
  def hash
177
- [unsigned_payload, signed_payload, transaction_hash, status].hash
221
+ [network_id, from_address_id, unsigned_payload, signed_payload, transaction_hash, transaction_link, status].hash
178
222
  end
179
223
 
180
224
  # Builds the object from hash
@@ -34,9 +34,13 @@ module Coinbase::Client
34
34
  # The ID of the asset being transferred
35
35
  attr_accessor :asset_id
36
36
 
37
+ attr_accessor :asset
38
+
37
39
  # The ID of the transfer
38
40
  attr_accessor :transfer_id
39
41
 
42
+ attr_accessor :transaction
43
+
40
44
  # The unsigned payload of the transfer. This is the payload that needs to be signed by the sender.
41
45
  attr_accessor :unsigned_payload
42
46
 
@@ -80,7 +84,9 @@ module Coinbase::Client
80
84
  :'destination' => :'destination',
81
85
  :'amount' => :'amount',
82
86
  :'asset_id' => :'asset_id',
87
+ :'asset' => :'asset',
83
88
  :'transfer_id' => :'transfer_id',
89
+ :'transaction' => :'transaction',
84
90
  :'unsigned_payload' => :'unsigned_payload',
85
91
  :'signed_payload' => :'signed_payload',
86
92
  :'transaction_hash' => :'transaction_hash',
@@ -102,7 +108,9 @@ module Coinbase::Client
102
108
  :'destination' => :'String',
103
109
  :'amount' => :'String',
104
110
  :'asset_id' => :'String',
111
+ :'asset' => :'Asset',
105
112
  :'transfer_id' => :'String',
113
+ :'transaction' => :'Transaction',
106
114
  :'unsigned_payload' => :'String',
107
115
  :'signed_payload' => :'String',
108
116
  :'transaction_hash' => :'String',
@@ -167,12 +175,24 @@ module Coinbase::Client
167
175
  self.asset_id = nil
168
176
  end
169
177
 
178
+ if attributes.key?(:'asset')
179
+ self.asset = attributes[:'asset']
180
+ else
181
+ self.asset = nil
182
+ end
183
+
170
184
  if attributes.key?(:'transfer_id')
171
185
  self.transfer_id = attributes[:'transfer_id']
172
186
  else
173
187
  self.transfer_id = nil
174
188
  end
175
189
 
190
+ if attributes.key?(:'transaction')
191
+ self.transaction = attributes[:'transaction']
192
+ else
193
+ self.transaction = nil
194
+ end
195
+
176
196
  if attributes.key?(:'unsigned_payload')
177
197
  self.unsigned_payload = attributes[:'unsigned_payload']
178
198
  else
@@ -223,10 +243,18 @@ module Coinbase::Client
223
243
  invalid_properties.push('invalid value for "asset_id", asset_id cannot be nil.')
224
244
  end
225
245
 
246
+ if @asset.nil?
247
+ invalid_properties.push('invalid value for "asset", asset cannot be nil.')
248
+ end
249
+
226
250
  if @transfer_id.nil?
227
251
  invalid_properties.push('invalid value for "transfer_id", transfer_id cannot be nil.')
228
252
  end
229
253
 
254
+ if @transaction.nil?
255
+ invalid_properties.push('invalid value for "transaction", transaction cannot be nil.')
256
+ end
257
+
230
258
  if @unsigned_payload.nil?
231
259
  invalid_properties.push('invalid value for "unsigned_payload", unsigned_payload cannot be nil.')
232
260
  end
@@ -248,7 +276,9 @@ module Coinbase::Client
248
276
  return false if @destination.nil?
249
277
  return false if @amount.nil?
250
278
  return false if @asset_id.nil?
279
+ return false if @asset.nil?
251
280
  return false if @transfer_id.nil?
281
+ return false if @transaction.nil?
252
282
  return false if @unsigned_payload.nil?
253
283
  return false if @status.nil?
254
284
  status_validator = EnumAttributeValidator.new('String', ["pending", "broadcast", "complete", "failed"])
@@ -277,7 +307,9 @@ module Coinbase::Client
277
307
  destination == o.destination &&
278
308
  amount == o.amount &&
279
309
  asset_id == o.asset_id &&
310
+ asset == o.asset &&
280
311
  transfer_id == o.transfer_id &&
312
+ transaction == o.transaction &&
281
313
  unsigned_payload == o.unsigned_payload &&
282
314
  signed_payload == o.signed_payload &&
283
315
  transaction_hash == o.transaction_hash &&
@@ -293,7 +325,7 @@ module Coinbase::Client
293
325
  # Calculates hash code according to all attributes.
294
326
  # @return [Integer] Hash code
295
327
  def hash
296
- [network_id, wallet_id, address_id, destination, amount, asset_id, transfer_id, unsigned_payload, signed_payload, transaction_hash, status].hash
328
+ [network_id, wallet_id, address_id, destination, amount, asset_id, asset, transfer_id, transaction, unsigned_payload, signed_payload, transaction_hash, status].hash
297
329
  end
298
330
 
299
331
  # Builds the object from hash
@@ -23,6 +23,9 @@ module Coinbase::Client
23
23
 
24
24
  attr_accessor :default_address
25
25
 
26
+ # The features enabled for the wallet
27
+ attr_accessor :enabled_features
28
+
26
29
  # The status of the Server-Signer for the wallet if present.
27
30
  attr_accessor :server_signer_status
28
31
 
@@ -54,6 +57,7 @@ module Coinbase::Client
54
57
  :'id' => :'id',
55
58
  :'network_id' => :'network_id',
56
59
  :'default_address' => :'default_address',
60
+ :'enabled_features' => :'enabled_features',
57
61
  :'server_signer_status' => :'server_signer_status'
58
62
  }
59
63
  end
@@ -69,6 +73,7 @@ module Coinbase::Client
69
73
  :'id' => :'String',
70
74
  :'network_id' => :'String',
71
75
  :'default_address' => :'Address',
76
+ :'enabled_features' => :'Array<Feature>',
72
77
  :'server_signer_status' => :'String'
73
78
  }
74
79
  end
@@ -110,6 +115,14 @@ module Coinbase::Client
110
115
  self.default_address = attributes[:'default_address']
111
116
  end
112
117
 
118
+ if attributes.key?(:'enabled_features')
119
+ if (value = attributes[:'enabled_features']).is_a?(Array)
120
+ self.enabled_features = value
121
+ end
122
+ else
123
+ self.enabled_features = nil
124
+ end
125
+
113
126
  if attributes.key?(:'server_signer_status')
114
127
  self.server_signer_status = attributes[:'server_signer_status']
115
128
  end
@@ -128,6 +141,10 @@ module Coinbase::Client
128
141
  invalid_properties.push('invalid value for "network_id", network_id cannot be nil.')
129
142
  end
130
143
 
144
+ if @enabled_features.nil?
145
+ invalid_properties.push('invalid value for "enabled_features", enabled_features cannot be nil.')
146
+ end
147
+
131
148
  invalid_properties
132
149
  end
133
150
 
@@ -137,6 +154,7 @@ module Coinbase::Client
137
154
  warn '[DEPRECATED] the `valid?` method is obsolete'
138
155
  return false if @id.nil?
139
156
  return false if @network_id.nil?
157
+ return false if @enabled_features.nil?
140
158
  server_signer_status_validator = EnumAttributeValidator.new('String', ["pending_seed_creation", "active_seed"])
141
159
  return false unless server_signer_status_validator.valid?(@server_signer_status)
142
160
  true
@@ -160,6 +178,7 @@ module Coinbase::Client
160
178
  id == o.id &&
161
179
  network_id == o.network_id &&
162
180
  default_address == o.default_address &&
181
+ enabled_features == o.enabled_features &&
163
182
  server_signer_status == o.server_signer_status
164
183
  end
165
184
 
@@ -172,7 +191,7 @@ module Coinbase::Client
172
191
  # Calculates hash code according to all attributes.
173
192
  # @return [Integer] Hash code
174
193
  def hash
175
- [id, network_id, default_address, server_signer_status].hash
194
+ [id, network_id, default_address, enabled_features, server_signer_status].hash
176
195
  end
177
196
 
178
197
  # Builds the object from hash
@@ -24,6 +24,7 @@ Coinbase::Client.autoload :Asset, 'coinbase/client/models/asset'
24
24
  Coinbase::Client.autoload :Balance, 'coinbase/client/models/balance'
25
25
  Coinbase::Client.autoload :BroadcastTradeRequest, 'coinbase/client/models/broadcast_trade_request'
26
26
  Coinbase::Client.autoload :BroadcastTransferRequest, 'coinbase/client/models/broadcast_transfer_request'
27
+ Coinbase::Client.autoload :BuildStakingOperationRequest, 'coinbase/client/models/build_staking_operation_request'
27
28
  Coinbase::Client.autoload :CreateAddressRequest, 'coinbase/client/models/create_address_request'
28
29
  Coinbase::Client.autoload :CreateServerSignerRequest, 'coinbase/client/models/create_server_signer_request'
29
30
  Coinbase::Client.autoload :CreateTradeRequest, 'coinbase/client/models/create_trade_request'
@@ -32,14 +33,17 @@ Coinbase::Client.autoload :CreateWalletRequest, 'coinbase/client/models/create_w
32
33
  Coinbase::Client.autoload :CreateWalletRequestWallet, 'coinbase/client/models/create_wallet_request_wallet'
33
34
  Coinbase::Client.autoload :Error, 'coinbase/client/models/error'
34
35
  Coinbase::Client.autoload :FaucetTransaction, 'coinbase/client/models/faucet_transaction'
36
+ Coinbase::Client.autoload :Feature, 'coinbase/client/models/feature'
35
37
  Coinbase::Client.autoload :SeedCreationEvent, 'coinbase/client/models/seed_creation_event'
36
38
  Coinbase::Client.autoload :SeedCreationEventResult, 'coinbase/client/models/seed_creation_event_result'
37
39
  Coinbase::Client.autoload :ServerSigner, 'coinbase/client/models/server_signer'
38
40
  Coinbase::Client.autoload :ServerSignerEvent, 'coinbase/client/models/server_signer_event'
39
41
  Coinbase::Client.autoload :ServerSignerEventEvent, 'coinbase/client/models/server_signer_event_event'
40
42
  Coinbase::Client.autoload :ServerSignerEventList, 'coinbase/client/models/server_signer_event_list'
43
+ Coinbase::Client.autoload :ServerSignerList, 'coinbase/client/models/server_signer_list'
41
44
  Coinbase::Client.autoload :SignatureCreationEvent, 'coinbase/client/models/signature_creation_event'
42
45
  Coinbase::Client.autoload :SignatureCreationEventResult, 'coinbase/client/models/signature_creation_event_result'
46
+ Coinbase::Client.autoload :StakingOperation, 'coinbase/client/models/staking_operation'
43
47
  Coinbase::Client.autoload :Trade, 'coinbase/client/models/trade'
44
48
  Coinbase::Client.autoload :TradeList, 'coinbase/client/models/trade_list'
45
49
  Coinbase::Client.autoload :Transaction, 'coinbase/client/models/transaction'
@@ -53,6 +57,7 @@ Coinbase::Client.autoload :WalletList, 'coinbase/client/models/wallet_list'
53
57
  # APIs
54
58
  Coinbase::Client.autoload :AddressesApi, 'coinbase/client/api/addresses_api'
55
59
  Coinbase::Client.autoload :ServerSignersApi, 'coinbase/client/api/server_signers_api'
60
+ Coinbase::Client.autoload :StakeApi, 'coinbase/client/api/stake_api'
56
61
  Coinbase::Client.autoload :TradesApi, 'coinbase/client/api/trades_api'
57
62
  Coinbase::Client.autoload :TransfersApi, 'coinbase/client/api/transfers_api'
58
63
  Coinbase::Client.autoload :UsersApi, 'coinbase/client/api/users_api'
@@ -76,6 +76,8 @@ module Coinbase
76
76
  InvalidSignedPayloadError.new(err)
77
77
  when 'invalid_transfer_status'
78
78
  InvalidTransferStatusError.new(err)
79
+ when 'network_feature_unsupported'
80
+ NetworkFeatureUnsupportedError.new(err)
79
81
  else
80
82
  APIError.new(err)
81
83
  end
@@ -117,4 +119,5 @@ module Coinbase
117
119
  class FaucetLimitReachedError < APIError; end
118
120
  class InvalidSignedPayloadError < APIError; end
119
121
  class InvalidTransferStatusError < APIError; end
122
+ class NetworkFeatureUnsupportedError < APIError; end
120
123
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'client'
4
+
5
+ module Coinbase
6
+ # A representation of a Server-Signer. Server-Signers are assigned to sign transactions for a Wallet.
7
+ class ServerSigner
8
+ # Returns a new Server-Signer object. Do not use this method directly. Instead, use ServerSigner.default.
9
+ def initialize(model)
10
+ @model = model
11
+ end
12
+
13
+ class << self
14
+ # Returns the default ServerSigner for the CDP Project.
15
+ # @return [Coinbase::ServerSigner] the default Server-Signer
16
+ def default
17
+ response = Coinbase.call_api do
18
+ server_signers_api.list_server_signers
19
+ end
20
+
21
+ raise 'No Server-Signer is associated with the project' if response.data.empty?
22
+
23
+ new(response.data.first)
24
+ end
25
+
26
+ private
27
+
28
+ def server_signers_api
29
+ Coinbase::Client::ServerSignersApi.new(Coinbase.configuration.api_client)
30
+ end
31
+ end
32
+
33
+ # Returns the Server-Signer ID.
34
+ # @return [String] the Server-Signer ID
35
+ def id
36
+ @model.server_signer_id
37
+ end
38
+
39
+ # Returns the IDs of the Wallet's the Server-Signer can sign for.
40
+ # @return [Array<String>] the wallet IDs
41
+ def wallets
42
+ @model.wallets
43
+ end
44
+
45
+ # Returns a string representation of the Server-Signer.
46
+ # @return [String] a string representation of the Server-Signer
47
+ def to_s
48
+ "Coinbase::ServerSigner{server_signer_id: '#{id}', wallets: [#{wallets.join(', ')}]}"
49
+ end
50
+
51
+ # Same as to_s.
52
+ # @return [String] a string representation of the Server-Signer
53
+ def inspect
54
+ to_s
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'constants'
4
+ require 'bigdecimal'
5
+ require 'eth'
6
+
7
+ module Coinbase
8
+ # A representation of a Trade, which trades an amount of an Asset to another Asset on a Network.
9
+ # The fee is assumed to be paid in the native Asset of the Network.
10
+ # Trades should be created through Wallet#trade or # Address#trade.
11
+ class Trade
12
+ # Returns a new Trade object. Do not use this method directly. Instead, use Wallet#trade or
13
+ # Address#trade.
14
+ # @param model [Coinbase::Client::Trade] The underlying Trade object
15
+ def initialize(model)
16
+ raise unless model.is_a?(Coinbase::Client::Trade)
17
+
18
+ @model = model
19
+ end
20
+
21
+ # Returns the Trade ID.
22
+ # @return [String] The Trade ID
23
+ def id
24
+ @model.trade_id
25
+ end
26
+
27
+ # Returns the Network ID of the Trade.
28
+ # @return [Symbol] The Network ID
29
+ def network_id
30
+ Coinbase.to_sym(@model.network_id)
31
+ end
32
+
33
+ # Returns the Wallet ID of the Trade.
34
+ # @return [String] The Wallet ID
35
+ def wallet_id
36
+ @model.wallet_id
37
+ end
38
+
39
+ # Returns the Address ID of the Trade.
40
+ # @return [String] The Address ID
41
+ def address_id
42
+ @model.address_id
43
+ end
44
+
45
+ # Returns the From Asset ID of the Trade.
46
+ # @return [Symbol] The From Asset ID
47
+ def from_asset_id
48
+ @model.from_asset.asset_id.to_sym
49
+ end
50
+
51
+ # Returns the amount of the from asset for the Trade.
52
+ # @return [BigDecimal] The amount of the from asset
53
+ def from_amount
54
+ BigDecimal(@model.from_amount) / BigDecimal(10).power(@model.from_asset.decimals)
55
+ end
56
+
57
+ # Returns the To Asset ID of the Trade.
58
+ # @return [Symbol] The To Asset ID
59
+ def to_asset_id
60
+ @model.to_asset.asset_id.to_sym
61
+ end
62
+
63
+ # Returns the amount of the to asset for the Trade.
64
+ # @return [BigDecimal] The amount of the to asset
65
+ def to_amount
66
+ BigDecimal(@model.to_amount) / BigDecimal(10).power(@model.to_asset.decimals)
67
+ end
68
+
69
+ # Returns the Trade transaction.
70
+ # @return [Coinbase::Transaction] The Trade transaction
71
+ def transaction
72
+ @transaction ||= Coinbase::Transaction.new(@model.transaction)
73
+ end
74
+
75
+ def approve_transaction
76
+ @approve_transaction ||= @model.approve_transaction ? Coinbase::Transaction.new(@model.approve_transaction) : nil
77
+ end
78
+
79
+ # Returns the status of the Trade.
80
+ # @return [Symbol] The status
81
+ def status
82
+ transaction.status
83
+ end
84
+
85
+ # Waits until the Trade is completed or failed by polling the Network at the given interval. Raises a
86
+ # Timeout::Error if the Trade takes longer than the given timeout.
87
+ # @param interval_seconds [Integer] The interval at which to poll the Network, in seconds
88
+ # @param timeout_seconds [Integer] The maximum amount of time to wait for the Trade to complete, in seconds
89
+ # @return [Trade] The completed Trade object
90
+ def wait!(interval_seconds = 0.2, timeout_seconds = 10)
91
+ start_time = Time.now
92
+
93
+ loop do
94
+ reload
95
+
96
+ # Wait for the trade transaction to be in a terminal state.
97
+ # The approve transaction is optional and must last first, so we don't need to wait for it.
98
+ # We may want to handle a situation where the approve transaction fails and the
99
+ # trade transaction does not ever get broadcast.
100
+ break if transaction.terminal_state?
101
+
102
+ raise Timeout::Error, 'Trade timed out' if Time.now - start_time > timeout_seconds
103
+
104
+ self.sleep interval_seconds
105
+ end
106
+
107
+ self
108
+ end
109
+
110
+ # Reloads the Trade model with the latest version from the server side.
111
+ # @return [Trade] The most recent version of Trade from the server.
112
+ def reload
113
+ @model = Coinbase.call_api do
114
+ trades_api.get_trade(wallet_id, address_id, id)
115
+ end
116
+
117
+ # Update the memoized transaction.
118
+ @transaction = Coinbase::Transaction.new(@model.transaction)
119
+
120
+ # Update the memoized approve transaction if it exists.
121
+ @approve_transaction = @model.approve_transaction ? Coinbase::Transaction.new(@model.approve_transaction) : nil
122
+
123
+ self
124
+ end
125
+
126
+ # Returns a String representation of the Trade.
127
+ # @return [String] a String representation of the Trade
128
+ def to_s
129
+ "Coinbase::Trade{transfer_id: '#{id}', network_id: '#{network_id}', " \
130
+ "address_id: '#{address_id}', from_asset_id: '#{from_asset_id}', " \
131
+ "to_asset_id: '#{to_asset_id}', from_amount: '#{from_amount}', " \
132
+ "to_amount: '#{to_amount}' status: '#{status}'}"
133
+ end
134
+
135
+ # Same as to_s.
136
+ # @return [String] a String representation of the Trade
137
+ def inspect
138
+ to_s
139
+ end
140
+
141
+ private
142
+
143
+ def trades_api
144
+ @trades_api ||= Coinbase::Client::TradesApi.new(Coinbase.configuration.api_client)
145
+ end
146
+ end
147
+ end