UNPKG

@helium/http

Version:

HTTP library for interacting with the Helium blockchain API

311 lines 13.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; 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.rewardSumFixture = void 0; const nock_1 = __importDefault(require("nock")); const address_1 = __importStar(require("@helium/address")); const Client_1 = __importDefault(require("../../Client")); const accountFixture = (params = {}) => (Object.assign({ speculative_nonce: 4, sec_nonce: 2, sec_balance: 3, nonce: 4, dc_nonce: 5, dc_balance: 6, block: 293601, balance: 10000, address: 'my-address' }, params)); const accountStatsFixture = (params = {}) => (Object.assign({ last_week: [ { timestamp: '2020-06-30T00:00:00.000000Z', balance: 1, }, ], last_month: [ { timestamp: '2020-06-30T00:00:00.000000Z', balance: 1, }, { timestamp: '2020-06-29T16:00:00.000000Z', balance: 2, }, ], last_day: [] }, params)); address_1.default.fromB58 = jest.fn(() => new address_1.default(0, address_1.NetTypes.MAINNET, 0, new Uint8Array())); describe('get', () => { (0, nock_1.default)('https://api.helium.io').get('/v1/accounts/my-address').reply(200, { data: accountFixture(), }); it('retreives a specific account', async () => { var _a, _b; const client = new Client_1.default(); const account = await client.accounts.get('my-address'); expect(account.speculativeNonce).toBe(4); expect((_a = account.balance) === null || _a === void 0 ? void 0 : _a.integerBalance).toBe(10000); expect((_b = account.balance) === null || _b === void 0 ? void 0 : _b.toString()).toBe('0.0001 HNT'); }); }); describe('get at max block', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts/my-address?max_block=250000') .reply(200, { data: accountFixture({ balance: 1000000 }), }); it("retreives a specific account's status at a maximum block height", async () => { var _a; const client = new Client_1.default(); const maxBlock = 250000; const account = await client.accounts.get('my-address', { maxBlock }); expect((_a = account.balance) === null || _a === void 0 ? void 0 : _a.integerBalance).toBe(1000000); }); }); describe('getStats', () => { (0, nock_1.default)('https://api.helium.io').get('/v1/accounts/my-address/stats').reply(200, { data: accountStatsFixture(), }); it('retrieves account stats', async () => { const client = new Client_1.default(); const stats = await client.accounts.getStats('my-address'); expect(stats.lastWeek.length).toBe(1); expect(stats.lastMonth.length).toBe(2); expect(stats.lastDay.length).toBe(0); }); }); describe('list', () => { describe('with manual pagination', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts') .reply(200, { data: [accountFixture({ address: 'address-1' }), accountFixture({ address: 'address-2' })], cursor: 'cursor-1', }); (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts') .query({ cursor: 'cursor-1' }) .reply(200, { data: [accountFixture({ address: 'address-3' }), accountFixture({ address: 'address-4' })], }); it('lists a page of accounts and exposes pagination functions', async () => { const client = new Client_1.default(); const pageOne = await client.accounts.list(); expect(pageOne.data[0].address).toBe('address-1'); expect(pageOne.data[1].address).toBe('address-2'); const pageTwo = await pageOne.nextPage(); expect(pageTwo.data[0].address).toBe('address-3'); expect(pageTwo.data[1].address).toBe('address-4'); }); }); describe('with automatic pagination', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts') .reply(200, { data: [accountFixture({ address: 'address-1' }), accountFixture({ address: 'address-2' })], cursor: 'cursor-1', }); (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts') .query({ cursor: 'cursor-1' }) .reply(200, { data: [accountFixture({ address: 'address-3' }), accountFixture({ address: 'address-4' })], }); it('lists accounts as an auto-paginating iterator', async () => { var _a, e_1, _b, _c; const client = new Client_1.default(); const accounts = await client.accounts.list(); const addresses = []; try { // eslint-disable-next-line no-restricted-syntax for (var _d = true, accounts_1 = __asyncValues(accounts), accounts_1_1; accounts_1_1 = await accounts_1.next(), _a = accounts_1_1.done, !_a; _d = true) { _c = accounts_1_1.value; _d = false; const account = _c; addresses.push(account.address); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = accounts_1.return)) await _b.call(accounts_1); } finally { if (e_1) throw e_1.error; } } expect(addresses).toEqual(['address-1', 'address-2', 'address-3', 'address-4']); }); }); describe('list richest accounts', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts/rich') .reply(200, { data: [ accountFixture({ address: 'rich-address-1', balance: 10000000000000000 }), accountFixture({ address: 'rich-address-2', balance: 9999999900000000 }), accountFixture({ address: 'rich-address-3', balance: 9999999800000000 }), ], }); it('lists rich accounts', async () => { var _a, _b, _c; const client = new Client_1.default(); const accounts = await client.accounts.listRich(); expect(accounts.data[0].address).toBe('rich-address-1'); expect((_a = accounts.data[0].balance) === null || _a === void 0 ? void 0 : _a.integerBalance).toBe(10000000000000000); expect(accounts.data[1].address).toBe('rich-address-2'); expect((_b = accounts.data[1].balance) === null || _b === void 0 ? void 0 : _b.integerBalance).toBe(9999999900000000); expect(accounts.data[2].address).toBe('rich-address-3'); expect((_c = accounts.data[2].balance) === null || _c === void 0 ? void 0 : _c.integerBalance).toBe(9999999800000000); }); }); }); 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 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 rewards', () => { (0, nock_1.default)('https://api.helium.io') .get('/v1/accounts/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/accounts/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/accounts/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/accounts/fake-address/rewards/sum?min_time=-1%20day&bucket=day') .reply(200, (0, exports.rewardSumListFixture)()); it('gets account 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.account('fake-address').rewards.sum.get(minTime, maxTime); expect(rewards.balanceTotal.floatBalance).toBe(13.17717245); expect(rewards.data.total).toBe(13.17717245); }); it('list account 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.account('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 account 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.account('fake-address').rewards.sum.list({ minTime, maxTime }); } catch (error) { //@ts-ignore expect(error.message).toBe('missing bucket param'); } }); it('list account 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 .account('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); }); it('list account reward sums by bucket', async () => { const minTime = '-1 day'; const client = new Client_1.default(); const rewardsList = await client .account('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); }); }); //# sourceMappingURL=Accounts.spec.js.map