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