UNPKG

@helium/http

Version:

HTTP library for interacting with the Helium blockchain API

437 lines 19.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rewardsFixture = exports.rewardSumListFixture = exports.challengeSumListFixture = exports.rewardSumFixture = exports.hotspotFixture = void 0; const nock_1 = __importDefault(require("nock")); const Client_1 = __importDefault(require("../../Client")); const Challenges_spec_1 = require("./Challenges.spec"); // eslint-disable-next-line import/prefer-default-export const hotspotFixture = (params = {}) => (Object.assign({ score_update_height: 213456, score: 0.25, reward_scale: 0.07049560546875, owner: 'fake-owner-address', payer: 'fake-payer-address', name: 'some-hotspot-name', location: 'an-h3-address', lng: -123.03528172874591, lat: 55.82000831418664, geocode: { short_street: 'Market St', short_state: 'CA', short_country: 'US', short_city: 'San Francisco', long_street: 'Market Street', long_state: 'California', long_country: 'United States', long_city: 'San Francisco', }, block: 123456, timestamp_added: '2020-11-24T02:52:12.000000Z', last_poc_challenge: 213456, last_change_block: 213456, address: 'fake-hotspot-address', gain: 12, elevation: 3, status: { height: 832252, listen_addrs: [ '/ip4/75.87.195.241/tcp/44158', '/p2p/11SV4RTqrgQo8FdQeRRSLrhY9ge4FepycGHz5S8qz1GF8WChAVP/p2p-circuit/p2p/112sGGLw2v8qHkxguGijb28daDTsJdc9LyoexyvHXFfD2FY1K9HA', ], online: 'online', } }, params)); exports.hotspotFixture = hotspotFixture; const rewardSumFixture = () => ({ meta: { min_time: '2020-12-17T00:00:00Z', max_time: '2020-12-18T00:00:00Z', }, data: { total: 13.17717245, sum: 1317717245, stddev: 1.10445133, min: 0, median: 1.98726309, max: 2, avg: 1.4641302722222223, }, }); exports.rewardSumFixture = rewardSumFixture; const challengeSumListFixture = () => ({ meta: { min_time: '2020-12-17T00:00:00Z', max_time: '2020-12-18T00:00:00Z', }, data: [ { timestamp: '2020-12-17T00:00:00Z', sum: '40', stddev: 0.22629428592141426, min: 1, median: 1, max: 2, avg: 1.0526315789473684, }, { timestamp: '2020-12-18T00:00:00Z', sum: '37', stddev: 0, min: 1, median: 1, max: 1, avg: 1, }, ], }); exports.challengeSumListFixture = challengeSumListFixture; const rewardSumListFixture = () => ({ meta: { min_time: '2020-12-17T00:00:00Z', max_time: '2020-12-18T00:00:00Z', }, data: [ { total: 13.17717245, sum: 1317717245, stddev: 1.10445133, min: 0, median: 1.98726309, max: 2, avg: 1.4641302722222223, }, ], }); exports.rewardSumListFixture = rewardSumListFixture; const rewardsFixture = () => ({ data: [ { timestamp: '2020-12-17T19:23:30.000000Z', hash: 'mock-hash', gateway: 'mock-gateway', block: 681810, amount: 206665349, account: 'mock-account', }, { timestamp: '2020-12-17T17:31:36.000000Z', hash: 'mock-hash', gateway: 'mock-gateway', block: 681693, amount: 240226051, account: 'mock-account', }, { timestamp: '2020-12-17T16:51:34.000000Z', hash: 'mock-hash', gateway: 'mock-gateway', block: 681645, amount: 6454681, account: 'mock-account', }, ], }); exports.rewardsFixture = rewardsFixture; describe('get', () => { (0, nock_1.default)('https://api.helium.io').get('/v1/hotspots/fake-hotspot-address').reply(200, { data: (0, exports.hotspotFixture)(), }); it('retrieves a hotspot by address', async () => { const client = new Client_1.default(); const hotspot = await client.hotspots.get('fake-hotspot-address'); expect(hotspot.name).toBe('some-hotspot-name'); expect(hotspot.timestampAdded).toBe('2020-11-24T02:52:12.000000Z'); expect(hotspot.lastPocChallenge).toBe(213456); expect(hotspot.lastChangeBlock).toBe(213456); expect(hotspot.rewardScale).toBe(0.07049560546875); }); }); describe('elected', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/elected/123456') .reply(200, { data: [ (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-1' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-2' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-3' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-4' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-5' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-6' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-7' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-8' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-9' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-10' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-11' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-12' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-13' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-14' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-15' }), (0, exports.hotspotFixture)({ name: 'previous-consensus-hotspot-16' }), ], }); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/elected') .reply(200, { data: [ (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-1' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-2' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-3' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-4' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-5' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-6' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-7' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-8' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-9' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-10' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-11' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-12' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-13' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-14' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-15' }), (0, exports.hotspotFixture)({ name: 'current-consensus-hotspot-16' }), ], }); it('retrieves elected hotspots at a given block height', async () => { const client = new Client_1.default(); const list = await client.hotspots.elected(123456); const elected = await list.take(16); expect(elected.length).toBe(16); expect(elected[0].name).toBe('previous-consensus-hotspot-1'); expect(elected[elected.length - 1].name).toBe('previous-consensus-hotspot-16'); }); it('retrieves currently elected hotspots', async () => { const client = new Client_1.default(); const list = await client.hotspots.elected(); const elected = await list.take(16); expect(elected.length).toBe(16); expect(elected[0].name).toBe('current-consensus-hotspot-1'); expect(elected[elected.length - 1].name).toBe('current-consensus-hotspot-16'); }); }); describe('list', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' })], }); it('lists hotspots', async () => { var _a, _b; const client = new Client_1.default(); const list = await client.hotspots.list(); const hotspots = await list.take(2); expect(hotspots[0].name).toBe('hotspot-1'); expect((_a = hotspots[0].status) === null || _a === void 0 ? void 0 : _a.listenAddrs[0]).toBe('/ip4/75.87.195.241/tcp/44158'); expect(hotspots[1].name).toBe('hotspot-2'); expect((_b = hotspots[1].status) === null || _b === void 0 ? void 0 : _b.listenAddrs[0]).toBe('/ip4/75.87.195.241/tcp/44158'); }); }); describe('listJson', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots') .reply(200, { data: [ (0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' }), (0, exports.hotspotFixture)({ name: 'hotspot-3' }), (0, exports.hotspotFixture)({ name: 'hotspot-4' }), ], }); it('lists hotspots', async () => { const client = new Client_1.default(); const list = await client.hotspots.list(); const hotspots = await list.takeJSON(2); expect(hotspots[0].name).toBe('hotspot-1'); expect(hotspots[1].name).toBe('hotspot-2'); const hotspots2 = await list.takeJSON(2); expect(hotspots2[0].name).toBe('hotspot-3'); expect(hotspots2[1].name).toBe('hotspot-4'); }); }); describe('search by hotspot name', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/name') .query({ search: 'chicken-burrito' }) .reply(200, { data: [ (0, exports.hotspotFixture)({ name: 'chicken-burrito-guacamole' }), (0, exports.hotspotFixture)({ name: 'chicken-burrito-salsa' }), ], }); it('lists hotspots', async () => { const client = new Client_1.default(); const list = await client.hotspots.search('chicken-burrito'); const hotspots = await list.take(2); expect(hotspots[0].name).toBe('chicken-burrito-guacamole'); expect(hotspots[1].name).toBe('chicken-burrito-salsa'); }); }); describe('get rewards', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/rewards/sum?min_time=2020-12-17T00%3A00%3A00.000Z&max_time=2020-12-18T00%3A00%3A00.000Z') .reply(200, (0, exports.rewardSumFixture)()); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/rewards?min_time=2020-12-17T00%3A00%3A00.000Z&max_time=2020-12-18T00%3A00%3A00.000Z') .reply(200, (0, exports.rewardsFixture)()); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/rewards/sum?min_time=2020-12-17T00%3A00%3A00.000Z&max_time=2020-12-18T00%3A00%3A00.000Z&bucket=day') .reply(200, (0, exports.rewardSumListFixture)()); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/rewards/sum?min_time=-1%20day&bucket=day') .reply(200, (0, exports.rewardSumListFixture)()); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/rewards/sum?min_time=-1%20day') .reply(200, (0, exports.rewardSumFixture)()); it('gets hotspot rewards sum', async () => { const minTime = new Date('2020-12-17T00:00:00Z'); const maxTime = new Date('2020-12-18T00:00:00Z'); const client = new Client_1.default(); const rewards = await client.hotspot('fake-address').rewards.sum.get(minTime, maxTime); expect(rewards.balanceTotal.floatBalance).toBe(13.17717245); expect(rewards.total).toBe(13.17717245); expect(rewards.data.total).toBe(13.17717245); }); it('gets hotspot rewards sum with natural date', async () => { const minTime = '-1 day'; const client = new Client_1.default(); const rewards = await client.hotspot('fake-address').rewards.sum.get(minTime); expect(rewards.balanceTotal.floatBalance).toBe(13.17717245); expect(rewards.total).toBe(13.17717245); expect(rewards.data.total).toBe(13.17717245); }); it('list hotspot rewards', async () => { const minTime = new Date('2020-12-17T00:00:00Z'); const maxTime = new Date('2020-12-18T00:00:00Z'); const client = new Client_1.default(); const rewardsList = await client.hotspot('fake-address').rewards.list({ minTime, maxTime }); const rewards = await rewardsList.take(5); expect(rewards.length).toBe(3); expect(rewards[0].gateway).toBe('mock-gateway'); expect(rewards[1].gateway).toBe('mock-gateway'); expect(rewards[2].gateway).toBe('mock-gateway'); expect(rewards[0].gateway).toBe('mock-gateway'); }); it('list hotspot reward sums no bucket', async () => { const minTime = new Date('2020-12-17T00:00:00Z'); const maxTime = new Date('2020-12-18T00:00:00Z'); const client = new Client_1.default(); expect.assertions(1); try { await client.hotspot('fake-address').rewards.sum.list({ minTime, maxTime }); } catch (error) { // @ts-ignore expect(error.message).toBe('missing bucket param'); } }); it('list hotspot reward sums by date', async () => { const minTime = new Date('2020-12-17T00:00:00Z'); const maxTime = new Date('2020-12-18T00:00:00Z'); const client = new Client_1.default(); const rewardsList = await client .hotspot('fake-address') .rewards.sum.list({ minTime, maxTime, bucket: 'day' }); const rewards = await rewardsList.take(5); expect(rewards.length).toBe(1); expect(rewards[0].balanceTotal.floatBalance).toBe(13.17717245); expect(rewards[0].total).toBe(13.17717245); }); it('list hotspot reward sums by bucket', async () => { const minTime = '-1 day'; const client = new Client_1.default(); const rewardsList = await client .hotspot('fake-address') .rewards.sum.list({ minTime, bucket: 'day' }); const rewards = await rewardsList.take(5); expect(rewards.length).toBe(1); expect(rewards[0].balanceTotal.floatBalance).toBe(13.17717245); expect(rewards[0].total).toBe(13.17717245); }); }); describe('list from account', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts/fake-address/hotspots') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' })], }); it('lists hotspots from an account', async () => { const client = new Client_1.default(); const list = await client.account('fake-address').hotspots.list(); const hotspots = await list.take(2); expect(hotspots[0].name).toBe('hotspot-1'); expect(hotspots[1].name).toBe('hotspot-2'); }); }); describe('list from city', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/cities/fake-address/hotspots') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' })], }); it('lists hotspots in a city', async () => { const client = new Client_1.default(); const list = await client.city('fake-address').hotspots.list(); const hotspots = await list.take(2); expect(hotspots[0].name).toBe('hotspot-1'); expect(hotspots[1].name).toBe('hotspot-2'); }); }); describe('challenges', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/challenges') .reply(200, { data: [(0, Challenges_spec_1.challengeFixture)({ hash: 'fake-hash-1' }), (0, Challenges_spec_1.challengeFixture)({ hash: 'fake-hash-2' })], }); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/fake-address/challenges/sum?min_time=2020-12-17T00%3A00%3A00.000Z&max_time=2020-12-18T00%3A00%3A00.000Z&bucket=day') .reply(200, (0, exports.challengeSumListFixture)()); it('list challenges from hotspot', async () => { const client = new Client_1.default(); const list = await client.hotspot('fake-address').challenges.list(); const challenges = await list.take(2); expect(challenges[0].hash).toBe('fake-hash-1'); expect(challenges[1].hash).toBe('fake-hash-2'); }); it('list challenge sums from hotspot', async () => { const minTime = new Date('2020-12-17T00:00:00Z'); const maxTime = new Date('2020-12-18T00:00:00Z'); const client = new Client_1.default(); const list = await client .hotspot('fake-address') .challenges.sum.list({ minTime, maxTime, bucket: 'day' }); const challenges = await list.take(2); expect(challenges[0].sum).toBe(40); expect(challenges[1].sum).toBe(37); }); }); describe('hexes', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/hex/882664ca8dfffff') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' })], cursor: 'cursor-1', }); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/hex/882664ca8dfffff?cursor=cursor-1') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-3' }), (0, exports.hotspotFixture)({ name: 'hotspot-4' })], }); it('lists hotspots within a res8 hex index', async () => { const client = new Client_1.default(); const list = await client.hotspots.hex('882664ca8dfffff'); const hotspots = await list.take(3); expect(hotspots[0].name).toBe('hotspot-1'); expect(hotspots[1].name).toBe('hotspot-2'); expect(hotspots[2].name).toBe('hotspot-3'); }); }); describe('location distance', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/location/distance') .query({ lat: 37.77, lon: -122.41, distance: 1000 }) .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-1' }), (0, exports.hotspotFixture)({ name: 'hotspot-2' })], cursor: 'cursor-1', }); (0, nock_1.default)('https://api.helium.io') .get('/v1/hotspots/location/distance?cursor=cursor-1') .reply(200, { data: [(0, exports.hotspotFixture)({ name: 'hotspot-3' }), (0, exports.hotspotFixture)({ name: 'hotspot-4' })], }); it('lists hotspots within a given distance from a lat/lng coord', async () => { const client = new Client_1.default(); const list = await client.hotspots.locationDistance({ lat: 37.77, lon: -122.41, distance: 1000, }); const hotspots = await list.take(3); expect(hotspots[0].name).toBe('hotspot-1'); expect(hotspots[1].name).toBe('hotspot-2'); expect(hotspots[2].name).toBe('hotspot-3'); }); }); //# sourceMappingURL=Hotspots.spec.js.map