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 install --save @magic-ext/solana

Initializing Extension

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 Transaction

Getting Test LAMPORTS

Before you can send transaction on the Solana blockchain, you'll need to acquire some test LAMPORTS (Solana's native cryptocurrency for test network).

  1. Go to our Solana Example application
  2. Login with your email address
  3. Copy your Solana public address
  4. Use Solana's request air drop function to request test LAMPORTS
  5. 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 a standard Solana blockchain transaction, you can call the magic.solana.sendAndConfirmTransaction method.

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}`);