Solana
Installation
Magic interacts with the Solana blockchain via Magic's extension NPM package @magic-ext/solana
. The Solana extension also lets you interact with the blockchain using methods from Solana's Javascript SDK.
note
You can skip straight to our kitchen sink example directly:
๐ Solana Example
- NPM
- Yarn
npm install --save @magic-ext/solana
Initializing Extension
- ES Modules/TypeScript
- CDN
import { Magic } from 'magic-sdk';
import { SolanaExtension } from '@magic-ext/solana';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new SolanaExtension({
rpcUrl: 'SOLANA_RPC_NODE_URL'
})
]
});
Send and Sign Transaction
Getting Test LAMPORTS
Before you can send or sign transaction on the Solana blockchain, you'll need to acquire some test LAMPORTS (Solana's native cryptocurrency for test network).
- Go to our Solana Example application
- Login with your email address
- Copy your Solana public address
- Use Solana's request air drop function to request test LAMPORTS
- Now you can use your test LAMPORTS in our example app
Call Extension Method
Note that the Magic Solana extension follows the method names and conventions by Solana's Javascript SDK. To send or sign a standard Solana blockchain transaction, you can call the magic.solana.sendAndConfirmTransaction
or magic.solana.signTransaction
method.
- ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { SolanaExtension } from '@magic-ext/solana';
import * as web3 from "@solana/web3.js";
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new SolanaExtension({
rpcUrl: 'SOLANA_RPC_NODE_URL'
})
]
});
const metadata = await magic.user.getMetadata();
const transaction = web3.SystemProgram.transfer({
fromPubkey: metadata.publicAddress,
toPubkey: destinationAddress,
lamports: sendAmount
});
const signature = await magic.solana.sendAndConfirmTransaction(transaction);
console.log(`Transaction signature: ${signature}`);
// or sign transaction only
const signedTransaction = await magic.solana.signTransaction(transaction);
console.log("Signed transaction", signedTransaction);