-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e653a58
commit 5b4b1d3
Showing
13 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[programs.localnet] | ||
declare_program = "Dec1areProgram11111111111111111111111111111" | ||
external = "Externa111111111111111111111111111111111111" | ||
|
||
[provider] | ||
cluster = "localnet" | ||
wallet = "~/.config/solana/id.json" | ||
|
||
[scripts] | ||
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[workspace] | ||
members = [ | ||
"programs/*" | ||
] | ||
resolver = "2" | ||
|
||
[profile.release] | ||
overflow-checks = true | ||
lto = "fat" | ||
codegen-units = 1 | ||
[profile.release.build-override] | ||
opt-level = 3 | ||
incremental = false | ||
codegen-units = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
{ | ||
"address": "Externa111111111111111111111111111111111111", | ||
"metadata": { | ||
"name": "external", | ||
"version": "0.1.0", | ||
"spec": "0.1.0", | ||
"description": "Created with Anchor" | ||
}, | ||
"instructions": [ | ||
{ | ||
"name": "init", | ||
"discriminator": [ | ||
220, | ||
59, | ||
207, | ||
236, | ||
108, | ||
250, | ||
47, | ||
100 | ||
], | ||
"accounts": [ | ||
{ | ||
"name": "authority", | ||
"writable": true, | ||
"signer": true | ||
}, | ||
{ | ||
"name": "my_account", | ||
"writable": true, | ||
"pda": { | ||
"seeds": [ | ||
{ | ||
"kind": "account", | ||
"path": "authority" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"name": "system_program", | ||
"address": "11111111111111111111111111111111" | ||
} | ||
], | ||
"args": [] | ||
}, | ||
{ | ||
"name": "update", | ||
"discriminator": [ | ||
219, | ||
200, | ||
88, | ||
176, | ||
158, | ||
63, | ||
253, | ||
127 | ||
], | ||
"accounts": [ | ||
{ | ||
"name": "authority", | ||
"signer": true | ||
}, | ||
{ | ||
"name": "my_account", | ||
"writable": true, | ||
"pda": { | ||
"seeds": [ | ||
{ | ||
"kind": "account", | ||
"path": "authority" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"args": [ | ||
{ | ||
"name": "value", | ||
"type": "u32" | ||
} | ||
] | ||
} | ||
], | ||
"accounts": [ | ||
{ | ||
"name": "MyAccount", | ||
"discriminator": [ | ||
246, | ||
28, | ||
6, | ||
87, | ||
251, | ||
45, | ||
50, | ||
42 | ||
] | ||
} | ||
], | ||
"types": [ | ||
{ | ||
"name": "MyAccount", | ||
"type": { | ||
"kind": "struct", | ||
"fields": [ | ||
{ | ||
"name": "field", | ||
"type": "u32" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "declare-program", | ||
"version": "0.29.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
"homepage": "https://github.com/coral-xyz/anchor#readme", | ||
"bugs": { | ||
"url": "https://github.com/coral-xyz/anchor/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/coral-xyz/anchor.git" | ||
}, | ||
"engines": { | ||
"node": ">=17" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "declare-program" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
rust-version = "1.60" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "declare_program" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
idl-build = ["anchor-lang/idl-build"] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("Dec1areProgram11111111111111111111111111111"); | ||
|
||
declare_program!(external); | ||
use external::program::External; | ||
|
||
#[program] | ||
pub mod declare_program { | ||
use super::*; | ||
|
||
pub fn cpi(ctx: Context<Cpi>, value: u32) -> Result<()> { | ||
let cpi_my_account = &mut ctx.accounts.cpi_my_account; | ||
require_keys_eq!(external::accounts::MyAccount::owner(), external::ID); | ||
require_eq!(cpi_my_account.field, 0); | ||
|
||
let cpi_ctx = CpiContext::new( | ||
ctx.accounts.external_program.to_account_info(), | ||
external::cpi::accounts::Update { | ||
authority: ctx.accounts.authority.to_account_info(), | ||
my_account: cpi_my_account.to_account_info(), | ||
}, | ||
); | ||
external::cpi::update(cpi_ctx, value)?; | ||
|
||
cpi_my_account.reload()?; | ||
require_eq!(cpi_my_account.field, value); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Cpi<'info> { | ||
pub authority: Signer<'info>, | ||
#[account(mut)] | ||
pub cpi_my_account: Account<'info, external::accounts::MyAccount>, | ||
pub external_program: Program<'info, External>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "external" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
rust-version = "1.60" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
idl-build = ["anchor-lang/idl-build"] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("Externa111111111111111111111111111111111111"); | ||
|
||
#[program] | ||
pub mod external { | ||
use super::*; | ||
|
||
pub fn init(_ctx: Context<Init>) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
pub fn update(ctx: Context<Update>, value: u32) -> Result<()> { | ||
ctx.accounts.my_account.field = value; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Init<'info> { | ||
#[account(mut)] | ||
pub authority: Signer<'info>, | ||
#[account( | ||
init, | ||
payer = authority, | ||
space = 8 + 4, | ||
seeds = [authority.key.as_ref()], | ||
bump | ||
)] | ||
pub my_account: Account<'info, MyAccount>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Update<'info> { | ||
pub authority: Signer<'info>, | ||
#[account(mut, seeds = [authority.key.as_ref()], bump)] | ||
pub my_account: Account<'info, MyAccount>, | ||
} | ||
|
||
#[account] | ||
pub struct MyAccount { | ||
pub field: u32, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import assert from "assert"; | ||
|
||
import type { DeclareProgram } from "../target/types/declare_program"; | ||
import type { External } from "../target/types/external"; | ||
|
||
describe("declare-program", () => { | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
const program: anchor.Program<DeclareProgram> = | ||
anchor.workspace.declareProgram; | ||
const externalProgram: anchor.Program<External> = anchor.workspace.external; | ||
|
||
it("Can CPI", async () => { | ||
const { pubkeys } = await externalProgram.methods.init().rpcAndKeys(); | ||
|
||
const value = 5; | ||
await program.methods | ||
.cpi(value) | ||
.accounts({ cpiMyAccount: pubkeys.myAccount }) | ||
.rpc(); | ||
|
||
const myAccount = await externalProgram.account.myAccount.fetch( | ||
pubkeys.myAccount | ||
); | ||
assert.strictEqual(myAccount.field, value); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true, | ||
"strict": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters