-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.test.ts
More file actions
111 lines (94 loc) · 3.98 KB
/
Copy pathcounter.test.ts
File metadata and controls
111 lines (94 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { bignum } from "@metaplex-foundation/beet";
import {
Connection,
Keypair,
LAMPORTS_PER_SOL,
SystemProgram,
sendAndConfirmTransaction,
Transaction,
type TransactionInstruction,
} from "@solana/web3.js";
import { BN } from "bn.js";
import { assert } from "chai";
import { Counter, createIncrementInstruction, PROGRAM_ID } from "../ts";
function convertBignumToNumber(bignum: bignum): number {
return new BN(bignum).toNumber();
}
describe("Counter Solana Native", () => {
const connection = new Connection("http://localhost:8899");
it("Test allocate counter + increment tx", async () => {
// Randomly generate our wallet
const payerKeypair = Keypair.generate();
const payer = payerKeypair.publicKey;
// Randomly generate the account key
// to sign for setting up the Counter state
const counterKeypair = Keypair.generate();
const counter = counterKeypair.publicKey;
// Airdrop our wallet 1 Sol
await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
// Create a TransactionInstruction to interact with our counter program
const allocIx: TransactionInstruction = SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: counter,
lamports: await connection.getMinimumBalanceForRentExemption(Counter.byteSize),
space: Counter.byteSize,
programId: PROGRAM_ID,
});
const incrementIx: TransactionInstruction = createIncrementInstruction({
counter,
});
const tx = new Transaction().add(allocIx).add(incrementIx);
// Explicitly set the feePayer to be our wallet (this is set to first signer by default)
tx.feePayer = payer;
// Fetch a "timestamp" so validators know this is a recent transaction
tx.recentBlockhash = (await connection.getLatestBlockhash("confirmed")).blockhash;
// Send transaction to network (local network)
await sendAndConfirmTransaction(connection, tx, [payerKeypair, counterKeypair], {
skipPreflight: true,
commitment: "confirmed",
});
// Get the counter account info from network
const count = (await Counter.fromAccountAddress(connection, counter)).count;
assert(new BN(count).toNumber() === 1, "Expected count to have been 1");
console.log(`[alloc+increment] count is: ${count}`);
});
it("Test allocate tx and increment tx", async () => {
const payerKeypair = Keypair.generate();
const payer = payerKeypair.publicKey;
const counterKeypair = Keypair.generate();
const counter = counterKeypair.publicKey;
await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
// Check allocate tx
const allocIx: TransactionInstruction = SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: counter,
lamports: await connection.getMinimumBalanceForRentExemption(Counter.byteSize),
space: Counter.byteSize,
programId: PROGRAM_ID,
});
let tx = new Transaction().add(allocIx);
tx.feePayer = payer;
tx.recentBlockhash = (await connection.getLatestBlockhash("confirmed")).blockhash;
await sendAndConfirmTransaction(connection, tx, [payerKeypair, counterKeypair], {
skipPreflight: true,
commitment: "confirmed",
});
let count = (await Counter.fromAccountAddress(connection, counter)).count;
assert(convertBignumToNumber(count) === 0, "Expected count to have been 0");
console.log(`[allocate] count is: ${count}`);
// Check increment tx
const incrementIx: TransactionInstruction = createIncrementInstruction({
counter,
});
tx = new Transaction().add(incrementIx);
tx.feePayer = payer;
tx.recentBlockhash = (await connection.getLatestBlockhash("confirmed")).blockhash;
await sendAndConfirmTransaction(connection, tx, [payerKeypair], {
skipPreflight: true,
commitment: "confirmed",
});
count = (await Counter.fromAccountAddress(connection, counter)).count;
assert(convertBignumToNumber(count) === 1, "Expected count to have been 1");
console.log(`[increment] count is: ${count}`);
});
});