-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.stellar.spec.ts
More file actions
116 lines (95 loc) · 3.78 KB
/
api.stellar.spec.ts
File metadata and controls
116 lines (95 loc) · 3.78 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0
import { StellarApi } from './api.stellar';
import { SorobanServer } from './soroban.server';
const HTTP_ENDPOINT = 'https://horizon-futurenet.stellar.org';
const SOROBAN_ENDPOINT = 'https://rpc-futurenet.stellar.org';
jest.setTimeout(60000);
const prepareStellarApi = async function () {
const soroban = new SorobanServer(SOROBAN_ENDPOINT);
const api = new StellarApi(HTTP_ENDPOINT, soroban, {
sorobanTxMeta: true,
});
await api.init();
return api;
};
describe('StellarApi', () => {
let stellarApi: StellarApi;
beforeEach(async () => {
stellarApi = await prepareStellarApi();
});
it('should initialize chainId', () => {
expect(stellarApi.getChainId()).toEqual(
'Test SDF Future Network ; October 2022',
);
});
it('should get finalized block height', async () => {
const height = await stellarApi.getFinalizedBlockHeight();
expect(height).not.toBeNaN();
expect(height).toBeGreaterThan(0);
});
it('should get best block height', async () => {
const height = await stellarApi.getBestBlockHeight();
expect(height).not.toBeNaN();
expect(height).toBeGreaterThan(0);
});
it('should fetch block', async () => {
const latestHeight = await stellarApi.getFinalizedBlockHeight();
const block = (await stellarApi.fetchBlocks([latestHeight]))[0];
expect(block.getHeader().blockHeight).toEqual(latestHeight);
});
it('should throw on calling connect', async () => {
await expect(stellarApi.connect()).rejects.toThrow('Not implemented');
});
it('should throw on calling disconnect', async () => {
await expect(stellarApi.disconnect()).rejects.toThrow('Not implemented');
});
it('handleError - pruned node errors', () => {
const error = new Error('start is before oldest ledger');
const handled = stellarApi.handleError(error, 1000);
expect(handled.message).toContain(
'The requested ledger number 1000 is not available on the current blockchain node',
);
});
it('handleError - non pruned node errors should return the same error', () => {
const error = new Error('Generic error');
const handled = stellarApi.handleError(error, 1000);
expect(handled).toBe(error);
});
it('should get runtime chain', () => {
const runtimeChain = stellarApi.getRuntimeChain();
expect(runtimeChain).toEqual((stellarApi as any).chainId);
});
it('should return chainId for genesis hash', () => {
const genesisHash = stellarApi.getGenesisHash();
expect(genesisHash).toEqual(stellarApi.getChainId());
});
it('should get spec name', () => {
const specName = stellarApi.getSpecName();
expect(specName).toEqual('Stellar');
});
it('handleError - soroban node been reset', async () => {
const error = new Error('start is after newest ledger');
stellarApi.getAndWrapEvents = jest.fn(() => {
throw new Error('start is after newest ledger');
});
(stellarApi as any).fetchOperationsForLedger = jest.fn((seq: number) => [
{ type: { toString: () => 'invoke_host_function' } },
]);
await expect((stellarApi as any).fetchAndWrapLedger(100)).rejects.toThrow(
/(Gone|Not Found)/,
);
});
it('Able to correctly return sorabanTxs', async () => {
const latestHeight = await stellarApi.getFinalizedBlockHeight();
const { transactions: sorobanTxs } =
await stellarApi.sorobanClient.getTransactions({
startLedger: latestHeight - 100000,
});
// Get ledgers with transactions
const startLedger = sorobanTxs[0].ledger;
const block = (await stellarApi.fetchBlocks([startLedger]))[0];
expect(block.block.transactions[0].sorobanTxs).toBeDefined();
expect(block.block.transactions[0].sorobanTxs!.ledger).toEqual(startLedger);
});
});