coinbase-sdk 0.0.10 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/lib/coinbase/address/external_address.rb +3 -166
  3. data/lib/coinbase/address/wallet_address.rb +80 -107
  4. data/lib/coinbase/address.rb +128 -0
  5. data/lib/coinbase/client/api/addresses_api.rb +1 -1
  6. data/lib/coinbase/client/api/assets_api.rb +3 -3
  7. data/lib/coinbase/client/api/contract_events_api.rb +121 -0
  8. data/lib/coinbase/client/api/external_addresses_api.rb +1 -1
  9. data/lib/coinbase/client/api/server_signers_api.rb +1 -1
  10. data/lib/coinbase/client/api/stake_api.rb +242 -1
  11. data/lib/coinbase/client/api/trades_api.rb +1 -1
  12. data/lib/coinbase/client/api/transfers_api.rb +1 -1
  13. data/lib/coinbase/client/api/users_api.rb +1 -1
  14. data/lib/coinbase/client/api/validators_api.rb +1 -1
  15. data/lib/coinbase/client/api/wallets_api.rb +1 -1
  16. data/lib/coinbase/client/api/webhooks_api.rb +286 -0
  17. data/lib/coinbase/client/api_client.rb +1 -1
  18. data/lib/coinbase/client/api_error.rb +1 -1
  19. data/lib/coinbase/client/configuration.rb +11 -1
  20. data/lib/coinbase/client/models/address.rb +1 -1
  21. data/lib/coinbase/client/models/address_balance_list.rb +1 -1
  22. data/lib/coinbase/client/models/address_list.rb +1 -1
  23. data/lib/coinbase/client/models/asset.rb +1 -1
  24. data/lib/coinbase/client/models/balance.rb +1 -1
  25. data/lib/coinbase/client/models/broadcast_staking_operation_request.rb +239 -0
  26. data/lib/coinbase/client/models/broadcast_trade_request.rb +1 -1
  27. data/lib/coinbase/client/models/broadcast_transfer_request.rb +1 -1
  28. data/lib/coinbase/client/models/build_staking_operation_request.rb +1 -1
  29. data/lib/coinbase/client/models/contract_event.rb +336 -0
  30. data/lib/coinbase/client/models/contract_event_list.rb +259 -0
  31. data/lib/coinbase/client/models/create_address_request.rb +1 -1
  32. data/lib/coinbase/client/models/create_server_signer_request.rb +1 -1
  33. data/lib/coinbase/client/models/create_staking_operation_request.rb +274 -0
  34. data/lib/coinbase/client/models/create_trade_request.rb +1 -1
  35. data/lib/coinbase/client/models/create_transfer_request.rb +1 -1
  36. data/lib/coinbase/client/models/create_wallet_request.rb +1 -1
  37. data/lib/coinbase/client/models/create_wallet_request_wallet.rb +1 -1
  38. data/lib/coinbase/client/models/create_webhook_request.rb +282 -0
  39. data/lib/coinbase/client/models/error.rb +1 -1
  40. data/lib/coinbase/client/models/ethereum_validator_metadata.rb +1 -1
  41. data/lib/coinbase/client/models/faucet_transaction.rb +1 -1
  42. data/lib/coinbase/client/models/feature.rb +1 -1
  43. data/lib/coinbase/client/models/fetch_staking_rewards200_response.rb +1 -1
  44. data/lib/coinbase/client/models/fetch_staking_rewards_request.rb +1 -1
  45. data/lib/coinbase/client/models/get_staking_context_request.rb +1 -1
  46. data/lib/coinbase/client/models/native_eth_staking_context.rb +1 -1
  47. data/lib/coinbase/client/models/partial_eth_staking_context.rb +1 -1
  48. data/lib/coinbase/client/models/seed_creation_event.rb +1 -1
  49. data/lib/coinbase/client/models/seed_creation_event_result.rb +1 -1
  50. data/lib/coinbase/client/models/server_signer.rb +1 -1
  51. data/lib/coinbase/client/models/server_signer_event.rb +1 -1
  52. data/lib/coinbase/client/models/server_signer_event_event.rb +1 -1
  53. data/lib/coinbase/client/models/server_signer_event_list.rb +1 -1
  54. data/lib/coinbase/client/models/server_signer_list.rb +1 -1
  55. data/lib/coinbase/client/models/signature_creation_event.rb +1 -1
  56. data/lib/coinbase/client/models/signature_creation_event_result.rb +1 -1
  57. data/lib/coinbase/client/models/signed_voluntary_exit_message_metadata.rb +1 -1
  58. data/lib/coinbase/client/models/staking_context.rb +1 -1
  59. data/lib/coinbase/client/models/staking_context_context.rb +1 -1
  60. data/lib/coinbase/client/models/staking_operation.rb +16 -6
  61. data/lib/coinbase/client/models/staking_operation_metadata.rb +1 -1
  62. data/lib/coinbase/client/models/staking_reward.rb +1 -1
  63. data/lib/coinbase/client/models/staking_reward_format.rb +1 -1
  64. data/lib/coinbase/client/models/trade.rb +1 -1
  65. data/lib/coinbase/client/models/trade_list.rb +1 -1
  66. data/lib/coinbase/client/models/transaction.rb +1 -1
  67. data/lib/coinbase/client/models/transaction_type.rb +1 -1
  68. data/lib/coinbase/client/models/transfer.rb +5 -93
  69. data/lib/coinbase/client/models/transfer_list.rb +1 -1
  70. data/lib/coinbase/client/models/update_webhook_request.rb +289 -0
  71. data/lib/coinbase/client/models/user.rb +1 -1
  72. data/lib/coinbase/client/models/validator.rb +1 -1
  73. data/lib/coinbase/client/models/validator_details.rb +1 -1
  74. data/lib/coinbase/client/models/validator_list.rb +1 -1
  75. data/lib/coinbase/client/models/wallet.rb +1 -1
  76. data/lib/coinbase/client/models/wallet_list.rb +1 -1
  77. data/lib/coinbase/client/models/webhook.rb +299 -0
  78. data/lib/coinbase/client/models/webhook_event_filter.rb +236 -0
  79. data/lib/coinbase/client/models/webhook_event_type.rb +41 -0
  80. data/lib/coinbase/client/models/webhook_list.rb +244 -0
  81. data/lib/coinbase/client/version.rb +1 -1
  82. data/lib/coinbase/client.rb +13 -1
  83. data/lib/coinbase/constants.rb +3 -0
  84. data/lib/coinbase/destination.rb +51 -0
  85. data/lib/coinbase/errors.rb +14 -0
  86. data/lib/coinbase/middleware.rb +12 -0
  87. data/lib/coinbase/staking_operation.rb +106 -5
  88. data/lib/coinbase/trade.rb +87 -5
  89. data/lib/coinbase/transaction.rb +13 -2
  90. data/lib/coinbase/transfer.rb +86 -7
  91. data/lib/coinbase/wallet.rb +77 -4
  92. data/lib/coinbase.rb +3 -1
  93. metadata +29 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20237f97b110d59cc508ab2bf8d12c50e03de8c99722caf7c8afc0b44ae14d6f
4
- data.tar.gz: a94842743529bd1171179dbbe690034fcd04911fe16046c27b48b4aae2354a30
3
+ metadata.gz: e6e9201f406c2e00680635ef5a35745b7e0163590ca3f54b0b54e33ef0c6d677
4
+ data.tar.gz: c46b18a0a974942a1b81fb7c0fd240b7e8c5ce9fe4761932866141cc44b26c07
5
5
  SHA512:
6
- metadata.gz: b9affa919b3835243affb7ada3935280c4249348082055e79d598ff04b3df65979401078bebadbbd3c1e0ab32499a6eb00acb82221654c2f6a7e9474653436b7
7
- data.tar.gz: aa868c4e37d612a0727f05826ec2275f434f23ecccb6edffab13e553b1c1bafec1573793fe6a983c5c9572a21fdbcdae778e83de7907472109d6861c0a101a04
6
+ metadata.gz: b74ce16758ba14308beaf7166ad5162c808d984549cf029084f2b3f2f3758f6a97e3a1be4d619f0cb24002d9ec721754bef4c8a7cb363bf4f7d947bf529b2cc3
7
+ data.tar.gz: c03d22d0685413d4d732f75ef3216132af6ad0d3cd3fa2985378f559637a62c0490310da73f9607961321be4b60effb43d2ff8a063b1fba083f8c7e365322909
@@ -3,171 +3,8 @@
3
3
  require 'date'
4
4
 
5
5
  module Coinbase
6
- # A representation of a blockchain Address that do not belong to a Coinbase::Wallet.
7
- # External addresses can be used to fetch balances, request funds from the faucet, etc...,
6
+ # A representation of a blockchain Address that does not belong to a Coinbase::Wallet.
7
+ # External addresses can be used to fetch balances, request funds from the faucet, etc.,
8
8
  # but cannot be used to sign transactions.
9
- class ExternalAddress < Address
10
- # Builds a stake operation for the supplied asset. The stake operation
11
- # may take a few minutes to complete in the case when infrastructure is spun up.
12
- # @param amount [Integer,String,BigDecimal] The amount of the asset to stake
13
- # @param asset_id [Symbol] The asset to stake
14
- # @param mode [Symbol] The staking mode. Defaults to :default.
15
- # @param options [Hash] Additional options for the stake operation
16
- # @return [Coinbase::StakingOperation] The stake operation
17
- def build_stake_operation(amount, asset_id, mode: :default, options: {})
18
- validate_can_stake!(amount, asset_id, mode, options)
19
-
20
- build_staking_operation(amount, asset_id, 'stake', mode: mode, options: options)
21
- end
22
-
23
- # Builds an unstake operation for the supplied asset.
24
- # @param amount [Integer,String,BigDecimal] The amount of the asset to unstake
25
- # @param asset_id [Symbol] The asset to unstake
26
- # @param mode [Symbol] The staking mode. Defaults to :default.
27
- # @param options [Hash] Additional options for the unstake operation
28
- # @return [Coinbase::StakingOperation] The unstake operation
29
- def build_unstake_operation(amount, asset_id, mode: :default, options: {})
30
- validate_can_unstake!(amount, asset_id, mode, options)
31
-
32
- build_staking_operation(amount, asset_id, 'unstake', mode: mode, options: options)
33
- end
34
-
35
- # Builds an claim_stake operation for the supplied asset.
36
- # @param amount [Integer,String,BigDecimal] The amount of the asset to claim
37
- # @param asset_id [Symbol] The asset to claim
38
- # @param mode [Symbol] The staking mode. Defaults to :default.
39
- # @param options [Hash] Additional options for the claim_stake operation
40
- # @return [Coinbase::StakingOperation] The claim_stake operation
41
- def build_claim_stake_operation(amount, asset_id, mode: :default, options: {})
42
- validate_can_claim_stake!(amount, asset_id, mode, options)
43
-
44
- build_staking_operation(amount, asset_id, 'claim_stake', mode: mode, options: options)
45
- end
46
-
47
- # Retrieves the balances used for staking for the supplied asset.
48
- # @param asset_id [Symbol] The asset to retrieve staking balances for
49
- # @param mode [Symbol] The staking mode. Defaults to :default.
50
- # @param options [Hash] Additional options for the staking operation
51
- # @return [Hash] The staking balances
52
- # @return [BigDecimal] :stakeable_balance The amount of the asset that can be staked
53
- # @return [BigDecimal] :unstakeable_balance The amount of the asset that is currently staked and cannot be unstaked
54
- # @return [BigDecimal] :claimable_balance The amount of the asset that can be claimed
55
- def staking_balances(asset_id, mode: :default, options: {})
56
- context_model = Coinbase.call_api do
57
- stake_api.get_staking_context(
58
- {
59
- asset_id: asset_id,
60
- network_id: Coinbase.normalize_network(network_id),
61
- address_id: id,
62
- options: {
63
- mode: mode
64
- }.merge(options)
65
- }
66
- )
67
- end.context
68
-
69
- {
70
- stakeable_balance: Coinbase::Balance.from_model_and_asset_id(
71
- context_model.stakeable_balance,
72
- asset_id
73
- ).amount,
74
- unstakeable_balance: Coinbase::Balance.from_model_and_asset_id(
75
- context_model.unstakeable_balance,
76
- asset_id
77
- ).amount,
78
- claimable_balance: Coinbase::Balance.from_model_and_asset_id(
79
- context_model.claimable_balance,
80
- asset_id
81
- ).amount
82
- }
83
- end
84
-
85
- # Retrieves the stakeable balance for the supplied asset.
86
- # @param asset_id [Symbol] The asset to retrieve the stakeable balance for
87
- # @param mode [Symbol] The staking mode. Defaults to :default.
88
- # @param options [Hash] Additional options for the staking operation
89
- # @return [BigDecimal] The stakeable balance
90
- def stakeable_balance(asset_id, mode: :default, options: {})
91
- staking_balances(asset_id, mode: mode, options: options)[:stakeable_balance]
92
- end
93
-
94
- # Retrieves the unstakeable balance for the supplied asset.
95
- # @param asset_id [Symbol] The asset to retrieve the unstakeable balance for
96
- # @param mode [Symbol] The staking mode. Defaults to :default.
97
- # @param options [Hash] Additional options for the staking operation
98
- # @return [BigDecimal] The unstakeable balance
99
- def unstakeable_balance(asset_id, mode: :default, options: {})
100
- staking_balances(asset_id, mode: mode, options: options)[:unstakeable_balance]
101
- end
102
-
103
- # Retrieves the claimable balance for the supplied asset.
104
- # @param asset_id [Symbol] The asset to retrieve the claimable balance for
105
- # @param mode [Symbol] The staking mode. Defaults to :default.
106
- # @param options [Hash] Additional options for the staking operation
107
- # @return [BigDecimal] The claimable balance
108
- def claimable_balance(asset_id, mode: :default, options: {})
109
- staking_balances(asset_id, mode: mode, options: options)[:claimable_balance]
110
- end
111
-
112
- # Lists the staking rewards for the address.
113
- # @param asset_id [Symbol] The asset to retrieve staking rewards for
114
- # @param start_time [Time] The start time for the rewards. Defaults to 1 month ago.
115
- # @param end_time [Time] The end time for the rewards. Defaults to the current time.
116
- # @param format [Symbol] The format to return the rewards in. Defaults to :usd.
117
- # @return [Enumerable<Coinbase::StakingReward>] The staking rewards
118
- def staking_rewards(asset_id, start_time: DateTime.now.prev_month(1), end_time: DateTime.now, format: :usd)
119
- StakingReward.list(
120
- network_id,
121
- asset_id,
122
- [id],
123
- start_time: start_time,
124
- end_time: end_time,
125
- format: format
126
- )
127
- end
128
-
129
- private
130
-
131
- def validate_can_stake!(amount, asset_id, mode, options)
132
- stakeable_balance = stakeable_balance(asset_id, mode: mode, options: options)
133
-
134
- raise InsufficientFundsError.new(amount, stakeable_balance) unless stakeable_balance >= amount
135
- end
136
-
137
- def validate_can_unstake!(amount, asset_id, mode, options)
138
- unstakeable_balance = unstakeable_balance(asset_id, mode: mode, options: options)
139
-
140
- raise InsufficientFundsError.new(amount, unstakeable_balance) unless unstakeable_balance >= amount
141
- end
142
-
143
- def validate_can_claim_stake!(amount, asset_id, mode, options)
144
- claimable_balance = claimable_balance(asset_id, mode: mode, options: options)
145
-
146
- raise InsufficientFundsError.new(amount, claimable_balance) unless claimable_balance >= amount
147
- end
148
-
149
- def build_staking_operation(amount, asset_id, action, mode: :default, options: {})
150
- operation_model = Coinbase.call_api do
151
- asset = Coinbase::Asset.fetch(network_id, asset_id)
152
- stake_api.build_staking_operation(
153
- {
154
- asset_id: asset.primary_denomination.to_s,
155
- address_id: id,
156
- action: action,
157
- network_id: Coinbase.normalize_network(network_id),
158
- options: {
159
- amount: asset.to_atomic_amount(amount).to_i.to_s,
160
- mode: mode
161
- }.merge(options)
162
- }
163
- )
164
- end
165
-
166
- StakingOperation.new(operation_model)
167
- end
168
-
169
- def stake_api
170
- @stake_api ||= Coinbase::Client::StakeApi.new(Coinbase.configuration.api_client)
171
- end
172
- end
9
+ class ExternalAddress < Address; end
173
10
  end
@@ -8,8 +8,6 @@ module Coinbase
8
8
  # Addresses are used to send and receive Assets, and should be created using
9
9
  # Wallet#create_address. Addresses require an Eth::Key to sign transaction data.
10
10
  class WalletAddress < Address
11
- PAGE_LIMIT = 100
12
-
13
11
  # Returns a new Address object. Do not use this method directly. Instead, use Wallet#create_address, or use
14
12
  # the Wallet's default_address.
15
13
  # @param model [Coinbase::Client::Address] The underlying Address object
@@ -43,18 +41,25 @@ module Coinbase
43
41
  # default address. If a String, interprets it as the address ID.
44
42
  # @return [Coinbase::Transfer] The Transfer object.
45
43
  def transfer(amount, asset_id, destination)
46
- asset = Asset.fetch(network_id, asset_id)
47
-
48
- destination_address, destination_network = destination_address_and_network(destination)
49
-
50
- validate_can_transfer!(amount, asset, destination_network)
51
-
52
- transfer = create_transfer(amount, asset, destination_address)
44
+ ensure_can_sign!
45
+ ensure_sufficient_balance!(amount, asset_id)
46
+
47
+ transfer = Transfer.create(
48
+ address_id: id,
49
+ amount: amount,
50
+ asset_id: asset_id,
51
+ destination: destination,
52
+ network_id: network_id,
53
+ wallet_id: wallet_id
54
+ )
53
55
 
54
56
  # If a server signer is managing keys, it will sign and broadcast the underlying transfer transaction out of band.
55
57
  return transfer if Coinbase.use_server_signer?
56
58
 
57
- broadcast_transfer(transfer, transfer.transaction.sign(@key))
59
+ transfer.transaction.sign(@key)
60
+
61
+ transfer.broadcast!
62
+ transfer
58
63
  end
59
64
 
60
65
  # Trades the given amount of the given Asset for another Asset.
@@ -64,20 +69,63 @@ module Coinbase
64
69
  # @param to_asset_id [Symbol] The ID of the Asset to trade to. For Ether, :eth, :gwei, and :wei are supported.
65
70
  # @return [Coinbase::Trade] The Trade object.
66
71
  def trade(amount, from_asset_id, to_asset_id)
67
- from_asset = Asset.fetch(network_id, from_asset_id)
68
- to_asset = Asset.fetch(network_id, to_asset_id)
72
+ ensure_can_sign!
73
+ ensure_sufficient_balance!(amount, from_asset_id)
74
+
75
+ trade = Trade.create(
76
+ address_id: id,
77
+ amount: amount,
78
+ from_asset_id: from_asset_id,
79
+ to_asset_id: to_asset_id,
80
+ network_id: network_id,
81
+ wallet_id: wallet_id
82
+ )
83
+
84
+ # If a server signer is managing keys, it will sign and broadcast the underlying trade transaction out of band.
85
+ return trade if Coinbase.use_server_signer?
86
+
87
+ trade.transactions.each do |tx|
88
+ tx.sign(@key)
89
+ end
69
90
 
70
- validate_can_trade!(amount, from_asset)
91
+ trade.broadcast!
92
+ trade
93
+ end
94
+
95
+ # Stakes the given amount of the given Asset
96
+ # @param amount [Integer, Float, BigDecimal] The amount of the Asset to stake.
97
+ # @param asset_id [Symbol] The ID of the Asset to stake. For Ether, :eth, :gwei, and :wei are supported.
98
+ # @param mode [Symbol] The staking mode. Defaults to :default.
99
+ # @param options [Hash] Additional options for the stake operation
100
+ # @return [Coinbase::StakingOperation] The staking operation
101
+ def stake(amount, asset_id, mode: :default, options: {})
102
+ validate_can_perform_staking_action!(amount, asset_id, 'stakeable_balance', mode, options)
71
103
 
72
- trade = create_trade(amount, from_asset, to_asset)
104
+ complete_staking_operation(amount, asset_id, 'stake', mode: mode, options: options)
105
+ end
73
106
 
74
- # NOTE: Trading does not yet support server signers at this point.
107
+ # Unstakes the given amount of the given Asset
108
+ # @param amount [Integer, Float, BigDecimal] The amount of the Asset to unstake.
109
+ # @param asset_id [Symbol] The ID of the Asset to stake. For Ether, :eth, :gwei, and :wei are supported.
110
+ # @param mode [Symbol] The staking mode. Defaults to :default.
111
+ # @param options [Hash] Additional options for the stake operation
112
+ # @return [Coinbase::StakingOperation] The staking operation
113
+ def unstake(amount, asset_id, mode: :default, options: {})
114
+ validate_can_perform_staking_action!(amount, asset_id, 'unstakeable_balance', mode, options)
75
115
 
76
- payloads = { signed_payload: trade.transaction.sign(@key) }
116
+ complete_staking_operation(amount, asset_id, 'unstake', mode: mode, options: options)
117
+ end
77
118
 
78
- payloads[:approve_tx_signed_payload] = trade.approve_transaction.sign(@key) unless trade.approve_transaction.nil?
119
+ # Claims the given amount of the given Asset
120
+ # @param amount [Integer, Float, BigDecimal] The amount of the Asset to claim.
121
+ # @param asset_id [Symbol] The ID of the Asset to stake. For Ether, :eth, :gwei, and :wei are supported.
122
+ # @param mode [Symbol] The staking mode. Defaults to :default.
123
+ # @param options [Hash] Additional options for the stake operation
124
+ # @return [Coinbase::StakingOperation] The staking operation
125
+ def claim_stake(amount, asset_id, mode: :default, options: {})
126
+ validate_can_perform_staking_action!(amount, asset_id, 'claimable_balance', mode, options)
79
127
 
80
- broadcast_trade(trade, **payloads)
128
+ complete_staking_operation(amount, asset_id, 'claim_stake', mode: mode, options: options)
81
129
  end
82
130
 
83
131
  # Returns whether the Address has a private key backing it to sign transactions.
@@ -99,9 +147,7 @@ module Coinbase
99
147
  # converted to an array, etc...
100
148
  # @return [Enumerable<Coinbase::Transfer>] Enumerator that returns the address's transfers
101
149
  def transfers
102
- Coinbase::Pagination.enumerate(lambda(&method(:fetch_transfers_page))) do |transfer|
103
- Coinbase::Transfer.new(transfer)
104
- end
150
+ Transfer.list(wallet_id: wallet_id, address_id: id)
105
151
  end
106
152
 
107
153
  # Enumerates the trades associated with the address.
@@ -109,9 +155,7 @@ module Coinbase
109
155
  # converted to an array, etc...
110
156
  # @return [Enumerable<Coinbase::Trade>] Enumerator that returns the address's trades
111
157
  def trades
112
- Coinbase::Pagination.enumerate(lambda(&method(:fetch_trades_page))) do |trade|
113
- Coinbase::Trade.new(trade)
114
- end
158
+ Trade.list(wallet_id: wallet_id, address_id: id)
115
159
  end
116
160
 
117
161
  # Returns a String representation of the WalletAddress.
@@ -122,98 +166,27 @@ module Coinbase
122
166
 
123
167
  private
124
168
 
125
- def fetch_transfers_page(page)
126
- transfers_api.list_transfers(wallet_id, id, { limit: PAGE_LIMIT, page: page })
127
- end
128
-
129
- def fetch_trades_page(page)
130
- trades_api.list_trades(wallet_id, id, { limit: PAGE_LIMIT, page: page })
131
- end
169
+ def ensure_can_sign!
170
+ return if Coinbase.use_server_signer?
171
+ return if can_sign?
132
172
 
133
- def transfers_api
134
- @transfers_api ||= Coinbase::Client::TransfersApi.new(Coinbase.configuration.api_client)
173
+ raise AddressCannotSignError
135
174
  end
136
175
 
137
- def trades_api
138
- @trades_api ||= Coinbase::Client::TradesApi.new(Coinbase.configuration.api_client)
139
- end
140
-
141
- def destination_address_and_network(destination)
142
- return [destination.default_address.id, destination.network_id] if destination.is_a?(Wallet)
143
- return [destination.id, destination.network_id] if destination.is_a?(Address)
144
-
145
- [destination, network_id]
146
- end
147
-
148
- def validate_can_transfer!(amount, asset, destination_network_id)
149
- raise 'Cannot transfer from address without private key loaded' unless can_sign? || Coinbase.use_server_signer?
150
-
151
- raise ArgumentError, 'Transfer must be on the same Network' unless destination_network_id == network_id
152
-
153
- current_balance = balance(asset.asset_id)
176
+ def ensure_sufficient_balance!(amount, asset_id)
177
+ current_balance = balance(asset_id)
154
178
 
155
179
  return unless current_balance < amount
156
180
 
157
- raise ArgumentError, "Insufficient funds: #{amount} requested, but only #{current_balance} available"
181
+ raise InsufficientFundsError.new(amount, current_balance)
158
182
  end
159
183
 
160
- def create_transfer(amount, asset, destination)
161
- create_transfer_request = {
162
- amount: asset.to_atomic_amount(amount).to_i.to_s,
163
- network_id: Coinbase.normalize_network(network_id),
164
- asset_id: asset.primary_denomination.to_s,
165
- destination: destination
166
- }
167
-
168
- transfer_model = Coinbase.call_api do
169
- transfers_api.create_transfer(wallet_id, id, create_transfer_request)
184
+ def complete_staking_operation(amount, asset_id, action, mode: :default, options: {})
185
+ op = StakingOperation.create(amount, network_id, asset_id, id, wallet_id, action, mode, options)
186
+ op.transactions.each do |transaction|
187
+ transaction.sign(@key)
170
188
  end
171
-
172
- Coinbase::Transfer.new(transfer_model)
173
- end
174
-
175
- def broadcast_transfer(transfer, signed_payload)
176
- transfer_model = Coinbase.call_api do
177
- transfers_api.broadcast_transfer(wallet_id, id, transfer.id, { signed_payload: signed_payload })
178
- end
179
-
180
- Coinbase::Transfer.new(transfer_model)
181
- end
182
-
183
- def validate_can_trade!(amount, from_asset)
184
- raise 'Cannot trade from address without private key loaded' unless can_sign?
185
-
186
- current_balance = balance(from_asset.asset_id)
187
-
188
- return unless current_balance < amount
189
-
190
- raise ArgumentError, "Insufficient funds: #{amount} requested, but only #{current_balance} available"
191
- end
192
-
193
- def create_trade(amount, from_asset, to_asset)
194
- create_trade_request = {
195
- amount: from_asset.to_atomic_amount(amount).to_i.to_s,
196
- from_asset_id: from_asset.primary_denomination.to_s,
197
- to_asset_id: to_asset.primary_denomination.to_s
198
- }
199
-
200
- trade_model = Coinbase.call_api do
201
- trades_api.create_trade(wallet_id, id, create_trade_request)
202
- end
203
-
204
- Coinbase::Trade.new(trade_model)
205
- end
206
-
207
- def broadcast_trade(trade, signed_payload:, approve_tx_signed_payload: nil)
208
- req = { signed_payload: signed_payload }
209
-
210
- req[:approve_transaction_signed_payload] = approve_tx_signed_payload unless approve_tx_signed_payload.nil?
211
-
212
- trade_model = Coinbase.call_api do
213
- trades_api.broadcast_trade(wallet_id, id, trade.id, req)
214
- end
215
-
216
- Coinbase::Trade.new(trade_model)
189
+ op.broadcast!
217
190
  end
218
191
  end
219
192
  end
@@ -75,10 +75,138 @@ module Coinbase
75
75
  end
76
76
  end
77
77
 
78
+ # Builds a stake operation for the supplied asset. The stake operation
79
+ # may take a few minutes to complete in the case when infrastructure is spun up.
80
+ # @param amount [Integer,String,BigDecimal] The amount of the asset to stake
81
+ # @param asset_id [Symbol] The asset to stake
82
+ # @param mode [Symbol] The staking mode. Defaults to :default.
83
+ # @param options [Hash] Additional options for the stake operation
84
+ # @return [Coinbase::StakingOperation] The stake operation
85
+ def build_stake_operation(amount, asset_id, mode: :default, options: {})
86
+ validate_can_perform_staking_action!(amount, asset_id, 'stakeable_balance', mode, options)
87
+
88
+ StakingOperation.build(amount, network_id, asset_id, id, 'stake', mode, options)
89
+ end
90
+
91
+ # Builds an unstake operation for the supplied asset.
92
+ # @param amount [Integer,String,BigDecimal] The amount of the asset to unstake
93
+ # @param asset_id [Symbol] The asset to unstake
94
+ # @param mode [Symbol] The staking mode. Defaults to :default.
95
+ # @param options [Hash] Additional options for the unstake operation
96
+ # @return [Coinbase::StakingOperation] The unstake operation
97
+ def build_unstake_operation(amount, asset_id, mode: :default, options: {})
98
+ validate_can_perform_staking_action!(amount, asset_id, 'unstakeable_balance', mode, options)
99
+
100
+ StakingOperation.build(amount, network_id, asset_id, id, 'unstake', mode, options)
101
+ end
102
+
103
+ # Builds a claim_stake operation for the supplied asset.
104
+ # @param amount [Integer,String,BigDecimal] The amount of the asset to claim
105
+ # @param asset_id [Symbol] The asset to claim
106
+ # @param mode [Symbol] The staking mode. Defaults to :default.
107
+ # @param options [Hash] Additional options for the claim_stake operation
108
+ # @return [Coinbase::StakingOperation] The claim_stake operation
109
+ def build_claim_stake_operation(amount, asset_id, mode: :default, options: {})
110
+ validate_can_perform_staking_action!(amount, asset_id, 'claimable_balance', mode, options)
111
+
112
+ StakingOperation.build(amount, network_id, asset_id, id, 'claim_stake', mode, options)
113
+ end
114
+
115
+ # Retrieves the balances used for staking for the supplied asset.
116
+ # @param asset_id [Symbol] The asset to retrieve staking balances for
117
+ # @param mode [Symbol] The staking mode. Defaults to :default.
118
+ # @param options [Hash] Additional options for the staking operation
119
+ # @return [Hash] The staking balances
120
+ # @return [BigDecimal] :stakeable_balance The amount of the asset that can be staked
121
+ # @return [BigDecimal] :unstakeable_balance The amount of the asset that is currently staked and cannot be unstaked
122
+ # @return [BigDecimal] :claimable_balance The amount of the asset that can be claimed
123
+ def staking_balances(asset_id, mode: :default, options: {})
124
+ context_model = Coinbase.call_api do
125
+ stake_api.get_staking_context(
126
+ {
127
+ asset_id: asset_id,
128
+ network_id: Coinbase.normalize_network(network_id),
129
+ address_id: id,
130
+ options: {
131
+ mode: mode
132
+ }.merge(options)
133
+ }
134
+ )
135
+ end.context
136
+
137
+ {
138
+ stakeable_balance: Coinbase::Balance.from_model_and_asset_id(
139
+ context_model.stakeable_balance,
140
+ asset_id
141
+ ).amount,
142
+ unstakeable_balance: Coinbase::Balance.from_model_and_asset_id(
143
+ context_model.unstakeable_balance,
144
+ asset_id
145
+ ).amount,
146
+ claimable_balance: Coinbase::Balance.from_model_and_asset_id(
147
+ context_model.claimable_balance,
148
+ asset_id
149
+ ).amount
150
+ }
151
+ end
152
+
153
+ # Retrieves the stakeable balance for the supplied asset.
154
+ # @param asset_id [Symbol] The asset to retrieve the stakeable balance for
155
+ # @param mode [Symbol] The staking mode. Defaults to :default.
156
+ # @param options [Hash] Additional options for the staking operation
157
+ # @return [BigDecimal] The stakeable balance
158
+ def stakeable_balance(asset_id, mode: :default, options: {})
159
+ staking_balances(asset_id, mode: mode, options: options)[:stakeable_balance]
160
+ end
161
+
162
+ # Retrieves the unstakeable balance for the supplied asset.
163
+ # @param asset_id [Symbol] The asset to retrieve the unstakeable balance for
164
+ # @param mode [Symbol] The staking mode. Defaults to :default.
165
+ # @param options [Hash] Additional options for the staking operation
166
+ # @return [BigDecimal] The unstakeable balance
167
+ def unstakeable_balance(asset_id, mode: :default, options: {})
168
+ staking_balances(asset_id, mode: mode, options: options)[:unstakeable_balance]
169
+ end
170
+
171
+ # Retrieves the claimable balance for the supplied asset.
172
+ # @param asset_id [Symbol] The asset to retrieve the claimable balance for
173
+ # @param mode [Symbol] The staking mode. Defaults to :default.
174
+ # @param options [Hash] Additional options for the staking operation
175
+ # @return [BigDecimal] The claimable balance
176
+ def claimable_balance(asset_id, mode: :default, options: {})
177
+ staking_balances(asset_id, mode: mode, options: options)[:claimable_balance]
178
+ end
179
+
180
+ # Lists the staking rewards for the address.
181
+ # @param asset_id [Symbol] The asset to retrieve staking rewards for
182
+ # @param start_time [Time] The start time for the rewards. Defaults to 1 week ago.
183
+ # @param end_time [Time] The end time for the rewards. Defaults to the current time.
184
+ # @param format [Symbol] The format to return the rewards in. Defaults to :usd.
185
+ # @return [Enumerable<Coinbase::StakingReward>] The staking rewards
186
+ def staking_rewards(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now, format: :usd)
187
+ StakingReward.list(
188
+ network_id,
189
+ asset_id,
190
+ [id],
191
+ start_time: start_time,
192
+ end_time: end_time,
193
+ format: format
194
+ )
195
+ end
196
+
78
197
  private
79
198
 
199
+ def validate_can_perform_staking_action!(amount, asset_id, balance_type, mode, options)
200
+ current_staking_balance = staking_balances(asset_id, mode: mode, options: options)[balance_type.to_sym]
201
+ raise InsufficientFundsError.new(amount, current_staking_balance) unless current_staking_balance >= amount
202
+ end
203
+
80
204
  def addresses_api
81
205
  @addresses_api ||= Coinbase::Client::ExternalAddressesApi.new(Coinbase.configuration.api_client)
82
206
  end
207
+
208
+ def stake_api
209
+ @stake_api ||= Coinbase::Client::StakeApi.new(Coinbase.configuration.api_client)
210
+ end
83
211
  end
84
212
  end
@@ -6,7 +6,7 @@
6
6
  The version of the OpenAPI document: 0.0.1-alpha
7
7
  Contact: yuga.cohler@coinbase.com
8
8
  Generated by: https://openapi-generator.tech
9
- Generator version: 7.6.0
9
+ Generator version: 7.7.0
10
10
 
11
11
  =end
12
12
 
@@ -6,7 +6,7 @@
6
6
  The version of the OpenAPI document: 0.0.1-alpha
7
7
  Contact: yuga.cohler@coinbase.com
8
8
  Generated by: https://openapi-generator.tech
9
- Generator version: 7.6.0
9
+ Generator version: 7.7.0
10
10
 
11
11
  =end
12
12
 
@@ -22,7 +22,7 @@ module Coinbase::Client
22
22
  # Get the asset for the specified asset ID.
23
23
  # Get the asset for the specified asset ID.
24
24
  # @param network_id [String] The ID of the blockchain network
25
- # @param asset_id [String] The ID of the asset to fetch
25
+ # @param asset_id [String] The ID of the asset to fetch. This could be a symbol or an ERC20 contract address.
26
26
  # @param [Hash] opts the optional parameters
27
27
  # @return [Asset]
28
28
  def get_asset(network_id, asset_id, opts = {})
@@ -33,7 +33,7 @@ module Coinbase::Client
33
33
  # Get the asset for the specified asset ID.
34
34
  # Get the asset for the specified asset ID.
35
35
  # @param network_id [String] The ID of the blockchain network
36
- # @param asset_id [String] The ID of the asset to fetch
36
+ # @param asset_id [String] The ID of the asset to fetch. This could be a symbol or an ERC20 contract address.
37
37
  # @param [Hash] opts the optional parameters
38
38
  # @return [Array<(Asset, Integer, Hash)>] Asset data, response status code and response headers
39
39
  def get_asset_with_http_info(network_id, asset_id, opts = {})