Skip to content

Commit db2e9c7

Browse files
transfer sol using poseidon
1 parent 3a36468 commit db2e9c7

File tree

14 files changed

+262
-0
lines changed

14 files changed

+262
-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+
poseidon = "7ogRvKFEYpbceu1CorL3V9jFdwPKkWdpUgUqNdsVswbG"
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 = "poseidon"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "poseidon"
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 = "0.30.1"
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use anchor_lang::prelude::*;
2+
use anchor_lang::system_program::{Transfer, transfer};
3+
declare_id!("7ogRvKFEYpbceu1CorL3V9jFdwPKkWdpUgUqNdsVswbG");
4+
#[program]
5+
pub mod poseidon {
6+
use super::*;
7+
pub fn transfer_sol(ctx: Context<TransferSolContext>, amount: u64) -> Result<()> {
8+
let transfer_accounts = Transfer {
9+
from: ctx.accounts.payer.to_account_info(),
10+
to: ctx.accounts.recipient.to_account_info(),
11+
};
12+
let cpi_ctx = CpiContext::new(
13+
ctx.accounts.system_program.to_account_info(),
14+
transfer_accounts,
15+
);
16+
transfer(cpi_ctx, amount)?;
17+
Ok(())
18+
}
19+
}
20+
#[derive(Accounts)]
21+
pub struct TransferSolContext<'info> {
22+
#[account(mut)]
23+
pub recipient: SystemAccount<'info>,
24+
#[account(mut)]
25+
pub payer: Signer<'info>,
26+
pub system_program: Program<'info, System>,
27+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { describe, it } from 'node:test';
2+
import * as anchor from '@coral-xyz/anchor';
3+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
4+
import { BankrunProvider } from 'anchor-bankrun';
5+
import { startAnchor } from 'solana-bankrun';
6+
import type { poseidon } from '../target/types/poseidon';
7+
8+
const IDL = require('../target/idl/poseidon.json');
9+
const PROGRAM_ID = new PublicKey(IDL.address);
10+
11+
describe('Bankrun example', async () => {
12+
const context = await startAnchor('', [{ name: 'transfer_sol', programId: PROGRAM_ID }], []);
13+
const provider = new BankrunProvider(context);
14+
const payer = provider.wallet as anchor.Wallet;
15+
const program = new anchor.Program<poseidon>(IDL, provider);
16+
17+
// 1 SOL
18+
const transferAmount = 1 * LAMPORTS_PER_SOL;
19+
20+
// Generate a new keypair for the recipient
21+
const recipient = new Keypair();
22+
23+
// Generate a new keypair to create an account owned by our program
24+
const programOwnedAccount = new Keypair();
25+
26+
it('Transfer SOL with CPI', async () => {
27+
await getBalances(payer.publicKey, recipient.publicKey, 'Beginning');
28+
29+
await program.methods
30+
.transferSolWithCpi(new anchor.BN(transferAmount))
31+
.accounts({
32+
payer: payer.publicKey,
33+
recipient: recipient.publicKey,
34+
})
35+
.rpc();
36+
37+
await getBalances(payer.publicKey, recipient.publicKey, 'Resulting');
38+
});
39+
40+
it('Create and fund account owned by our program', async () => {
41+
const instruction = SystemProgram.createAccount({
42+
fromPubkey: payer.publicKey,
43+
newAccountPubkey: programOwnedAccount.publicKey,
44+
space: 0,
45+
lamports: 1 * LAMPORTS_PER_SOL, // 1 SOL
46+
programId: program.programId, // Program Owner, our program's address
47+
});
48+
49+
const transaction = new Transaction().add(instruction);
50+
51+
await sendAndConfirmTransaction(provider.connection, transaction, [payer.payer, programOwnedAccount]);
52+
});
53+
54+
it('Transfer SOL with Program', async () => {
55+
await getBalances(programOwnedAccount.publicKey, payer.publicKey, 'Beginning');
56+
57+
await program.methods
58+
.transferSolWithProgram(new anchor.BN(transferAmount))
59+
.accounts({
60+
payer: programOwnedAccount.publicKey,
61+
recipient: payer.publicKey,
62+
})
63+
.rpc();
64+
65+
await getBalances(programOwnedAccount.publicKey, payer.publicKey, 'Resulting');
66+
});
67+
68+
async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) {
69+
const payerBalance = await provider.connection.getBalance(payerPubkey);
70+
const recipientBalance = await provider.connection.getBalance(recipientPubkey);
71+
console.log(`${timeframe} balances:`);
72+
console.log(` Payer: ${payerBalance / LAMPORTS_PER_SOL}`);
73+
console.log(` Recipient: ${recipientBalance / LAMPORTS_PER_SOL}`);
74+
}
75+
});

0 commit comments

Comments
 (0)