UNPKG

@parity/api

Version:

The Parity Promise-based API library for interfacing with Ethereum over RPC

488 lines (487 loc) 19.4 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: MIT Object.defineProperty(exports, "__esModule", { value: true }); var bignumber_js_1 = require("bignumber.js"); var address_1 = require("@parity/abi/lib/util/address"); var types_1 = require("@parity/abi/lib/util/types"); var output_1 = require("./output"); describe('format/output', function () { var address = '0x63cf90d3f0410092fc0fca41846f596223979195'; var checksum = '0x63Cf90D3f0410092FC0fca41846f596223979195'; describe('outAccountInfo', function () { it('returns meta objects parsed', function () { expect(output_1.outAccountInfo({ '0x63cf90d3f0410092fc0fca41846f596223979195': { name: 'name', uuid: 'uuid', meta: '{"name":"456"}' } })).toEqual({ '0x63Cf90D3f0410092FC0fca41846f596223979195': { name: 'name', uuid: 'uuid', meta: { name: '456' } } }); }); it('returns objects without meta & uuid as required', function () { expect(output_1.outAccountInfo({ '0x63cf90d3f0410092fc0fca41846f596223979195': { name: 'name' } })).toEqual({ '0x63Cf90D3f0410092FC0fca41846f596223979195': { name: 'name' } }); }); }); describe('outAddress', function () { it('retuns the address as checksummed', function () { expect(output_1.outAddress(address)).toEqual(checksum); }); it('retuns the checksum as checksummed', function () { expect(output_1.outAddress(checksum)).toEqual(checksum); }); }); describe('outBlock', function () { ['author', 'miner'].forEach(function (input) { it("formats " + input + " address as address", function () { var block = {}; block[input] = address; var formatted = output_1.outBlock(block)[input]; expect(address_1.isAddress(formatted)).toBe(true); expect(formatted).toEqual(checksum); }); }); [ 'difficulty', 'gasLimit', 'gasUsed', 'number', 'nonce', 'totalDifficulty' ].forEach(function (input) { it("formats " + input + " number as hexnumber", function () { var block = {}; // @ts-ignore block[input] = 0x123; var formatted = output_1.outBlock(block)[input]; expect(types_1.isInstanceOf(formatted, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.toString(16)).toEqual('123'); }); }); ['timestamp'].forEach(function (input) { it("formats " + input + " number as Date", function () { var block = {}; // @ts-ignore block[input] = 0x57513668; var formatted = output_1.outBlock(block)[input]; expect(types_1.isInstanceOf(formatted, Date)).toBe(true); expect(formatted.getTime()).toEqual(1464940136000); }); }); it('ignores and passes through unknown keys', function () { expect(output_1.outBlock({ someRandom: 'someRandom' })).toEqual({ someRandom: 'someRandom' }); }); it('formats a block with all the info converted', function () { expect(output_1.outBlock({ author: address, miner: address, difficulty: '0x100', gasLimit: '0x101', gasUsed: '0x102', number: '0x103', nonce: '0x104', totalDifficulty: '0x105', timestamp: '0x57513668', extraData: 'someExtraStuffInHere' })).toEqual({ author: checksum, miner: checksum, difficulty: new bignumber_js_1.default('0x100'), gasLimit: new bignumber_js_1.default('0x101'), gasUsed: new bignumber_js_1.default('0x102'), number: new bignumber_js_1.default('0x103'), nonce: new bignumber_js_1.default('0x104'), totalDifficulty: new bignumber_js_1.default('0x105'), timestamp: new Date('2016-06-03T07:48:56.000Z'), extraData: 'someExtraStuffInHere' }); }); }); describe('outChainStatus', function () { it('formats blockGap values', function () { var status = { blockGap: [0x1234, '0x5678'] }; expect(output_1.outChainStatus(status)).toEqual({ blockGap: [new bignumber_js_1.default(0x1234), new bignumber_js_1.default(0x5678)] }); }); it('handles null blockGap values', function () { var status = { blockGap: undefined }; expect(output_1.outChainStatus(status)).toEqual(status); }); }); describe('outDate', function () { it('converts a second date in unix timestamp', function () { expect(output_1.outDate(0x57513668)).toEqual(new Date('2016-06-03T07:48:56.000Z')); }); }); describe('outHistogram', function () { ['bucketBounds', 'counts'].forEach(function (type) { it("formats " + type + " as number arrays", function () { var _a, _b; expect(output_1.outHistogram((_a = {}, _a[type] = [0x123, 0x456, 0x789], _a))).toEqual((_b = {}, _b[type] = [ new bignumber_js_1.default(0x123), new bignumber_js_1.default(0x456), new bignumber_js_1.default(0x789) ], _b)); }); }); }); describe('outHwAccountInfo', function () { it('returns objects with formatted addresses', function () { expect(output_1.outHwAccountInfo({ '0x63cf90d3f0410092fc0fca41846f596223979195': { manufacturer: 'mfg', name: 'type' } })).toEqual({ '0x63Cf90D3f0410092FC0fca41846f596223979195': { manufacturer: 'mfg', name: 'type' } }); }); }); describe('outNodeKind', function () { it('formats the input as received', function () { var kind = { availability: 'personal', capability: 'full' }; expect(output_1.outNodeKind(kind)).toEqual(kind); }); }); describe('outNumber', function () { it('returns a BigNumber equalling the value', function () { var bn = output_1.outNumber('0x123456'); expect(types_1.isInstanceOf(bn, bignumber_js_1.default)).toBe(true); expect(bn.eq(0x123456)).toBe(true); }); it('assumes 0 when ivalid input', function () { expect(output_1.outNumber().eq(0)).toBe(true); }); }); describe('outPeer', function () { it('converts all internal numbers to BigNumbers', function () { expect(output_1.outPeer({ caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: { par: { difficulty: '0x0f', head: '0x02', version: 63 } } })).toEqual({ caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: { par: { difficulty: new bignumber_js_1.default(15), head: '0x02', version: 63 } } }); }); it('does not output null protocols', function () { expect(output_1.outPeer({ caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: { les: null } })).toEqual({ caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: {} }); }); }); describe('outPeers', function () { it('converts all internal numbers to BigNumbers', function () { expect(output_1.outPeers({ active: 789, connected: '456', max: 0x7b, peers: [ { caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: { par: { difficulty: '0x0f', head: '0x02', version: 63 }, les: null } } ] })).toEqual({ active: new bignumber_js_1.default(789), connected: new bignumber_js_1.default(456), max: new bignumber_js_1.default(123), peers: [ { caps: ['par/1'], id: '0x01', name: 'Parity', network: { localAddress: '10.0.0.1', remoteAddress: '10.0.0.1' }, protocols: { par: { difficulty: new bignumber_js_1.default(15), head: '0x02', version: 63 } } } ] }); }); }); describe('outReceipt', function () { ['contractAddress'].forEach(function (input) { it("formats " + input + " address as address", function () { var block = {}; block[input] = address; var formatted = output_1.outReceipt(block)[input]; expect(address_1.isAddress(formatted)).toBe(true); expect(formatted).toEqual(checksum); }); }); [ 'blockNumber', 'cumulativeGasUsed', 'cumulativeGasUsed', 'gasUsed', 'transactionIndex' ].forEach(function (input) { it("formats " + input + " number as hexnumber", function () { var block = {}; // @ts-ignore block[input] = 0x123; var formatted = output_1.outReceipt(block)[input]; expect(types_1.isInstanceOf(formatted, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.toString(16)).toEqual('123'); }); }); it('ignores and passes through unknown keys', function () { // @ts-ignore expect(output_1.outReceipt({ someRandom: 'someRandom' })).toEqual({ someRandom: 'someRandom' }); }); it('formats a receipt with all the info converted', function () { expect(output_1.outReceipt({ contractAddress: address, blockNumber: '0x100', cumulativeGasUsed: '0x101', gasUsed: '0x102', transactionIndex: '0x103', extraData: 'someExtraStuffInHere' })).toEqual({ contractAddress: checksum, blockNumber: new bignumber_js_1.default('0x100'), cumulativeGasUsed: new bignumber_js_1.default('0x101'), gasUsed: new bignumber_js_1.default('0x102'), transactionIndex: new bignumber_js_1.default('0x103'), extraData: 'someExtraStuffInHere' }); }); }); describe('outSyncing', function () { [ 'currentBlock', 'highestBlock', 'startingBlock', 'warpChunksAmount', 'warpChunksProcessed' ].forEach(function (input) { it("formats " + input + " numbers as a number", function () { var _a, _b; expect(output_1.outSyncing((_a = {}, _a[input] = '0x123', _a))).toEqual((_b = {}, _b[input] = new bignumber_js_1.default('0x123'), _b)); }); }); it('formats blockGap properly', function () { expect(output_1.outSyncing({ blockGap: [0x123, 0x456] })).toEqual({ blockGap: [new bignumber_js_1.default(0x123), new bignumber_js_1.default(0x456)] }); }); }); describe('outTransaction', function () { ['from', 'to'].forEach(function (input) { it("formats " + input + " address as address", function () { var block = {}; block[input] = address; var formatted = output_1.outTransaction(block)[input]; expect(address_1.isAddress(formatted)).toBe(true); expect(formatted).toEqual(checksum); }); }); [ 'blockNumber', 'gasPrice', 'gas', 'nonce', 'transactionIndex', 'value' ].forEach(function (input) { it("formats " + input + " number as hexnumber", function () { var block = {}; // @ts-ignore block[input] = 0x123; var formatted = output_1.outTransaction(block)[input]; expect(types_1.isInstanceOf(formatted, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.toString(16)).toEqual('123'); }); }); it('passes condition as null when null', function () { expect(output_1.outTransaction({ condition: null })).toEqual({ condition: null }); }); it('ignores and passes through unknown keys', function () { // @ts-ignore expect(output_1.outTransaction({ someRandom: 'someRandom' })).toEqual({ someRandom: 'someRandom' }); }); it('formats a transaction with all the info converted', function () { expect(output_1.outTransaction({ from: address, to: address, blockNumber: '0x100', gasPrice: '0x101', gas: '0x102', nonce: '0x103', transactionIndex: '0x104', value: '0x105', extraData: 'someExtraStuffInHere' })).toEqual({ from: checksum, to: checksum, blockNumber: new bignumber_js_1.default('0x100'), gasPrice: new bignumber_js_1.default('0x101'), gas: new bignumber_js_1.default('0x102'), nonce: new bignumber_js_1.default('0x103'), transactionIndex: new bignumber_js_1.default('0x104'), value: new bignumber_js_1.default('0x105'), extraData: 'someExtraStuffInHere' }); }); }); describe('outTrace', function () { it('ignores and passes through unknown keys', function () { // @ts-ignore expect(output_1.outTrace({ someRandom: 'someRandom' })).toEqual({ someRandom: 'someRandom' }); }); it('formats a trace with all the info converted', function () { var formatted = output_1.outTrace({ type: 'call', action: { from: address, to: address, value: '0x06', gas: '0x07', input: '0x1234', callType: 'call' }, result: { gasUsed: '0x08', output: '0x5678' }, traceAddress: ['0x2'], subtraces: 3, transactionPosition: '0xb', transactionHash: '0x000000000000000000000000000000000000000000000000000000000000000c', blockNumber: '0x0d', blockHash: '0x000000000000000000000000000000000000000000000000000000000000000e' }); // @ts-ignore expect(types_1.isInstanceOf(formatted.action.gas, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.action.gas.toNumber()).toEqual(7); // @ts-ignore expect(types_1.isInstanceOf(formatted.action.value, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.action.value.toNumber()).toEqual(6); // @ts-ignore expect(formatted.action.from).toEqual(checksum); // @ts-ignore expect(formatted.action.to).toEqual(checksum); // @ts-ignore expect(types_1.isInstanceOf(formatted.blockNumber, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.blockNumber.toNumber()).toEqual(13); // @ts-ignore expect(types_1.isInstanceOf(formatted.transactionPosition, bignumber_js_1.default)).toBe(true); // @ts-ignore expect(formatted.transactionPosition.toNumber()).toEqual(11); }); }); describe('outVaultMeta', function () { it('returns an exmpt object on null', function () { expect(output_1.outVaultMeta(null)).toEqual({}); }); it('returns the original value if not string', function () { expect(output_1.outVaultMeta({ test: 123 })).toEqual({ test: 123 }); }); it('returns an object from JSON string', function () { expect(output_1.outVaultMeta('{"test":123}')).toEqual({ test: 123 }); }); it('returns an empty object on invalid JSON', function () { expect(output_1.outVaultMeta('{"test"}')).toEqual({}); }); }); });