UNPKG

infoblock

Version:

A typed wrapper of the blockchain.info API

226 lines (225 loc) 11.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = __importDefault(require("../../lib/index")); var Block_1 = require("../../lib/Block"); var client = new index_1.default(); expect.extend({ toJSONMatch: function (received, instance) { var message = "JSON.stringify of the 2 instances is not the same"; var pass = JSON.stringify(received) == JSON.stringify(instance); return { pass: pass, message: function () { return message; } }; } }); test('getBalance', function () { return __awaiter(void 0, void 0, void 0, function () { var nakamotoAddress, balance; return __generator(this, function (_a) { switch (_a.label) { case 0: nakamotoAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'; return [4 /*yield*/, client.getBalance(nakamotoAddress)]; case 1: balance = _a.sent(); expect(balance).toBe(6852268489); expect(client.getBalance('Invalid Address')) .rejects.toHaveProperty('message', 'Invalid Input'); return [2 /*return*/]; } }); }); }); test('getBlockByHeight', function () { return __awaiter(void 0, void 0, void 0, function () { var genesisHeight, genesisHash, preGenesisHash, nakamotoAddress, block, _a, txIn, txOut; return __generator(this, function (_b) { switch (_b.label) { case 0: genesisHeight = 0; genesisHash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; preGenesisHash = '0000000000000000000000000000000000000000000000000000000000000000'; nakamotoAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'; return [4 /*yield*/, client.getBlockByHeight(genesisHeight)]; case 1: block = _b.sent(); expect(block.height).toBe(genesisHeight); expect(block.hash).toBe(genesisHash); expect(block.previousBlockHash).toBe(preGenesisHash); expect(block.transactions).toHaveLength(1); _a = block.transactions[0], txIn = _a.txInputs[0], txOut = _a.txOutputs[0]; expect(txIn.previousTxOutput).toBe(null); expect(txOut.address).toEqual(nakamotoAddress); expect(txOut.amount).toEqual(5 * 10e8); expect(txOut.spent).toBeFalsy(); expect(client.getBlockByHeight(-1)) .rejects.toHaveProperty('message', 'Invalid Block Height'); return [2 /*return*/]; } }); }); }); test('getBlock', function () { return __awaiter(void 0, void 0, void 0, function () { var genesisHash, blockByHeight, blockByHash; return __generator(this, function (_a) { switch (_a.label) { case 0: genesisHash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; return [4 /*yield*/, client.getBlockByHeight(0)]; case 1: blockByHeight = _a.sent(); return [4 /*yield*/, client.getBlock(genesisHash)]; case 2: blockByHash = _a.sent(); expect(blockByHash).toJSONMatch(blockByHeight); expect(client.getBlock('wrong hash')) .rejects.toHaveProperty('message', 'Not Found'); return [2 /*return*/]; } }); }); }); test('getLatestBlock', function () { return __awaiter(void 0, void 0, void 0, function () { var block, properties, _i, properties_1, property; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, client.getLatestBlock()]; case 1: block = _a.sent(); properties = Object.getOwnPropertyNames(block); expect(block).toBeInstanceOf(Block_1.Block); for (_i = 0, properties_1 = properties; _i < properties_1.length; _i++) { property = properties_1[_i]; expect(block[property]).toBeDefined(); } return [2 /*return*/]; } }); }); }); test('getTransaction', function () { return __awaiter(void 0, void 0, void 0, function () { var pizzaTransactionID, transaction, txOutput; return __generator(this, function (_a) { switch (_a.label) { case 0: pizzaTransactionID = 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'; return [4 /*yield*/, client.getTransaction(pizzaTransactionID)]; case 1: transaction = _a.sent(); expect(transaction.blockHeight).toBe(57043); expect(transaction.time.getTime()).toBe((new Date('2010-05-22T18:16:31.000Z')).getTime()); expect(transaction.txInputs).toHaveLength(131); txOutput = transaction.txOutputs.pop(); expect(txOutput.amount).toBe(1000000000000); expect(txOutput.scriptPubKey).toBe('76a91446af3fb481837fadbb421727f9959c2d32a3682988ac'); expect(txOutput.address).toBe('17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'); expect(txOutput.spent).toBe(true); return [2 /*return*/]; } }); }); }); test('getUnconfirmedTransactions', function () { return __awaiter(void 0, void 0, void 0, function () { var transactions, _i, transactions_1, transaction; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, client.getUnconfirmedTransactions()]; case 1: transactions = _a.sent(); expect(transactions).toBeDefined(); expect(Array.isArray(transactions)).toBe(true); for (_i = 0, transactions_1 = transactions; _i < transactions_1.length; _i++) { transaction = transactions_1[_i]; expect(transaction.blockHeight).toBe(null); expect(transaction.time).toBeDefined(); expect(transaction.fee).toBeDefined(); expect(Array.isArray(transaction.txInputs)).toBe(true); expect(Array.isArray(transaction.txOutputs)).toBe(true); } return [2 /*return*/]; } }); }); }); test('getAddress', function () { return __awaiter(void 0, void 0, void 0, function () { var nakamotoAddress, address, properties, _i, properties_2, property; return __generator(this, function (_a) { switch (_a.label) { case 0: nakamotoAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'; return [4 /*yield*/, client.getAddressInfo(nakamotoAddress)]; case 1: address = _a.sent(); expect(address).toBeDefined(); expect(address.address).toEqual('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); properties = Object.getOwnPropertyNames(address); for (_i = 0, properties_2 = properties; _i < properties_2.length; _i++) { property = properties_2[_i]; expect(address[property]).toBeDefined(); } // If it breaks, it's totally worth it expect(address.totalSent).toEqual(0); expect(address.transactionsCount).toEqual(address.unredeemedTransactionsCount); expect(Array.isArray(address.transactions)).toBe(true); expect(client.getAddressInfo('Invalid Address')) .rejects.toHaveProperty('message', 'Not Found'); return [2 /*return*/]; } }); }); }); test('getDifficulty', function () { return __awaiter(void 0, void 0, void 0, function () { var difficulty; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, client.getDifficulty()]; case 1: difficulty = _a.sent(); expect(difficulty).toBeDefined(); expect(difficulty).toBeGreaterThan(0); return [2 /*return*/]; } }); }); }); test('getBlockCount', function () { return __awaiter(void 0, void 0, void 0, function () { var blockCount; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, client.getBlockCount()]; case 1: blockCount = _a.sent(); expect(blockCount).toBeDefined(); // Height as of 3/10/21 expect(blockCount).toBeGreaterThan(703348); return [2 /*return*/]; } }); }); });