Skip to content

add pinocchio example for hello solana program #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ members = [
"basics/cross-program-invocation/anchor/programs/*",
"basics/hello-solana/native/program",
"basics/hello-solana/anchor/programs/*",
"basics/hello-solana/pinocchio/program",
"basics/pda-rent-payer/native/program",
"basics/pda-rent-payer/anchor/programs/*",
"basics/processing-instructions/native/program",
8 changes: 8 additions & 0 deletions basics/hello-solana/pinocchio/cicd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This script is for quick building & deploying of the program.
# It also serves as a reference for the commands used for building & deploying Solana programs.
# Run this bad boy with "bash cicd.sh" or "./cicd.sh"

cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so
solana program deploy ./program/target/so/program.so
22 changes: 22 additions & 0 deletions basics/hello-solana/pinocchio/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/index.test.ts",
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
"deploy": "solana program deploy ./program/target/so/hello_solana_program_pinocchio.so"
},
"dependencies": {
"@solana/web3.js": "^1.47.3"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^9.1.1",
"@types/node": "^22.15.2",
"chai": "^4.3.4",
"mocha": "^9.0.3",
"solana-bankrun": "^0.3.0",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
}
1,352 changes: 1,352 additions & 0 deletions basics/hello-solana/pinocchio/pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions basics/hello-solana/pinocchio/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "hello-solana-program-pinocchio"
version = "0.1.0"
edition = "2021"

[dependencies]
pinocchio = "=0.8.1"
pinocchio-log = "0.4.0"


[lib]
crate-type = ["cdylib", "rlib"]
27 changes: 27 additions & 0 deletions basics/hello-solana/pinocchio/program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use pinocchio::{
account_info::AccountInfo,
default_panic_handler, no_allocator, program_entrypoint,
pubkey::{self, Pubkey},
ProgramResult,
};
use pinocchio_log::log;

// This is the program entrypoint.
program_entrypoint!(process_instruction);
// Do not allocate memory.
no_allocator!();
// Use the default panic handler.
default_panic_handler!();

#[inline(always)]
fn process_instruction(
program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
log!("Hello, Solana!");

pubkey::log(program_id);

Ok(())
}
48 changes: 48 additions & 0 deletions basics/hello-solana/pinocchio/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
PublicKey,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";
import { assert } from "chai";
import { ProgramTestContext, start } from "solana-bankrun";

describe("hello-solana", () => {
const PROGRAM_ID = PublicKey.unique();

// load program in solana-bankrun
let context: ProgramTestContext;
before(async () => {
context = await start(
[{ name: "hello_solana_program_pinocchio", programId: PROGRAM_ID }],
[]
);
});

it("Say hello!", async () => {
const client = context.banksClient;
const payer = context.payer;
const blockhash = context.lastBlockhash;
// We set up our instruction first.
const ix = new TransactionInstruction({
keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
programId: PROGRAM_ID,
data: Buffer.from([]), // No data
});

const tx = new Transaction();
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer);

// Now we process the transaction
const transaction = await client.processTransaction(tx);

assert(transaction.logMessages[0].startsWith(`Program ${PROGRAM_ID}`));
assert(transaction.logMessages[1] === "Program log: Hello, Solana!");
assert(transaction.logMessages[2] === `Program log: ${PROGRAM_ID}`);
assert(
transaction.logMessages[3].startsWith(`Program ${PROGRAM_ID} consumed`)
);
assert(transaction.logMessages[4] === `Program ${PROGRAM_ID} success`);
assert(transaction.logMessages.length === 5);
});
});
10 changes: 10 additions & 0 deletions basics/hello-solana/pinocchio/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"types": ["mocha", "chai", "node"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}