|
| 1 | +import { |
| 2 | + PublicKey, |
| 3 | + Transaction, |
| 4 | + TransactionInstruction, |
| 5 | + Keypair, |
| 6 | + SystemProgram, |
| 7 | + LAMPORTS_PER_SOL, |
| 8 | +} from "@solana/web3.js"; |
| 9 | +import pkg from "@solana/web3.js"; |
| 10 | +import { assert } from "chai"; |
| 11 | +import { test } from "mocha"; |
| 12 | +import { ProgramTestContext, start } from "solana-bankrun"; |
| 13 | +import BN from "bn.js"; |
| 14 | +describe("Transfer Sol Tests", () => { |
| 15 | + const programId = new PublicKey( |
| 16 | + "8TpdLD58VBWsdzxRi2yRcmKJD9UcE2GuUrBwsyCwpbUN", |
| 17 | + ); |
| 18 | + |
| 19 | + test("Test Cpi Transfer Works", async () => { |
| 20 | + const context = await start( |
| 21 | + [{ programId, name: "./program/target/deploy/pinocchio_transfer_sol" }], |
| 22 | + [], |
| 23 | + ); |
| 24 | + |
| 25 | + const client = context.banksClient; |
| 26 | + const payer = context.payer; |
| 27 | + const blockhash = context.lastBlockhash; |
| 28 | + |
| 29 | + const param = { amount: LAMPORTS_PER_SOL }; |
| 30 | + |
| 31 | + const ix_data_buffer = Buffer.concat([ |
| 32 | + Uint8Array.from([0]), |
| 33 | + Uint8Array.from(new BN(param.amount).toArray("le", 8)), |
| 34 | + ]); |
| 35 | + const sender = Keypair.generate(); |
| 36 | + const receiver = PublicKey.unique(); |
| 37 | + |
| 38 | + const initialCredit: any = { |
| 39 | + fromPubkey: payer.publicKey, |
| 40 | + lamports: 1.5 * LAMPORTS_PER_SOL, |
| 41 | + toPubkey: sender.publicKey, |
| 42 | + }; |
| 43 | + |
| 44 | + const ix1 = [SystemProgram.transfer(initialCredit)]; |
| 45 | + |
| 46 | + const tx1 = new Transaction(); |
| 47 | + tx1.recentBlockhash = blockhash; |
| 48 | + tx1.add(...ix1); |
| 49 | + |
| 50 | + tx1.sign(payer); |
| 51 | + |
| 52 | + await client.simulateTransaction(tx1); |
| 53 | + await client.processTransaction(tx1); |
| 54 | + const ix2 = [ |
| 55 | + new TransactionInstruction({ |
| 56 | + programId, |
| 57 | + keys: [ |
| 58 | + { pubkey: sender.publicKey, isSigner: true, isWritable: true }, |
| 59 | + { pubkey: receiver, isSigner: false, isWritable: true }, |
| 60 | + { |
| 61 | + pubkey: SystemProgram.programId, |
| 62 | + isSigner: false, |
| 63 | + isWritable: false, |
| 64 | + }, |
| 65 | + ], |
| 66 | + data: ix_data_buffer, |
| 67 | + }), |
| 68 | + ]; |
| 69 | + |
| 70 | + const tx2 = new Transaction(); |
| 71 | + tx2.recentBlockhash = blockhash; |
| 72 | + tx2.add(...ix2); |
| 73 | + tx2.feePayer = payer.publicKey; |
| 74 | + tx2.sign(sender, payer); |
| 75 | + |
| 76 | + const senderBalanceBefore = await client.getBalance( |
| 77 | + sender.publicKey, |
| 78 | + "finalized", |
| 79 | + ); |
| 80 | + await client.simulateTransaction(tx2, "finalized"); |
| 81 | + const processedTxn = await client.processTransaction(tx2); |
| 82 | + |
| 83 | + assert( |
| 84 | + processedTxn.logMessages[0].startsWith(`Program ${programId.toString()}`), |
| 85 | + ); |
| 86 | + |
| 87 | + assert( |
| 88 | + processedTxn.logMessages[processedTxn.logMessages.length - 1] == |
| 89 | + `Program ${programId.toString()} success`, |
| 90 | + ); |
| 91 | + |
| 92 | + const senderBalanceAfter = await client.getBalance( |
| 93 | + sender.publicKey, |
| 94 | + "finalized", |
| 95 | + ); |
| 96 | + const receiverBalanceAfter = await client.getBalance(receiver, "finalized"); |
| 97 | + |
| 98 | + assert(receiverBalanceAfter.toString() == param.amount.toString()); |
| 99 | + |
| 100 | + assert( |
| 101 | + new BN(senderBalanceBefore) |
| 102 | + .sub(new BN(senderBalanceAfter)) |
| 103 | + .eq(new BN(param.amount)), |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
0 commit comments