Solana- Story of Wallets & Smart Contracts
By Lekshmi PG, R & D Engineer, Kerala Blockchain Academy
Prefer watching videos over reading text? Check out our YouTube video on Solana Wallets and Smart Contracts here.
In the previous article, we have been playing with the small waves of Solana. Now, let’s take a little deeper dive. In this article, we are going to explore wallets and smart contracts. We are picking the Phantom wallet, which is also the community pick for Solana. But there are other wallets like Slope, Solflare, Glow, Torus, etc.
Setting Up Phantom
First things first: Let’s add it to the browser. Go to the Phantom Wallet site and click Download for Firefox (here, it depends on your browser).
Then, a tab will come asking for permission to add Phantom Wallet to the browser
Then, a pop-up will appear asking permission to add Phantom as an extension to the browser.
Add it, and your browser will now have the phantom wallet extension. You can pin the extension for easy access.
Now, we are going to create a new Wallet. To do so, click on Create a new wallet option.
Now, you have to enter a strong password for your wallet.
After this, you can see the Secret Phrase(12 English words). So you have to back up this recovery phrase, and we are Done.
By default, the phantom is connected to Solana, Ethereum, and Polygon mainnets. But for development purposes, we need to connect to the test networks. To do so, open the menu and go to Settings.
If we look at the Active Networks, we can only see the connections to mainnets. Go back and scroll down. Select Developer Settings and enable Testnet Mode, which allows us to connect to various test networks.
We will go with Solana Devnet. Both Testnet and Devnet are Solana’s test networks, with Devnet configured for quick development and debugging. Meanwhile, the Testnet is configured to resemble the Solana Mainnet. Now, as you can see, we are in Testnet Mode, and we are connected to Devnet with zero SOL. Let’s change that by getting some SOLs from the faucet sites.
For that, we can use Solana Faucet, copy the address from the wallet, and use it to request Devnet test SOL and confirm the airdrop in the Solana Explorer and the Phantom wallet.
And our Solana Devnet got credited with 5 SOL.
We can do the same for Testnet. So, our wallet is ready; let’s move on to development.
Solana Playground
Let’s explore Solana Playground, an online IDE that enables us to code Solana smart contracts, build them, deploy them to Devnet, Testnet, or the Mainnet, and interact with them.
We can create a new project or pick a tutorial and follow the flow. Here, we chose the Hello Solana tutorial, which will give a clear-cut idea for writing a Hello World smart contract and interacting with it. This will provide us with a minimal project setup.
As we can see, it has already imported the necessary modules for us.
It has an account_info module for accessing the account information, an entry point module for setting the entry point to the program and program output, a msg module for logging out messages, and a pubkey module for the identity of the smart contracts. First, let’s start by creating a starting point for our contract; every interaction within the contract will begin from the process_instruction function.
process_instruction function takes in three arguments. The program_id is the contract address. Accounts related to the contract, any input parameters when calling the function. All these will be made available by the Solana run time, at the time of executing this function. Now, for the function body., we will log out the message Hello World.
Let’s try building the contract.
We got a warning here, and that’s because we forgot to set the process instruction function as the entry point to our contract.
Now define the entrypoint.
Let us build again. Great, everything seems good.
Now, let’s deploy the contract. For that, first, Connect to the Playground wallet.
So, Wallet is now connected.
Now you can deploy your contract using the Deploy button.
Finally, the Contract is deployed successfully to Solana Devnet.
Building Client in Playground
Now, we need to create a client file to interact with the deployed contract. To do that, just go to the Client section in the tutorial, which will, in turn, generate the file for us- client.ts.
This is typescript. In client.ts, We can start by logging out the Program ID, aka the contract address. To create our own transaction to interact with the contract, we need to provide the latest block hash information. With that, we will create an instance of transaction. Then, add instructions to call the function to output the Hello World message. For which we need to pass in the program ID, then any accounts associated with our execution and any input data, both of which are empty for this particular operation. Now, Sign the transaction with our wallet. Send the transaction, and then console.log the transaction hash. Let us run the script using the Run button in the file Explorer.
The output will be like this.
Copy the transaction hash, and use the command Solana confirm -v transaction-hash to get the transaction log.
We can see the transaction receipt and the Hello World message.
So, this was a small sneak peek into Solana’s vast development ecosystem. Let’s explore in deep later.
References