Integrate carax-wallet into Dapp
To build a dapp, connecting the wallet and utilizing it is crucial. It is the way for end users to interact with the blockchain and perform transactions such as transferring funds, moving assets, conducting verification, and signing transactions. For the RENEC blockchain, we have developed and packaged the wallet usage through the "@renec-foundation/wallet-adapter-react" library. Dapps will collectively use this library.
#
GuidelinesInstall:
Ensure you have Node.js and npm installed, then install the wallet adapter library using the following command:
Integrate into Your Dapp:
Ensure that the Dapp projects are developed using ReactJS or NextJS.
First of all, for each project, we need to package the existing code of the current project in the wallet-provider as follows:
app.tsxThere are two crucial attributes:
isMainnet
: Used to determine whether the project is being tested in the testnet or production environment, with a data type of boolean.locale
: Used to display text in the wallet module based on the current locale (default is "en").
Access Wallet State:
The library provides a button component for the Dapp with full functionality to connect the wallet, display text, and surrounding elements.
button.tsxThere are two optional attributes:
style
: Customize button's style (background-color, text color, etc).listMenuItems
: Customize the menu items that are displayed when the button is clicked.
Additionally, if you want to customize the button and only use the wallet connection function of the library, you can use:
Furthermore, to access the wallet state, such as the connected wallet address or the connection to the blockchain, you can use the Solana Web3 library @solana/wallet-adapter-react
Handle transactions
Utilize the wallet provider's functions for handling transactions, signing, and other blockchain interactions.
For Dapps that utilize their own smart contract with a specific program ID, initially, we need to initialize a program variable under the AnchorProvider class and the corresponding IDL for that program (you can write it as a hook or create an instance within your Dapp).
We can use program variable to interact with RENEC blockchain
To execute a transaction on the wallet, we need to create instructions within a transaction and send them to the blockchain for validation. We can follow the steps as follows:
We need to notice that
sendTransactionWithSigners
function. This is function that can help user to interact wallet and send transaction. Besides, RENEC blockchain supported gasless service, users can use it and sign transaction without feeIf you do not want to allow users to use the gasless service, you can directly use the
sendTransaction
function instead ofsendTransactionWithSigners
with the same variables.