wormhole-explorer/blockchain-watcher/test/infrastructure/repositories/Web3SolanaSlotRepository.te...

146 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-11-21 04:37:21 -08:00
import { expect, describe, it } from "@jest/globals";
import { PublicKey } from "@solana/web3.js";
2023-11-21 04:37:21 -08:00
import { solana } from "../../../src/domain/entities";
import { Web3SolanaSlotRepository } from "../../../src/infrastructure/repositories";
describe("Web3SolanaSlotRepository", () => {
2023-11-27 04:10:56 -08:00
2023-11-21 04:37:21 -08:00
describe("getLatestSlot", () => {
it("should return the latest slot number", async () => {
2023-11-27 04:10:56 -08:00
// Given
2023-11-21 04:37:21 -08:00
const connectionMock = {
getSlot: () => Promise.resolve(100),
};
const repository = new Web3SolanaSlotRepository(connectionMock as any);
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// When
2023-11-28 11:33:23 -08:00
const latestSlot = await repository.getLatestSlot("finalized");
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// Then
2023-11-21 04:37:21 -08:00
expect(latestSlot).toBe(100);
});
});
describe("getBlock", () => {
it("should return a block for a given slot number", async () => {
2023-11-27 04:10:56 -08:00
// Given
2023-11-21 04:37:21 -08:00
const expected = {
blockTime: 100,
transactions: [
{
signature: "signature1",
slot: 100,
transaction: {
message: {
version: "legacy",
accountKeys: [new PublicKey("3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5")],
instructions: [],
compiledInstructions: [],
},
},
},
{
signature: "signature1",
slot: 100,
transaction: {
message: {
version: 0,
staticAccountKeys: [new PublicKey("3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5")],
instructions: [],
compiledInstructions: [],
},
},
},
],
2023-11-21 04:37:21 -08:00
};
const connectionMock = {
getBlock: (slot: number) => Promise.resolve(expected),
};
const repo = new Web3SolanaSlotRepository(connectionMock as any);
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// When
2023-11-28 10:47:46 -08:00
const block = (await repo.getBlock(100)).getValue();
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// Then
2023-11-21 04:37:21 -08:00
expect(block.blockTime).toBe(expected.blockTime);
expect(block.transactions).toHaveLength(expected.transactions.length);
});
it("should return an error when the block is not found", async () => {
const connectionMock = {
getBlock: (slot: number) => Promise.resolve(null),
};
const repository = new Web3SolanaSlotRepository(connectionMock as any);
const block = await repository.getBlock(100);
expect(block.getError()).toBeDefined();
});
2023-11-21 04:37:21 -08:00
});
describe("getSignaturesForAddress", () => {
it("should return confirmed signature info for a given address", async () => {
2023-11-27 04:10:56 -08:00
// Given
2023-11-21 04:37:21 -08:00
const expected = [
{
signature: "signature1",
slot: 100,
},
{
signature: "signature2",
slot: 200,
},
];
const connectionMock = {
getSignaturesForAddress: () => Promise.resolve(expected),
};
const repo = new Web3SolanaSlotRepository(connectionMock as any);
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// When
2023-11-28 10:47:46 -08:00
const signatures = await repo.getSignaturesForAddress(
2023-11-21 04:37:21 -08:00
"BTcueXFisZiqE49Ne2xTZjHV9bT5paVZhpKc1k4L3n1c",
"before",
"after",
10
);
2023-11-27 04:10:56 -08:00
// Then
2023-11-21 04:37:21 -08:00
expect(signatures).toBe(expected);
});
});
describe("getTransactions", () => {
it("should return transactions for a given array of confirmed signature info", async () => {
2023-11-27 04:10:56 -08:00
// Given
2023-11-21 04:37:21 -08:00
const expected = [
{
signature: "signature1",
slot: 100,
transaction: {
message: {
2023-11-21 07:45:28 -08:00
version: "legacy",
2023-11-21 04:37:21 -08:00
accountKeys: [],
instructions: [],
2023-11-21 07:45:28 -08:00
compiledInstructions: [],
2023-11-21 04:37:21 -08:00
},
},
},
];
const connectionMock = {
getTransactions: (sigs: solana.ConfirmedSignatureInfo[]) => Promise.resolve(expected),
};
const repo = new Web3SolanaSlotRepository(connectionMock as any);
2023-11-21 04:37:21 -08:00
2023-11-27 04:10:56 -08:00
// When
2023-11-28 10:47:46 -08:00
const transactions = await repo.getTransactions([
2023-11-21 04:37:21 -08:00
{
signature: "signature1",
},
]);
2023-11-27 04:10:56 -08:00
// Then
2023-11-21 04:37:21 -08:00
expect(transactions).toStrictEqual(expected);
});
});
});