Solana-Web3.js
Learn how to integrate Capsule with Solana using @usecapsule/solana-web3.js-v1-integration
This guide will walk you through the process of setting up Capsule with Solana using the
@usecapsule/solana-web3.js-v1-integration
package.
Installation
First, you’ll need to install the necessary packages:
npm install @usecapsule/solana-web3.js-v1-integration @solana/web3.js
Setting Up Capsule with Solana
Here’s how to set up Capsule with Solana:
import { CapsuleSolanaWeb3Signer } from '@usecapsule/solana-web3.js-v1-integration';
import * as solana from '@solana/web3.js';
import Capsule, { Environment } from '@usecapsule/web-sdk';
// Initialize Capsule
const capsule = new Capsule(Environment.BETA, YOUR_API_KEY, {
supportedWalletTypes: { SOLANA: true }
});
// Set up the Solana connection
const connection = new solana.Connection(solana.clusterApiUrl('devnet'), 'confirmed');
// Create the Capsule Solana Signer
const solanaSigner = new CapsuleSolanaWeb3Signer(capsule, connection);
Usage
Once you’ve set up the CapsuleSolanaWeb3Signer
, you can use it to interact with the Solana
network. Here’s a basic example:
// Get the signer's public key
const publicKey = solanaSigner.sender;
console.log('Signer public key:', publicKey.toBase58());
// Get the balance
const balance = await connection.getBalance(publicKey);
console.log('Balance:', balance / solana.LAMPORTS_PER_SOL, 'SOL');
Signing Transactions
To sign and send a transaction:
const transaction = new solana.Transaction().add(
solana.SystemProgram.transfer({
fromPubkey: solanaSigner.sender,
toPubkey: new solana.PublicKey('RECIPIENT_ADDRESS'),
lamports: solana.LAMPORTS_PER_SOL * 0.1, // 0.1 SOL
})
);
try {
const signature = await solanaSigner.sendTransaction(transaction, {
skipPreflight: false,
preflightCommitment: 'confirmed',
});
console.log('Transaction sent:', signature);
} catch (error) {
console.error('Error sending transaction:', error);
}
Server-Side Usage
If you need to use Capsule with Solana on the server-side, please check our Server-Side Signing Guide for specific instructions.
Remember to always handle errors appropriately and ensure your user is authenticated with Capsule before attempting to use the signer.
For more advanced usage and a complete API reference, please refer to the Solana Web3.js documentation.