Skip to content

Commit

Permalink
Fix Provider and tests
Browse files Browse the repository at this point in the history
- Breaking change from previously release, the returned values are not encapsulated in an .data field any longer.
  • Loading branch information
sondreb committed Jul 20, 2022
1 parent bf95d8d commit 037c5b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 77 deletions.
73 changes: 26 additions & 47 deletions src/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { Address, ChainListEntry, RichListEntry, Supply, WalletListEntry } from

export class Provider {
private baseUrl: string;
private response: {
data: unknown | undefined;
};

public constructor(baseUrlOrNetwork?: string) {
baseUrlOrNetwork = baseUrlOrNetwork || 'CITY';
Expand All @@ -15,8 +12,6 @@ export class Provider {
} else {
this.baseUrl = this.getNetworkUrl(baseUrlOrNetwork);
}

this.response = { data: undefined };
}

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

// protected async executeGet<AxiosResponse>(endpoint: string): Promise<AxiosResponse | undefined> {
// await axios
// .get(endpoint)
// .then((res) => {
// this.response = {
// data: res.data,
// error: undefined,
// };
// })
// .catch((error) => {
// this.response.error = error;
// });

// return this.response as unknown as AxiosResponse;
// }

public setNetwork(network: string): void {
this.baseUrl = this.getNetworkUrl(network);
}
Expand Down Expand Up @@ -103,55 +82,55 @@ export class Provider {
return this.fetchJson<Address>(`${this.baseUrl}/api/query/address/${address}`);
}

public async getAddressTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions`);
public async getAddressTransactions(address: string) {
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions`);
}

public async getAddressUnconfirmedTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/unconfirmed`);
public async getAddressUnconfirmedTransactions(address: string) {
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unconfirmed`);
}

public async getAddressSpentTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/spent`);
public async getAddressSpentTransactions(address: string) {
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/spent`);
}

public async getAddressUnspentTransactions<AxiosResponse>(address: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/address/${address}/transactions/unspent`);
public async getAddressUnspentTransactions(address: string) {
return this.fetchJson(`${this.baseUrl}/api/query/address/${address}/transactions/unspent`);
}

public async getMempoolTransactions<AxiosResponse>(): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/mempool/transactions`);
public async getMempoolTransactions() {
return this.fetchJson(`${this.baseUrl}/api/query/mempool/transactions`);
}

public async getMempoolTransactionsCount<AxiosResponse>(): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/mempool/transactions/count`);
public async getMempoolTransactionsCount() {
return this.fetchText(`${this.baseUrl}/api/query/mempool/transactions/count`);
}

public async getTransactionById<AxiosResponse>(id: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/transaction/${id}`);
public async getTransactionById(id: string) {
return this.fetchJson(`${this.baseUrl}/api/query/transaction/${id}`);
}

public async getBlock<AxiosResponse>(): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block`);
public async getBlock() {
return this.fetchJson(`${this.baseUrl}/api/query/block`);
}

public async getBlockTransactionsByHash<AxiosResponse>(hash: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block/${hash}/transactions`);
public async getBlockTransactionsByHash(hash: string) {
return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}/transactions`);
}

public async getBlockByHash<AxiosResponse>(hash: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block/${hash}`);
public async getBlockByHash(hash: string) {
return this.fetchJson(`${this.baseUrl}/api/query/block/${hash}`);
}

public async getBlockByIndex<AxiosResponse>(index: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block/index/${index}`);
public async getBlockByIndex(index: string) {
return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}`);
}

public async getBlockTransactionsByIndex<AxiosResponse>(index: string): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block/index/${index}/transactions`);
public async getBlockTransactionsByIndex(index: string) {
return this.fetchJson(`${this.baseUrl}/api/query/block/index/${index}/transactions`);
}

public async getLatestBlock<AxiosResponse>(): Promise<AxiosResponse | undefined> {
return this.executeGet(`${this.baseUrl}/api/query/block/latest`);
public async getLatestBlock() {
return this.fetchJson(`${this.baseUrl}/api/query/block/latest`);
}
}
27 changes: 16 additions & 11 deletions test/provider.city.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import { AxiosError } from 'axios';
import { Provider } from '../src/index.js';
import test from 'ava';

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

t.assert(result.data.total > 13759461317); // Previous test: 13762606311, API returns now: 13759461318
t.assert(result.data.rewards > 24854552);
t.assert(result.data.height > 1338666);
t.assert(result.total > 13759461317); // Previous test: 13762606311, API returns now: 13759461318
t.assert(result.rewards > 24854552);
t.assert(result.height > 1338666);
});

test('should get block by index', async (t) => {
let provider = new Provider('https://city.indexer.blockcore.net');
const result: any = await provider.getBlockByIndex('1');
t.assert(result.data.blockHash === '10ff8948145eab119c528301e44316a977b6adb2d82526f44f296b02370a6d41');
t.assert(result.data.nonce === 16639);

t.assert(result.blockHash === '10ff8948145eab119c528301e44316a977b6adb2d82526f44f296b02370a6d41');
t.assert(result.nonce === 16639);
});

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

t.assert(result.data.symbol === 'CITY');
t.assert(result.data.blockHash === '3ef76cbcd4c125bfab252f20e11cdec64a495b1c3d6caa77d407f1e0420f71e7');
t.assert(result.data.blockIndex === 50000);
t.assert(result.data.transactionId === 'f75756e8cd24e5c15c2f68a1a9eb2e6299ad8dd6e196940b27d8c933a1654c96');
t.assert(result.symbol === 'CITY');
t.assert(result.blockHash === '3ef76cbcd4c125bfab252f20e11cdec64a495b1c3d6caa77d407f1e0420f71e7');
t.assert(result.blockIndex === 50000);
t.assert(result.transactionId === 'f75756e8cd24e5c15c2f68a1a9eb2e6299ad8dd6e196940b27d8c933a1654c96');
});

test('should test if getEstimateRewards method returns a number', async (t) => {
let provider = new Provider('https://city.indexer.blockcore.net');
const result: any = await provider.getEstimateRewards();
t.truthy(result);
});
31 changes: 12 additions & 19 deletions test/provider.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import { AxiosError } from 'axios';
import { Provider } from '../src/index.js';
import test from 'ava';

test('should get networks', async (t) => {
let provider = new Provider();
let result: any = await provider.getNetworks();
t.assert(result.data[0].symbol === 'BTC');
t.assert(result[0].symbol === 'BTC');
});

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

let result = await provider.getNetworkUrl('CITY');
t.assert(result === 'https://city.indexer.blockcore.net');
let result = await provider.getNetworkUrl('CITY');
t.assert(result === 'https://city.indexer.blockcore.net');

result = await provider.getNetworkUrl('EXOS');
t.assert(result === 'https://exos.indexer.blockcore.net');
result = await provider.getNetworkUrl('EXOS');
t.assert(result === 'https://exos.indexer.blockcore.net');

const provider2 = new Provider('https://custom.indexer.blockcore.net');
t.assert(provider2.getBaseUrl() === 'https://custom.indexer.blockcore.net');
const provider2 = new Provider('https://custom.indexer.blockcore.net');
t.assert(provider2.getBaseUrl() === 'https://custom.indexer.blockcore.net');
});

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

t.assert(result.data.total > 303049697);
t.assert(result.data.rewards > 2158270);
t.assert(result.data.height > 1218270);
t.assert(result.total > 303049697);
t.assert(result.rewards > 2158270);
t.assert(result.height > 1218270);
});

test('should test if getCirculatingSupply method returns a number', async (t) => {
let provider = new Provider();
const result: any = await provider.getCirculatingSupply();
t.truthy(result.data);
t.truthy(result);
});

test('should test if getTotalSupply method returns a number', async (t) => {
let provider = new Provider();
const result: any = await provider.getTotalSupply();
t.truthy(result.data);
});

test('should test if getEstimateRewards method returns a number', async (t) => {
let provider = new Provider();
const result: any = await provider.getEstimateRewards();
t.truthy(result.data);
t.truthy(result);
});

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

0 comments on commit 037c5b1

Please sign in to comment.