Interact with the Contract(Frontend)
Please ensure that all necessary development environments and dependencies are properly installed and configured.
This section will guide you through interacting with your deployed smart contract from the frontend.
First, clone the hello-world
example repository to your local machine. If you have already cloned, skip this and cd to the project directory.
Change into the project directory:
You can now access your front-end page through the local path. The default path in the code is set to the local environment: http://localhost:3000
The process is broken down as follows:
Wallet Connection: Ensure the user is connected to a Solana wallet using
@solana/wallet-adapter-react
.Program Interaction: Use
solana/web3.js
to interact with the Solana blockchain, including creating accounts and sending transactions.RPC Setup: Connect to the SOON Testnet via the RPC URL (https://rpc.testnet.soo.network/rpc).
User Interface: A simple button triggers the interaction, and feedback is displayed using the
toast
library for success or failure.
The following part walks through the code, explaining key sections for setting up your front end to interact with the contract.
useWallet
: This hook allows you to connect and interact with the user's Solana wallet.Connection
: This object establishes the connection with the Solana blockchain via the SOON Testnet RPC URL.PublicKey
,Transaction
,TransactionInstruction
,SystemProgram
: These are Solana web3.js components used to create, sign, and send transactions.
Connecting to SOON Testnet
In the Hero
component, we establish a connection to the SOON Testnet:
Here, we connect to the SOON Testnet by specifying the RPC URL and the Program ID where the smart contract is deployed. Make sure to replace "7qhC7bD9cDV9LTwjgVmGJHs5rGtrMY4pSBw1KswuaBfk"
with your actual program ID.
Function: sayHello
sayHello
This function is triggered when the user clicks the "Say Hello" button. It creates the necessary account on-chain if it doesn't exist, and then sends a "Hello World" transaction.
Check Wallet Connection: The function first checks if the wallet is connected. If not, it prompts the user to connect.
Create Address (Public Key): The address for interacting with the program is derived from the user's public key and the program's seed ("hello").
Creating and Sending Transactions
If the greeting account doesn't exist, a new one is created. Otherwise, it proceeds to send the "Hello World" transaction.
Creating Account: If the account doesn't exist, the transaction creates it, allocating enough lamports to cover the minimum rent exemption.
Sending Transaction: Once the transaction is created, it is signed and sent to the blockchain.
Sending the "Hello" Instruction
Here, the instruction is sent to the program, indicating that the user is saying "Hello" to the program. After successfully sending the transaction, the function confirms it on-chain and displays a success message. Finally, the React component renders a button that triggers the sayHello
function.
Front-end display effect
You can see a simple webpage with a button in the middle and a wallet in the upper right corner.
Click the button to call the wallet to sign the transaction and send it to SOON Testnet RPC.
When you successfully send a transaction, the browser will send a toast to remind you of success.
And now you can check the transaction on SOON Testnet Explorer:
https://explorer.testnet.soo.network/
You can see in the log that the message has been correctly transmitted.
Last updated