Guest User

Untitled

a guest
Oct 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. function contribute(BountyInterface.Bounty storage _bounty, uint _amount) external {
  2. //For now accept only contributions in the token that the bounty is in
  3. require(_bounty.status == BountyInterface.statusOptions.ACTIVE);
  4. BountyInterface.Contribution memory newContribution;
  5. newContribution.contributer = msg.sender;
  6. newContribution.token = _bounty.token;
  7. newContribution.amount = _amount;
  8. newContribution.timestamp = now;
  9. _bounty.contributions.push(newContribution);
  10. _bounty.totalContributions += _amount; //SAFEMATH THIS
  11. if (!ERC20(_bounty.token).transferFrom(msg.sender, _bounty.self, _amount)) revert();
  12. emit logContribute(msg.sender, _bounty.self, _amount, now);
  13. }
  14.  
  15. export async function bountyActivityFeed(address) {
  16. try {
  17. //We can get created from the timestamp
  18. //startWork
  19. let event = (new Interface(BountyLib.abi)).events.logStartWork
  20. console.log(event.topics)
  21. let topics = [event.topics[0], null, hexlify(padZeros(arrayify(address), 32))]
  22. let startWorkLogs = await this.provider.getLogs({
  23. fromBlock: 1,
  24. toBlock: 'latest',
  25. topics: topics
  26. })
  27. startWorkLogs = startWorkLogs.map(log => {
  28. let ev = event.parse(log.topics, log.data)
  29. return {
  30. by: ev._by,
  31. timestamp: moment((ev._timestamp.toString(10) * 1000), "x"),
  32. type: 'startWork',
  33. extraData: null
  34. }
  35. })
  36. //Commits
  37. event = (new Interface(BountyLib.abi)).events.logSubmission
  38. topics = [event.topics[0], null, hexlify(padZeros(arrayify(address), 32))]
  39. let submissionLogs = await this.provider.getLogs({
  40. fromBlock: 1,
  41. toBlock: 'latest',
  42. topics: topics
  43. })
  44. submissionLogs = submissionLogs.map(log => {
  45. let ev = event.parse(log.topics, log.data)
  46. return {
  47. by: ev._by,
  48. timestamp:moment((ev._timestamp.toString(10) * 1000), "x"),
  49. type: 'commit',
  50. extraData: ev._id
  51. }
  52. })
  53. //contributions
  54. event = (new Interface(BountyLib.abi)).events.logContribute
  55. topics = [event.topics[0], null, hexlify(padZeros(arrayify(address), 32))]
  56. let contributionLogs = await this.provider.getLogs({
  57. fromblock: 1,
  58. toBlock: 'latest',
  59. topics: topics
  60. })
  61. contributionLogs = contributionLogs.map(log => {
  62. console.log(log)
  63. let ev = event.parse(log.topics, log.data)
  64. return {
  65. by: ev._by,
  66. timestamp: moment((ev._timestamp.toString(10) * 1000), "x"),
  67. type: 'contribute',
  68. extraData: parseFloat(formatEther(ev._amount)).toFixed(2)
  69. }
  70. })
  71. //Acceptance
  72. event = (new Interface(BountyLib.abi)).events.logAccepted
  73. topics = [event.topics[0], null, hexlify(padZeros(arrayify(address), 32))]
  74. let acceptedLog = await this.provider.getLogs({
  75. fromBlock: 1,
  76. toBlock: 'latest',
  77. topics: topics
  78. })
  79. acceptedLog = acceptedLog.map(log => {
  80. let ev = event.parse(log.topics, log.data)
  81. return {
  82. by: ev._winner,
  83. timestamp: moment((ev._timestamp.toString(10) * 1000), "x"),
  84. type: 'accepted'
  85. }
  86. })
  87. let activityLog = [...startWorkLogs, ...submissionLogs, ...contributionLogs, ...acceptedLog]
  88. activityLog = activityLog.sort((a, b) => {
  89. if (a.timestamp.isBefore(b.timestamp)) {
  90. return -1
  91. } else {
  92. return 1
  93. }
  94. })
  95. return activityLog
  96. } catch (err) {
  97. console.log(err)
  98. }
  99. }
  100.  
  101. *getContribution()*
  102. Array(3) [ {…}, {…}, {…} ]
  103.  
  104. *logContribute*
  105. ContributionLogs
  106. [],
  107. length: 0
Add Comment
Please, Sign In to add comment