forked from StellarRouter/StellarRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscval.test.ts
More file actions
91 lines (79 loc) · 2.74 KB
/
Copy pathscval.test.ts
File metadata and controls
91 lines (79 loc) · 2.74 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
import { Keypair, StrKey, scValToNative, xdr } from "@stellar/stellar-sdk";
import {
quoteRequestToScVal,
routeMetadataToScVal,
toAddress,
toBool,
toI128,
toMap,
toNone,
toSymbol,
toU32,
toU64,
toVec,
} from "../src/scval.js";
const G = Keypair.random().publicKey();
const CONTRACT = StrKey.encodeContract(Buffer.alloc(32));
describe("scval encoding", () => {
it("encodes u32 with the correct ScVal arm", () => {
const scv = toU32(100);
expect(scv.switch().name).toBe("scvU32");
expect(scValToNative(scv)).toBe(100);
});
it("rejects out-of-range or non-integer u32 values", () => {
expect(() => toU32(-1)).toThrow(RangeError);
expect(() => toU32(2 ** 32)).toThrow(RangeError);
expect(() => toU32(1.5)).toThrow(RangeError);
});
it("encodes u64/i128 as bigint-backed arms that round-trip", () => {
expect(scValToNative(toU64(123n))).toBe(123n);
const i128 = toI128(10_000n);
expect(i128.switch().name).toBe("scvI128");
expect(scValToNative(i128)).toBe(10_000n);
});
it("encodes addresses as scvAddress", () => {
const scv = toAddress(G);
expect(scv.switch().name).toBe("scvAddress");
expect(scValToNative(scv)).toBe(G);
});
it("encodes strings, symbols, booleans, and None", () => {
expect(scValToNative(toSymbol("oracle"))).toBe("oracle");
expect(scValToNative(toBool(true))).toBe(true);
expect(scValToNative(toNone())).toBe(null);
});
it("encodes vectors", () => {
const scv = toVec([toSymbol("a"), toSymbol("b")]);
expect(scValToNative(scv)).toEqual(["a", "b"]);
});
it("encodes maps keyed by symbols", () => {
const scv = toMap({ name: toSymbol("oracle"), enabled: toBool(true) });
expect(scValToNative(scv)).toEqual({ name: "oracle", enabled: true });
});
it("encodes a QuoteRequest struct", () => {
const scv = quoteRequestToScVal({ route: "uniswap", tokenIn: G, tokenOut: CONTRACT, amountIn: 1000n });
const native = scValToNative(scv);
expect(native).toEqual({
route: "uniswap",
token_in: G,
token_out: CONTRACT,
amount_in: 1000n,
});
expect(scv.switch().name).toBe("scvMap");
});
it("encodes RouteMetadata with tags as symbols and owner as address", () => {
const scv = routeMetadataToScVal({ description: "Price feed", tags: ["defi", "oracle"], owner: G });
const native = scValToNative(scv);
expect(native).toEqual({
description: "Price feed",
tags: ["defi", "oracle"],
owner: G,
});
});
});
describe("xdr map construction", () => {
it("encodes with object-form ScMapEntry", () => {
const entry = new xdr.ScMapEntry({ key: toSymbol("k"), val: toU32(1) });
expect(scValToNative(entry.key())).toBe("k");
expect(scValToNative(entry.val())).toBe(1);
});
});