-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpayments.test.ts
87 lines (76 loc) · 2.82 KB
/
payments.test.ts
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 { create, fromBinary } from "@bufbuild/protobuf";
import { beforeAll, describe, expect, it } from "vitest";
import { ZodError } from "zod";
import { PriceQuoteRequestSchema } from "#/gen-proto/nillion/payments/v1/quote_pb";
import { ReceiptSchema } from "#/gen-proto/nillion/payments/v1/receipt_pb";
import { PaymentClientBuilder, createSignerFromKey } from "#/payment";
import { type VmClient, VmClientBuilder } from "#/vm";
import { Env, PrivateKeyPerSuite } from "./helpers";
describe("PaymentClient", () => {
let client: VmClient;
const fundsAmount = BigInt(20000);
beforeAll(async () => {
const signer = await createSignerFromKey(PrivateKeyPerSuite.Payments);
client = await new VmClientBuilder()
// Random seed is required to avoid failures when the tests are run multiple times consecutively
.seed(Math.random().toString(36))
.bootnodeUrl(Env.bootnodeUrl)
.chainUrl(Env.nilChainUrl)
.signer(signer)
.build();
});
it("builder rejects if missing values", async () => {
try {
const builder = new PaymentClientBuilder();
await builder.build();
} catch (e) {
expect(e).toBeInstanceOf(ZodError);
expect((e as ZodError).issues).toHaveLength(5);
}
expect.assertions(2);
});
it("can pay for an operation", async () => {
const request = create(PriceQuoteRequestSchema, {
operation: {
case: "poolStatus",
value: {},
},
});
const signedReceipt = await client.payer.payForOperation(request);
const receipt = fromBinary(ReceiptSchema, signedReceipt.receipt);
expect(receipt.expiresAt?.seconds).toBeGreaterThan(new Date().getSeconds());
expect(receipt.identifier).toHaveLength(16);
});
it("can query payments config", async () => {
const paymentsConfig = await client.payer.paymentsConfig();
expect(paymentsConfig.minimumAddFundsPayment).toBeDefined;
});
it("can query account balance", async () => {
const account = await client.payer.accountBalance();
expect(account.balance).toBe(BigInt(0));
});
it("add not enough funds", async () => {
await expect(() => client.payer.addFunds(BigInt(0))).rejects.toThrowError(
"Not enough unil amount",
);
});
it("add enough funds", async () => {
const result = await client.payer.addFunds(fundsAmount);
expect(result).toBeNull;
});
it("account is funded", async () => {
const account = await client.payer.accountBalance();
expect(account.balance).toBe(BigInt(2));
});
it("can pay from balance", async () => {
const request = create(PriceQuoteRequestSchema, {
operation: {
case: "poolStatus",
value: {},
},
});
await client.payer.payForOperation(request);
const account = await client.payer.accountBalance();
expect(account.balance).toBeLessThan(fundsAmount);
});
});