1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-23 02:00:27 +01:00

simple consume

This commit is contained in:
mihaisc 2020-07-31 12:12:32 +03:00
parent 19eb296ab2
commit d64ec0617e
9 changed files with 3653 additions and 344 deletions

3900
example/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,19 @@
import React from 'react'
import { useOcean, usePublish } from '@oceanprotocol/react'
import { Metadata, DDO } from '@oceanprotocol/lib'
import { DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { useEffect } from 'react'
import shortid from 'shortid'
export function AllDdos() {
const { accountId, ocean } = useOcean()
const { accountId,account, ocean } = useOcean()
const [ddos, setDdos] = useState<DDO[] | undefined>()
useEffect(() => {
async function init() {
if (ocean === undefined) return
if (ocean === undefined || account === undefined) return
const assets = await ocean.assets.query({
page: 1,
offset: 10,
@ -22,7 +24,7 @@ export function AllDdos() {
setDdos(assets.results)
}
init()
}, [ocean])
}, [ocean,account])
return (
<>

View File

@ -3,7 +3,7 @@ import './App.css'
import { OceanProvider } from '@oceanprotocol/react'
import { Wallet } from './Wallet'
import { Publish } from './Publish'
import { Config } from '@oceanprotocol/lib'
import { Config, ConfigHelper } from '@oceanprotocol/lib'
import { AllDdos } from './AllDdos'
import { ConsumeDdo } from './ConsumeDdo'
@ -16,12 +16,15 @@ function App() {
factoryAddress: '0x2fC1fd21cb222Dc180Ef817dE4c426fd9230b5A5'
} as Config
const configRinkeby = {
metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
nodeUri: `https://rinkeby.infura.io/a983b53583044593956054de049922fd`,
factoryAddress: '0xB9d406D24B310A7D821D0b782a36909e8c925471'
} as Config
// const configRinkeby = {
// metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
// providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
// nodeUri: `https://rinkeby.infura.io/a983b53583044593956054de049922fd`,
// factoryAddress: '0xcDfEe5D80041224cDCe9AE2334E85B3236385EA3',
// oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07',
// } as Config
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
const init = async () => {}
useEffect(() => {
init()
@ -29,7 +32,7 @@ function App() {
return (
<div className="app">
<OceanProvider config={config}>
<OceanProvider config={configRinkeby}>
<div className="container">
<div>
<Wallet />

View File

@ -10,6 +10,7 @@ import { Metadata, DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { useEffect } from 'react'
export function ConsumeDdo() {
const { accountId, ocean } = useOcean()
const { consumeStepText, consume, consumeError } = useConsume()
@ -27,6 +28,7 @@ export function ConsumeDdo() {
await consume(did, ddo.dataToken, 'access')
}
const computeDid = async () => {
if (did === undefined) return
const ddo = await ocean.assets.resolve(did)

View File

@ -1,10 +1,11 @@
import React from 'react'
import { useOcean, usePublish } from '@oceanprotocol/react'
import { Metadata, DDO } from '@oceanprotocol/lib'
import { DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata'
export function Publish() {
const { accountId } = useOcean()
const { accountId,ocean } = useOcean()
const { publish, publishStepText } = usePublish()
const [ddo, setDdo] = useState<DDO | undefined>()
@ -29,14 +30,13 @@ export function Publish() {
}
}
const marketAddress = '0x4D156A2ef69ffdDC55838176C6712C90f60a2285'
const publishAsset = async () => {
const ddo = await publish(asset as Metadata, '4', marketAddress, [
const ddo = await publish(asset as Metadata, '90', [
{ serviceType: 'access', cost: '1' },
{ serviceType: 'compute', cost: '1' }
])
console.log(ddo)
const pool = ocean.pool.createDTPool(accountId,ddo.dataToken,'90','9','0.03')
setDdo(ddo)
}
return (

6
package-lock.json generated
View File

@ -1298,9 +1298,9 @@
"integrity": "sha512-ZQ5RHQWp6xkmATt7Sl12LhnH4dovewgKPX1gGeZoDSyFcmpjMDngtJpDns8jMsaclU61tPScw7K/EmxS1ydiCg=="
},
"@oceanprotocol/lib": {
"version": "0.1.9",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.9.tgz",
"integrity": "sha512-o4qjpoPm5s3NutNfwAV3ALPjAzvWknn6p/aI+GGH2ZtDkDE9zx0k+TqaWCsMaM/3e5v23eCof517y4Hd459EZg==",
"version": "0.1.10",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.10.tgz",
"integrity": "sha512-wb0SOX0/ulLMho2Ftm1dFFLCz1+vM8mJQYNS5Ay62AUEny6fymsAMpcTLBsDU5CseqU87fHU6A5g5Rvlu7RRwQ==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.3.1",

View File

@ -25,7 +25,7 @@
"dist/"
],
"dependencies": {
"@oceanprotocol/lib": "^0.1.9",
"@oceanprotocol/lib": "^0.1.10",
"axios": "^0.19.2",
"react": "^16.9.41",
"web3": "^1.2.11",

View File

@ -33,17 +33,56 @@ function useConsume(): UseConsume {
function setStep(index: number) {
setConsumeStep(index)
setConsumeStepText(consumeFeedback[index])
}
async function getCheapestPool(dataTokenAddress): Promise<{poolAddress: string,poolPrice: string}> {
const tokenPools = await ocean.pool.searchPoolforDT(accountId, dataTokenAddress)
Logger.log('DT Pool found', tokenPools)
let cheapestPoolAddress
let cheapestPoolPrice = 999999
if (tokenPools) {
for (let i = 0; i < tokenPools.length; i++) {
const poolPrice = await ocean.pool.getDTPrice(accountId, tokenPools[i])
Logger.log('Pool price ',tokenPools[i],poolPrice)
if (poolPrice < cheapestPoolPrice) {
cheapestPoolPrice = poolPrice
cheapestPoolAddress = tokenPools[i]
}
}
}
return { poolAddress:cheapestPoolAddress, poolPrice: cheapestPoolPrice.toString()}
}
async function consume(
did: string,
dataTokenAddress: string,
serviceType: ServiceType = 'access'
): Promise<void> {
if (!ocean || !account) return
if (!ocean || !account || !accountId) return
setIsLoading(true)
setConsumeError(undefined)
try {
const userOwnedTokens = await ocean.accounts.getTokenBalance(dataTokenAddress, account)
Logger.log(`User has ${userOwnedTokens} tokens`)
let cheapestPool
if (userOwnedTokens === '0') {
cheapestPool = await getCheapestPool(dataTokenAddress)
let maxPrice: number = +cheapestPool.poolPrice *10
Logger.log('Buying token', cheapestPool,accountId, maxPrice.toString())
let buyResponse = await ocean.pool.buyDT(accountId,cheapestPool.poolAddress,'1','100','999999999999999999999999999999999999999999')
Logger.log('DT buy response', buyResponse)
if(buyResponse === null) {
return
}
}
setStep(0)
setStep(1)
const order = await ocean.assets.order(did, serviceType, accountId)

View File

@ -53,7 +53,7 @@ function usePublish(): UsePublish {
setIsLoading(true)
setPublishError(undefined)
try {
if (dtAddress) {
if (!dtAddress) {
setStep(0)
const data = { t: 1, url: config.metadataStoreUri }
const blob = JSON.stringify(data)
@ -154,6 +154,11 @@ function usePublish(): UsePublish {
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
}
async function createBalancerPool()
{
ocean
}
return {
publish,
mint,