-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathcreateAndFundedKeypair.ts
More file actions
87 lines (68 loc) · 2.93 KB
/
Copy pathcreateAndFundedKeypair.ts
File metadata and controls
87 lines (68 loc) · 2.93 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
import { createAndFundedKeypair } from "../fixtures/createAndFundedKeypair";
import { airdropFactory, generateKeyPairSigner, lamports } from "gill";
jest.mock("gill", () => {
const actual = jest.requireActual("gill");
return {
...actual,
airdropFactory: jest.fn(),
generateKeyPairSigner: jest.fn(),
};
});
describe("createAndFundedKeypair", () => {
let mockRpc: any;
let mockRpcSubscriptions: any;
let mockAirdrop: jest.Mock;
const DEFAULT_LAMPORTS = 10_000_000_000n;
beforeEach(() => {
jest.clearAllMocks();
mockAirdrop = jest.fn().mockResolvedValue("mock-signature-abc123");
(airdropFactory as jest.Mock).mockReturnValue(mockAirdrop);
(generateKeyPairSigner as jest.Mock).mockResolvedValue({ address: "mock-address" });
mockRpc = {
getBalance: jest.fn().mockReturnValue({
send: jest.fn().mockResolvedValue({ value: lamports(DEFAULT_LAMPORTS) }),
}),
};
mockRpcSubscriptions = {};
});
it("should create and fund a new keypair with default amount", async () => {
const result = await createAndFundedKeypair(mockRpc, mockRpcSubscriptions);
expect(result.fundedKeypair.address).toBe("mock-address");
expect(result.transactionSignature).toBe("mock-signature-abc123");
expect(result.balance).toBe(lamports(DEFAULT_LAMPORTS));
expect(mockAirdrop).toHaveBeenCalledWith({
commitment: "confirmed",
lamports: DEFAULT_LAMPORTS,
recipientAddress: "mock-address",
});
expect(mockRpc.getBalance).toHaveBeenCalledWith("mock-address");
});
it("should create and fund a new keypair with custom amount", async () => {
const customAmount = 5_000_000_000n;
mockRpc.getBalance = jest.fn().mockReturnValue({
send: jest.fn().mockResolvedValue({ value: lamports(customAmount) }),
});
const result = await createAndFundedKeypair(mockRpc, mockRpcSubscriptions, lamports(customAmount));
expect(result.balance).toBe(lamports(customAmount));
expect(mockAirdrop).toHaveBeenCalledWith({
commitment: "confirmed",
lamports: customAmount,
recipientAddress: "mock-address",
});
});
it("should throw an error if lamports amount is zero", async () => {
await expect(createAndFundedKeypair(mockRpc, mockRpcSubscriptions, lamports(0n))).rejects.toThrow(
"Airdrop amount must be greater than zero lamports",
);
});
it("should throw an error if airdrop fails", async () => {
mockAirdrop.mockRejectedValue(new Error("RPC error: Airdrop failed"));
await expect(createAndFundedKeypair(mockRpc, mockRpcSubscriptions)).rejects.toThrow("RPC error: Airdrop failed");
});
it("should throw an error if balance fetch fails", async () => {
mockRpc.getBalance = jest.fn().mockReturnValue({
send: jest.fn().mockRejectedValue(new Error("RPC error: getBalance failed")),
});
await expect(createAndFundedKeypair(mockRpc, mockRpcSubscriptions)).rejects.toThrow("RPC error: getBalance failed");
});
});