-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
b28deb3
commit 6bd9879
Showing
12 changed files
with
278 additions
and
47 deletions.
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
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
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
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
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
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 = "init-if-needed" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "init_if_needed" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang", features = ["init-if-needed"] } |
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,61 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("BZoppwWi6jMnydnUBEJzotgEXHwLr3b3NramJgZtWeF2"); | ||
|
||
#[program] | ||
pub mod init_if_needed { | ||
use super::*; | ||
|
||
// _val only used to make tx different so that it doesn't result | ||
// in dup tx error | ||
pub fn initialize(ctx: Context<Initialize>, _val: u8) -> Result<()> { | ||
ctx.accounts.acc.val = 1000; | ||
Ok(()) | ||
} | ||
|
||
pub fn second_initialize(ctx: Context<SecondInitialize>, _val: u8) -> Result<()> { | ||
ctx.accounts.acc.other_val = 2000; | ||
Ok(()) | ||
} | ||
|
||
pub fn close(ctx: Context<Close>) -> Result<()> { | ||
ctx.accounts.acc.val = 5000; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[account] | ||
pub struct MyData { | ||
pub val: u64, | ||
} | ||
|
||
#[account] | ||
pub struct OtherData { | ||
pub other_val: u64, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Initialize<'info> { | ||
#[account(init_if_needed, payer = payer, space = 8 + 8)] | ||
pub acc: Account<'info, MyData>, | ||
#[account(mut)] | ||
pub payer: Signer<'info>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct SecondInitialize<'info> { | ||
#[account(init, payer = payer, space = 8 + 8)] | ||
pub acc: Account<'info, OtherData>, | ||
#[account(mut)] | ||
pub payer: Signer<'info>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Close<'info> { | ||
#[account(mut, close = receiver)] | ||
pub acc: Account<'info, MyData>, | ||
#[account(mut)] | ||
pub receiver: UncheckedAccount<'info>, | ||
} |
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 @@ | ||
[scripts] | ||
test = "yarn run ts-mocha -t 1000000 ./tests/init-if-needed/*.ts" |
Oops, something went wrong.