UNPKG

@helium/http

Version:

HTTP library for interacting with the Helium blockchain API

82 lines 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const camelcase_keys_1 = __importDefault(require("camelcase-keys")); const snakecase_keys_1 = __importDefault(require("snakecase-keys")); const Block_1 = __importDefault(require("../models/Block")); const Account_1 = __importDefault(require("../models/Account")); const Transaction_1 = __importDefault(require("../models/Transaction")); const PendingTransaction_1 = __importDefault(require("../models/PendingTransaction")); const ResourceList_1 = __importDefault(require("../ResourceList")); const Hotspot_1 = __importDefault(require("../models/Hotspot")); const Validator_1 = __importDefault(require("../models/Validator")); class Transactions { constructor(client, context) { this.client = client; this.context = context; } async submit(txn) { const url = '/pending_transactions'; const { data: { data }, } = await this.client.post(url, { txn }); return new PendingTransaction_1.default(data); } async get(hash, params) { const url = `/transactions/${hash}`; const { data: { data }, } = await this.client.get(url, (0, snakecase_keys_1.default)(params || {})); return Transaction_1.default.fromJsonObject(data); } async list(params = {}) { var _a, _b; const { data: { data: txns, cursor }, } = await this.client.get(this.activityUrl, { cursor: params.cursor, filter_types: params.filterTypes ? params.filterTypes.join() : undefined, min_time: params.minTime instanceof Date ? (_a = params.minTime) === null || _a === void 0 ? void 0 : _a.toISOString() : params.minTime, max_time: params.maxTime instanceof Date ? (_b = params.maxTime) === null || _b === void 0 ? void 0 : _b.toISOString() : params.maxTime, limit: params.limit, }); const data = txns.map((d) => Transaction_1.default.fromJsonObject(d)); return new ResourceList_1.default(data, this.list.bind(this), cursor); } async count(params) { const url = `${this.activityUrl}/count`; const { data: { data }, } = await this.client.get(url, { filter_types: (params === null || params === void 0 ? void 0 : params.filterTypes) ? params.filterTypes.join() : undefined, }); return (0, camelcase_keys_1.default)(data); } get activityUrl() { let url; if (this.context instanceof Block_1.default) { const block = this.context; if (block.height) { url = `/blocks/${block.height}/transactions`; } else if (block.hash) { url = `/blocks/hash/${block.hash}/transactions`; } else { throw new Error('Block must have either height or hash'); } } else if (this.context instanceof Account_1.default) { const account = this.context; url = `/accounts/${account.address}/activity`; } else if (this.context instanceof Hotspot_1.default) { const hotspot = this.context; url = `/hotspots/${hotspot.address}/activity`; } else if (this.context instanceof Validator_1.default) { const validator = this.context; url = `/validators/${validator.address}/activity`; } else { throw new Error('Must provide a context to list transactions from'); } return url; } } exports.default = Transactions; //# sourceMappingURL=Transactions.js.map