Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. app.get('/api/getHistory/:contractAddress/:method', function (req, res) {
  2. const contractAddress = req.params.contractAddress
  3. const method = req.params.method
  4. let contract = null
  5. // First we obtain the contract.
  6. return Parity.getContract(contractAddress)
  7. // Then, we get the history of transactions
  8. .then(function (parsedContract) {
  9. contract = parsedContract
  10. return Parity.getHistory(contractAddress)
  11. })
  12. .then(function (events) {
  13. // console.log(events)
  14. Parity.generateDataPoints(events, contract, method, res)
  15. // return res.status(200).json(history)
  16. })
  17. .then(results => res.status(200).json(results))
  18. .catch(function (err) {
  19. console.log(err)
  20. return res.status(400).json(err.message)
  21. })
  22. })
  23.  
  24.  
  25. /////////////////////////////////////////////
  26.  
  27. generateDataPoints: function (events, contract, method, res) {
  28. Promise.all(
  29. events.map(function(event) {
  30. console.log('mapping')
  31. return Promise.all([
  32. Parity.getBlockTime(event.blockNumber.valueOf()),
  33. Parity.queryAtBlock(contract[method], event.blockNumber.valueOf())
  34. ])
  35. })
  36. )
  37. .then(function (pairs) {
  38. console.log('filtering')
  39. let prevTime = 0
  40. return pairs.filter(([time, val]) => {
  41. if (time !== prevTime) {
  42. prevTime = time;
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. })
  48. })
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement