Overview
TheFund
component provides a comprehensive solution for implementing cryptocurrency purchasing flows using fiat currency. It is designed to be highly customizable and composable, allowing developers to create tailored onramp experiences while maintaining a consistent and secure purchasing process.
The core features include:
- Support for multiple payment methods (e.g., debit card, bank transfer)
- Real-time exchange rate fetching and display
- Preset amount inputs for quick selection
- Multi-view interface (form, error, transaction status)
- Composable UI that gives developers full control over layout and styling
- Integrated transaction monitoring and status updates
- Centralized state management for the purchasing process
Architecture
The component is built using a composition pattern that allows for maximum flexibility while providing sensible defaults when customization is not needed.Composition model
TheFund
component is composed of several subcomponents that work together to create the complete purchasing experience. This approach allows developers to customize the UI structure while maintaining the underlying functionality.
The main components are:
Fund
: The root wrapper component that provides theFundContext
and handles state managementFundTitle
: Renders the title for the fund interface (defaults to “Deposit [cryptoCurrency]”)FundForm
: Manages the multi-view form interface for amount input + payment selection, error screens, and transaction statusFundFooter
: The “Secured by Coinbase” footer component
Fund
TheFund
component accepts a children
prop that can be either React nodes or a render function. When using a render function, it receives the current FundState
as an argument, providing access to all state values without needing to use the useFundContext
hook directly.
Example of children
as a render function:
State management (FundProvider and FundContext)
The entire purchasing flow’s state is managed byFundProvider
and accessed via the useFundContext
hook. This context contains:
country
,subdivision
: User’s location for regulatory compliancecryptoCurrency
,fiatCurrency
: Selected currencies for the transactionnetwork
: The blockchain network for the purchasecryptoAmount
,fiatAmount
: The amounts being purchased/spentselectedInputType
: Whether the user is entering crypto or fiat amountsselectedPaymentMethod
: The chosen payment methodexchangeRate
: Current exchange rate between currenciestransactionStatus
: Current status of the transaction (init, error, transactionSuccess, etc.)error
: Any error that occurred during the processpaymentMethods
: Available payment methods based on user location
FundForm
TheFundForm
component is the core of the purchasing interface, managing the display of different views based on the state. It handles three main views:
form
: The main input interface for amount selection and payment methoderror
: Error display when something goes wrongtransaction-status
: Shows the status of an ongoing or completed transaction
FundForm
provides a children
render prop that receives an object containing the current view
and the Content
component. This allows for wrapping the form content or injecting additional components based on the current state and view.
FundTitle
TheFundTitle
component displays a contextual title for the fund interface. By default, it shows “Deposit [cryptoCurrency]” but can be customized by providing children. It accepts an as
prop to render as different HTML elements (default is h2
).
Transaction lifecycle
The Fund component monitors and reflects a complete transaction lifecycle with the following statuses:init
: Initial state, ready for user inputexit
: User has exited the flowerror
: An error occurred during the processtransactionSuccess
: Transaction completed successfullytransactionPending
: Transaction is being processed
onStatus
, onError
, and onSuccess
callbacks provided to the Fund component.
Example: Basic usage
Implement a simple cryptocurrency purchase interface:Example: Custom layout with page title
Customize the layout and add a page-level title:Example: Monitoring transaction lifecycle
Track and respond to transaction status changes:API Reference
Required Props
country
: ISO 3166-1 alpha-2 country codecryptoCurrency
: The cryptocurrency to purchase (e.g., “ETH”, “BTC”)fiatCurrency
: The fiat currency to use for payment (e.g., “USD”, “EUR”)network
: The blockchain network for the purchasefetchBuyQuote
: Async function to fetch exchange rate quotesfetchBuyOptions
: Async function to fetch available payment methods
Optional Props
subdivision
: ISO 3166-2 subdivision code (e.g., “NY” for New York)presetAmountInputs
: Array of preset fiat amounts for quick selectioninputType
: Initial input type (“fiat” or “crypto”)openIn
: Where to open the payment window (“tab” or “popup”)submitLabel
: Custom label for the submit buttontitle
: Custom title for the fund interfacelocale
: Locale for formatting (defaults to browser locale)cryptoDecimalPlaces
: Number of decimal places for crypto amountsfiatDecimalPlaces
: Number of decimal places for fiat amountsonStatus
: Callback for transaction status changesonSuccess
: Callback for successful transactionsonError
: Callback for transaction errorschildren
: React nodes or render function receiving FundState
Notes
- Exchange rates are fetched in real-time to ensure accurate pricing
- Payment processing is handled by Coinbase’s secure infrastructure
- The component handles regulatory requirements based on user location