Module: Coinbase

Defined in:
lib/coinbase.rb,
lib/coinbase/user.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/transfer.rb,
lib/coinbase/constants.rb,
lib/coinbase/middleware.rb,
lib/coinbase/balance_map.rb,
lib/coinbase/transaction.rb,
lib/coinbase/authenticator.rb,
lib/coinbase/server_signer.rb,
lib/coinbase/faucet_transaction.rb

Overview

The Coinbase SDK.

Defined Under Namespace

Modules: Client, Middleware Classes: APIError, Address, AlreadyExistsError, Asset, Authenticator, Balance, BalanceMap, Configuration, FaucetLimitReachedError, FaucetTransaction, InternalError, InvalidAddressError, InvalidAddressIDError, InvalidAmountError, InvalidAssetIDError, InvalidConfiguration, InvalidDestinationError, InvalidLimitError, InvalidNetworkIDError, InvalidPageError, InvalidSignedPayloadError, InvalidTransferIDError, InvalidTransferStatusError, InvalidWalletError, InvalidWalletIDError, MalformedRequestError, Network, NetworkFeatureUnsupportedError, NotFoundError, ResourceExhaustedError, ServerSigner, Trade, Transaction, Transfer, UnauthorizedError, UnimplementedError, UnsupportedAssetError, User, Wallet

Constant Summary collapse

ETH =

The Assets supported on Base Sepolia by the Coinbase SDK.

Asset.new(network_id: :base_sepolia, asset_id: :eth, display_name: 'Ether')
USDC =
Asset.new(network_id: :base_sepolia, asset_id: :usdc, display_name: 'USD Coin',
address_id: '0x036CbD53842c5426634e7929541eC2318f3dCF7e')
WETH =
Asset.new(network_id: :base_sepolia, asset_id: :weth, display_name: 'Wrapped Ether',
address_id: '0x4200000000000000000000000000000000000006')
BASE_SEPOLIA =

The Base Sepolia Network.

Network.new(
  network_id: :base_sepolia,
  display_name: 'Base Sepolia',
  protocol_family: :evm,
  is_testnet: true,
  assets: [ETH, USDC],
  native_asset_id: :eth,
  chain_id: 84_532
)
WEI_PER_ETHER =

The amount of Wei per Ether.

1_000_000_000_000_000_000
WEI_PER_GWEI =

The amount of Wei per Gwei.

1_000_000_000
GWEI_PER_ETHER =

The amount of Gwei per Ether.

1_000_000_000
ATOMIC_UNITS_PER_USDC =

The amount of atomic units of USDC per USDC.

1_000_000
SUPPORTED_ASSET_IDS =

A map of supported Asset IDs.

{
  eth: true, # Ether, the native asset of most EVM networks.
  gwei: true, # A medium denomination of Ether, typically used in gas prices.
  wei: true, # The smallest denomination of Ether.
  usdc: true, # USD Coin, a stablecoin pegged to the US Dollar.
  weth: true # Wrapped Ether, the ERC-20 compatible version of Ether.
}.freeze

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.



122
123
124
125
126
127
128
# File 'lib/coinbase.rb', line 122

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

.configurationConfiguration

Returns the configuration object.

Returns:



28
29
30
# File 'lib/coinbase.rb', line 28

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

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

Configures the Coinbase SDK.

Yields:

Returns:

  • (String)

    A string indicating successful configuration

Raises:



34
35
36
37
38
39
40
41
# File 'lib/coinbase.rb', line 34

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:



47
48
49
50
51
52
53
54
# File 'lib/coinbase.rb', line 47

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

.default_userCoinbase::User

Returns the default user.

Returns:



101
102
103
# File 'lib/coinbase.rb', line 101

def self.default_user
  @default_user ||= load_default_user
end

.load_default_userCoinbase::User

Loads the default user.

Returns:



114
115
116
117
118
# File 'lib/coinbase.rb', line 114

def self.load_default_user
  users_api = Coinbase::Client::UsersApi.new(configuration.api_client)
  user_model = users_api.get_current_user
  Coinbase::User.new(user_model)
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



108
109
110
# File 'lib/coinbase.rb', line 108

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.



132
133
134
# File 'lib/coinbase.rb', line 132

def self.use_server_signer?
  Coinbase.configuration.use_server_signer
end