Skip to main content

Example Repository

A complete working example using this integration approach.
This guide covers integrating Sonar using the @echoxyz/sonar-react library. All authentication and API calls happen client-side, with tokens stored in browser storage.
For apps with a backend, consider the Frontend with Backend approach for improved security.

How it works

Installation

Configuration

Wrap your app in SonarProvider:
Find your OAuth client UUID and configure your redirect URI in the Echo founder dashboard.

Implementing the OAuth flow

Sonar uses OAuth to allow users to authenticate and authorize your application to access their data. You will need buttons that allow users to login and logout from Sonar. Logging in will redirect the user to the Sonar site to login. If they haven’t yet authorized your application, they will be prompted to do so.
Sonar will redirect the user back to your application at your redirectURI with code and state querystring parameters. You will need a page at your redirectURI that will handle the OAuth callback.

Fetching the state of a user’s Sonar entities

You can fetch the state of a user’s Sonar entities with the useSonarEntities hook.
Reference docs for the useSonarEntity hook. See the example app EntityStateDescription for more detail on how to interpret the state and display it to the user.

Running pre-purchase checks

Once the sale is live, you can use the useSonarPurchase hook to run pre-purchase checks. Reference docs for the useSonarPurchase hook. If the entity is not ready to purchase, the reason why will be returned in the failureReason field. If a liveness check is required, the livenessCheckURL field will be returned and you should redirect the user to this URL to complete the liveness check.
See the example app PurchaseCard for more detail on how to interpret the state and display it to the user.

Generating a purchase permit to send to the sale contract

If the entity is ready to purchase and you are running an onchain sale, you will need to:
  1. Use the generatePurchasePermit function to fetch a signed purchase permit from Sonar
  2. Construct a transaction including the purchase permit and have the user sign it
  3. Send the transaction to the sale contract
In this example we are going to use the wagmi library. First, you will need to get a copy of the sale contract’s ABI and include it in your project. If you are using the generic SettlementSale contract, you can find the ABI here. Then, let’s create a hook for managing the transaction lifecycle.
Then in a component, we can use this hook and call the returned commitWithPermit function as part of a button click handler.
If you are implementing your own contract, then you must validate that the purchase permit has been signed by Sonar and that the permit data is valid. See the custom contracts guide for more details.

Further reading

Retrieving user investment history

This is an optional feature for retrieving users past Echo private group investments.
Once authenticated, you can retrieve a user’s investment history using the useEntityInvestmentHistory hook. This shows all deals they’ve previously participated in, which can be useful if you are intending to give reputational boost/extra points if they have invested in a particulat deal. Reference docs for the useEntityInvestmentHistory hook.