Source: utils/logUtils.js

  1. /**
  2. * @file Manages helper functions to work with logs
  3. * @author Ivan Violentov <ivan.violentov@jibrel.network>
  4. */
  5. import Promise from 'bluebird'
  6. import config from '../config'
  7. /**
  8. * @function filter
  9. *
  10. * @description Gets filter object, that can be used for getting of past logs or watching of new
  11. *
  12. * @param {object} [options={}] - Filter options
  13. * @param {(number|string)} [options.fromBlock] - The number of the earliest block
  14. * @param {(number|string)} [options.toBlock] - The number of the latest block
  15. * @param {(string|string[])} [options.address] - An address(es) to get logs from
  16. * @param {string[]} [options.topics] - Allows to manually set the topics for the event filter
  17. *
  18. * @returns {object} (@see {@link https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethfilter})
  19. */
  20. export function filter(options = {}) {
  21. return jWeb3.eth.filter(options)
  22. }
  23. /**
  24. * @async
  25. * @function getLogs
  26. *
  27. * @description Gets past logs
  28. *
  29. * @param {object} [options={}] - Filter options
  30. * @param {(number|string)} [options.fromBlock] - The number of the earliest block
  31. * @param {(number|string)} [options.toBlock] - The number of the latest block
  32. * @param {(string|string[])} [options.address] - An address(es) to get logs from
  33. * @param {string[]} [options.topics] - Allows to manually set the topics for the event filter
  34. *
  35. * @returns Promise that will be resolved with past logs
  36. */
  37. export function getLogs(options = {}) {
  38. const filterObject = jWeb3.eth.filter(options)
  39. /**
  40. * filter.get uses instance methods inside,
  41. * but bluebird promisify don't save context,
  42. * so need to bind to filter object directly
  43. */
  44. return Promise
  45. .promisify(filterObject.get.bind(filterObject))()
  46. .timeout(
  47. config.promiseTimeout,
  48. new Error(`Can not get past logs within ${config.promiseTimeout}ms`)
  49. )
  50. }