/ src / modules / BlockchainExample / BlockchainExample.jsx
BlockchainExample.jsx
  1  import React from 'react'
  2  import exampleImage from './dapp.image'
  3  
  4  import BlockchainSDK from '../../common/blockchain'
  5  
  6  let SERVICES = ''
  7  
  8  const DAPP_DATA = {
  9    name: 'Test1',
 10    url: 'https://www.test1.com/',
 11    description: 'Decentralized Test DApp',
 12    category: 'test',
 13    dateCreated: Date.now(),
 14    image: exampleImage.image,
 15  }
 16  
 17  // setTimeout is used in order to wait a transaction to be mined
 18  const getResult = async function(method, params) {
 19    return new Promise((resolve, reject) => {
 20      setTimeout(async () => {
 21        const result = await SERVICES.DiscoverService[method](...params)
 22        resolve(result)
 23      }, 2000)
 24    })
 25  }
 26  
 27  /*
 28    Each transaction-function return tx hash
 29    createDApp returns tx hash + dapp id
 30  */
 31  class Example extends React.Component {
 32    async getFullDApp(id) {
 33      return getResult('getDAppDataById', [id])
 34    }
 35  
 36    async createDApp() {
 37      await SERVICES.SNTService.generateTokens()
 38      // return SERVICES.DiscoverService.createDApp(10000, DAPP_DATA)
 39    }
 40  
 41    async upvote(id) {
 42      return getResult('upVote', [id, 1000])
 43    }
 44  
 45    async downvote(id, amount) {
 46      return getResult('downVote', [id, amount])
 47    }
 48  
 49    async withdraw(id) {
 50      return getResult('withdraw', [id, 500])
 51    }
 52  
 53    async upVoteEffect(id) {
 54      return getResult('upVoteEffect', [id, 10000])
 55    }
 56  
 57    async downVoteCost(id) {
 58      return getResult('downVoteCost', [id])
 59    }
 60  
 61    async setMetadata(id) {
 62      DAPP_DATA.category = 'updated'
 63      return getResult('setMetadata', [id, DAPP_DATA])
 64    }
 65  
 66    async logDiscoverMethods() {
 67      SERVICES = await BlockchainSDK.getInstance()
 68      // console.log(await SERVICES.DiscoverService.getDApps())
 69      const createdDApp = await this.createDApp()
 70  
 71      // const dappData = await this.getFullDApp(createdDApp.id)
 72      // console.log(`Created DApp :  ${JSON.stringify(dappData)}`)
 73  
 74      // document.getElementById('testImage').src = dappData.metadata.image
 75  
 76      // const downVote = await this.downVoteCost(createdDApp.id)
 77      // console.log(
 78      //   `Downvote TX Hash :  ${await this.downvote(createdDApp.id, downVote.c)}`,
 79      // )
 80      // console.log(`Upvote TX Hash :  ${await this.upvote(createdDApp.id)}`)
 81      // console.log(`Withdraw TX Hash :  ${await this.withdraw(createdDApp.id)}`)
 82      // console.log(
 83      //   `UpvoteEffect Result :  ${await this.upVoteEffect(createdDApp.id)}`,
 84      // )
 85      // console.log(
 86      //   `DownVoteCost Result :  ${await this.downVoteCost(createdDApp.id)}`,
 87      // )
 88  
 89      // console.log(
 90      //   `Set metadata TX Hash :  ${await this.setMetadata(createdDApp.id)}`,
 91      // )
 92      // console.log(
 93      //   `Updated DApp :  ${JSON.stringify(
 94      //     await this.getFullDApp(createdDApp.id),
 95      //   )}`,
 96      // )
 97    }
 98  
 99    render() {
100      return (
101        <div>
102          <h1 onLoad={this.logDiscoverMethods()} />
103          <img id="testImage" />
104        </div>
105      )
106    }
107  }
108  
109  export default Example