From bc06a250c3065510e801733d687ade2b351b0227 Mon Sep 17 00:00:00 2001 From: 4rjunc Date: Wed, 25 Jun 2025 09:48:03 +0530 Subject: [PATCH 1/3] fix: borsh `0.10` -> borsh `1.5.7` - change brosh version --- .../cross-program-invocation/native/programs/hand/Cargo.toml | 3 +-- .../cross-program-invocation/native/programs/lever/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/basics/cross-program-invocation/native/programs/hand/Cargo.toml b/basics/cross-program-invocation/native/programs/hand/Cargo.toml index c904eb64..f074294f 100644 --- a/basics/cross-program-invocation/native/programs/hand/Cargo.toml +++ b/basics/cross-program-invocation/native/programs/hand/Cargo.toml @@ -8,8 +8,7 @@ no-entrypoint = [] cpi = ["no-entrypoint"] [dependencies] -borsh = "0.10" -borsh-derive = "0.10" +borsh = "1.5.7" solana-program = "2.0" cross-program-invocatio-native-lever = { path = "../lever", features = ["cpi"] } diff --git a/basics/cross-program-invocation/native/programs/lever/Cargo.toml b/basics/cross-program-invocation/native/programs/lever/Cargo.toml index 893fc812..58ca4222 100644 --- a/basics/cross-program-invocation/native/programs/lever/Cargo.toml +++ b/basics/cross-program-invocation/native/programs/lever/Cargo.toml @@ -8,7 +8,7 @@ no-entrypoint = [] cpi = ["no-entrypoint"] [dependencies] -borsh = "0.10" +borsh = "1.5.7" borsh-derive = "0.10" solana-program = "2.0" From 0cb7f3a03a91423254f6b32a7818e38abc73896a Mon Sep 17 00:00:00 2001 From: 4rjunc Date: Wed, 25 Jun 2025 09:48:28 +0530 Subject: [PATCH 2/3] fix: `.try_to_vec` -> `to_vec()` --- .../cross-program-invocation/native/programs/lever/Cargo.toml | 1 - .../cross-program-invocation/native/programs/lever/src/lib.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/basics/cross-program-invocation/native/programs/lever/Cargo.toml b/basics/cross-program-invocation/native/programs/lever/Cargo.toml index 58ca4222..88c32a2b 100644 --- a/basics/cross-program-invocation/native/programs/lever/Cargo.toml +++ b/basics/cross-program-invocation/native/programs/lever/Cargo.toml @@ -9,7 +9,6 @@ cpi = ["no-entrypoint"] [dependencies] borsh = "1.5.7" -borsh-derive = "0.10" solana-program = "2.0" [lib] diff --git a/basics/cross-program-invocation/native/programs/lever/src/lib.rs b/basics/cross-program-invocation/native/programs/lever/src/lib.rs index 98251154..c58c3a7c 100644 --- a/basics/cross-program-invocation/native/programs/lever/src/lib.rs +++ b/basics/cross-program-invocation/native/programs/lever/src/lib.rs @@ -1,4 +1,4 @@ -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::{to_vec, BorshDeserialize, BorshSerialize}; #[cfg(not(feature = "no-entrypoint"))] use solana_program::entrypoint; use solana_program::{ @@ -42,7 +42,7 @@ pub fn initialize( let user = next_account_info(accounts_iter)?; let system_program = next_account_info(accounts_iter)?; - let account_span = (power_status.try_to_vec()?).len(); + let account_span = (to_vec(&power_status))?.len(); let lamports_required = (Rent::get()?).minimum_balance(account_span); invoke( From de35e5138fd9b1ddf817d7674ff10da861bd6b62 Mon Sep 17 00:00:00 2001 From: 4rjunc Date: Wed, 25 Jun 2025 10:09:34 +0530 Subject: [PATCH 3/3] fix: add `solana-bankrun` to testcase --- .../native/package.json | 3 +- .../native/pnpm-lock.yaml | 67 +++++++++++++++++++ .../native/tests/test.ts | 53 ++++++++++++--- 3 files changed, 111 insertions(+), 12 deletions(-) diff --git a/basics/cross-program-invocation/native/package.json b/basics/cross-program-invocation/native/package.json index e4562921..f6269f69 100644 --- a/basics/cross-program-invocation/native/package.json +++ b/basics/cross-program-invocation/native/package.json @@ -9,7 +9,8 @@ "@solana/web3.js": "^1.47.3", "borsh": "^0.7.0", "buffer": "^6.0.3", - "fs": "^0.0.1-security" + "fs": "^0.0.1-security", + "solana-bankrun": "^0.4.0" }, "devDependencies": { "@types/bn.js": "^5.1.0", diff --git a/basics/cross-program-invocation/native/pnpm-lock.yaml b/basics/cross-program-invocation/native/pnpm-lock.yaml index 144f2a38..242c4a24 100644 --- a/basics/cross-program-invocation/native/pnpm-lock.yaml +++ b/basics/cross-program-invocation/native/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: fs: specifier: ^0.0.1-security version: 0.0.1-security + solana-bankrun: + specifier: ^0.4.0 + version: 0.4.0(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) devDependencies: '@types/bn.js': specifier: ^5.1.0 @@ -512,6 +515,39 @@ packages: serialize-javascript@6.0.0: resolution: {integrity: sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==} + solana-bankrun-darwin-arm64@0.4.0: + resolution: {integrity: sha512-6dz78Teoz7ez/3lpRLDjktYLJb79FcmJk2me4/YaB8WiO6W43OdExU4h+d2FyuAryO2DgBPXaBoBNY/8J1HJmw==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [darwin] + + solana-bankrun-darwin-universal@0.4.0: + resolution: {integrity: sha512-zSSw/Jx3KNU42pPMmrEWABd0nOwGJfsj7nm9chVZ3ae7WQg3Uty0hHAkn5NSDCj3OOiN0py9Dr1l9vmRJpOOxg==} + engines: {node: '>= 10'} + os: [darwin] + + solana-bankrun-darwin-x64@0.4.0: + resolution: {integrity: sha512-LWjs5fsgHFtyr7YdJR6r0Ho5zrtzI6CY4wvwPXr8H2m3b4pZe6RLIZjQtabCav4cguc14G0K8yQB2PTMuGub8w==} + engines: {node: '>= 10'} + cpu: [x64] + os: [darwin] + + solana-bankrun-linux-x64-gnu@0.4.0: + resolution: {integrity: sha512-SrlVrb82UIxt21Zr/XZFHVV/h9zd2/nP25PMpLJVLD7Pgl2yhkhfi82xj3OjxoQqWe+zkBJ+uszA0EEKr67yNw==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + solana-bankrun-linux-x64-musl@0.4.0: + resolution: {integrity: sha512-Nv328ZanmURdYfcLL+jwB1oMzX4ZzK57NwIcuJjGlf0XSNLq96EoaO5buEiUTo4Ls7MqqMyLbClHcrPE7/aKyA==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + solana-bankrun@0.4.0: + resolution: {integrity: sha512-NMmXUipPBkt8NgnyNO3SCnPERP6xT/AMNMBooljGA3+rG6NN8lmXJsKeLqQTiFsDeWD74U++QM/DgcueSWvrIg==} + engines: {node: '>= 10'} + source-map-support@0.5.21: resolution: {integrity: sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==} @@ -1145,6 +1181,37 @@ snapshots: dependencies: randombytes: 2.1.0 + solana-bankrun-darwin-arm64@0.4.0: + optional: true + + solana-bankrun-darwin-universal@0.4.0: + optional: true + + solana-bankrun-darwin-x64@0.4.0: + optional: true + + solana-bankrun-linux-x64-gnu@0.4.0: + optional: true + + solana-bankrun-linux-x64-musl@0.4.0: + optional: true + + solana-bankrun@0.4.0(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10): + dependencies: + '@solana/web3.js': 1.98.2(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) + bs58: 4.0.1 + optionalDependencies: + solana-bankrun-darwin-arm64: 0.4.0 + solana-bankrun-darwin-universal: 0.4.0 + solana-bankrun-darwin-x64: 0.4.0 + solana-bankrun-linux-x64-gnu: 0.4.0 + solana-bankrun-linux-x64-musl: 0.4.0 + transitivePeerDependencies: + - bufferutil + - encoding + - typescript + - utf-8-validate + source-map-support@0.5.21: dependencies: buffer-from: 1.1.2 diff --git a/basics/cross-program-invocation/native/tests/test.ts b/basics/cross-program-invocation/native/tests/test.ts index f9e300f9..809770a9 100644 --- a/basics/cross-program-invocation/native/tests/test.ts +++ b/basics/cross-program-invocation/native/tests/test.ts @@ -1,26 +1,41 @@ import { Buffer } from 'node:buffer'; +import { readFileSync } from 'node:fs'; +import { homedir } from 'node:os'; import { Connection, Keypair, SystemProgram, Transaction, TransactionInstruction, sendAndConfirmTransaction } from '@solana/web3.js'; import * as borsh from 'borsh'; +import { start } from 'solana-bankrun'; + function createKeypairFromFile(path: string): Keypair { - return Keypair.fromSecretKey(Buffer.from(JSON.parse(require('node:fs').readFileSync(path, 'utf-8')))); + return Keypair.fromSecretKey(Buffer.from(JSON.parse(readFileSync(path, 'utf-8')))); } -describe('CPI Example', () => { - const connection = new Connection('http://localhost:8899', 'confirmed'); - const payer = createKeypairFromFile(`${require('node:os').homedir()}/.config/solana/id.json`); - const hand = createKeypairFromFile('./target/so/hand-keypair.json'); - const lever = createKeypairFromFile('./target/so/lever-keypair.json'); +describe('CPI Example', async () => { + //const connection = new Connection('http://localhost:8899', 'confirmed'); + + const hand = createKeypairFromFile('./target/deploy/cross_program_invocatio_native_hand-keypair.json'); + const lever = createKeypairFromFile('./target/deploy/cross_program_invocatio_native_lever-keypair.json'); + + + const context = await start([ + { name: 'cross_program_invocatio_native_hand', programId: hand.publicKey }, + { name: 'cross_program_invocatio_native_lever', programId: lever.publicKey } + ], []) + + const client = context.banksClient; + const payer = context.payer; class Assignable { - constructor(properties) { + constructor(properties: any) { for (const [key, value] of Object.entries(properties)) { - this[key] = value; + (this as any)[key] = value; } } } class PowerStatus extends Assignable { + is_on!: number; + toBuffer() { return Buffer.from(borsh.serialize(PowerStatusSchema, this)); } @@ -28,6 +43,8 @@ describe('CPI Example', () => { const PowerStatusSchema = new Map([[PowerStatus, { kind: 'struct', fields: [['is_on', 'u8']] }]]); class SetPowerStatus extends Assignable { + name!: string; + toBuffer() { return Buffer.from(borsh.serialize(SetPowerStatusSchema, this)); } @@ -47,7 +64,12 @@ describe('CPI Example', () => { data: new PowerStatus({ is_on: true }).toBuffer(), }); - await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, powerAccount]); + + const tx = new Transaction(); + tx.recentBlockhash = context.lastBlockhash; + tx.add(ix).sign(payer); + + await client.processTransaction(tx); }); it('Pull the lever!', async () => { @@ -60,7 +82,11 @@ describe('CPI Example', () => { data: new SetPowerStatus({ name: 'Chris' }).toBuffer(), }); - await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]); + const tx = new Transaction(); + tx.recentBlockhash = context.lastBlockhash; + tx.add(ix).sign(payer); + + await client.processTransaction(tx); }); it('Pull it again!', async () => { @@ -73,6 +99,11 @@ describe('CPI Example', () => { data: new SetPowerStatus({ name: 'Ashley' }).toBuffer(), }); - await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]); + + const tx = new Transaction(); + tx.recentBlockhash = context.lastBlockhash; + tx.add(ix).sign(payer); + + await client.processTransaction(tx); }); });