Skip to content

Commit 037c5b1

Browse files
committed
Fix Provider and tests
- Breaking change from previously release, the returned values are not encapsulated in an .data field any longer.
1 parent bf95d8d commit 037c5b1

File tree

3 files changed

+54
-77
lines changed

3 files changed

+54
-77
lines changed

src/Provider.ts

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { Address, ChainListEntry, RichListEntry, Supply, WalletListEntry } from
33

44
export class Provider {
55
private baseUrl: string;
6-
private response: {
7-
data: unknown | undefined;
8-
};
96

107
public constructor(baseUrlOrNetwork?: string) {
118
baseUrlOrNetwork = baseUrlOrNetwork || 'CITY';
@@ -15,8 +12,6 @@ export class Provider {
1512
} else {
1613
this.baseUrl = this.getNetworkUrl(baseUrlOrNetwork);
1714
}
18-
19-
this.response = { data: undefined };
2015
}
2116

2217
private async fetchText(url: string): Promise<string> {
@@ -43,22 +38,6 @@ export class Provider {
4338
});
4439
}
4540

46-
// protected async executeGet<AxiosResponse>(endpoint: string): Promise<AxiosResponse | undefined> {
47-
// await axios
48-
// .get(endpoint)
49-
// .then((res) => {
50-
// this.response = {
51-
// data: res.data,
52-
// error: undefined,
53-
// };
54-
// })
55-
// .catch((error) => {
56-
// this.response.error = error;
57-
// });
58-
59-
// return this.response as unknown as AxiosResponse;
60-
// }
61-
6241
public setNetwork(network: string): void {
6342
this.baseUrl = this.getNetworkUrl(network);
6443
}
@@ -103,55 +82,55 @@ export class Provider {
10382
return this.fetchJson<Address>(`${this.baseUrl}/api/query/address/${address}`);
10483
}
10584

106-
public async getAddressTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
107-
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions`);
85+
public async getAddressTransactions(address: string) {
86+
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions`);
10887
}
10988

110-
public async getAddressUnconfirmedTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
111-
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/unconfirmed`);
89+
public async getAddressUnconfirmedTransactions(address: string) {
90+
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unconfirmed`);
11291
}
11392

114-
public async getAddressSpentTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
115-
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/spent`);
93+
public async getAddressSpentTransactions(address: string) {
94+
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/spent`);
11695
}
11796

118-
public async getAddressUnspentTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
119-
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/unspent`);
97+
public async getAddressUnspentTransactions(address: string) {
98+
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unspent`);
12099
}
121100

122-
public async getMempoolTransactions<AxiosResponse>(): Promise<AxiosResponse | undefined> {
123-
return this.executeGet(`${this.baseUrl}/api/query/mempool/transactions`);
101+
public async getMempoolTransactions() {
102+
return this.fetchJson(`${this.baseUrl}/api/query/mempool/transactions`);
124103
}
125104

126-
public async getMempoolTransactionsCount<AxiosResponse>(): Promise<AxiosResponse | undefined> {
127-
return this.executeGet(`${this.baseUrl}/api/query/mempool/transactions/count`);
105+
public async getMempoolTransactionsCount() {
106+
return this.fetchText(`${this.baseUrl}/api/query/mempool/transactions/count`);
128107
}
129108

130-
public async getTransactionById<AxiosResponse>(id: string): Promise<AxiosResponse | undefined> {
131-
return this.executeGet(`${this.baseUrl}/api/query/transaction/${id}`);
109+
public async getTransactionById(id: string) {
110+
return this.fetchJson(`${this.baseUrl}/api/query/transaction/${id}`);
132111
}
133112

134-
public async getBlock<AxiosResponse>(): Promise<AxiosResponse | undefined> {
135-
return this.executeGet(`${this.baseUrl}/api/query/block`);
113+
public async getBlock() {
114+
return this.fetchJson(`${this.baseUrl}/api/query/block`);
136115
}
137116

138-
public async getBlockTransactionsByHash<AxiosResponse>(hash: string): Promise<AxiosResponse | undefined> {
139-
return this.executeGet(`${this.baseUrl}/api/query/block/${hash}/transactions`);
117+
public async getBlockTransactionsByHash(hash: string) {
118+
return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}/transactions`);
140119
}
141120

142-
public async getBlockByHash<AxiosResponse>(hash: string): Promise<AxiosResponse | undefined> {
143-
return this.executeGet(`${this.baseUrl}/api/query/block/${hash}`);
121+
public async getBlockByHash(hash: string) {
122+
return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}`);
144123
}
145124

146-
public async getBlockByIndex<AxiosResponse>(index: string): Promise<AxiosResponse | undefined> {
147-
return this.executeGet(`${this.baseUrl}/api/query/block/index/${index}`);
125+
public async getBlockByIndex(index: string) {
126+
return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}`);
148127
}
149128

150-
public async getBlockTransactionsByIndex<AxiosResponse>(index: string): Promise<AxiosResponse | undefined> {
151-
return this.executeGet(`${this.baseUrl}/api/query/block/index/${index}/transactions`);
129+
public async getBlockTransactionsByIndex(index: string) {
130+
return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}/transactions`);
152131
}
153132

154-
public async getLatestBlock<AxiosResponse>(): Promise<AxiosResponse | undefined> {
155-
return this.executeGet(`${this.baseUrl}/api/query/block/latest`);
133+
public async getLatestBlock() {
134+
return this.fetchJson(`${this.baseUrl}/api/query/block/latest`);
156135
}
157136
}

test/provider.city.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
import { AxiosError } from 'axios';
21
import { Provider } from '../src/index.js';
32
import test from 'ava';
43

54
test('should verify that city chain network is correct', async (t) => {
65
let provider = new Provider('https://city.indexer.blockcore.net');
76
const result: any = await provider.getSupply();
87

9-
t.assert(result.data.total > 13759461317); // Previous test: 13762606311, API returns now: 13759461318
10-
t.assert(result.data.rewards > 24854552);
11-
t.assert(result.data.height > 1338666);
8+
t.assert(result.total > 13759461317); // Previous test: 13762606311, API returns now: 13759461318
9+
t.assert(result.rewards > 24854552);
10+
t.assert(result.height > 1338666);
1211
});
1312

1413
test('should get block by index', async (t) => {
1514
let provider = new Provider('https://city.indexer.blockcore.net');
1615
const result: any = await provider.getBlockByIndex('1');
17-
18-
t.assert(result.data.blockHash === '10ff8948145eab119c528301e44316a977b6adb2d82526f44f296b02370a6d41');
19-
t.assert(result.data.nonce === 16639);
16+
17+
t.assert(result.blockHash === '10ff8948145eab119c528301e44316a977b6adb2d82526f44f296b02370a6d41');
18+
t.assert(result.nonce === 16639);
2019
});
2120

2221
test('should get transaction by id', async (t) => {
2322
let provider = new Provider('https://city.indexer.blockcore.net');
2423
const result: any = await provider.getTransactionById('f75756e8cd24e5c15c2f68a1a9eb2e6299ad8dd6e196940b27d8c933a1654c96'); // Block 50000
2524

26-
t.assert(result.data.symbol === 'CITY');
27-
t.assert(result.data.blockHash === '3ef76cbcd4c125bfab252f20e11cdec64a495b1c3d6caa77d407f1e0420f71e7');
28-
t.assert(result.data.blockIndex === 50000);
29-
t.assert(result.data.transactionId === 'f75756e8cd24e5c15c2f68a1a9eb2e6299ad8dd6e196940b27d8c933a1654c96');
25+
t.assert(result.symbol === 'CITY');
26+
t.assert(result.blockHash === '3ef76cbcd4c125bfab252f20e11cdec64a495b1c3d6caa77d407f1e0420f71e7');
27+
t.assert(result.blockIndex === 50000);
28+
t.assert(result.transactionId === 'f75756e8cd24e5c15c2f68a1a9eb2e6299ad8dd6e196940b27d8c933a1654c96');
29+
});
30+
31+
test('should test if getEstimateRewards method returns a number', async (t) => {
32+
let provider = new Provider('https://city.indexer.blockcore.net');
33+
const result: any = await provider.getEstimateRewards();
34+
t.truthy(result);
3035
});

test/provider.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
1-
import { AxiosError } from 'axios';
21
import { Provider } from '../src/index.js';
32
import test from 'ava';
43

54
test('should get networks', async (t) => {
65
let provider = new Provider();
76
let result: any = await provider.getNetworks();
8-
t.assert(result.data[0].symbol === 'BTC');
7+
t.assert(result[0].symbol === 'BTC');
98
});
109

1110
test('should get correct network url', async (t) => {
1211
let provider = new Provider();
1312

14-
let result = await provider.getNetworkUrl('CITY');
15-
t.assert(result === 'https://city.indexer.blockcore.net');
13+
let result = await provider.getNetworkUrl('CITY');
14+
t.assert(result === 'https://city.indexer.blockcore.net');
1615

17-
result = await provider.getNetworkUrl('EXOS');
18-
t.assert(result === 'https://exos.indexer.blockcore.net');
16+
result = await provider.getNetworkUrl('EXOS');
17+
t.assert(result === 'https://exos.indexer.blockcore.net');
1918

20-
const provider2 = new Provider('https://custom.indexer.blockcore.net');
21-
t.assert(provider2.getBaseUrl() === 'https://custom.indexer.blockcore.net');
19+
const provider2 = new Provider('https://custom.indexer.blockcore.net');
20+
t.assert(provider2.getBaseUrl() === 'https://custom.indexer.blockcore.net');
2221
});
2322

2423
test('should test getSupply method', async (t) => {
2524
let provider = new Provider();
2625
const result: any = await provider.getSupply();
2726

28-
t.assert(result.data.total > 303049697);
29-
t.assert(result.data.rewards > 2158270);
30-
t.assert(result.data.height > 1218270);
27+
t.assert(result.total > 303049697);
28+
t.assert(result.rewards > 2158270);
29+
t.assert(result.height > 1218270);
3130
});
3231

3332
test('should test if getCirculatingSupply method returns a number', async (t) => {
3433
let provider = new Provider();
3534
const result: any = await provider.getCirculatingSupply();
36-
t.truthy(result.data);
35+
t.truthy(result);
3736
});
3837

3938
test('should test if getTotalSupply method returns a number', async (t) => {
4039
let provider = new Provider();
4140
const result: any = await provider.getTotalSupply();
42-
t.truthy(result.data);
43-
});
44-
45-
test('should test if getEstimateRewards method returns a number', async (t) => {
46-
let provider = new Provider();
47-
const result: any = await provider.getEstimateRewards();
48-
t.truthy(result.data);
41+
t.truthy(result);
4942
});
5043

5144
// test('should test if getWallets method returns an object containing Burnt account', async (t) => {

0 commit comments

Comments
 (0)