Module: Coinbase

Defined in:
lib/coinbase.rb,
lib/coinbase/asset.rb,
lib/coinbase/trade.rb,
lib/coinbase/errors.rb,
lib/coinbase/wallet.rb,
lib/coinbase/address.rb,
lib/coinbase/balance.rb,
lib/coinbase/network.rb,
lib/coinbase/version.rb,
lib/coinbase/webhook.rb,
lib/coinbase/transfer.rb,
lib/coinbase/constants.rb,
lib/coinbase/validator.rb,
lib/coinbase/fund_quote.rb,
lib/coinbase/middleware.rb,
lib/coinbase/pagination.rb,
lib/coinbase/balance_map.rb,
lib/coinbase/correlation.rb,
lib/coinbase/destination.rb,
lib/coinbase/fiat_amount.rb,
lib/coinbase/transaction.rb,
lib/coinbase/wallet/data.rb,
lib/coinbase/authenticator.rb,
lib/coinbase/crypto_amount.rb,
lib/coinbase/server_signer.rb,
lib/coinbase/contract_event.rb,
lib/coinbase/fund_operation.rb,
lib/coinbase/smart_contract.rb,
lib/coinbase/sponsored_send.rb,
lib/coinbase/staking_reward.rb,
lib/coinbase/staking_balance.rb,
lib/coinbase/payload_signature.rb,
lib/coinbase/staking_operation.rb,
lib/coinbase/address_reputation.rb,
lib/coinbase/faucet_transaction.rb,
lib/coinbase/historical_balance.rb,
lib/coinbase/contract_invocation.rb,
lib/coinbase/address/wallet_address.rb,
lib/coinbase/address/external_address.rb

Overview

The Coinbase SDK.

Defined Under Namespace

Modules: Client, Middleware, Pagination Classes: APIError, Address, AddressCannotSignError, AddressReputation, AlreadyExistsError, AlreadySignedError, Asset, Authenticator, Balance, BalanceMap, Configuration, ContractEvent, ContractInvocation, Correlation, CryptoAmount, Destination, ExternalAddress, FaucetLimitReachedError, FaucetTransaction, FiatAmount, FundOperation, FundQuote, HistoricalBalance, InsufficientFundsError, InternalError, InvalidAddressError, InvalidAddressIDError, InvalidAmountError, InvalidAssetIDError, InvalidConfiguration, InvalidDestinationError, InvalidLimitError, InvalidNetworkIDError, InvalidPageError, InvalidSignedPayloadError, InvalidTransferIDError, InvalidTransferStatusError, InvalidWalletError, InvalidWalletIDError, MalformedRequestError, ManageExternalContractError, Network, NetworkFeatureUnsupportedError, NetworkUnsupportedError, NotFoundError, PayloadSignature, ResourceExhaustedError, ServerSigner, SmartContract, SponsoredSend, StakingBalance, StakingOperation, StakingReward, Trade, Transaction, TransactionNotSignedError, Transfer, UnauthorizedError, UnimplementedError, UnsupportedAssetError, Validator, Wallet, WalletAddress, Webhook

Constant Summary collapse

ERROR_CODE_TO_ERROR_CLASS =
{
  'unimplemented' => UnimplementedError,
  'unauthorized' => UnauthorizedError,
  'internal' => InternalError,
  'not_found' => NotFoundError,
  'invalid_wallet_id' => InvalidWalletIDError,
  'invalid_address_id' => InvalidAddressIDError,
  'invalid_wallet' => InvalidWalletError,
  'invalid_address' => InvalidAddressError,
  'invalid_amount' => InvalidAmountError,
  'invalid_transfer_id' => InvalidTransferIDError,
  'invalid_page_token' => InvalidPageError,
  'invalid_page_limit' => InvalidLimitError,
  'already_exists' => AlreadyExistsError,
  'malformed_request' => MalformedRequestError,
  'unsupported_asset' => UnsupportedAssetError,
  'invalid_asset_id' => InvalidAssetIDError,
  'invalid_destination' => InvalidDestinationError,
  'invalid_network_id' => InvalidNetworkIDError,
  'resource_exhausted' => ResourceExhaustedError,
  'faucet_limit_reached' => FaucetLimitReachedError,
  'invalid_signed_payload' => InvalidSignedPayloadError,
  'invalid_transfer_status' => InvalidTransferStatusError,
  'network_feature_unsupported' => NetworkFeatureUnsupportedError
}.freeze
VERSION =
'0.13.0'
GWEI_DECIMALS =

The number of decimal places in Gwei.

9
DEFAULT_PAGE_LIMIT =

The default page limit for paginated API requests.

100
DEFAULT_TRANSACTION_PAGE_LIMIT =

The default page limit for transaction paginated API request.

10

Class Method Summary collapse

Class Method Details

.call_apiObject

Wraps a call to the Platform API to ensure that the error is caught and wrapped as an APIError.



141
142
143
144
145
# File 'lib/coinbase.rb', line 141

def self.call_api
  yield
rescue Coinbase::Client::ApiError => e
  raise Coinbase::APIError.from_error(e), cause: nil
end

.configurationConfiguration

Returns the configuration object.

Returns:



49
50
51
# File 'lib/coinbase.rb', line 49

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ String

Configures the Coinbase SDK.

Yields:

Returns:

  • (String)

    A string indicating successful configuration

Raises:



55
56
57
58
59
60
61
62
# File 'lib/coinbase.rb', line 55

def self.configure
  yield(configuration)

  raise InvalidConfiguration, 'API key private key is not set' unless configuration.api_key_private_key
  raise InvalidConfiguration, 'API key name is not set' unless configuration.api_key_name

  'Successfully configured Coinbase SDK'
end

.configure_from_json(file_path = 'cdp_api_key.json') ⇒ String

Configures the Coinbase SDK from the given CDP API Key JSON file. file in the root directory by default.

Parameters:

  • file_path (String) (defaults to: 'cdp_api_key.json')

    (Optional) the path to the CDP API Key JSON file

Returns:

  • (String)

    A string indicating successful configuration

Raises:



68
69
70
71
72
73
74
75
# File 'lib/coinbase.rb', line 68

def self.configure_from_json(file_path = 'cdp_api_key.json')
  configuration.from_json(file_path)

  raise InvalidConfiguration, 'API key private key is not set' unless configuration.api_key_private_key
  raise InvalidConfiguration, 'API key name is not set' unless configuration.api_key_name

  'Successfully configured Coinbase SDK'
end

.configured?bool

Returns whether the SDK is configured.

Returns:

  • (bool)

    whether the SDK is configured



172
173
174
# File 'lib/coinbase.rb', line 172

def self.configured?
  !Coinbase.configuration.api_key_name.nil? && !Coinbase.configuration.api_key_private_key.nil?
end

.default_networkCoinbase::Network

Returns the default network.

Returns:



166
167
168
# File 'lib/coinbase.rb', line 166

def self.default_network
  Coinbase.configuration.default_network
end

.normalize_network(network) ⇒ String

Converts a Network object or network symbol with the string representation of the network ID, replacing underscores with hyphens.

Parameters:

  • network_sym (Coinbase::Network, Symbol)

    The network or network symbol to convert

Returns:

  • (String)

    The string representation of the network ID



133
134
135
136
137
# File 'lib/coinbase.rb', line 133

def self.normalize_network(network)
  network_sym = network.is_a?(Coinbase::Network) ? network.id : network

  network_sym.to_s.gsub('_', '-')
end

.pretty_print_object(klass, **details) ⇒ String

Returns a pretty-printed object string that contains the object’s class name and the details of the object, filtering out nil values.

Parameters:

  • klass (Class)

    the class of the object

  • details (Hash)

    the details of the object

Returns:

  • (String)

    the pretty-printed object string



152
153
154
155
156
# File 'lib/coinbase.rb', line 152

def self.pretty_print_object(klass, **details)
  filtered_details = details.filter { |_, v| !v.nil? }.map { |k, v| "#{k}: '#{v}'" }

  "#{klass}{#{filtered_details.join(', ')}}"
end

.to_sym(value) ⇒ Symbol

Converts a string to a symbol, replacing hyphens with underscores.

Parameters:

  • string (String)

    the string to convert

Returns:

  • (Symbol)

    the converted symbol



125
126
127
# File 'lib/coinbase.rb', line 125

def self.to_sym(value)
  value.to_s.gsub('-', '_').to_sym
end

.use_server_signer?bool

Returns whether to use a server signer to manage private keys.

Returns:

  • (bool)

    whether to use a server signer to manage private keys.



160
161
162
# File 'lib/coinbase.rb', line 160

def self.use_server_signer?
  Coinbase.configuration.use_server_signer
end