Interacting with Wallets
API
Wallets
A wallet allows you to store, receive, and send cryptocurrency. It contains one or more public addresses and private keys, which are used to interact with the blockchain. A private key is used to access and control funds stored in a public address. Some wallets also include additional features such as exchange functionality, backup and recovery options, and multi-sig support. It is important to securely store private keys and to use a reputable wallet provider.
There are multiple software wallets, but for this course we are using the Phantom Wallet.
Keypairs
Wallets heavily rely on Keypairs, which was covered in Assignment 2
Wallet Adapters
A wallet adapter is a software component that facilitates communication between a cryptocurrency wallet and a blockchain network. It acts as an interface between the two, converting the data format and protocols used by the wallet to those required by the blockchain and vice versa. This enables the wallet to securely interact with the blockchain, allowing users to store, send and receive cryptocurrency.
For example, in the context of a web wallet, the adapter would handle the connection to the blockchain, processing of transactions, and storage of private keys.
The Solana Wallet Adapter:
Comprises multiple modular packages, including @solana/wallet-adapter-base, @solana/wallet-adapter-react, and packages for specific wallets like @solana/wallet-adapter-phantom.
Contains packages for UI components, such as @solana/wallet-adapter-react-ui
Connecting to Wallets
Package Installations
Sample Connection
Assignment
Create a new React component that displays the balance and a list of transactions of a Solana account.
Use the
@solana/web3.jsAPI to query the balance and transactions of the account.Use the
PhantomWalletAdapterto get the public address of the user's wallet.Display the balance, public address, and a list of transactions (sorted by date) on the component.
Add a button that allows the user to refresh the transaction list.
Use the component in the
Homecomponent as a child of theWalletProvider.
Deliverables:
The updated
Homecomponent that displays the balance, public address, and a list of transactions of the user's Solana account.A brief explanation of how you queried the balance and transactions and used the
PhantomWalletAdapterto get the public address.
Solution and Explanation
Explanation
This React component uses the
useStateanduseEffecthooks to query the balance, public address, and transactions of the user's Solana account.It starts by getting the instance of the
PhantomWalletAdapterwhich returns the public key of the user's wallet.Then, it creates an instance of the
Clientfrom the@solana/web3.jslibrary to query the balance and transactions of the account using the public key.The balance is converted from lamports to SOL, and the transactions are sorted by date.
The component displays the public address, balance, and a list of recent transactions in a list format.
Finally, it has a button that reloads the page to refresh the transaction list.
Once you’re done, make a PR with your react app to the homework repository on Github.
Last updated