Skip to content

Commit

Permalink
refactored codebase, added poseidon_cross_program_invocation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adpthegreat committed Oct 21, 2024
1 parent 771832c commit 335a143
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ resolution = true
skip-lint = false

[programs.localnet]
cross_program_invocation = "D4aA71us8bTcdXeZQpXyXidW2xPugVwUuoXx3b1bnvXa"
hand = "Cd86dtBUzQKYTFtcB8zDxPRUPCtKPocyetWZSnq6PNxv"
lever = "9aM9w7ozrZwXx9bQHbBx6QjWc6F46tdN9ayt86vt9uLL"

[registry]
url = "https://api.apr.dev"
Expand All @@ -15,4 +16,4 @@ cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
test = "pnpm run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.

const anchor = require("@coral-xyz/anchor");
const anchor = require('@coral-xyz/anchor');

module.exports = async function (provider) {
module.exports = async (provider) => {
// Configure client to use the provider.
anchor.setProvider(provider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1",
"@solana/web3.js": "^1.95.4",
"anchor-bankrun": "^0.5.0",
"solana-bankrun": "^0.4.0"
},
Expand Down

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
@@ -1,12 +1,12 @@
[package]
name = "cross_program_invocation"
name = "hand"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "cross_program_invocation"
name = "hand"

[features]
default = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use anchor_lang::prelude::*;
declare_id!("Cd86dtBUzQKYTFtcB8zDxPRUPCtKPocyetWZSnq6PNxv");
#[program]
pub mod hand {
use super::*;
pub fn initialize(ctx: Context<InitializeContext>) -> Result<()> {
Ok(())
}
pub fn pull_lever(ctx: Context<PullLeverContext>) -> Result<()> {
Ok(())
}
}
#[derive(Accounts)]
pub struct InitializeContext<'info> {
#[account(mut)]
pub power: Signer<'info>,
#[account(mut)]
pub user: Signer<'info>,
}
#[derive(Accounts)]
pub struct PullLeverContext<'info> {
#[account(init, payer = user, space = 8, seeds = [b"hand"], bump)]
pub power: Account<'info, PowerStatus>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct PowerStatus {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "lever"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

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

[features]
default = []
cpi = ["no-entrypoint"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "0.30.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use anchor_lang::prelude::*;
declare_id!("D4aA71us8bTcdXeZQpXyXidW2xPugVwUuoXx3b1bnvXa");
declare_id!("9aM9w7ozrZwXx9bQHbBx6QjWc6F46tdN9ayt86vt9uLL");
#[program]
pub mod cross_program_invocation {
pub mod lever {
use super::*;
pub fn initialize(ctx: Context<InitializeContext>) -> Result<()> {
Ok(())
}
pub fn switch_power(ctx: Context<SwitchPowerContext>, name: String) -> Result<()> {
Ok(())
}
pub fn pull_lever(ctx: Context<PullLeverContext>, name: String) -> Result<()> {
Ok(())
}
pub fn initialize_lever(ctx: Context<InitializeLeverContext>) -> Result<()> {
Ok(())
}
Expand All @@ -20,25 +14,18 @@ pub mod cross_program_invocation {
}
}
#[derive(Accounts)]
pub struct InitializeContext<'info> {}
#[derive(Accounts)]
pub struct SwitchPowerContext<'info> {}
#[derive(Accounts)]
pub struct PullLeverContext<'info> {
#[account()]
pub lever_program: Account<'info, Lever>,
pub struct InitializeContext<'info> {
#[account(mut)]
pub power: Signer<'info>,
#[account(mut)]
pub user: Signer<'info>,
#[account()]
pub power: Account<'info, PowerStatus>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct InitializeLeverContext<'info> {
#[account(init, payer = user, space = 8, seeds = [b"power"], bump)]
pub power: Account<'info, PowerStatus>,
#[account(mut)]
pub user: Signer<'info>,
#[account(init, payer = user, space = 8, seeds = [b"lever"], bump)]
pub power: Account<'info, PowerStatus>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
Expand All @@ -51,5 +38,3 @@ pub struct SetPowerStatusContext<'info> {
}
#[account]
pub struct PowerStatus {}
#[account]
pub struct Lever {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it } from 'node:test';
import * as anchor from '@coral-xyz/anchor';
import { Keypair, PublicKey } from '@solana/web3.js';
import { BankrunProvider } from 'anchor-bankrun';
import { startAnchor } from 'solana-bankrun';
import type { Hand } from '../target/types/hand';
import type { Lever } from '../target/types/lever';

const HAND_IDL = require('../target/idl/hand.json');
const LEVER_IDL = require('../target/idl/lever.json');
const HAND_PROGRAM_ID = new PublicKey(HAND_IDL.address);
const LEVER_PROGRAM_ID = new PublicKey(LEVER_IDL.address);

describe('cpi', async () => {
const context = await startAnchor(
'',
[
{
name: 'hand',
programId: HAND_PROGRAM_ID,
},
{
name: 'lever',
programId: LEVER_PROGRAM_ID,
},
],
[],
);
const provider = new BankrunProvider(context);

const hand = new anchor.Program<Hand>(HAND_IDL, provider);
const lever = new anchor.Program<Lever>(LEVER_IDL, provider);

// Generate a new keypair for the power account
const powerAccount = new anchor.web3.Keypair();

it('Initialize the lever!', async () => {
await lever.methods
.initialize()
.accounts({
power: powerAccount.publicKey,
user: provider.wallet.publicKey,
})
.signers([powerAccount])
.rpc();
});

it('Pull the lever!', async () => {
await hand.methods.pullLever().accounts({}).rpc();
});
});

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Account, Pubkey, Result, Signer } from '@solanaturbine/poseidon';

export default class Hand {
static PROGRAM_ID = new Pubkey('Cd86dtBUzQKYTFtcB8zDxPRUPCtKPocyetWZSnq6PNxv');

initialize(user: Signer, power: Signer) {}

pullLever(
user: Signer,
power: PowerStatus,
// name: String
): Result {
power.derive(['hand']).init();
}
// switchPower(name: String) {}
}

export interface PowerStatus extends Account {
// is_on: bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Account, Pubkey, Result, Signer } from '@solanaturbine/poseidon';

export default class Lever {
static PROGRAM_ID = new Pubkey('9aM9w7ozrZwXx9bQHbBx6QjWc6F46tdN9ayt86vt9uLL');

initialize(user: Signer, power: Signer) {}

// switchPower(name: String) {}

initializeLever(user: Signer, power: PowerStatus): Result {
power.derive(['lever']).init();
}

setPowerStatus(user: Signer, power: PowerStatus): Result {}
}

export interface PowerStatus extends Account {
// is_on: bool
}

0 comments on commit 335a143

Please sign in to comment.