|
| 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