mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
simple consume
This commit is contained in:
parent
19eb296ab2
commit
d64ec0617e
3900
example/package-lock.json
generated
3900
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,17 +1,19 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useOcean, usePublish } from '@oceanprotocol/react'
|
import { useOcean, usePublish } from '@oceanprotocol/react'
|
||||||
import { Metadata, DDO } from '@oceanprotocol/lib'
|
import { DDO } from '@oceanprotocol/lib'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
import shortid from 'shortid'
|
import shortid from 'shortid'
|
||||||
export function AllDdos() {
|
export function AllDdos() {
|
||||||
const { accountId, ocean } = useOcean()
|
const { accountId,account, ocean } = useOcean()
|
||||||
|
|
||||||
const [ddos, setDdos] = useState<DDO[] | undefined>()
|
const [ddos, setDdos] = useState<DDO[] | undefined>()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function init() {
|
async function init() {
|
||||||
if (ocean === undefined) return
|
if (ocean === undefined || account === undefined) return
|
||||||
|
|
||||||
|
|
||||||
const assets = await ocean.assets.query({
|
const assets = await ocean.assets.query({
|
||||||
page: 1,
|
page: 1,
|
||||||
offset: 10,
|
offset: 10,
|
||||||
@ -22,7 +24,7 @@ export function AllDdos() {
|
|||||||
setDdos(assets.results)
|
setDdos(assets.results)
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [ocean])
|
}, [ocean,account])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -3,7 +3,7 @@ import './App.css'
|
|||||||
import { OceanProvider } from '@oceanprotocol/react'
|
import { OceanProvider } from '@oceanprotocol/react'
|
||||||
import { Wallet } from './Wallet'
|
import { Wallet } from './Wallet'
|
||||||
import { Publish } from './Publish'
|
import { Publish } from './Publish'
|
||||||
import { Config } from '@oceanprotocol/lib'
|
import { Config, ConfigHelper } from '@oceanprotocol/lib'
|
||||||
import { AllDdos } from './AllDdos'
|
import { AllDdos } from './AllDdos'
|
||||||
import { ConsumeDdo } from './ConsumeDdo'
|
import { ConsumeDdo } from './ConsumeDdo'
|
||||||
|
|
||||||
@ -16,12 +16,15 @@ function App() {
|
|||||||
factoryAddress: '0x2fC1fd21cb222Dc180Ef817dE4c426fd9230b5A5'
|
factoryAddress: '0x2fC1fd21cb222Dc180Ef817dE4c426fd9230b5A5'
|
||||||
} as Config
|
} as Config
|
||||||
|
|
||||||
const configRinkeby = {
|
// const configRinkeby = {
|
||||||
metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
|
// metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
|
||||||
providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
|
// providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
|
||||||
nodeUri: `https://rinkeby.infura.io/a983b53583044593956054de049922fd`,
|
// nodeUri: `https://rinkeby.infura.io/a983b53583044593956054de049922fd`,
|
||||||
factoryAddress: '0xB9d406D24B310A7D821D0b782a36909e8c925471'
|
// factoryAddress: '0xcDfEe5D80041224cDCe9AE2334E85B3236385EA3',
|
||||||
} as Config
|
// oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07',
|
||||||
|
// } as Config
|
||||||
|
|
||||||
|
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
|
||||||
const init = async () => {}
|
const init = async () => {}
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
init()
|
init()
|
||||||
@ -29,7 +32,7 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<div className="app">
|
||||||
<OceanProvider config={config}>
|
<OceanProvider config={configRinkeby}>
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<div>
|
<div>
|
||||||
<Wallet />
|
<Wallet />
|
||||||
|
@ -10,6 +10,7 @@ import { Metadata, DDO } from '@oceanprotocol/lib'
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
|
|
||||||
|
|
||||||
export function ConsumeDdo() {
|
export function ConsumeDdo() {
|
||||||
const { accountId, ocean } = useOcean()
|
const { accountId, ocean } = useOcean()
|
||||||
const { consumeStepText, consume, consumeError } = useConsume()
|
const { consumeStepText, consume, consumeError } = useConsume()
|
||||||
@ -27,6 +28,7 @@ export function ConsumeDdo() {
|
|||||||
await consume(did, ddo.dataToken, 'access')
|
await consume(did, ddo.dataToken, 'access')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const computeDid = async () => {
|
const computeDid = async () => {
|
||||||
if (did === undefined) return
|
if (did === undefined) return
|
||||||
const ddo = await ocean.assets.resolve(did)
|
const ddo = await ocean.assets.resolve(did)
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useOcean, usePublish } from '@oceanprotocol/react'
|
import { useOcean, usePublish } from '@oceanprotocol/react'
|
||||||
import { Metadata, DDO } from '@oceanprotocol/lib'
|
import { DDO } from '@oceanprotocol/lib'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
|
import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata'
|
||||||
|
|
||||||
export function Publish() {
|
export function Publish() {
|
||||||
const { accountId } = useOcean()
|
const { accountId,ocean } = useOcean()
|
||||||
const { publish, publishStepText } = usePublish()
|
const { publish, publishStepText } = usePublish()
|
||||||
const [ddo, setDdo] = useState<DDO | undefined>()
|
const [ddo, setDdo] = useState<DDO | undefined>()
|
||||||
|
|
||||||
@ -29,14 +30,13 @@ export function Publish() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const marketAddress = '0x4D156A2ef69ffdDC55838176C6712C90f60a2285'
|
|
||||||
|
|
||||||
const publishAsset = async () => {
|
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: 'access', cost: '1' },
|
||||||
{ serviceType: 'compute', cost: '1' }
|
{ serviceType: 'compute', cost: '1' }
|
||||||
])
|
])
|
||||||
console.log(ddo)
|
console.log(ddo)
|
||||||
|
const pool = ocean.pool.createDTPool(accountId,ddo.dataToken,'90','9','0.03')
|
||||||
setDdo(ddo)
|
setDdo(ddo)
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -1298,9 +1298,9 @@
|
|||||||
"integrity": "sha512-ZQ5RHQWp6xkmATt7Sl12LhnH4dovewgKPX1gGeZoDSyFcmpjMDngtJpDns8jMsaclU61tPScw7K/EmxS1ydiCg=="
|
"integrity": "sha512-ZQ5RHQWp6xkmATt7Sl12LhnH4dovewgKPX1gGeZoDSyFcmpjMDngtJpDns8jMsaclU61tPScw7K/EmxS1ydiCg=="
|
||||||
},
|
},
|
||||||
"@oceanprotocol/lib": {
|
"@oceanprotocol/lib": {
|
||||||
"version": "0.1.9",
|
"version": "0.1.10",
|
||||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.9.tgz",
|
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.10.tgz",
|
||||||
"integrity": "sha512-o4qjpoPm5s3NutNfwAV3ALPjAzvWknn6p/aI+GGH2ZtDkDE9zx0k+TqaWCsMaM/3e5v23eCof517y4Hd459EZg==",
|
"integrity": "sha512-wb0SOX0/ulLMho2Ftm1dFFLCz1+vM8mJQYNS5Ay62AUEny6fymsAMpcTLBsDU5CseqU87fHU6A5g5Rvlu7RRwQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@ethereum-navigator/navigator": "^0.5.0",
|
"@ethereum-navigator/navigator": "^0.5.0",
|
||||||
"@oceanprotocol/contracts": "^0.3.1",
|
"@oceanprotocol/contracts": "^0.3.1",
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
"dist/"
|
"dist/"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@oceanprotocol/lib": "^0.1.9",
|
"@oceanprotocol/lib": "^0.1.10",
|
||||||
"axios": "^0.19.2",
|
"axios": "^0.19.2",
|
||||||
"react": "^16.9.41",
|
"react": "^16.9.41",
|
||||||
"web3": "^1.2.11",
|
"web3": "^1.2.11",
|
||||||
|
@ -33,17 +33,56 @@ function useConsume(): UseConsume {
|
|||||||
function setStep(index: number) {
|
function setStep(index: number) {
|
||||||
setConsumeStep(index)
|
setConsumeStep(index)
|
||||||
setConsumeStepText(consumeFeedback[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(
|
async function consume(
|
||||||
did: string,
|
did: string,
|
||||||
dataTokenAddress: string,
|
dataTokenAddress: string,
|
||||||
serviceType: ServiceType = 'access'
|
serviceType: ServiceType = 'access'
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!ocean || !account) return
|
if (!ocean || !account || !accountId) return
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setConsumeError(undefined)
|
setConsumeError(undefined)
|
||||||
|
|
||||||
try {
|
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(0)
|
||||||
setStep(1)
|
setStep(1)
|
||||||
const order = await ocean.assets.order(did, serviceType, accountId)
|
const order = await ocean.assets.order(did, serviceType, accountId)
|
||||||
|
@ -53,7 +53,7 @@ function usePublish(): UsePublish {
|
|||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setPublishError(undefined)
|
setPublishError(undefined)
|
||||||
try {
|
try {
|
||||||
if (dtAddress) {
|
if (!dtAddress) {
|
||||||
setStep(0)
|
setStep(0)
|
||||||
const data = { t: 1, url: config.metadataStoreUri }
|
const data = { t: 1, url: config.metadataStoreUri }
|
||||||
const blob = JSON.stringify(data)
|
const blob = JSON.stringify(data)
|
||||||
@ -154,6 +154,11 @@ function usePublish(): UsePublish {
|
|||||||
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createBalancerPool()
|
||||||
|
{
|
||||||
|
ocean
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
publish,
|
publish,
|
||||||
mint,
|
mint,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user