Browse Source

recommended layout native

jpcaulfi 3 years ago
parent
commit
5ef8b723b9

+ 8 - 0
program-basics/recommended-program-layout/native/cicd.sh

@@ -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-bpf --manifest-path=./program/Cargo.toml --bpf-out-dir=./program/target/so
+solana program deploy ./program/target/so/program.so

+ 18 - 0
program-basics/recommended-program-layout/native/package.json

@@ -0,0 +1,18 @@
+{
+  "scripts": {
+    "test": "yarn run ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts"
+  },
+  "dependencies": {
+    "@solana/web3.js": "^1.47.3",
+    "fs": "^0.0.1-security"
+  },
+  "devDependencies": {
+    "@types/bn.js": "^5.1.0",
+    "@types/chai": "^4.3.1",
+    "@types/mocha": "^9.1.1",
+    "chai": "^4.3.4",
+    "mocha": "^9.0.3",
+    "ts-mocha": "^10.0.0",
+    "typescript": "^4.3.5"
+  }
+}

+ 12 - 0
program-basics/recommended-program-layout/native/program/Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "program"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+borsh = "0.9.3"
+borsh-derive = "0.9.1"
+solana-program = "1.10.12"
+
+[lib]
+crate-type = ["cdylib", "lib"]

+ 19 - 0
program-basics/recommended-program-layout/native/program/src/entrypoint.rs

@@ -0,0 +1,19 @@
+use solana_program::{
+    account_info::AccountInfo, 
+    entrypoint, 
+    entrypoint::ProgramResult, 
+    pubkey::Pubkey,
+};
+
+
+entrypoint!(process_instruction);
+
+
+fn process_instruction(
+    _program_id: &Pubkey,
+    _accounts: &[AccountInfo],
+    instruction_data: &[u8],
+) -> ProgramResult {
+
+    crate::processor::process_instruction(_program_id, _accounts, instruction_data)
+}

+ 2 - 0
program-basics/recommended-program-layout/native/program/src/error.rs

@@ -0,0 +1,2 @@
+
+// For any custom errors

+ 9 - 0
program-basics/recommended-program-layout/native/program/src/instruction.rs

@@ -0,0 +1,9 @@
+use borsh::{BorshDeserialize, BorshSerialize};
+
+// the various types of instruction data possible
+
+#[derive(BorshSerialize, BorshDeserialize, Debug)]
+pub struct InstructionData {
+    pub name: String,
+    pub height: u32,
+}

+ 8 - 0
program-basics/recommended-program-layout/native/program/src/lib.rs

@@ -0,0 +1,8 @@
+
+// For setting up modules & lib configs
+
+pub mod entrypoint;
+pub mod error;
+pub mod instruction;
+pub mod processor;
+pub mod state;

+ 30 - 0
program-basics/recommended-program-layout/native/program/src/processor.rs

@@ -0,0 +1,30 @@
+use solana_program::{
+    account_info::AccountInfo, 
+    entrypoint::ProgramResult, 
+    msg, 
+    pubkey::Pubkey,
+};
+use borsh::BorshDeserialize;
+use crate::instruction::InstructionData;
+
+
+// For processing the instructions
+
+
+pub fn process_instruction(
+    _program_id: &Pubkey,
+    _accounts: &[AccountInfo],
+    instruction_data: &[u8],
+) -> ProgramResult {
+
+    let instruction_data_object = InstructionData::try_from_slice(&instruction_data)?;
+
+    msg!("Welcome to the park, {}!", instruction_data_object.name);
+    if instruction_data_object.height > 5 {
+        msg!("You are tall enough to ride this ride. Congratulations.");
+    } else {
+        msg!("You are NOT tall enough to ride this ride. Sorry mate.");
+    };
+
+    Ok(())
+}

+ 2 - 0
program-basics/recommended-program-layout/native/program/src/state.rs

@@ -0,0 +1,2 @@
+
+// For any object- or type-oriented logic (such as impl)

+ 83 - 0
program-basics/recommended-program-layout/native/tests/test.ts

@@ -0,0 +1,83 @@
+import {
+    Connection,
+    Keypair,
+    sendAndConfirmTransaction,
+    Transaction,
+    TransactionInstruction,
+} from '@solana/web3.js';
+import * as borsh from "borsh";
+import { Buffer } from "buffer";
+
+
+function createKeypairFromFile(path: string): Keypair {
+    return Keypair.fromSecretKey(
+        Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
+    )
+};
+
+
+describe("custom-instruction-data", () => {
+
+    const connection = new Connection(`http://localhost:8899`, 'confirmed');
+    const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
+    const program = createKeypairFromFile('./program/target/so/program-keypair.json');
+
+    class Assignable {
+        constructor(properties) {
+            Object.keys(properties).map((key) => {
+                return (this[key] = properties[key]);
+            });
+        };
+    };
+
+    class InstructionData extends Assignable {};
+
+    const InstructionDataSchema = new Map([
+        [
+            InstructionData, {
+                kind: 'struct',
+                fields: [
+                    ['name', 'string'],
+                    ['height', 'u32'],
+                ]
+            }
+        ]
+    ]);
+  
+    it("Go to the park!", async () => {
+
+        const jimmy = new InstructionData({
+            name: "Jimmy",
+            height: 3
+        });
+
+        const mary = new InstructionData({
+            name: "Mary",
+            height: 10
+        });
+
+        function toBuffer(obj: InstructionData): Buffer {
+            return Buffer.from(borsh.serialize(InstructionDataSchema, obj));
+        }
+
+        let ix1 = new TransactionInstruction({
+            keys: [
+                {pubkey: payer.publicKey, isSigner: true, isWritable: true}
+            ],
+            programId: program.publicKey,
+            data: toBuffer(jimmy),
+        });
+
+        let ix2 = new TransactionInstruction({
+            ...ix1,
+            data: toBuffer(mary),
+        });
+
+        await sendAndConfirmTransaction(
+            connection, 
+            new Transaction().add(ix1).add(ix2),
+            [payer]
+        );
+    });
+  });
+  

+ 10 - 0
program-basics/recommended-program-layout/native/tsconfig.json

@@ -0,0 +1,10 @@
+{
+  "compilerOptions": {
+    "types": ["mocha", "chai"],
+    "typeRoots": ["./node_modules/@types"],
+    "lib": ["es2015"],
+    "module": "commonjs",
+    "target": "es6",
+    "esModuleInterop": true
+  }
+}