coinbase-sdk 0.0.14 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/coinbase/address/wallet_address.rb +31 -21
  3. data/lib/coinbase/address.rb +51 -17
  4. data/lib/coinbase/asset.rb +11 -8
  5. data/lib/coinbase/client/api/contract_events_api.rb +17 -9
  6. data/lib/coinbase/client/api/external_addresses_api.rb +85 -0
  7. data/lib/coinbase/client/api/networks_api.rb +85 -0
  8. data/lib/coinbase/client/api/stake_api.rb +74 -195
  9. data/lib/coinbase/client/api/validators_api.rb +2 -2
  10. data/lib/coinbase/client/api/wallet_stake_api.rb +263 -0
  11. data/lib/coinbase/client/models/address.rb +21 -4
  12. data/lib/coinbase/client/models/{native_eth_staking_context.rb → address_historical_balance_list.rb} +39 -35
  13. data/lib/coinbase/client/models/contract_event.rb +99 -8
  14. data/lib/coinbase/client/models/create_address_request.rb +14 -4
  15. data/lib/coinbase/client/models/create_transfer_request.rb +14 -4
  16. data/lib/coinbase/client/models/ethereum_validator_metadata.rb +11 -11
  17. data/lib/coinbase/client/models/feature.rb +2 -1
  18. data/lib/coinbase/client/models/feature_set.rb +307 -0
  19. data/lib/coinbase/client/models/{partial_eth_staking_context.rb → fetch_historical_staking_balances200_response.rb} +39 -35
  20. data/lib/coinbase/client/models/historical_balance.rb +273 -0
  21. data/lib/coinbase/client/models/network.rb +365 -0
  22. data/lib/coinbase/client/models/network_identifier.rb +44 -0
  23. data/lib/coinbase/client/models/sponsored_send.rb +338 -0
  24. data/lib/coinbase/client/models/staking_balance.rb +289 -0
  25. data/lib/coinbase/client/models/staking_context_context.rb +222 -74
  26. data/lib/coinbase/client/models/staking_operation.rb +2 -2
  27. data/lib/coinbase/client/models/staking_reward.rb +22 -6
  28. data/lib/coinbase/client/models/staking_reward_format.rb +2 -1
  29. data/lib/coinbase/client/models/staking_reward_usd_value.rb +257 -0
  30. data/lib/coinbase/client/models/transaction.rb +17 -7
  31. data/lib/coinbase/client/models/transaction_type.rb +2 -1
  32. data/lib/coinbase/client/models/transfer.rb +101 -8
  33. data/lib/coinbase/client/models/validator.rb +23 -2
  34. data/lib/coinbase/client/models/validator_status.rb +52 -0
  35. data/lib/coinbase/client/models/wallet.rb +13 -16
  36. data/lib/coinbase/client/models/webhook_event_type.rb +2 -1
  37. data/lib/coinbase/client.rb +12 -3
  38. data/lib/coinbase/constants.rb +0 -10
  39. data/lib/coinbase/contract_event.rb +104 -0
  40. data/lib/coinbase/correlation.rb +30 -0
  41. data/lib/coinbase/destination.rb +11 -9
  42. data/lib/coinbase/errors.rb +14 -0
  43. data/lib/coinbase/historical_balance.rb +53 -0
  44. data/lib/coinbase/middleware.rb +2 -0
  45. data/lib/coinbase/network.rb +103 -20
  46. data/lib/coinbase/server_signer.rb +14 -3
  47. data/lib/coinbase/smart_contract.rb +106 -0
  48. data/lib/coinbase/sponsored_send.rb +110 -0
  49. data/lib/coinbase/staking_balance.rb +92 -0
  50. data/lib/coinbase/staking_operation.rb +134 -36
  51. data/lib/coinbase/staking_reward.rb +18 -0
  52. data/lib/coinbase/trade.rb +10 -9
  53. data/lib/coinbase/transaction.rb +13 -3
  54. data/lib/coinbase/transfer.rb +65 -36
  55. data/lib/coinbase/validator.rb +18 -10
  56. data/lib/coinbase/version.rb +5 -0
  57. data/lib/coinbase/wallet/data.rb +31 -0
  58. data/lib/coinbase/wallet.rb +143 -182
  59. data/lib/coinbase/webhook.rb +181 -0
  60. data/lib/coinbase.rb +64 -21
  61. metadata +51 -5
  62. data/lib/coinbase/user.rb +0 -65
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'constants'
4
- require 'bigdecimal'
5
3
  require 'eth'
6
4
  require 'json'
7
5
 
@@ -70,6 +68,12 @@ module Coinbase
70
68
  @model.from_address_id
71
69
  end
72
70
 
71
+ # Returns the to address for the Transaction.
72
+ # @return [String] The to address
73
+ def to_address_id
74
+ @model.to_address_id
75
+ end
76
+
73
77
  # Returns whether the Transaction is in a terminal state.
74
78
  # @return [Boolean] Whether the Transaction is in a terminal state
75
79
  def terminal_state?
@@ -113,12 +117,18 @@ module Coinbase
113
117
  @raw = Eth::Tx::Eip1559.new(Eth::Tx.validate_eip1559_params(params))
114
118
  end
115
119
 
120
+ # Returns the signature of the Transaction.
121
+ # @return [String] The hex-encode signature
122
+ def signature
123
+ raw.hex
124
+ end
125
+
116
126
  # Signs the Transaction with the provided key and returns the hex signing payload.
117
127
  # @return [String] The hex-encoded signed payload
118
128
  def sign(key)
119
129
  raw.sign(key)
120
130
 
121
- raw.hex
131
+ signature
122
132
  end
123
133
 
124
134
  # Returns whether the Transaction has been signed.
@@ -3,7 +3,6 @@
3
3
  require_relative 'constants'
4
4
  require 'bigdecimal'
5
5
  require 'eth'
6
- require 'json'
7
6
 
8
7
  module Coinbase
9
8
  # A representation of a Transfer, which moves an amount of an Asset from
@@ -21,12 +20,13 @@ module Coinbase
21
20
  # If the destination is a Wallet, it uses the default Address of the Wallet.
22
21
  # If the destination is an Address, it uses the Address's ID.
23
22
  # If the destination is a String, it uses it as the Address ID.
24
- # @param network_id [Symbol] The Network ID of the Asset
23
+ # @param network [Coinbase::Network, Symbol] The Network or Network ID of the Asset
25
24
  # @param wallet_id [String] The Wallet ID of the sending Wallet
26
25
  # @return [Transfer] The new pending Transfer object
27
26
  # @raise [Coinbase::ApiError] If the Transfer fails
28
- def create(address_id:, amount:, asset_id:, destination:, network_id:, wallet_id:)
29
- asset = Asset.fetch(network_id, asset_id)
27
+ def create(address_id:, amount:, asset_id:, destination:, network:, wallet_id:, gasless: false)
28
+ network = Coinbase::Network.from_id(network)
29
+ asset = network.get_asset(asset_id)
30
30
 
31
31
  model = Coinbase.call_api do
32
32
  transfers_api.create_transfer(
@@ -35,8 +35,9 @@ module Coinbase
35
35
  {
36
36
  amount: asset.to_atomic_amount(amount).to_i.to_s,
37
37
  asset_id: asset.primary_denomination.to_s,
38
- destination: Coinbase::Destination.new(destination, network_id: network_id).address_id,
39
- network_id: Coinbase.normalize_network(network_id)
38
+ destination: Coinbase::Destination.new(destination, network: network).address_id,
39
+ network_id: network.normalized_id,
40
+ gasless: gasless
40
41
  }
41
42
  )
42
43
  end
@@ -82,10 +83,10 @@ module Coinbase
82
83
  @model.transfer_id
83
84
  end
84
85
 
85
- # Returns the Network ID of the Transfer.
86
- # @return [Symbol] The Network ID
87
- def network_id
88
- Coinbase.to_sym(@model.network_id)
86
+ # Returns the Network of the Transfer.
87
+ # @return [Symbol] The Network
88
+ def network
89
+ @network ||= Coinbase::Network.from_id(@model.network_id)
89
90
  end
90
91
 
91
92
  # Returns the Wallet ID of the Transfer.
@@ -122,40 +123,52 @@ module Coinbase
122
123
  BigDecimal(@model.amount) / BigDecimal(10).power(@model.asset.decimals)
123
124
  end
124
125
 
125
- # Returns the link to the transaction on the blockchain explorer.
126
- # @return [String] The link to the transaction on the blockchain explorer
127
- def transaction_link
128
- transaction.transaction_link
129
- end
126
+ # Signs the Transfer with the given key. This is required before broadcasting the Transfer.
127
+ # @param key [Eth::Key] The key to sign the Transfer with
128
+ # @raise [RuntimeError] If the key is not an Eth::Key
129
+ # @return [Transfer] The Transfer object
130
+ def sign(key)
131
+ raise unless key.is_a?(Eth::Key)
130
132
 
131
- # Returns the Unsigned Payload of the Transfer.
132
- # @return [String] The Unsigned Payload
133
- def unsigned_payload
134
- transaction.unsigned_payload
135
- end
133
+ unless sponsored_send.nil?
134
+ sponsored_send.sign(key)
135
+
136
+ return
137
+ end
136
138
 
137
- # Returns the Signed Payload of the Transfer.
138
- # @return [String] The Signed Payload
139
- def signed_payload
140
- transaction.signed_payload
139
+ transaction.sign(key)
140
+
141
+ self
141
142
  end
142
143
 
143
144
  # Returns the Transfer transaction.
144
145
  # @return [Coinbase::Transaction] The Transfer transaction
145
146
  def transaction
146
- @transaction ||= Coinbase::Transaction.new(@model.transaction)
147
+ @transaction ||= @model.transaction.nil? ? nil : Coinbase::Transaction.new(@model.transaction)
147
148
  end
148
149
 
149
- # Returns the Transaction Hash of the Transfer.
150
- # @return [String] The Transaction Hash
151
- def transaction_hash
152
- transaction.transaction_hash
150
+ # Returns the SponsoredSend of the Transfer, if the transfer is gasless.
151
+ # @return [Coinbase::SponsoredSend] The SponsoredSend object
152
+ def sponsored_send
153
+ @sponsored_send ||= @model.sponsored_send.nil? ? nil : Coinbase::SponsoredSend.new(@model.sponsored_send)
153
154
  end
154
155
 
155
156
  # Returns the status of the Transfer.
156
157
  # @return [Symbol] The status
157
158
  def status
158
- transaction.status
159
+ send_tx_delegate.status
160
+ end
161
+
162
+ # Returns the link to the transaction on the blockchain explorer.
163
+ # @return [String] The link to the transaction on the blockchain explorer
164
+ def transaction_link
165
+ send_tx_delegate.transaction_link
166
+ end
167
+
168
+ # Returns the Transaction Hash of the Transfer.
169
+ # @return [String] The Transaction Hash
170
+ def transaction_hash
171
+ send_tx_delegate.transaction_hash
159
172
  end
160
173
 
161
174
  # Broadcasts the Transfer to the Network.
@@ -163,18 +176,19 @@ module Coinbase
163
176
  # @raise [RuntimeError] If the Transfer is not signed
164
177
  # @return [Transfer] The Transfer object
165
178
  def broadcast!
166
- raise TransactionNotSignedError unless transaction.signed?
179
+ raise TransactionNotSignedError unless send_tx_delegate.signed?
167
180
 
168
181
  @model = Coinbase.call_api do
169
182
  transfers_api.broadcast_transfer(
170
183
  wallet_id,
171
184
  from_address_id,
172
185
  id,
173
- { signed_payload: transaction.raw.hex }
186
+ { signed_payload: send_tx_delegate.signature }
174
187
  )
175
188
  end
176
189
 
177
- update_transaction(@model)
190
+ update_transaction(@model) unless @model.transaction.nil?
191
+ update_sponsored_send(@model) unless @model.sponsored_send.nil?
178
192
 
179
193
  self
180
194
  end
@@ -186,7 +200,8 @@ module Coinbase
186
200
  transfers_api.get_transfer(wallet_id, from_address_id, id)
187
201
  end
188
202
 
189
- update_transaction(@model)
203
+ update_transaction(@model) unless @model.transaction.nil?
204
+ update_sponsored_send(@model) unless @model.sponsored_send.nil?
190
205
 
191
206
  self
192
207
  end
@@ -202,7 +217,7 @@ module Coinbase
202
217
  loop do
203
218
  reload
204
219
 
205
- return self if transaction.terminal_state?
220
+ return self if terminal_state?
206
221
 
207
222
  raise Timeout::Error, 'Transfer timed out' if Time.now - start_time > timeout_seconds
208
223
 
@@ -215,7 +230,7 @@ module Coinbase
215
230
  # Returns a String representation of the Transfer.
216
231
  # @return [String] a String representation of the Transfer
217
232
  def to_s
218
- "Coinbase::Transfer{transfer_id: '#{id}', network_id: '#{network_id}', " \
233
+ "Coinbase::Transfer{transfer_id: '#{id}', network_id: '#{network.id}', " \
219
234
  "from_address_id: '#{from_address_id}', destination_address_id: '#{destination_address_id}', " \
220
235
  "asset_id: '#{asset_id}', amount: '#{amount}', transaction_link: '#{transaction_link}', " \
221
236
  "status: '#{status}'}"
@@ -229,6 +244,16 @@ module Coinbase
229
244
 
230
245
  private
231
246
 
247
+ def terminal_state?
248
+ send_tx_delegate.terminal_state?
249
+ end
250
+
251
+ def send_tx_delegate
252
+ return sponsored_send unless sponsored_send.nil?
253
+
254
+ transaction
255
+ end
256
+
232
257
  def transfers_api
233
258
  @transfers_api ||= Coinbase::Client::TransfersApi.new(Coinbase.configuration.api_client)
234
259
  end
@@ -236,5 +261,9 @@ module Coinbase
236
261
  def update_transaction(model)
237
262
  @transaction = Coinbase::Transaction.new(model.transaction)
238
263
  end
264
+
265
+ def update_sponsored_send(model)
266
+ @sponsored_send = Coinbase::SponsoredSend.new(model.sponsored_send)
267
+ end
239
268
  end
240
269
  end
@@ -10,27 +10,31 @@ module Coinbase
10
10
  end
11
11
 
12
12
  # Returns a list of Validators for the provided network and asset.
13
- # @param network_id [Symbol] The network ID
13
+ # @param network [Coinbase::Nework, Symbol] The Network or Network ID
14
14
  # @param asset_id [Symbol] The asset ID
15
15
  # @param status [Symbol] The status of the validator. Defaults to nil.
16
16
  # @return [Enumerable<Coinbase::Validator>] The validators
17
- def self.list(network_id, asset_id, status: nil)
18
- Coinbase::Pagination.enumerate(
19
- ->(page) { list_page(network_id, asset_id, status, page) }
20
- ) do |validator|
17
+ def self.list(network, asset_id, status: nil)
18
+ network = Coinbase::Network.from_id(network)
19
+
20
+ Coinbase::Pagination.enumerate(lambda { |page|
21
+ list_page(network, asset_id, status, page)
22
+ }) do |validator|
21
23
  new(validator)
22
24
  end
23
25
  end
24
26
 
25
27
  # Returns a Validator for the provided network, asset, and validator.
26
- # @param network_id [Symbol] The network ID
28
+ # @param network [Coinbase::Network, Symbol] The Network or Network ID
27
29
  # @param asset_id [Symbol] The asset ID
28
30
  # @param validator_id [String] The validator ID
29
31
  # @return [Coinbase::Validator] The validator
30
- def self.fetch(network_id, asset_id, validator_id)
32
+ def self.fetch(network, asset_id, validator_id)
33
+ network = Coinbase::Network.from_id(network)
34
+
31
35
  validator = Coinbase.call_api do
32
36
  validators_api.get_validator(
33
- network_id,
37
+ network.normalized_id,
34
38
  asset_id,
35
39
  validator_id
36
40
  )
@@ -62,10 +66,10 @@ module Coinbase
62
66
  to_s
63
67
  end
64
68
 
65
- def self.list_page(network_id, asset_id, status, page)
69
+ def self.list_page(network, asset_id, status, page)
66
70
  Coinbase.call_api do
67
71
  validators_api.list_validators(
68
- network_id,
72
+ network.normalized_id,
69
73
  asset_id,
70
74
  {
71
75
  status: status,
@@ -75,8 +79,12 @@ module Coinbase
75
79
  end
76
80
  end
77
81
 
82
+ private_class_method :list_page
83
+
78
84
  def self.validators_api
79
85
  Coinbase::Client::ValidatorsApi.new(Coinbase.configuration.api_client)
80
86
  end
87
+
88
+ private_class_method :validators_api
81
89
  end
82
90
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coinbase
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coinbase
4
+ class Wallet
5
+ # The data required to recreate a Wallet.
6
+ class Data
7
+ attr_reader :wallet_id, :seed
8
+
9
+ # Returns a new Data object.
10
+ # @param wallet_id [String] The ID of the Wallet
11
+ # @param seed [String] The seed of the Wallet
12
+ def initialize(wallet_id:, seed:)
13
+ @wallet_id = wallet_id
14
+ @seed = seed
15
+ end
16
+
17
+ # Converts the Data object to a Hash.
18
+ # @return [Hash] The Hash representation of the Data object
19
+ def to_hash
20
+ { wallet_id: wallet_id, seed: seed }
21
+ end
22
+
23
+ # Creates a Data object from the given Hash.
24
+ # @param data [Hash] The Hash to create the Data object from
25
+ # @return [Data] The new Data object
26
+ def self.from_hash(data)
27
+ Data.new(wallet_id: data['wallet_id'], seed: data['seed'])
28
+ end
29
+ end
30
+ end
31
+ end