Skip to content

Commit e76ac6c

Browse files
author
jpcaulfi
committed
newer examples
1 parent 3ed97da commit e76ac6c

File tree

55 files changed

+1199
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1199
-30
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.template
22

3+
test-ledger/
4+
35
**/*/node_modules
46
**/*/package-lock.json
57
**/*/Cargo.lock

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ Regardless of what you may want to add on top of existing Solana programs, the n
3838
:mag: *Got something you want to see here? Add it to the list. Or better yet, write one & create a PR!*
3939
* ### [ ] Basics
4040
* [x] Hello Solana
41-
* [x] Processing Instructions
42-
* [x] Repository Layout
4341
* [x] Checking Accounts
4442
* [x] Create an Account
45-
* [ ] Assign Data to an Account
43+
* [x] Assign Data to an Account
4644
* [x] Rent
4745
* [ ] Reallocate an Account's Data
4846
* [ ] Transfer an Account's Ownership
4947
* [ ] Destroy an Account
48+
* [x] Processing Instructions
49+
* [x] Create a Simple PDA
50+
* [ ] PDAs Expanded
51+
* [x] Repository Layout
5052
* [x] Transfer SOL
5153
* [ ] Stake SOL with a Validator
5254
* [x] Cross-program Invocation
53-
* [ ] Create a Simple PDA
54-
* [ ] PDAs Expanded
55-
* [ ] PDAs as a Database
5655
* ### [ ] Tokens
5756
* [ ] Create an SPL Token
5857
* [ ] Token Metadata
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[features]
2+
seeds = false
3+
[programs.localnet]
4+
anchor_program_example = "FFKtnYFyzPj1qFjE9epkrfYHJwZMdh8CvJrB6XsKeFVz"
5+
6+
[registry]
7+
url = "https://anchor.projectserum.com"
8+
9+
[provider]
10+
cluster = "localnet"
11+
wallet = "~/.config/solana/id.json"
12+
13+
[scripts]
14+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

basics/account-data/anchor/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
6+
[profile.release]
7+
overflow-checks = true
8+
lto = "fat"
9+
codegen-units = 1
10+
[profile.release.build-override]
11+
opt-level = 3
12+
incremental = false
13+
codegen-units = 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"dependencies": {
3+
"@project-serum/anchor": "^0.24.2"
4+
},
5+
"devDependencies": {
6+
"@types/bn.js": "^5.1.0",
7+
"@types/chai": "^4.3.0",
8+
"@types/mocha": "^9.0.0",
9+
"chai": "^4.3.4",
10+
"mocha": "^9.0.3",
11+
"ts-mocha": "^10.0.0",
12+
"typescript": "^4.3.5"
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "anchor-program-example"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "anchor_program_example"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
18+
[dependencies]
19+
anchor-lang = "0.24.2"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::state::AddressInfo;
4+
5+
6+
pub fn create_address_info(
7+
ctx: Context<CreateAddressInfo>,
8+
name: String,
9+
house_number: u8,
10+
street: String,
11+
city: String,
12+
) -> Result<()> {
13+
14+
let address_info = AddressInfo::new(
15+
name,
16+
house_number,
17+
street,
18+
city,
19+
);
20+
21+
let account_span = (address_info.try_to_vec()?).len();
22+
let lamports_required = (Rent::get()?).minimum_balance(account_span);
23+
24+
system_program::create_account(
25+
CpiContext::new(
26+
ctx.accounts.system_program.to_account_info(),
27+
system_program::CreateAccount {
28+
from: ctx.accounts.payer.to_account_info(),
29+
to: ctx.accounts.address_info.to_account_info(),
30+
},
31+
),
32+
lamports_required,
33+
account_span as u64,
34+
&ctx.accounts.system_program.key(),
35+
)?;
36+
37+
let address_info_account = &mut ctx.accounts.address_info;
38+
address_info_account.set_inner(address_info);
39+
Ok(())
40+
}
41+
42+
#[derive(Accounts)]
43+
pub struct CreateAddressInfo<'info> {
44+
#[account(mut)]
45+
address_info: Account<'info, AddressInfo>,
46+
#[account(mut)]
47+
payer: Signer<'info>,
48+
system_program: Program<'info, System>,
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod create;
2+
3+
pub use create::*;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use anchor_lang::prelude::*;
2+
3+
use instructions::*;
4+
5+
pub mod instructions;
6+
pub mod state;
7+
8+
9+
declare_id!("FFKtnYFyzPj1qFjE9epkrfYHJwZMdh8CvJrB6XsKeFVz");
10+
11+
12+
#[program]
13+
pub mod anchor_program_example {
14+
use super::*;
15+
16+
pub fn create_address_info(
17+
ctx: Context<CreateAddressInfo>,
18+
name: String,
19+
house_number: u8,
20+
street: String,
21+
city: String,
22+
) -> Result<()> {
23+
24+
instructions::create::create_address_info(
25+
ctx,
26+
name,
27+
house_number,
28+
street,
29+
city,
30+
)
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use anchor_lang::prelude::*;
2+
3+
4+
#[account]
5+
pub struct AddressInfo {
6+
pub name: String,
7+
pub house_number: u8,
8+
pub street: String,
9+
pub city: String,
10+
}
11+
12+
impl AddressInfo {
13+
14+
pub fn new(
15+
name: String,
16+
house_number: u8,
17+
street: String,
18+
city: String,
19+
) -> Self {
20+
AddressInfo {
21+
name,
22+
house_number,
23+
street,
24+
city,
25+
}
26+
}
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod address_info;
2+
3+
pub use address_info::*;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as anchor from "@project-serum/anchor";
2+
import { AnchorProgramExample } from "../target/types/anchor_program_example";
3+
4+
describe("Account Data!", () => {
5+
6+
const provider = anchor.AnchorProvider.env();
7+
anchor.setProvider(provider);
8+
const payer = provider.wallet as anchor.Wallet;
9+
const program = anchor.workspace.AnchorProgramExample as anchor.Program<AnchorProgramExample>;
10+
11+
const addressInfoAccount = anchor.web3.Keypair.generate();
12+
13+
it("Create the address info account", async () => {
14+
console.log(`Payer Address : ${payer.publicKey}`);
15+
console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`);
16+
await program.methods.createAddressInfo(
17+
"Joe C",
18+
136,
19+
"Mile High Dr.",
20+
"Solana Beach",
21+
)
22+
.accounts({
23+
addressInfo: addressInfoAccount.publicKey,
24+
payer: payer.publicKey,
25+
systemProgram: anchor.web3.SystemProgram.programId,
26+
})
27+
.signers([payer.payer])
28+
.rpc();
29+
});
30+
31+
it("Read the new account's data", async () => {
32+
const addressInfo = await program.account.addressInfo.fetch(
33+
addressInfoAccount.publicKey
34+
);
35+
console.log(`Name : ${addressInfo.name}`);
36+
console.log(`House Num: ${addressInfo.houseNumber}`);
37+
console.log(`Street : ${addressInfo.street}`);
38+
console.log(`City : ${addressInfo.city}`);
39+
});
40+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "commonjs",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
10+
}

basics/account-data/native/cicd.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# This script is for quick building & deploying of the program.
4+
# It also serves as a reference for the commands used for building & deploying Solana programs.
5+
# Run this bad boy with "bash cicd.sh" or "./cicd.sh"
6+
7+
cargo build-bpf --manifest-path=./program/Cargo.toml --bpf-out-dir=./program/target/so
8+
solana program deploy ./program/target/so/program.so
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"scripts": {
3+
"test": "yarn run ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts"
4+
},
5+
"dependencies": {
6+
"@solana/web3.js": "^1.47.3",
7+
"fs": "^0.0.1-security"
8+
},
9+
"devDependencies": {
10+
"@types/bn.js": "^5.1.0",
11+
"@types/chai": "^4.3.1",
12+
"@types/mocha": "^9.1.1",
13+
"chai": "^4.3.4",
14+
"mocha": "^9.0.3",
15+
"ts-mocha": "^10.0.0",
16+
"typescript": "^4.3.5"
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "program"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
solana-program = "1.10.12"
8+
borsh = "0.9.3"
9+
borsh-derive = "0.9.1"
10+
11+
[lib]
12+
crate-type = ["cdylib", "lib"]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use borsh::{ BorshDeserialize, BorshSerialize };
2+
use solana_program::{
3+
account_info::{ AccountInfo, next_account_info },
4+
entrypoint::ProgramResult,
5+
program::invoke,
6+
pubkey::Pubkey,
7+
rent::Rent,
8+
system_instruction,
9+
system_program,
10+
sysvar::Sysvar,
11+
};
12+
13+
use crate::state::AddressInfo;
14+
15+
16+
pub fn create_address_info(
17+
program_id: &Pubkey,
18+
accounts: &[AccountInfo],
19+
address_info: AddressInfo,
20+
) -> ProgramResult {
21+
22+
let accounts_iter = &mut accounts.iter();
23+
let address_info_account = next_account_info(accounts_iter)?;
24+
let payer = next_account_info(accounts_iter)?;
25+
let system_program = next_account_info(accounts_iter)?;
26+
27+
let account_span = (address_info.try_to_vec()?).len();
28+
let lamports_required = (Rent::get()?).minimum_balance(account_span);
29+
30+
invoke(
31+
&system_instruction::create_account(
32+
&payer.key,
33+
&address_info_account.key,
34+
lamports_required,
35+
account_span as u64,
36+
program_id,
37+
),
38+
&[
39+
payer.clone(), address_info_account.clone(), system_program.clone()
40+
]
41+
)?;
42+
43+
address_info.serialize(&mut &mut address_info_account.data.borrow_mut()[..])?;
44+
Ok(())
45+
}
46+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod create;
2+
3+
pub use create::*;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use solana_program::entrypoint;
2+
3+
use processor::process_instruction;
4+
5+
pub mod instructions;
6+
pub mod processor;
7+
pub mod state;
8+
9+
10+
entrypoint!(process_instruction);

0 commit comments

Comments
 (0)