Module: Coinbase

Defined in:
lib/coinbase.rb,
lib/coinbase/user.rb,
lib/coinbase/asset.rb,
lib/coinbase/errors.rb,
lib/coinbase/wallet.rb,
lib/coinbase/address.rb,
lib/coinbase/network.rb,
lib/coinbase/transfer.rb,
lib/coinbase/constants.rb,
lib/coinbase/middleware.rb,
lib/coinbase/balance_map.rb,
lib/coinbase/authenticator.rb,
lib/coinbase/faucet_transaction.rb

Overview

The Coinbase SDK.

Defined Under Namespace

Modules: Client, Middleware Classes: APIError, Address, AlreadyExistsError, Asset, Authenticator, BalanceMap, Configuration, FaucetLimitReached, FaucetLimitReachedError, FaucetTransaction, InternalError, InvalidAddressError, InvalidAddressIDError, InvalidAmountError, InvalidAssetIDError, InvalidConfiguration, InvalidDestinationError, InvalidLimitError, InvalidNetworkIDError, InvalidPageError, InvalidSignedPayloadError, InvalidTransferIDError, InvalidTransferStatusError, InvalidWalletError, InvalidWalletIDError, MalformedRequestError, Network, NotFoundError, ResourceExhaustedError, 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.



137
138
139
140
141
142
143
# File 'lib/coinbase.rb', line 137

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:



25
26
27
# File 'lib/coinbase.rb', line 25

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

.configure {|configuration| ... } ⇒ Object

Configures the Coinbase SDK.

Yields:

Raises:



30
31
32
33
34
35
# File 'lib/coinbase.rb', line 30

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
end

.configure_from_json(file_path = 'coinbase_cloud_api_key.json') ⇒ Object

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: 'coinbase_cloud_api_key.json')

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

Raises:



40
41
42
43
44
45
# File 'lib/coinbase.rb', line 40

def self.configure_from_json(file_path = 'coinbase_cloud_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
end

.default_userCoinbase::User

Returns the default user.

Returns:



92
93
94
# File 'lib/coinbase.rb', line 92

def self.default_user
  @default_user ||= load_default_user
end

.load_default_userCoinbase::User

Loads the default user.

Returns:



129
130
131
132
133
# File 'lib/coinbase.rb', line 129

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_balance_map(address_balance_list) ⇒ BalanceMap

Converts a Coinbase::Client::AddressBalanceList to a BalanceMap.

Parameters:

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/coinbase.rb', line 106

def self.to_balance_map(address_balance_list)
  balances = {}

  address_balance_list.data.each do |balance|
    asset_id = Coinbase.to_sym(balance.asset.asset_id.downcase)
    amount = case asset_id
             when :eth
               BigDecimal(balance.amount) / BigDecimal(Coinbase::WEI_PER_ETHER)
             when :usdc
               BigDecimal(balance.amount) / BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC)
             when :weth
               BigDecimal(balance.amount) / BigDecimal(Coinbase::WEI_PER_ETHER)
             else
               BigDecimal(balance.amount)
             end
    balances[asset_id] = amount
  end

  BalanceMap.new(balances)
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



99
100
101
# File 'lib/coinbase.rb', line 99

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