Skip to content

Fixes to native program example on CPI #392

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion basics/cross-program-invocation/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
67 changes: 67 additions & 0 deletions basics/cross-program-invocation/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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(
Expand Down
53 changes: 42 additions & 11 deletions basics/cross-program-invocation/native/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
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));
}
}
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));
}
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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);
});
});