coinbase-sdk 0.0.16 → 0.1.0

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.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/lib/coinbase/address/wallet_address.rb +24 -17
  3. data/lib/coinbase/address.rb +17 -19
  4. data/lib/coinbase/asset.rb +11 -8
  5. data/lib/coinbase/client/api/addresses_api.rb +1 -1
  6. data/lib/coinbase/client/api/assets_api.rb +1 -1
  7. data/lib/coinbase/client/api/contract_events_api.rb +18 -10
  8. data/lib/coinbase/client/api/external_addresses_api.rb +1 -1
  9. data/lib/coinbase/client/api/networks_api.rb +1 -1
  10. data/lib/coinbase/client/api/server_signers_api.rb +1 -1
  11. data/lib/coinbase/client/api/stake_api.rb +1 -242
  12. data/lib/coinbase/client/api/trades_api.rb +1 -1
  13. data/lib/coinbase/client/api/transfers_api.rb +1 -1
  14. data/lib/coinbase/client/api/users_api.rb +1 -1
  15. data/lib/coinbase/client/api/validators_api.rb +3 -3
  16. data/lib/coinbase/client/api/wallet_stake_api.rb +263 -0
  17. data/lib/coinbase/client/api/wallets_api.rb +1 -1
  18. data/lib/coinbase/client/api/webhooks_api.rb +1 -1
  19. data/lib/coinbase/client/api_client.rb +1 -1
  20. data/lib/coinbase/client/api_error.rb +1 -1
  21. data/lib/coinbase/client/configuration.rb +11 -1
  22. data/lib/coinbase/client/models/address.rb +22 -5
  23. data/lib/coinbase/client/models/address_balance_list.rb +1 -1
  24. data/lib/coinbase/client/models/address_historical_balance_list.rb +1 -1
  25. data/lib/coinbase/client/models/address_list.rb +1 -1
  26. data/lib/coinbase/client/models/asset.rb +1 -1
  27. data/lib/coinbase/client/models/balance.rb +1 -1
  28. data/lib/coinbase/client/models/broadcast_staking_operation_request.rb +1 -1
  29. data/lib/coinbase/client/models/broadcast_trade_request.rb +1 -1
  30. data/lib/coinbase/client/models/broadcast_transfer_request.rb +1 -1
  31. data/lib/coinbase/client/models/build_staking_operation_request.rb +1 -1
  32. data/lib/coinbase/client/models/contract_event.rb +100 -9
  33. data/lib/coinbase/client/models/contract_event_list.rb +1 -1
  34. data/lib/coinbase/client/models/create_address_request.rb +15 -5
  35. data/lib/coinbase/client/models/create_server_signer_request.rb +1 -1
  36. data/lib/coinbase/client/models/create_staking_operation_request.rb +1 -1
  37. data/lib/coinbase/client/models/create_trade_request.rb +1 -1
  38. data/lib/coinbase/client/models/create_transfer_request.rb +1 -1
  39. data/lib/coinbase/client/models/create_wallet_request.rb +1 -1
  40. data/lib/coinbase/client/models/create_wallet_request_wallet.rb +1 -1
  41. data/lib/coinbase/client/models/create_webhook_request.rb +1 -1
  42. data/lib/coinbase/client/models/error.rb +1 -1
  43. data/lib/coinbase/client/models/ethereum_validator_metadata.rb +12 -12
  44. data/lib/coinbase/client/models/faucet_transaction.rb +1 -1
  45. data/lib/coinbase/client/models/feature.rb +3 -2
  46. data/lib/coinbase/client/models/feature_set.rb +1 -1
  47. data/lib/coinbase/client/models/fetch_historical_staking_balances200_response.rb +1 -1
  48. data/lib/coinbase/client/models/fetch_staking_rewards200_response.rb +1 -1
  49. data/lib/coinbase/client/models/fetch_staking_rewards_request.rb +1 -1
  50. data/lib/coinbase/client/models/get_staking_context_request.rb +1 -1
  51. data/lib/coinbase/client/models/historical_balance.rb +1 -1
  52. data/lib/coinbase/client/models/network.rb +15 -5
  53. data/lib/coinbase/client/models/network_identifier.rb +1 -1
  54. data/lib/coinbase/client/models/seed_creation_event.rb +1 -1
  55. data/lib/coinbase/client/models/seed_creation_event_result.rb +1 -1
  56. data/lib/coinbase/client/models/server_signer.rb +1 -1
  57. data/lib/coinbase/client/models/server_signer_event.rb +1 -1
  58. data/lib/coinbase/client/models/server_signer_event_event.rb +1 -1
  59. data/lib/coinbase/client/models/server_signer_event_list.rb +1 -1
  60. data/lib/coinbase/client/models/server_signer_list.rb +1 -1
  61. data/lib/coinbase/client/models/signature_creation_event.rb +1 -1
  62. data/lib/coinbase/client/models/signature_creation_event_result.rb +1 -1
  63. data/lib/coinbase/client/models/signed_voluntary_exit_message_metadata.rb +1 -1
  64. data/lib/coinbase/client/models/sponsored_send.rb +1 -1
  65. data/lib/coinbase/client/models/staking_balance.rb +1 -1
  66. data/lib/coinbase/client/models/staking_context.rb +1 -1
  67. data/lib/coinbase/client/models/staking_context_context.rb +1 -1
  68. data/lib/coinbase/client/models/staking_operation.rb +3 -3
  69. data/lib/coinbase/client/models/staking_operation_metadata.rb +1 -1
  70. data/lib/coinbase/client/models/staking_reward.rb +1 -1
  71. data/lib/coinbase/client/models/staking_reward_format.rb +1 -1
  72. data/lib/coinbase/client/models/staking_reward_usd_value.rb +1 -1
  73. data/lib/coinbase/client/models/trade.rb +1 -1
  74. data/lib/coinbase/client/models/trade_list.rb +1 -1
  75. data/lib/coinbase/client/models/transaction.rb +16 -6
  76. data/lib/coinbase/client/models/transaction_type.rb +1 -1
  77. data/lib/coinbase/client/models/transfer.rb +1 -1
  78. data/lib/coinbase/client/models/transfer_list.rb +1 -1
  79. data/lib/coinbase/client/models/update_webhook_request.rb +1 -1
  80. data/lib/coinbase/client/models/user.rb +1 -1
  81. data/lib/coinbase/client/models/validator.rb +24 -3
  82. data/lib/coinbase/client/models/validator_details.rb +1 -1
  83. data/lib/coinbase/client/models/validator_list.rb +1 -1
  84. data/lib/coinbase/client/models/validator_status.rb +52 -0
  85. data/lib/coinbase/client/models/wallet.rb +1 -1
  86. data/lib/coinbase/client/models/wallet_list.rb +1 -1
  87. data/lib/coinbase/client/models/webhook.rb +1 -1
  88. data/lib/coinbase/client/models/webhook_event_filter.rb +1 -1
  89. data/lib/coinbase/client/models/webhook_event_type.rb +1 -1
  90. data/lib/coinbase/client/models/webhook_list.rb +1 -1
  91. data/lib/coinbase/client/version.rb +1 -1
  92. data/lib/coinbase/client.rb +3 -1
  93. data/lib/coinbase/constants.rb +0 -10
  94. data/lib/coinbase/contract_event.rb +104 -0
  95. data/lib/coinbase/correlation.rb +30 -0
  96. data/lib/coinbase/destination.rb +11 -9
  97. data/lib/coinbase/errors.rb +7 -0
  98. data/lib/coinbase/middleware.rb +2 -0
  99. data/lib/coinbase/network.rb +103 -20
  100. data/lib/coinbase/smart_contract.rb +106 -0
  101. data/lib/coinbase/staking_balance.rb +11 -5
  102. data/lib/coinbase/staking_operation.rb +134 -36
  103. data/lib/coinbase/staking_reward.rb +1 -1
  104. data/lib/coinbase/trade.rb +10 -9
  105. data/lib/coinbase/transaction.rb +6 -0
  106. data/lib/coinbase/transfer.rb +11 -10
  107. data/lib/coinbase/validator.rb +18 -10
  108. data/lib/coinbase/version.rb +5 -0
  109. data/lib/coinbase/wallet.rb +137 -145
  110. data/lib/coinbase/webhook.rb +181 -0
  111. data/lib/coinbase.rb +50 -19
  112. metadata +9 -9
  113. data/lib/coinbase/client/models/ethereum_validator.rb +0 -374
  114. data/lib/coinbase/client/models/get_validator200_response.rb +0 -221
  115. data/lib/coinbase/client/models/get_validator200_response_validator.rb +0 -214
  116. data/lib/coinbase/client/models/native_eth_staking_context.rb +0 -254
  117. data/lib/coinbase/client/models/partial_eth_staking_context.rb +0 -254
  118. data/lib/coinbase/client/models/validator_list_data.rb +0 -216
  119. data/lib/coinbase/user.rb +0 -65
@@ -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
 
@@ -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
 
@@ -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
 
@@ -78,6 +78,7 @@ Coinbase::Client.autoload :User, 'coinbase/client/models/user'
78
78
  Coinbase::Client.autoload :Validator, 'coinbase/client/models/validator'
79
79
  Coinbase::Client.autoload :ValidatorDetails, 'coinbase/client/models/validator_details'
80
80
  Coinbase::Client.autoload :ValidatorList, 'coinbase/client/models/validator_list'
81
+ Coinbase::Client.autoload :ValidatorStatus, 'coinbase/client/models/validator_status'
81
82
  Coinbase::Client.autoload :Wallet, 'coinbase/client/models/wallet'
82
83
  Coinbase::Client.autoload :WalletList, 'coinbase/client/models/wallet_list'
83
84
  Coinbase::Client.autoload :Webhook, 'coinbase/client/models/webhook'
@@ -97,6 +98,7 @@ Coinbase::Client.autoload :TradesApi, 'coinbase/client/api/trades_api'
97
98
  Coinbase::Client.autoload :TransfersApi, 'coinbase/client/api/transfers_api'
98
99
  Coinbase::Client.autoload :UsersApi, 'coinbase/client/api/users_api'
99
100
  Coinbase::Client.autoload :ValidatorsApi, 'coinbase/client/api/validators_api'
101
+ Coinbase::Client.autoload :WalletStakeApi, 'coinbase/client/api/wallet_stake_api'
100
102
  Coinbase::Client.autoload :WalletsApi, 'coinbase/client/api/wallets_api'
101
103
  Coinbase::Client.autoload :WebhooksApi, 'coinbase/client/api/webhooks_api'
102
104
 
@@ -4,16 +4,6 @@ require_relative 'asset'
4
4
  require_relative 'network'
5
5
 
6
6
  module Coinbase
7
- # The Base Sepolia Network.
8
- BASE_SEPOLIA = Network.new(
9
- network_id: :base_sepolia,
10
- display_name: 'Base Sepolia',
11
- protocol_family: :evm,
12
- is_testnet: true,
13
- native_asset_id: :eth,
14
- chain_id: 84_532
15
- )
16
-
17
7
  # The number of decimal places in Gwei.
18
8
  GWEI_DECIMALS = 9
19
9
 
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coinbase
4
+ # Represents a single contract event
5
+ class ContractEvent
6
+ # Returns a new ContractEvent object.
7
+ # @param model [Coinbase::Client::ContractEvent] The underlying ContractEvent object
8
+ def initialize(model)
9
+ @model = model
10
+ end
11
+
12
+ # Returns the network ID of the ContractEvent.
13
+ # @return [String] The network ID
14
+ def network_id
15
+ @model.network_id
16
+ end
17
+
18
+ # Returns the protocol name of the ContractEvent.
19
+ # @return [String] The protocol name
20
+ def protocol_name
21
+ @model.protocol_name
22
+ end
23
+
24
+ # Returns the contract name of the ContractEvent.
25
+ # @return [String] The contract name
26
+ def contract_name
27
+ @model.contract_name
28
+ end
29
+
30
+ # Returns the event name of the ContractEvent.
31
+ # @return [String] The event name
32
+ def event_name
33
+ @model.event_name
34
+ end
35
+
36
+ # Returns the signature of the ContractEvent.
37
+ # @return [String] The event signature
38
+ def sig
39
+ @model.sig
40
+ end
41
+
42
+ # Returns the four bytes of the Keccak hash of the event signature.
43
+ # @return [String] The four bytes of the event signature hash
44
+ def four_bytes
45
+ @model.four_bytes
46
+ end
47
+
48
+ # Returns the contract address of the ContractEvent.
49
+ # @return [String] The contract address
50
+ def contract_address
51
+ @model.contract_address
52
+ end
53
+
54
+ # Returns the block time of the ContractEvent.
55
+ # @return [Time] The block time
56
+ def block_time
57
+ Time.parse(@model.block_time)
58
+ end
59
+
60
+ # Returns the block height of the ContractEvent.
61
+ # @return [Integer] The block height
62
+ def block_height
63
+ @model.block_height
64
+ end
65
+
66
+ # Returns the transaction hash of the ContractEvent.
67
+ # @return [String] The transaction hash
68
+ def tx_hash
69
+ @model.tx_hash
70
+ end
71
+
72
+ # Returns the transaction index of the ContractEvent.
73
+ # @return [Integer] The transaction index
74
+ def tx_index
75
+ @model.tx_index
76
+ end
77
+
78
+ # Returns the event index of the ContractEvent.
79
+ # @return [Integer] The event index
80
+ def event_index
81
+ @model.event_index
82
+ end
83
+
84
+ # Returns the event data of the ContractEvent.
85
+ # @return [String] The event data
86
+ def data
87
+ @model.data
88
+ end
89
+
90
+ # Returns a string representation of the ContractEvent.
91
+ # @return [String] a string representation of the ContractEvent
92
+ def to_s
93
+ "Coinbase::ContractEvent{network_id: '#{network_id}', protocol_name: '#{protocol_name}', " \
94
+ "contract_name: '#{contract_name}', event_name: '#{event_name}', contract_address: '#{contract_address}', " \
95
+ "block_height: #{block_height}, tx_hash: '#{tx_hash}', data: '#{data}'}"
96
+ end
97
+
98
+ # Same as to_s.
99
+ # @return [String] a string representation of the ContractEvent
100
+ def inspect
101
+ to_s
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'securerandom'
5
+
6
+ module Coinbase
7
+ # A middleware that injects correlation data into the request headers.
8
+ class Correlation < Faraday::Middleware
9
+ # Initializes the Correlation middleware.
10
+ # @param app [Faraday::Connection] The Faraday connection
11
+ def initialize(app)
12
+ super(app)
13
+ @app = app
14
+ end
15
+
16
+ # Processes the request by adding the Correlation Data to the request headers.
17
+ # @param env [Faraday::Env] The Faraday request environment
18
+ def call(env)
19
+ env.request_headers['Correlation-Context'] = correlation_data
20
+ @app.call(env)
21
+ end
22
+
23
+ def correlation_data
24
+ @correlation_data ||= {
25
+ sdk_version: Coinbase::VERSION,
26
+ sdk_language: 'ruby'
27
+ }.map { |key, val| "#{key}=#{CGI.escape(val)}" }.join(',')
28
+ end
29
+ end
30
+ end
@@ -20,32 +20,34 @@ module Coinbase
20
20
  # If the destination is a String, it uses it as the Address ID.
21
21
  # @param network_id [Symbol] The ID of the Network to which the Destination belongs
22
22
  # @return [Destination] The Destination object
23
- def initialize(model, network_id: nil)
23
+ def initialize(model, network:)
24
+ network = Coinbase::Network.from_id(network)
25
+
24
26
  case model
25
27
  when Coinbase::Destination
26
- raise ArgumentError, 'destination network must match destination' unless model.network_id == network_id
28
+ raise ArgumentError, 'destination network must match destination' unless model.network == network
27
29
 
28
30
  @address_id = model.address_id
29
- @network_id = model.network_id
31
+ @network = model.network
30
32
  when Coinbase::Wallet
31
- raise ArgumentError, 'destination network must match wallet' unless model.network_id == network_id
33
+ raise ArgumentError, 'destination network must match wallet' unless model.network == network
32
34
  raise ArgumentError, 'destination wallet must have default address' if model.default_address.nil?
33
35
 
34
36
  @address_id = model.default_address.id
35
- @network_id = model.network_id
37
+ @network = model.network
36
38
  when Coinbase::Address
37
- raise ArgumentError, 'destination network must match address' unless model.network_id == network_id
39
+ raise ArgumentError, 'destination network must match address' unless model.network == network
38
40
 
39
41
  @address_id = model.id
40
- @network_id = model.network_id
42
+ @network = model.network
41
43
  when String
42
44
  @address_id = model
43
- @network_id = network_id
45
+ @network = network
44
46
  else
45
47
  raise ArgumentError, "unsupported destination type: #{model.class}"
46
48
  end
47
49
  end
48
50
 
49
- attr_reader :address_id, :network_id
51
+ attr_reader :address_id, :network
50
52
  end
51
53
  end
@@ -56,6 +56,13 @@ module Coinbase
56
56
  end
57
57
  end
58
58
 
59
+ # An error raised when an operation is attempted with insufficient funds.
60
+ class NetworkUnsupportedError < StandardError
61
+ def initialize(network_id)
62
+ super("Network #{network_id} is not supported")
63
+ end
64
+ end
65
+
59
66
  # An error raised when an operation is attempted with insufficient funds.
60
67
  class InsufficientFundsError < StandardError
61
68
  def initialize(expected, exact, msg = 'Insufficient funds')
@@ -9,6 +9,7 @@ module Coinbase
9
9
  # A module for middleware that can be used with Faraday.
10
10
  module Middleware
11
11
  Faraday::Request.register_middleware authenticator: -> { Coinbase::Authenticator }
12
+ Faraday::Request.register_middleware correlation: -> { Coinbase::Correlation }
12
13
 
13
14
  # Returns the default middleware configuration for the Coinbase SDK.
14
15
  def self.config
@@ -19,6 +20,7 @@ module Coinbase
19
20
  config.host = uri.host + (uri.port ? ":#{uri.port}" : '')
20
21
  config.scheme = uri.scheme if uri.scheme
21
22
  config.request(:authenticator)
23
+ config.request(:correlation)
22
24
  retry_options = {
23
25
  max: Coinbase.configuration.max_network_tries,
24
26
  interval: 0.05,
@@ -3,39 +3,122 @@
3
3
  module Coinbase
4
4
  # A blockchain network.
5
5
  class Network
6
- attr_reader :chain_id
6
+ # Returns the Network object for the given ID, if supported.
7
+ # @param network_id [Symbol, String] The ID of the network
8
+ # @return [Network] The network object
9
+ def self.from_id(network_id)
10
+ return network_id if network_id.is_a?(Network)
7
11
 
8
- # Returns a new Network object. Do not use this method directly. Instead, use the Network constants defined in
12
+ network = NETWORK_MAP.fetch(Coinbase.to_sym(network_id), nil)
13
+
14
+ return network unless network.nil?
15
+
16
+ raise NetworkUnsupportedError, network_id
17
+ end
18
+
19
+ # Constructs a new Network object. Do not use this method directly. Instead, use the Network constants defined in
9
20
  # the Coinbase module.
10
- # @param network_id [Symbol] The Network ID
11
- # @param display_name [String] The Network's display name
12
- # @param protocol_family [String] The protocol family to which the Network belongs
13
- # (e.g., "evm")
14
- # @param is_testnet [Boolean] Whether the Network is a testnet
15
- # @param native_asset_id [String] The ID of the Network's native Asset
16
- # @param chain_id [Integer] The Chain ID of the Network
17
- def initialize(network_id:, display_name:, protocol_family:, is_testnet:, native_asset_id:, chain_id:)
18
- @network_id = network_id
19
- @display_name = display_name
20
- @protocol_family = protocol_family
21
- @is_testnet = is_testnet
22
- @native_asset_id = native_asset_id
23
- @chain_id = chain_id
21
+ # @param id [Symbol, String] The Network ID
22
+ # @return [Network] The new Network object
23
+ def initialize(id)
24
+ @id = ::Coinbase.to_sym(id)
25
+ end
26
+
27
+ # Returns the equality of the Network object with another Network object by ID.
28
+ # @param other [Coinbase::Network] The network object to compare
29
+ # @return [Boolean] Whether the Network objects are equal
30
+ def ==(other)
31
+ return false unless other.is_a?(Network)
32
+
33
+ id == other.id
34
+ end
35
+
36
+ attr_reader :id
37
+
38
+ def normalized_id
39
+ id.to_s.gsub('_', '-')
40
+ end
41
+
42
+ # The Chain ID of the Network.
43
+ # @return [Integer] The Chain ID of the Network
44
+ # @example
45
+ # network.chain_id #=> 84_532
46
+ def chain_id
47
+ model.chain_id
48
+ end
49
+
50
+ # Whether the Network is a testnet.
51
+ # @return [Boolean] Whether the Network is a testnet
52
+ # @example
53
+ # network.testnet? #=> true
54
+ def testnet?
55
+ model.is_testnet
56
+ end
57
+
58
+ # The display name of the Network.
59
+ # @return [String] The display name of the Network
60
+ # @example
61
+ # network.display_name #=> "Base Sepolia"
62
+ def display_name
63
+ model.display_name
64
+ end
65
+
66
+ # The protocol family to which the Network belongs. Example: `evm`.
67
+ # @return [String] The protocol family to which the Network belongs.
68
+ # @example
69
+ # network.protocol_family #=> "evm"
70
+ def protocol_family
71
+ model.protocol_family
72
+ end
73
+
74
+ # The address path prefix of the Network.
75
+ # @return [String] The address path prefix of the Network
76
+ # @example
77
+ # network.address_path_prefix #=> "m/44'/60'/0'/0"
78
+ def address_path_prefix
79
+ model.address_path_prefix
24
80
  end
25
81
 
26
82
  # Gets the Asset with the given ID.
27
- #
28
83
  # @param asset_id [Symbol] The ID of the Asset
29
84
  # @return [Asset] The Asset with the given ID
30
85
  def get_asset(asset_id)
31
- Asset.fetch(@network_id, asset_id)
86
+ Asset.fetch(@id, asset_id)
32
87
  end
33
88
 
34
89
  # Gets the native Asset of the Network.
35
- #
36
90
  # @return [Asset] The native Asset of the Network
37
91
  def native_asset
38
- @native_asset ||= get_asset(@native_asset_id)
92
+ @native_asset ||= Coinbase::Asset.from_model(model.native_asset)
93
+ end
94
+
95
+ def to_s
96
+ details = { id: id }
97
+
98
+ # Only include optional details if the model is already fetched.
99
+ unless @model.nil?
100
+ Coinbase::Client::Network.attribute_map.each_key do |attr|
101
+ details[attr] = @model.send(attr)
102
+ end
103
+ end
104
+
105
+ Coinbase.pretty_print_object(self.class, **details)
106
+ end
107
+
108
+ def inspect
109
+ to_s
110
+ end
111
+
112
+ private
113
+
114
+ def networks_api
115
+ @networks_api ||= Coinbase::Client::NetworksApi.new(Coinbase.configuration.api_client)
116
+ end
117
+
118
+ def model
119
+ @model ||= Coinbase.call_api do
120
+ networks_api.get_network(Coinbase.normalize_network(id))
121
+ end
39
122
  end
40
123
  end
41
124
  end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coinbase
4
+ # A representation of a SmartContract on the blockchain.
5
+ class SmartContract
6
+ # Returns a list of ContractEvents for the provided network, contract, and event details.
7
+ # @param network_id [Symbol] The network ID
8
+ # @param protocol_name [String] The protocol name
9
+ # @param contract_address [String] The contract address
10
+ # @param contract_name [String] The contract name
11
+ # @param event_name [String] The event name
12
+ # @param from_block_height [Integer] The start block height
13
+ # @param to_block_height [Integer] The end block height
14
+ # @return [Enumerable<Coinbase::ContractEvent>] The contract events
15
+ def self.list_events(
16
+ network_id:,
17
+ protocol_name:,
18
+ contract_address:,
19
+ contract_name:,
20
+ event_name:,
21
+ from_block_height:,
22
+ to_block_height:
23
+ )
24
+ Coinbase::Pagination.enumerate(
25
+ lambda { |page|
26
+ list_events_page(network_id, protocol_name, contract_address, contract_name, event_name, from_block_height,
27
+ to_block_height, page)
28
+ }
29
+ ) do |contract_event|
30
+ Coinbase::ContractEvent.new(contract_event)
31
+ end
32
+ end
33
+
34
+ # Returns a new SmartContract object.
35
+ # @param model [Coinbase::Client::SmartContract] The underlying SmartContract object
36
+ def initialize(model)
37
+ @model = model
38
+ end
39
+
40
+ # Returns the network ID of the SmartContract.
41
+ # @return [String] The network ID
42
+ def network_id
43
+ Coinbase.to_sym(@model.network_id)
44
+ end
45
+
46
+ # Returns the protocol name of the SmartContract.
47
+ # @return [String] The protocol name
48
+ def protocol_name
49
+ @model.protocol_name
50
+ end
51
+
52
+ # Returns the contract name of the SmartContract.
53
+ # @return [String] The contract name
54
+ def contract_name
55
+ @model.contract_name
56
+ end
57
+
58
+ # Returns the address of the SmartContract.
59
+ # @return [String] The contract address
60
+ def address
61
+ @model.address
62
+ end
63
+
64
+ # Returns a string representation of the SmartContract.
65
+ # @return [String] a string representation of the SmartContract
66
+ def to_s
67
+ "Coinbase::SmartContract{
68
+ network_id: '#{network_id}',
69
+ protocol_name: '#{protocol_name}',
70
+ contract_name: '#{contract_name}',
71
+ address: '#{address}'}"
72
+ end
73
+
74
+ # Same as to_s.
75
+ # @return [String] a string representation of the SmartContract
76
+ def inspect
77
+ to_s
78
+ end
79
+
80
+ def self.contract_events_api
81
+ Coinbase::Client::ContractEventsApi.new(Coinbase.configuration.api_client)
82
+ end
83
+
84
+ def self.list_events_page(
85
+ network_id,
86
+ protocol_name,
87
+ contract_address,
88
+ contract_name,
89
+ event_name,
90
+ from_block_height,
91
+ to_block_height,
92
+ page
93
+ )
94
+ contract_events_api.list_contract_events(
95
+ Coinbase.normalize_network(network_id),
96
+ protocol_name,
97
+ contract_address,
98
+ contract_name,
99
+ event_name,
100
+ from_block_height,
101
+ to_block_height,
102
+ { next_page: page }
103
+ )
104
+ end
105
+ end
106
+ end
@@ -6,15 +6,17 @@ module Coinbase
6
6
  # A representation of a staking balance on a network for a given asset.
7
7
  class StakingBalance
8
8
  # Returns a list of StakingBalance for the provided network, asset, and addresses.
9
- # @param network_id [Symbol] The network ID
9
+ # @param network [Coinbase::Network, Symbol] The Network or Network ID
10
10
  # @param asset_id [Symbol] The asset ID
11
11
  # @param address_id [String] The address ID
12
12
  # @param start_time [Time] The start time. Defaults to one month ago.
13
13
  # @param end_time [Time] The end time. Defaults to the current time.
14
14
  # @return [Enumerable<Coinbase::StakingBalance>] The staking balances
15
- def self.list(network_id, asset_id, address_id, start_time: DateTime.now.prev_month(1), end_time: DateTime.now)
15
+ def self.list(network, asset_id, address_id, start_time: DateTime.now.prev_month(1), end_time: DateTime.now)
16
+ network = Coinbase::Network.from_id(network)
17
+
16
18
  Coinbase::Pagination.enumerate(
17
- ->(page) { list_page(network_id, asset_id, address_id, start_time, end_time, page) }
19
+ ->(page) { list_page(network, asset_id, address_id, start_time, end_time, page) }
18
20
  ) do |staking_balance|
19
21
  new(staking_balance)
20
22
  end
@@ -72,9 +74,11 @@ module Coinbase
72
74
  Coinbase::Client::StakeApi.new(Coinbase.configuration.api_client)
73
75
  end
74
76
 
75
- def self.list_page(network_id, asset_id, address_id, start_time, end_time, page)
77
+ private_class_method :stake_api
78
+
79
+ def self.list_page(network, asset_id, address_id, start_time, end_time, page)
76
80
  stake_api.fetch_historical_staking_balances(
77
- Coinbase.normalize_network(network_id),
81
+ network.normalized_id,
78
82
  asset_id,
79
83
  address_id,
80
84
  start_time.iso8601,
@@ -82,5 +86,7 @@ module Coinbase
82
86
  { next_page: page }
83
87
  )
84
88
  end
89
+
90
+ private_class_method :list_page
85
91
  end
86
92
  end