|
@@ -1,87 +1,90 @@
|
|
|
-import * as anchor from "@coral-xyz/anchor";
|
|
|
-import { TransferSol } from "../target/types/transfer_sol";
|
|
|
-
|
|
|
+import * as anchor from "@coral-xyz/anchor"
|
|
|
+import { TransferSol } from "../target/types/transfer_sol"
|
|
|
+import {
|
|
|
+ Keypair,
|
|
|
+ PublicKey,
|
|
|
+ LAMPORTS_PER_SOL,
|
|
|
+ SystemProgram,
|
|
|
+ Transaction,
|
|
|
+ sendAndConfirmTransaction,
|
|
|
+} from "@solana/web3.js"
|
|
|
describe("transfer-sol", () => {
|
|
|
- const provider = anchor.AnchorProvider.env();
|
|
|
- anchor.setProvider(provider);
|
|
|
- const payer = provider.wallet as anchor.Wallet;
|
|
|
- const program = anchor.workspace.TransferSol as anchor.Program<TransferSol>;
|
|
|
+ const provider = anchor.AnchorProvider.env()
|
|
|
+ anchor.setProvider(provider)
|
|
|
+ const payer = provider.wallet as anchor.Wallet
|
|
|
+ const program = anchor.workspace.TransferSol as anchor.Program<TransferSol>
|
|
|
+
|
|
|
+ // 1 SOL
|
|
|
+ const transferAmount = 1 * LAMPORTS_PER_SOL
|
|
|
|
|
|
- const transferAmount = 1 * anchor.web3.LAMPORTS_PER_SOL;
|
|
|
- const test1Recipient = anchor.web3.Keypair.generate();
|
|
|
- const test2Recipient1 = anchor.web3.Keypair.generate();
|
|
|
- const test2Recipient2 = anchor.web3.Keypair.generate();
|
|
|
+ // Generate a new keypair for the recipient
|
|
|
+ const recipient = new Keypair()
|
|
|
|
|
|
- it("Transfer between accounts using the system program", async () => {
|
|
|
- await getBalances(payer.publicKey, test1Recipient.publicKey, "Beginning");
|
|
|
+ // Generate a new keypair to create an account owned by our program
|
|
|
+ const programOwnedAccount = new Keypair()
|
|
|
+
|
|
|
+ it("Transfer SOL with CPI", async () => {
|
|
|
+ await getBalances(payer.publicKey, recipient.publicKey, "Beginning")
|
|
|
|
|
|
await program.methods
|
|
|
.transferSolWithCpi(new anchor.BN(transferAmount))
|
|
|
.accounts({
|
|
|
payer: payer.publicKey,
|
|
|
- recipient: test1Recipient.publicKey,
|
|
|
- systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ recipient: recipient.publicKey,
|
|
|
})
|
|
|
- .signers([payer.payer])
|
|
|
- .rpc();
|
|
|
+ .rpc()
|
|
|
+
|
|
|
+ await getBalances(payer.publicKey, recipient.publicKey, "Resulting")
|
|
|
+ })
|
|
|
|
|
|
- await getBalances(payer.publicKey, test1Recipient.publicKey, "Resulting");
|
|
|
- });
|
|
|
+ it("Create and fund account owned by our program", async () => {
|
|
|
+ const instruction = SystemProgram.createAccount({
|
|
|
+ fromPubkey: payer.publicKey,
|
|
|
+ newAccountPubkey: programOwnedAccount.publicKey,
|
|
|
+ space: 0,
|
|
|
+ lamports: 1 * LAMPORTS_PER_SOL, // 1 SOL
|
|
|
+ programId: program.programId, // Program Owner, our program's address
|
|
|
+ })
|
|
|
|
|
|
- it("Create two accounts for the following test", async () => {
|
|
|
- const ix = (pubkey: anchor.web3.PublicKey) => {
|
|
|
- return anchor.web3.SystemProgram.createAccount({
|
|
|
- fromPubkey: payer.publicKey,
|
|
|
- newAccountPubkey: pubkey,
|
|
|
- space: 0,
|
|
|
- lamports: 2 * anchor.web3.LAMPORTS_PER_SOL,
|
|
|
- programId: program.programId,
|
|
|
- });
|
|
|
- };
|
|
|
+ const transaction = new Transaction().add(instruction)
|
|
|
|
|
|
- await anchor.web3.sendAndConfirmTransaction(
|
|
|
- provider.connection,
|
|
|
- new anchor.web3.Transaction()
|
|
|
- .add(ix(test2Recipient1.publicKey))
|
|
|
- .add(ix(test2Recipient2.publicKey)),
|
|
|
- [payer.payer, test2Recipient1, test2Recipient2]
|
|
|
- );
|
|
|
- });
|
|
|
+ await sendAndConfirmTransaction(provider.connection, transaction, [
|
|
|
+ payer.payer,
|
|
|
+ programOwnedAccount,
|
|
|
+ ])
|
|
|
+ })
|
|
|
|
|
|
- it("Transfer between accounts using our program", async () => {
|
|
|
+ it("Transfer SOL with Program", async () => {
|
|
|
await getBalances(
|
|
|
- test2Recipient1.publicKey,
|
|
|
- test2Recipient2.publicKey,
|
|
|
+ programOwnedAccount.publicKey,
|
|
|
+ payer.publicKey,
|
|
|
"Beginning"
|
|
|
- );
|
|
|
+ )
|
|
|
|
|
|
await program.methods
|
|
|
.transferSolWithProgram(new anchor.BN(transferAmount))
|
|
|
.accounts({
|
|
|
- payer: test2Recipient1.publicKey,
|
|
|
- recipient: test2Recipient2.publicKey,
|
|
|
- systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ payer: programOwnedAccount.publicKey,
|
|
|
+ recipient: payer.publicKey,
|
|
|
})
|
|
|
- .rpc();
|
|
|
+ .rpc()
|
|
|
|
|
|
await getBalances(
|
|
|
- test2Recipient1.publicKey,
|
|
|
- test2Recipient2.publicKey,
|
|
|
+ programOwnedAccount.publicKey,
|
|
|
+ payer.publicKey,
|
|
|
"Resulting"
|
|
|
- );
|
|
|
- });
|
|
|
+ )
|
|
|
+ })
|
|
|
|
|
|
async function getBalances(
|
|
|
- payerPubkey: anchor.web3.PublicKey,
|
|
|
- recipientPubkey: anchor.web3.PublicKey,
|
|
|
+ payerPubkey: PublicKey,
|
|
|
+ recipientPubkey: PublicKey,
|
|
|
timeframe: string
|
|
|
) {
|
|
|
- let payerBalance = await provider.connection.getBalance(payerPubkey);
|
|
|
- let recipientBalance = await provider.connection.getBalance(
|
|
|
- recipientPubkey
|
|
|
- );
|
|
|
- console.log(`${timeframe} balances:`);
|
|
|
- console.log(` Payer: ${payerBalance}`);
|
|
|
- console.log(` Recipient: ${recipientBalance}`);
|
|
|
+ let payerBalance = await provider.connection.getBalance(payerPubkey)
|
|
|
+ let recipientBalance = await provider.connection.getBalance(recipientPubkey)
|
|
|
+ console.log(`${timeframe} balances:`)
|
|
|
+ console.log(` Payer: ${payerBalance / LAMPORTS_PER_SOL}`)
|
|
|
+ console.log(` Recipient: ${recipientBalance / LAMPORTS_PER_SOL}`)
|
|
|
}
|
|
|
-});
|
|
|
+})
|