Guest User

Untitled

a guest
Feb 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import { withWeb3 } from './../Web3Provider'
  3. import { BlockCard } from './../BlockCard/BlockCard'
  4. import { CardsContainer } from './primitives'
  5. import ContentLoader from 'react-content-loader'
  6.  
  7.  
  8. const Loader = props => (
  9. <ContentLoader
  10. height={97}
  11. width={400}
  12. speed={2}
  13. primaryColor={"#f3f3f3"}
  14. secondaryColor={"#ecebeb"}
  15. >
  16. <rect x="70" y="15" rx="4" ry="4" width="117" height="6.4" />
  17. <rect x="70" y="35" rx="3" ry="3" width="85" height="6.4" />
  18. <rect x="4" y="5.27" rx="0" ry="0" width="60.14" height="42.16" />
  19. </ContentLoader>
  20. )
  21.  
  22. class App extends Component {
  23. state = {
  24. blocks: [],
  25. transactions: [],
  26. loading: false,
  27. selected: []
  28. }
  29.  
  30. async componentDidMount() {
  31. const blockNumber = await this.handleGetBlockNumber()
  32. this.handleGetLatestBlocks(blockNumber)
  33. }
  34.  
  35. handleGetLatestBlocks = async blockNumber => {
  36. for (let i = 0; i < 10; i++) {
  37. this.setState({ loading: true })
  38. const { blocks, transactions } = this.state
  39. const newBlock = await this.handleGetBlock(blockNumber, i)
  40. if (newBlock) {
  41. this.setState({
  42. blocks: [...blocks, newBlock],
  43. loading: false,
  44. transactions: [
  45. ...transactions, {
  46. blockHash: newBlock.hash,
  47. transactions: newBlock.transactions
  48. }
  49. ]
  50. })
  51. }
  52. }
  53. }
  54.  
  55. handleGetBlockNumber = async () => {
  56. const { web3: { eth } } = this.props
  57. try {
  58. return await eth.getBlockNumber()
  59. } catch (e) {
  60. throw new Error('Failed to fetch Block Number', e)
  61. }
  62. }
  63.  
  64. handleGetBlock = async (blockNumber, i) => {
  65. const { web3: { eth } } = this.props
  66. try {
  67. return await eth.getBlock(blockNumber - i)
  68. } catch (e) {
  69. throw new Error('Failed to fetch Block', e)
  70. }
  71. }
  72.  
  73. handleGetTransaction = async hash => {
  74. const { web3: { eth } } = this.props
  75. try {
  76. return await eth.getTransaction(hash)
  77. } catch (e) {
  78. throw new Error('Failed to fetch Block', e)
  79. }
  80. }
  81.  
  82. handleOnBlockCardClick = async hash => {
  83. const { transactions } = this.state
  84. const selected = transactions.filter((item) => item.blockHash === hash && item).pop()
  85. const result = await Promise.all(selected.transactions.map(async (item) => await this.handleGetTransaction(item)))
  86. this.setState({
  87. selected: result
  88. })
  89. }
  90.  
  91. render() {
  92. const { blocks } = this.state
  93. return (
  94. <CardsContainer>
  95. {blocks.map(block =>
  96. <BlockCard
  97. number={block.number}
  98. timestamp={block.timestamp}
  99. miner={block.miner}
  100. txns={block.transactions.length}
  101. key={block.hash}
  102. hash={block.hash}
  103. onCardClick={this.handleOnBlockCardClick}
  104. />
  105. )}
  106. {this.state.loading && <Loader />}
  107. </CardsContainer>
  108. );
  109. }
  110. }
  111.  
  112. export default withWeb3(App)
Add Comment
Please, Sign In to add comment