UNPKG

apla-blockchain-tools

Version:

Module contains a number of tools to work with Apla Blockchain

138 lines (121 loc) 4.31 kB
"use strict"; // MIT License // // Copyright (c) 2016-2018 AplaProject // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. /* * Package: apla-blockchain-tools * Author: Anton Zuev * mail: a.zuev@apla.io * Company: apla.io */ const Session = require("../session/index"); const _ = require("underscore"); class TransactionsFilter{ /** * * @param apiUrl * @param options * @param options.numberOfBlocksToFetch - number of blocks to fetch at a time. */ constructor(apiUrl, options){ options = options || {}; this.session = new Session(apiUrl); this.numberOfBlocksToFetch = options.numberOfBlocksToFetch || 50; } /** * returns a list of transactions * @param filters * @return {Promise.<void>} */ async getTransactionsBy(filters){ let from = 0; let response = await this.session.getDetailedBlocks(from, this.numberOfBlocksToFetch); let blocks = await response.json(); let result = []; while(!blocks.error){ result.push(TransactionsFilter.processBlocks(blocks, filters)); from += this.numberOfBlocksToFetch; response = await this.session.getDetailedBlocks(from, this.numberOfBlocksToFetch); blocks = await response.json() } return _.flatten(result); } /** * processes blocks. Returns a list of transactions filtered by filters from blocks * @param blocks * @param filters */ static processBlocks(blocks, filters){ let result = []; for (let blockId in blocks){ result.push(TransactionsFilter.processBlock(blocks[blockId], filters)); } return _.flatten(result); } /** * processes one block. Returns a list of transactions filtered by filters from block * @param block * @param filters * @return {Array} */ static processBlock(block, filters) { let result = []; for (let transaction of block.transactions) { if (TransactionsFilter.processTransaction(transaction, filters)) { transaction.blockId = block.header.block_id; result.push(transaction) } } return result; } /** * processes one transaction and returns whether transaction correspond to at least one of the conditions or not * @param transaction * @param filters * @return {boolean} */ static processTransaction(transaction, filters) { for (let filter of filters){ if(TransactionsFilter.applyFilter(transaction, filter)){ return true; } } return false; } /** * checks whether transaction corresponds to the filter passed * @param transaction * @param filter * @return {boolean} */ static applyFilter(transaction, filter) { if(transaction.contract_name.toString() !== filter.contractName.toString()){ return false; } for (let key in filter.params){ if(filter.params[key].toString() !== transaction.params[key].toString()){ return false; } } return true; } } module.exports = TransactionsFilter;