diff --git a/src/Provider.ts b/src/Provider.ts index 9426ca8..df7cd2b 100644 --- a/src/Provider.ts +++ b/src/Provider.ts @@ -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'; @@ -15,8 +12,6 @@ export class Provider { } else { this.baseUrl = this.getNetworkUrl(baseUrlOrNetwork); } - - this.response = { data: undefined }; } private async fetchText(url: string): Promise { @@ -43,22 +38,6 @@ export class Provider { }); } - // protected async executeGet(endpoint: string): Promise { - // 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); } @@ -103,55 +82,55 @@ export class Provider { return this.fetchJson
(`${this.baseUrl}/api/query/address/${address}`); } - public async getAddressTransactions(address: string): Promise { - 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(address: string): Promise { - 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(address: string): Promise { - 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(address: string): Promise { - 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(): Promise { - return this.executeGet(`${this.baseUrl}/api/query/mempool/transactions`); + public async getMempoolTransactions() { + return this.fetchJson(`${this.baseUrl}/api/query/mempool/transactions`); } - public async getMempoolTransactionsCount(): Promise { - 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(id: string): Promise { - 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(): Promise { - return this.executeGet(`${this.baseUrl}/api/query/block`); + public async getBlock() { + return this.fetchJson(`${this.baseUrl}/api/query/block`); } - public async getBlockTransactionsByHash(hash: string): Promise { - 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(hash: string): Promise { - 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(index: string): Promise { - 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(index: string): Promise { - 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(): Promise { - return this.executeGet(`${this.baseUrl}/api/query/block/latest`); + public async getLatestBlock() { + return this.fetchJson(`${this.baseUrl}/api/query/block/latest`); } } diff --git a/test/provider.city.ts b/test/provider.city.ts index 40ed522..ed89c1e 100644 --- a/test/provider.city.ts +++ b/test/provider.city.ts @@ -1,4 +1,3 @@ -import { AxiosError } from 'axios'; import { Provider } from '../src/index.js'; import test from 'ava'; @@ -6,25 +5,31 @@ 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); }); diff --git a/test/provider.ts b/test/provider.ts index 13bbc00..a906e54 100644 --- a/test/provider.ts +++ b/test/provider.ts @@ -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) => {