Giter Site home page Giter Site logo

solana-super-basic-example's Introduction

Simplest Rust program I could make

Run

Edit the variables in cli/index.ts as the comments tell you, then:

In one terminal:

cargo build-bpf
solana program deploy target/deploy/scratch.so

Take note of the deployed program ID and then:

solana logs <that ID>

In another terminal:

cd cli
yarn install
# Make sure that the solana logs command is still
# running before you run the next line
npx ts-node index.ts

Then check the logs terminal and be happy.

How I created this from scratch

I built this by doing:

cargo new --lib scratch

Added this to Cargo.toml:

[dependencies]
solana-program = "1.9.6" # use latest

[lib]
crate-type = ["cdylib", "lib"]

Edited lib.rs to be a program that prints the 3 arguments on the entrypoint:

use solana_program::{
    account_info::AccountInfo,
    entrypoint,
    entrypoint::ProgramResult,
    msg,
    pubkey::Pubkey,
};

entrypoint!(process_instruction);

fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8]
) -> ProgramResult {
    
    msg!(
        "process_instruction: {}: {} accounts, data={:?}",
        program_id,
        accounts.len(),
        instruction_data
    );

    Ok(())
}

Created a CLI with:

mkdir cli
cd cli
yarn init
yarn add --dev typescript @types/node @solana/web3.js
touch index.ts

Edited package.json main to index.ts instead of index.js, and added this to index.ts:

import web3 = require("@solana/web3.js");

const SECRET = [];      // cat ~/.config/solana/id.json (not safe don't share the key)
const PROGRAMID = "";   // whatever you get after `solana program deploy target/deploy/scratch.so`

const conn = new web3.Connection(web3.clusterApiUrl("devnet"));
const key: Uint8Array = Uint8Array.from(SECRET);
const programId = new web3.PublicKey(PROGRAMID);

async function main() {
    const signer: web3.Keypair = web3.Keypair.fromSecretKey(key);

    const transaction: web3.Transaction = new web3.Transaction().add(
        new web3.TransactionInstruction({
            keys: [],
            programId,
            data: Buffer.from([14, 27, 49]) // random numbers
        })
    );

    await web3.sendAndConfirmTransaction(conn, transaction, [signer]);
}

main()
    .then(() => process.exit(0))
    .catch((err) => {
        console.error(err);
        process.exit(1);
    });

solana-super-basic-example's People

Contributors

guidodipietro avatar

Stargazers

 avatar

Watchers

 avatar  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.