1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-22 09:47:06 +01:00

get curation and curation utils

This commit is contained in:
mihaisc 2020-05-06 12:27:06 +03:00
parent 01ec93658f
commit e6186762bd
3 changed files with 102 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { useState, useEffect } from 'react'
import axios from 'axios'
import { DID, DDO, MetaData } from '@oceanprotocol/squid'
import { DID, DDO, MetaData, Curation } from '@oceanprotocol/squid'
import { useOcean } from '../../providers'
interface UseMetadata {
@ -9,6 +9,7 @@ interface UseMetadata {
title: string
getDDO: (did: DID | string) => Promise<DDO>
getMetadata: (did: DID | string) => Promise<MetaData>
getCuration: (did: DID | string) => Promise<Curation>
getTitle: (did: DID | string) => Promise<string>
getAllDIDs: () => Promise<DID[]>
}
@ -30,6 +31,11 @@ function useMetadata(did?: DID | string): UseMetadata {
return metadata.attributes
}
async function getCuration(did: DID | string): Promise<Curation> {
const metadata = await getMetadata(did)
return metadata.curation
}
async function getTitle(did: DID | string): Promise<string> {
const metadata = await getMetadata(did)
return metadata.main.name
@ -52,7 +58,16 @@ function useMetadata(did?: DID | string): UseMetadata {
init()
}, [])
return { ddo, metadata, title, getDDO, getMetadata, getTitle, getAllDIDs }
return {
ddo,
metadata,
title,
getDDO,
getMetadata,
getCuration,
getTitle,
getAllDIDs
}
}
export { useMetadata, UseMetadata }

View File

@ -3,3 +3,4 @@ import './@types/globals'
export * from './providers'
export * from './hooks'
// export * from './components'
export * from './utils/curationUtils'

View File

@ -0,0 +1,84 @@
import axios, { AxiosResponse } from 'axios'
import Web3 from 'web3'
import { DID } from '@oceanprotocol/squid'
export declare type RatingResponse = [string, number]
export declare type GetRatingResponse = {
comment: string
datePublished: string
vote: number
}
export function gethash(message: string) {
let hex = ''
for (let i = 0; i < message.length; i++) {
hex += '' + message.charCodeAt(i).toString(16)
}
const hexMessage = '0x' + hex
return hexMessage
}
export async function rateAsset({
did,
web3,
value,
configUrl
}: {
did: DID | string
web3: Web3
value: number
configUrl: string
}): Promise<RatingResponse | string> {
try {
const timestamp = Math.floor(+new Date() / 1000)
const accounts = await web3.eth.getAccounts()
const signature = await web3.eth.personal.sign(
gethash(`${timestamp}`),
accounts ? accounts[0] : '',
''
)
const ratingBody = {
did,
vote: value,
comment: '',
address: accounts[0],
timestamp: timestamp,
signature: signature
}
const response: AxiosResponse = await axios.post(configUrl, ratingBody)
if (!response) return 'No Response'
return response.data
} catch (error) {
console.error(error.message)
return `Error: ${error.message}`
}
}
export async function getAssetRating({
did,
account,
configUrl
}: {
did: DID | string
account: string
configUrl: string
}): Promise<GetRatingResponse | undefined> {
try {
if (!account) return
const response = await axios.get(configUrl, {
params: {
did: did,
address: account
}
})
const votesLength = response.data.length
if (votesLength > 0) {
return response.data[votesLength - 1]
}
} catch (error) {
console.error(error.message)
}
}