SoLibrary
  • Solana
  • The Meme Coin Problem and Solution
  • Developing on Solana
  • From Rust To Deployment
  • Solana Blockchain Explorer
  • Building a Solana dAPP
  • Deploying a Solana dApp
  • Deploying a Solana Memecoin using CLI
  • Solana Smart Contracts
  • Send Solana via javascript functions
  • Candy Machine
  • Pump fun APIs
  • Metaplex
  • Metaplex Program Library
  • Solana Program Library
  • UMI Framework
  • Umi and Web3js Differences
  • Fetching Accounts
  • UMI Helpers
  • HTTP Requests
  • Umi's Interfaces
  • Interface implementations
  • Kinobi
  • UMI Plugins
  • Registering Programs
  • Public keys and Signers
  • Connecting RPCS
  • Serializer
  • Storage
  • Transactions
  • Web 3.JS Adapters
  • Metaplex Umi Plugins
  • Core JS SDK v1.0
  • Local Validator
  • SolScriptions
  • FAQ
  • Initialize
  • Write Inscription Data
  • Fetch
  • Clear
  • Close
  • Authority
  • Sharding
  • Getting Started using JavaScript
  • Getting started using the Inscriptions CLI
  • Core Candy Machine
  • Getting Started using JavaScript
  • Candy Guard
  • Assets
  • Creating a Core Candy Machine
  • Inserting Items
  • Updating The Core Candy Machine
  • Guard Groups
  • Special Guard Instructions
  • Fetching a Core Candy Machine
  • Minting
  • Withdrawing a Core Candy Machine
  • Address Gate Guard
  • Allocation
  • Allowlist Guard
  • Asset Burn Guard
  • Asset Burn Multi
  • Asset Payment Guard
  • Asset Payment Multi
  • Asset Mint Limit
  • Bot Tax Guard
  • End Date Guard
  • Edition
  • Freeze Sol Payment guard
  • Freeze Token Payment Guard
  • Gatekeeper Guard
  • Mint Limit Guard
  • NFT Burn Guard
  • NFT Gate Guard
  • NFT Mint Limit Guard
  • NFT Payment Guard
  • Program Gate Guard
  • Redeemed Amount Guard
  • Sol Fixed Fee Guard
  • Sol Payment Guard
  • Start Date Guard
  • Third Party Signer Guard
  • Token Burn Guard
  • Token Gate Guard
  • Token Payment Guard
  • Token2022 Payment Guard
  • Generating Custom Guard Client for Core Candy Machine
Powered by GitBook
On this page
  • Step 1: Init JavaScript Project
  • Step 2: Install modules
  • Step 3: Create JavaScript script file
  • Step 4: Include installed modules
  • Step 5: Init connection and create Keypair object
  • Step 6: Create Trasnaction

Send Solana via javascript functions

quick guide how you can send transaction to Solana blockchain using Solana JavaScript SDK.

Step 1: Init JavaScript Project

npm init

Step 2: Install modules

npm install -S @solana/web3.js bs58

@solana/web3.js — Solana JavaScript SDK bs58 — encoding library

Step 3: Create JavaScript script file

You can use any text editor. Create an empty file with .js extension. For e.g: test.js

Step 4: Include installed modules

At the beginning of created file we will include required modules:

const web3 = require('@solana/web3.js');
const bs58 = require('bs58');

Step 5: Init connection and create Keypair object

const connection = new web3.Connection('https://api.mainnet-beta.solana.com', 'confirmed');

const privateKey = bs58.decode('619G6W.....XaWz4');
const from = web3.Keypair.fromSecretKey(privateKey)

In constant privateKey we will load wallet private key (you can find it in the settings of your wallet)

We will save Keypair object to constant from.

Step 6: Create Trasnaction

(async () => {
    const transaction = new web3.Transaction().add(
        web3.SystemProgram.transfer({
          fromPubkey: from.publicKey,
          toPubkey: '9aq15...NzYHSJdxMa5t',
          lamports: 900000
        }),
      );
    
      const signature = await web3.sendAndConfirmTransaction(
        connection,
        transaction,
        [from],
      );
      
      console.log('SIGNATURE', signature);
})()

At the second string we create transaction constant. We will pass to web3.Transaction().add() method result of calling web3.SystemProgram.transfer() method with following arguments:

  • fromPubkey — publicKey property of Keypair object

  • toPubkey — string with SOL address to where we want to send SOL (lamports)

  • lamports — amount of lamports that we want to send

signature constant contains a result of execution web3.sendAndConfirmTransaction() method where we passed connection constant, transaction data and sender Keypair object.

Expected output of following code is a transaction hash:

SIGNATURE MifLinkmkV.....wEjnDxi25Gcb4HfoU9DM7dfLoCyw3U6tmpznczMivTj

PreviousSolana Smart ContractsNextCandy Machine

Last updated 10 months ago

As an argument for web3.Connection method we will use any Solana RPC. For e.g default mainnet RPC:

fromPubkey also may be a string * toPubkey also may be a Keypair object (in this case we will use publicKey property) * lamports is a piece of SOL. We can use this for better understanding

https://api.mainnet-beta.solana.com
converter