GithubHelp home page GithubHelp logo

alexlup06 / solana-transaction-tool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tomoima525/solana-transaction-tool

0.0 0.0 0.0 235 KB

Simple transaction tool for Solana Block Chain

HTML 9.56% CSS 2.39% TypeScript 88.05%

solana-transaction-tool's Introduction

Solana Transaction Tool

  • This React Application showcases the usage of Solana Wallet Adapter and Solana web3
    • Connect to Phantom Wallet
    • Show transactions of a connected account
    • Send lamports through the application using Phantom Wallet
Screen.Recording.2021-08-30.at.11.22.20.AM.mov

Prerequisite

  • Install Phantom Wallet and set your account

How to run

$ yarn install
$ yarn start

By default this app points to "devnet". If you want to point it to the different network, update clusterApiUrl

How does it work?

Connecting Wallet

    <WalletProvider wallets={[PhantomWallet]} onError={onError} autoConnect>
      <WalletDialogProvider>{children}</WalletDialogProvider>
    </WalletProvider>
  • Once that succeeds, you can access your wallet through useWallet hooks
  • useWallet provides wallet information(publicKey, icon) and functionality to sign your transaction

Viewing transactions

  • You need to fetch transaction signatures using web3.Connection
  • We can retrieve transactions using that signatures
  const transSignatures = await connection.getConfirmedSignaturesForAddress2(
    address
  );
  for (let i = 0; i < transSignatures.length; i++) {
    const signature = transSignatures[i].signature;
    const confirmedTransaction = await connection.getConfirmedTransaction(
      signature
    );
    if (confirmedTransaction) {
      const transWithSignature = {
        signature,
        confirmedTransaction,
      };
      transactions.push(transWithSignature);
    }
  }

Sending transactions

  • First create instruction.
    const instruction = SystemProgram.transfer({
      fromPubkey: address!,
      toPubkey: destPubkey,
      lamports,
    });
  • Create a transaction with feePayer(usually yourself), recentBlockHash(this ensures that this transaction is legit) and instruction
  const recentBlockhash = await connection.getRecentBlockhash();
  console.log({ recentBlockhash });
  const options: TransactionCtorFields = {
    feePayer,
    recentBlockhash: recentBlockhash.blockhash,
  };
  const transaction = new Transaction(options);
  transaction.add(instruction);
  • Sign your transaction and send it to the network
const { signTransaction } = useWallet();

const signedTrans = await signTransaction(transaction);
const signature = await connection.sendRawTransaction(
    signedTrans.serialize()
);
  • Finally confirm your transaction
await connection.confirmTransaction(signature, "confirmed");

P.S. If you think this code was useful, tip me some SOL! 6i6zaZdsV9R7JZKFAMNULCdMtzQCSK7ukUpdL4CdPy74

solana-transaction-tool's People

Contributors

tomoima525 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.