lite-rpc/tests/client.test.ts

81 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-03-03 06:01:02 -08:00
import { Connection, Keypair, sendAndConfirmTransaction, Transaction, PublicKey, TransactionInstruction, BlockheightBasedTransactionConfirmationStrategy } from "@solana/web3.js";
2023-02-09 04:05:18 -08:00
import * as fs from "fs";
import * as os from "os";
2023-03-26 03:29:15 -07:00
import * as crypto from "crypto";
2023-01-04 05:46:43 -08:00
jest.setTimeout(60000);
2023-02-09 04:05:18 -08:00
const MEMO_PROGRAM_ID = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
2023-10-04 08:46:06 -07:00
const connection = new Connection('http://0.0.0.0:8899', 'confirmed');
2023-03-03 06:01:02 -08:00
const keypair_file = fs.readFileSync(`${os.homedir}/.config/solana/id.json`, 'utf-8');
const payer = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(keypair_file)));
2023-02-09 04:05:18 -08:00
2023-03-03 06:01:02 -08:00
function createTransaction(): Transaction {
2023-01-04 05:46:43 -08:00
const transaction = new Transaction();
transaction.add(
2023-02-09 04:05:18 -08:00
new TransactionInstruction({
programId: MEMO_PROGRAM_ID,
keys: [],
2023-03-26 03:29:15 -07:00
data: Buffer.from(crypto.randomBytes(20).toString('hex'))
2023-02-09 04:05:18 -08:00
})
2023-01-04 05:46:43 -08:00
);
2023-03-03 06:01:02 -08:00
return transaction;
}
2023-02-09 04:05:18 -08:00
2023-03-03 06:01:02 -08:00
test('send and confirm transaction BlockheightBasedTransactionConfirmationStrategy', async () => {
const tx = createTransaction();
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
2023-02-09 04:05:18 -08:00
2023-03-03 06:01:02 -08:00
const signature = await connection.sendTransaction(tx, [payer]);
await connection.confirmTransaction({
blockhash,
lastValidBlockHeight,
signature,
abortSignal: undefined
});
console.log(`https://explorer.solana.com/tx/${signature}`)
2023-01-04 05:46:43 -08:00
});
2023-03-03 06:01:02 -08:00
test('send and confirm transaction', async () => {
const tx = createTransaction();
await sendAndConfirmTransaction(connection, tx, [payer]);
});
2023-10-04 08:46:06 -07:00
test('get epoch info', async () => {
{
const {epoch, absoluteSlot, slotIndex, slotsInEpoch} = await connection.getEpochInfo();
expect(Math.floor(absoluteSlot/slotsInEpoch)).toBe(epoch);
}
let process_absoluteSlot;
{
const {epoch, absoluteSlot, slotIndex, slotsInEpoch} = await connection.getEpochInfo({ commitment: 'processed' });
expect(Math.floor(absoluteSlot/slotsInEpoch)).toBe(epoch);
process_absoluteSlot = absoluteSlot;
}
let confirmed_absoluteSlot;
{
const {epoch, absoluteSlot, slotIndex, slotsInEpoch} = await connection.getEpochInfo({ commitment: 'confirmed' });
expect(Math.floor(absoluteSlot/slotsInEpoch)).toBe(epoch);
confirmed_absoluteSlot = absoluteSlot;
}
expect(confirmed_absoluteSlot >= process_absoluteSlot);
let finalized_absoluteSlot;
{
const {epoch, absoluteSlot, slotIndex, slotsInEpoch} = await connection.getEpochInfo({ commitment: 'finalized' });
expect(Math.floor(absoluteSlot/slotsInEpoch)).toBe(epoch);
finalized_absoluteSlot = absoluteSlot;
}
expect(process_absoluteSlot > finalized_absoluteSlot);
expect(confirmed_absoluteSlot > finalized_absoluteSlot);
2023-10-04 08:46:06 -07:00
});