Skip to content

Commit b041a2a

Browse files
committed
feat: project 12
1 parent 1e0dfac commit b041a2a

File tree

36 files changed

+918
-0
lines changed

36 files changed

+918
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.anchor
2+
.DS_Store
3+
target
4+
**/*.rs.bk
5+
node_modules
6+
test-ledger
7+
.yarn
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.anchor
2+
.DS_Store
3+
target
4+
node_modules
5+
dist
6+
build
7+
test-ledger
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[toolchain]
2+
3+
[features]
4+
resolution = true
5+
skip-lint = false
6+
7+
[programs.localnet]
8+
bank_one = "HAAzYF1LMBi8R2avDaa4s2VZyMyGzA1RGQNFPXPjctZo"
9+
10+
[registry]
11+
url = "https://api.apr.dev"
12+
13+
[provider]
14+
cluster = "Localnet"
15+
wallet = "~/.config/solana/id.json"
16+
17+
[scripts]
18+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
resolver = "2"
6+
7+
[profile.release]
8+
overflow-checks = true
9+
lto = "fat"
10+
codegen-units = 1
11+
[profile.release.build-override]
12+
opt-level = 3
13+
incremental = false
14+
codegen-units = 1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Migrations are an early feature. Currently, they're nothing more than this
2+
// single deploy script that's invoked from the CLI, injecting a provider
3+
// configured from the workspace's Anchor.toml.
4+
5+
const anchor = require("@coral-xyz/anchor");
6+
7+
module.exports = async function (provider) {
8+
// Configure client to use the provider.
9+
anchor.setProvider(provider);
10+
11+
// Add your deploy script here.
12+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"license": "ISC",
3+
"scripts": {
4+
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
5+
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
6+
},
7+
"dependencies": {
8+
"@coral-xyz/anchor": "^0.30.1"
9+
},
10+
"devDependencies": {
11+
"chai": "^4.3.4",
12+
"mocha": "^9.0.3",
13+
"ts-mocha": "^10.0.0",
14+
"@types/bn.js": "^5.1.0",
15+
"@types/chai": "^4.3.0",
16+
"@types/mocha": "^9.0.0",
17+
"typescript": "^4.3.5",
18+
"prettier": "^2.6.2"
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "bank_one"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "bank_one"
10+
11+
[features]
12+
default = []
13+
cpi = ["no-entrypoint"]
14+
no-entrypoint = []
15+
no-idl = []
16+
no-log-ix-name = []
17+
idl-build = ["anchor-lang/idl-build"]
18+
19+
[dependencies]
20+
anchor-lang = { version = "0.30.1", features = ["init-if-needed"] }
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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use anchor_lang::prelude::*;
2+
use anchor_lang::system_program::{transfer, Transfer};
3+
4+
declare_id!("HAAzYF1LMBi8R2avDaa4s2VZyMyGzA1RGQNFPXPjctZo");
5+
6+
#[program]
7+
pub mod bank_one {
8+
use super::*;
9+
10+
pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
11+
*ctx.accounts.bank = Bank {
12+
authority: ctx.accounts.authority.key(),
13+
bank_balance: ctx.accounts.bank.bank_balance + amount,
14+
bump: ctx.bumps.bank,
15+
};
16+
msg!("{:#?}", ctx.accounts.bank);
17+
18+
transfer(
19+
CpiContext::new(
20+
ctx.accounts.system_program.to_account_info(),
21+
Transfer {
22+
from: ctx.accounts.authority.to_account_info(),
23+
to: ctx.accounts.bank.to_account_info(),
24+
},
25+
),
26+
amount,
27+
)?;
28+
Ok(())
29+
}
30+
31+
pub fn withdraw(ctx: Context<Withdraw>, amount: u64) -> Result<()> {
32+
ctx.accounts.bank.bank_balance -= amount;
33+
ctx.accounts.bank.sub_lamports(amount)?;
34+
ctx.accounts.authority.add_lamports(amount)?;
35+
msg!("{:#?}", ctx.accounts.bank);
36+
Ok(())
37+
}
38+
}
39+
40+
#[derive(Accounts)]
41+
pub struct Deposit<'info> {
42+
#[account(mut)]
43+
pub authority: Signer<'info>,
44+
#[account(
45+
init_if_needed,
46+
payer = authority,
47+
space = 8 + Bank::INIT_SPACE,
48+
seeds = [b"bank"],
49+
bump,
50+
)]
51+
pub bank: Account<'info, Bank>,
52+
pub system_program: Program<'info, System>,
53+
}
54+
55+
#[derive(Accounts)]
56+
pub struct Withdraw<'info> {
57+
#[account(mut)]
58+
pub authority: Signer<'info>,
59+
#[account(
60+
mut,
61+
has_one = authority,
62+
seeds = [b"bank"],
63+
bump,
64+
)]
65+
pub bank: Account<'info, Bank>,
66+
}
67+
68+
#[account]
69+
#[derive(InitSpace, Debug)]
70+
pub struct Bank {
71+
pub authority: Pubkey,
72+
pub bank_balance: u64,
73+
pub bump: u8,
74+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program } from "@coral-xyz/anchor";
3+
import { BankOne } from "../target/types/bank_one";
4+
import { assert } from "chai";
5+
6+
describe("bank_one", () => {
7+
const provider = anchor.AnchorProvider.env();
8+
const connection = provider.connection;
9+
const wallet = provider.wallet as anchor.Wallet;
10+
anchor.setProvider(provider);
11+
12+
const program = anchor.workspace.BankOne as Program<BankOne>;
13+
14+
const authority = new anchor.web3.Keypair();
15+
const amount = 1_000_000; // 1million lamports
16+
17+
before(async () => {
18+
const transferAmount = 1 * anchor.web3.LAMPORTS_PER_SOL;
19+
const transferTx = new anchor.web3.Transaction().add(
20+
anchor.web3.SystemProgram.transfer({
21+
fromPubkey: wallet.publicKey,
22+
toPubkey: authority.publicKey,
23+
lamports: transferAmount,
24+
})
25+
);
26+
await provider.sendAndConfirm(transferTx);
27+
});
28+
29+
it("Deposit", async () => {
30+
const transaction = await program.methods
31+
.deposit(new anchor.BN(amount))
32+
.accounts({ authority: authority.publicKey })
33+
.transaction();
34+
35+
const transactionSignature = await anchor.web3.sendAndConfirmTransaction(
36+
connection,
37+
transaction,
38+
[authority],
39+
{ commitment: "confirmed" }
40+
);
41+
console.log("Your transaction signature", transactionSignature);
42+
});
43+
44+
it("Withdraw", async () => {
45+
const walletInitialBalance = await connection.getBalance(wallet.publicKey);
46+
47+
const depositInstruction = await program.methods
48+
.deposit(new anchor.BN(0))
49+
.accounts({ authority: wallet.publicKey })
50+
.instruction();
51+
52+
const withdrawInstruction = await program.methods
53+
.withdraw(new anchor.BN(amount))
54+
.accounts({ authority: wallet.publicKey })
55+
.instruction();
56+
57+
const transaction = new anchor.web3.Transaction().add(
58+
depositInstruction,
59+
withdrawInstruction
60+
);
61+
62+
const transactionSignature = await anchor.web3.sendAndConfirmTransaction(
63+
connection,
64+
transaction,
65+
[wallet.payer],
66+
{ commitment: "confirmed" }
67+
);
68+
69+
console.log("Your transaction signature", transactionSignature);
70+
71+
const walletFinalBalance = await connection.getBalance(wallet.publicKey, {
72+
commitment: "confirmed",
73+
});
74+
75+
assert.equal(walletFinalBalance, walletInitialBalance + amount);
76+
});
77+
});

0 commit comments

Comments
 (0)