Javascript
How to Send and Transfer SOL on Solana
This guide walks you through setting up and minting your very own token on the Solana blockchain using Metaplex packages.
Prerequisite
- Code Editor of your choice (recomended Visual Studio Code)
- Node 18.x.x or above.
Initial Setup
Initializing
Start by initializing a new project (optional) with the package manager of your choice (npm, yarn, pnpm, bun) and fill in required details when prompted.
npm init
Required Packages
Install the required pacakges for this guide.
npm i @metaplex-foundation/umi
npm i @metaplex-foundation/umi-bundle-defaults
npm i @metaplex-foundation/mpl-toolbox;
Imports and Wrapper Function
Here we will define all needed imports for this particular guide and create a wrapper function where all our code will execute.
import { mplToolbox, transferSol } from '@metaplex-foundation/mpl-toolbox'
import {
generateSigner,
publicKey,
signerIdentity,
sol,
} from '@metaplex-foundation/umi'
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
import { base58 } from '@metaplex-foundation/umi/serializers'
// Create the wrapper function
const transferSolana = async () => {
///
///
/// all our code will go in here
///
///
}
// run the wrapper function
transferSolana()
Setting up Umi
This example is going to run through setting up Umi with a generatedSigner()
. If you wish to try this example with React you'll need to setup Umi via the React - Umi w/ Wallet Adapter
guide. Apart from the the wallet setup this guide will for fileStorage keys and wallet adapter.
Generating a New Wallet
const umi = createUmi('https://api.devnet.solana.com')
.use(mplCore())
.use(irysUploader())
// Generate a new keypair signer.
const signer = generateSigner(umi)
// Tell umit to use the new signer.
umi.use(signerIdentity(signer))
// This will airdrop SOL on devnet only for testing.
await umi.rpc.airdrop(umi.identity.publickey)
Use an Existing Wallet
const umi = createUmi('https://api.devnet.solana.com')
.use(mplCore())
.use(irysUploader())
// Generate a new keypair signer.
const signer = generateSigner(umi)
// You will need to us fs and navigate the filesystem to
// load the wallet you wish to use via relative pathing.
const walletFile = const imageFile = fs.readFileSync(
path.join(__dirname, './keypair.json')
)
Transfering Sol
// Here we call the transferSol() function and send it to the chain.
const res = await transferSol(umi, {
source: umi.identity,
destination: publicKey('111111111111111111111111111111'),
amount: sol(1),
}).sendAndConfirm(umi)
Full Code Example
import { mplToolbox, transferSol } from '@metaplex-foundation/mpl-toolbox'
import {
generateSigner,
publicKey,
signerIdentity,
sol,
} from '@metaplex-foundation/umi'
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
import { base58 } from '@metaplex-foundation/umi/serializers'
const transfer = async () => {
const umi = createUmi('https://api.devnet.solana.com').use(mplToolbox())
const signer = generateSigner(umi)
umi.use(signerIdentity(signer))
// Airdrop 1 SOL to the identity
// if you end up with a 429 too many requests error, you may have to use
// the filesystem wallet method or change rpcs.
await umi.rpc.airdrop(umi.identity.publicKey, sol(1))
//
// Transfer SOL
//
const res = await transferSol(umi, {
source: umi.identity,
destination: publicKey('111111111111111111111111111111'),
amount: sol(1),
}).sendAndConfirm(umi)
// Log the signature of the transaction
console.log(base58.deserialize(res.signature))
}
transfer()
Date created: 06-16-2024
Date updated: 06-24-2024