Getting Started on your First Project
We're going to use the React standard project as a base for our first project. This will minimise the amount of work that's required to dive into the core topics and get started.
You'll be using the Tari.js library for most of your interactions on the Ootle. The tari.js library has several providers that provide ready access to the Ootle via Universe, the OotleWalletDaemon or WalletConnect.
We'll be using WalletConnect to connect to our Ootle Wallet.
Creating the Logic to connect to your Ootle wallet via WalletConnect
In VS Code, go ahead and open up the App.tsx
file. We'll be adding a basic button to call the WalletConnect provider and connect your wallet to your app.
First, you'll want to add the following line to the beginning of the file (the line to add is highlighted)
This will import the provider for use within your app. Next, you'll want to add these lines - don't worry, we'll go through each of these shortly:
So let's start with the first line:
This initializes a state variableisConnected
with an initial value of false
. The setIsConnected
function is used to update the state when the wallet connection state changes (e.g., after a successful connection or disconnection).
const [errorMessage, setErrorMessage] = useState<string | null>(null); // Track any connection errors
errorMessage
that will either hold an error message string or be null
. setErrorMessage
is the function used to update the errorMessage
state (e.g., if there's an error connecting to the wallet, the error message will be stored here).
const [accountAddress, setAccountAddress] = useState<string | null>(null); // Store the account address
accountAddress
state variable as null
. It will hold the account address of the wallet after a successful connection, and setAccountAddress
is used to update this state.
const projectId = "1825b9dd9c17b5a33063ae91cbc48a6e";
const provider = new WalletConnectTariProvider(projectId);
projectId
with a string value. This projectId
is required by WalletConnect (a library that allows users to connect their wallets to decentralized applications) to authenticate the connection. This is the Tari projectID for WalletConnect.
The provider variable initializes a new provider
using WalletConnectTariProvider
, which is a custom provider for interacting with the WalletConnect API. The projectId
is passed to the provider so that it can authenticate and connect to the right WalletConnect project.
connectToWallet
. The async
keyword allows you to use await
inside the function to wait for promises (e.g., asynchronous operations like connecting to a wallet or fetching data).
This begins a try
block to catch any errors that may occur during the wallet connection process.
This attempts to establish a connection to the wallet by calling connect()
on the provider
. The await
keyword pauses the function until the connection attempt is either successful or throws an error.
If the connection is successful, this updates the isConnected
state to true
, indicating that the wallet is now connected.
This calls the getAccount()
method on the provider
to retrieve the account associated with the wallet. It uses await
to wait for the account details to be fetched.
This updates the accountAddress
state with the account address retrieved from provider.getAccount()
. This is the address of the connected wallet.
If the wallet connection is successful, any previous error message is cleared by setting errorMessage
to null
.
This catches any errors that occur within the try
block. If an error happens during the connection attempt, the code in the catch
block is executed.
If an error occurs, it logs the error to the console with the message "Error connecting to wallet:" to help with debugging.
This sets the errorMessage
state to a user-friendly message: "Failed to connect to wallet." This message can be displayed in the UI to notify the user that there was an issue with the wallet connection.
Since the connection failed, this updates the isConnected
state to false
to indicate that the wallet is not connected.
Adding the button and logic to your app
Next, in the return
section, you'll want to copy and paste the following, with the relevant sections highlighted. The comments indicate what the code does:
The above adds a button to the existing React project, just below the React and Vite logos.