mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
format/test
This commit is contained in:
parent
feb56b33b8
commit
53ca5ad6cb
2
example/package-lock.json
generated
2
example/package-lock.json
generated
@ -1702,7 +1702,7 @@
|
|||||||
"axios": "^0.19.2",
|
"axios": "^0.19.2",
|
||||||
"react": "^16.9.41",
|
"react": "^16.9.41",
|
||||||
"web3": "^1.2.11",
|
"web3": "^1.2.11",
|
||||||
"web3modal": "^1.8.0"
|
"web3modal": "^1.8.0",
|
||||||
"@ethereum-navigator/navigator": "^0.5.0",
|
"@ethereum-navigator/navigator": "^0.5.0",
|
||||||
"@oceanprotocol/contracts": "^0.3.1",
|
"@oceanprotocol/contracts": "^0.3.1",
|
||||||
"bignumber.js": "^9.0.0",
|
"bignumber.js": "^9.0.0",
|
||||||
|
@ -5,7 +5,7 @@ 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,account, ocean } = useOcean()
|
const { accountId, account, ocean } = useOcean()
|
||||||
|
|
||||||
const [ddos, setDdos] = useState<DDO[] | undefined>()
|
const [ddos, setDdos] = useState<DDO[] | undefined>()
|
||||||
|
|
||||||
@ -13,7 +13,6 @@ export function AllDdos() {
|
|||||||
async function init() {
|
async function init() {
|
||||||
if (ocean === undefined || account === 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,
|
||||||
@ -24,7 +23,7 @@ export function AllDdos() {
|
|||||||
setDdos(assets.results)
|
setDdos(assets.results)
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [ocean,account])
|
}, [ocean, account])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -52,24 +52,24 @@ function App() {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OceanProvider config={configRinkeby} web3ModalOpts={web3ModalOpts}>
|
<OceanProvider config={configRinkeby} web3ModalOpts={web3ModalOpts}>
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<div>
|
<div>
|
||||||
<Wallet />
|
<Wallet />
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<AllDdos />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<Publish />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<ConsumeDdo />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</OceanProvider>
|
<div>
|
||||||
|
<AllDdos />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<Publish />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<ConsumeDdo />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</OceanProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ 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()
|
||||||
@ -28,7 +27,6 @@ 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,11 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useOcean, usePublish } from '@oceanprotocol/react'
|
import { useOcean, usePublish } from '@oceanprotocol/react'
|
||||||
import { 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'
|
import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata'
|
||||||
|
|
||||||
export function Publish() {
|
export function Publish() {
|
||||||
const { accountId,ocean } = 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>()
|
||||||
|
|
||||||
@ -31,9 +31,15 @@ export function Publish() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const publishAsset = async () => {
|
const publishAsset = async () => {
|
||||||
const ddo = await publish(asset as Metadata, '90','access','','')
|
const ddo = await publish(asset as Metadata, '90', 'access', '', '')
|
||||||
console.log(ddo)
|
console.log(ddo)
|
||||||
const pool = ocean.pool.createDTPool(accountId,ddo.dataToken,'90','9','0.03')
|
const pool = ocean.pool.createDTPool(
|
||||||
|
accountId,
|
||||||
|
ddo.dataToken,
|
||||||
|
'90',
|
||||||
|
'9',
|
||||||
|
'0.03'
|
||||||
|
)
|
||||||
setDdo(ddo)
|
setDdo(ddo)
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
@ -65,7 +65,7 @@ function useCompute(): UseCompute {
|
|||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setStep(0)
|
setStep(0)
|
||||||
|
|
||||||
await checkAndBuyDT(ocean,dataTokenAddress, account)
|
await checkAndBuyDT(ocean, dataTokenAddress, account)
|
||||||
rawAlgorithmMeta.container = computeContainer
|
rawAlgorithmMeta.container = computeContainer
|
||||||
rawAlgorithmMeta.rawcode = algorithmRawCode
|
rawAlgorithmMeta.rawcode = algorithmRawCode
|
||||||
|
|
||||||
|
@ -2,8 +2,7 @@ import { useState } from 'react'
|
|||||||
import { useOcean } from '../../providers'
|
import { useOcean } from '../../providers'
|
||||||
import { feedback } from '../../utils'
|
import { feedback } from '../../utils'
|
||||||
import { DID, Logger, ServiceType } from '@oceanprotocol/lib'
|
import { DID, Logger, ServiceType } from '@oceanprotocol/lib'
|
||||||
import { getCheapestPool, checkAndBuyDT } from '../../utils/dtUtils'
|
import { checkAndBuyDT } from '../../utils/dtUtils'
|
||||||
const Decimal = require('decimal.js')
|
|
||||||
interface UseConsume {
|
interface UseConsume {
|
||||||
consume: (
|
consume: (
|
||||||
did: DID | string,
|
did: DID | string,
|
||||||
@ -46,10 +45,8 @@ function useConsume(): UseConsume {
|
|||||||
setConsumeError(undefined)
|
setConsumeError(undefined)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
setStep(0)
|
setStep(0)
|
||||||
await checkAndBuyDT(ocean,dataTokenAddress, account)
|
await checkAndBuyDT(ocean, dataTokenAddress, account)
|
||||||
|
|
||||||
|
|
||||||
setStep(1)
|
setStep(1)
|
||||||
const order = await ocean.assets.order(did, serviceType, accountId)
|
const order = await ocean.assets.order(did, serviceType, accountId)
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import {
|
import { DID, DDO, Metadata, MetadataStore, Logger } from '@oceanprotocol/lib'
|
||||||
DID,
|
|
||||||
DDO,
|
|
||||||
Metadata,
|
|
||||||
MetadataStore,
|
|
||||||
Logger,
|
|
||||||
} from '@oceanprotocol/lib'
|
|
||||||
import { useOcean } from '../../providers'
|
import { useOcean } from '../../providers'
|
||||||
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
|
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
|
||||||
import { getBestDataTokenPrice } from '../../utils/dtUtils'
|
import { getBestDataTokenPrice } from '../../utils/dtUtils'
|
||||||
@ -38,7 +32,7 @@ function useMetadata(did?: DID | string): UseMetadata {
|
|||||||
return ddo
|
return ddo
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getBestPrice(dataTokenAddress:string): Promise<string> {
|
async function getBestPrice(dataTokenAddress: string): Promise<string> {
|
||||||
return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress)
|
return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ interface UsePublish {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function usePublish(): UsePublish {
|
function usePublish(): UsePublish {
|
||||||
const { ocean, status, account, accountId, config } = useOcean()
|
const { ocean, status, account, accountId, config } = useOcean()
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
const [publishStep, setPublishStep] = useState<number | undefined>()
|
const [publishStep, setPublishStep] = useState<number | undefined>()
|
||||||
const [publishStepText, setPublishStepText] = useState<string | undefined>()
|
const [publishStepText, setPublishStepText] = useState<string | undefined>()
|
||||||
@ -73,7 +73,6 @@ function usePublish(): UsePublish {
|
|||||||
const timeout = 0
|
const timeout = 0
|
||||||
const services: Service[] = []
|
const services: Service[] = []
|
||||||
|
|
||||||
|
|
||||||
const price = ocean.datatokens.toWei('1')
|
const price = ocean.datatokens.toWei('1')
|
||||||
switch (serviceType) {
|
switch (serviceType) {
|
||||||
case 'access': {
|
case 'access': {
|
||||||
@ -156,10 +155,6 @@ 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,
|
||||||
|
@ -1,14 +1,25 @@
|
|||||||
import { Logger, Ocean, Account } from '@oceanprotocol/lib'
|
import { Logger, Ocean, Account } from '@oceanprotocol/lib'
|
||||||
const Decimal = require('decimal.js')
|
const Decimal = await require('decimal.js')
|
||||||
export async function getCheapestPool(ocean: Ocean, accountId: string, dataTokenAddress: string): Promise<{ poolAddress: string, poolPrice: string }> {
|
export async function getCheapestPool(
|
||||||
const tokenPools = await ocean.pool.searchPoolforDT(accountId, dataTokenAddress)
|
ocean: Ocean,
|
||||||
|
accountId: string,
|
||||||
|
dataTokenAddress: string
|
||||||
|
): Promise<{ poolAddress: string; poolPrice: string }> {
|
||||||
|
const tokenPools = await ocean.pool.searchPoolforDT(
|
||||||
|
accountId,
|
||||||
|
dataTokenAddress
|
||||||
|
)
|
||||||
Logger.log('DT Pool found', tokenPools)
|
Logger.log('DT Pool found', tokenPools)
|
||||||
let cheapestPoolAddress
|
let cheapestPoolAddress
|
||||||
let cheapestPoolPrice = 999999
|
let cheapestPoolPrice = 999999
|
||||||
|
|
||||||
if (tokenPools) {
|
if (tokenPools) {
|
||||||
for (let i = 0; i < tokenPools.length; i++) {
|
for (let i = 0; i < tokenPools.length; i++) {
|
||||||
const poolPrice = await ocean.pool.getOceanNeeded(accountId, tokenPools[i], '1')
|
const poolPrice = await ocean.pool.getOceanNeeded(
|
||||||
|
accountId,
|
||||||
|
tokenPools[i],
|
||||||
|
'1'
|
||||||
|
)
|
||||||
Logger.log('Pool price ', tokenPools[i], poolPrice)
|
Logger.log('Pool price ', tokenPools[i], poolPrice)
|
||||||
if (Decimal(poolPrice) < cheapestPoolPrice) {
|
if (Decimal(poolPrice) < cheapestPoolPrice) {
|
||||||
cheapestPoolPrice = Decimal(poolPrice)
|
cheapestPoolPrice = Decimal(poolPrice)
|
||||||
@ -17,31 +28,51 @@ export async function getCheapestPool(ocean: Ocean, accountId: string, dataToken
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { poolAddress: cheapestPoolAddress, poolPrice: cheapestPoolPrice.toString() }
|
return {
|
||||||
|
poolAddress: cheapestPoolAddress,
|
||||||
|
poolPrice: cheapestPoolPrice.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBestDataTokenPrice(ocean: Ocean, accountId: string, dataTokenAddress: string): Promise<string> {
|
export async function getBestDataTokenPrice(
|
||||||
|
ocean: Ocean,
|
||||||
|
accountId: string,
|
||||||
|
dataTokenAddress: string
|
||||||
|
): Promise<string> {
|
||||||
const bestPool = await getCheapestPool(ocean, accountId, dataTokenAddress)
|
const bestPool = await getCheapestPool(ocean, accountId, dataTokenAddress)
|
||||||
|
|
||||||
return bestPool.poolPrice
|
return bestPool.poolPrice
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkAndBuyDT(ocean: Ocean, dataTokenAddress: string, account: Account) {
|
export async function checkAndBuyDT(
|
||||||
const userOwnedTokens = await ocean.accounts.getTokenBalance(dataTokenAddress, account)
|
ocean: Ocean,
|
||||||
|
dataTokenAddress: string,
|
||||||
|
account: Account
|
||||||
|
) {
|
||||||
|
const userOwnedTokens = await ocean.accounts.getTokenBalance(
|
||||||
|
dataTokenAddress,
|
||||||
|
account
|
||||||
|
)
|
||||||
Logger.log(`User has ${userOwnedTokens} tokens`)
|
Logger.log(`User has ${userOwnedTokens} tokens`)
|
||||||
let cheapestPool
|
let cheapestPool
|
||||||
if (userOwnedTokens === '0') {
|
if (userOwnedTokens === '0') {
|
||||||
cheapestPool = await getCheapestPool(ocean,account.getId(),dataTokenAddress)
|
cheapestPool = await getCheapestPool(
|
||||||
Decimal.set({ precision: 5})
|
ocean,
|
||||||
const price = (new Decimal(cheapestPool.poolPrice)).times(1.05).toString()
|
account.getId(),
|
||||||
const maxPrice = (new Decimal(cheapestPool.poolPrice)).times(2).toString()
|
dataTokenAddress
|
||||||
Logger.log('Buying token', cheapestPool, account.getId(),price)
|
)
|
||||||
let buyResponse = await ocean.pool.buyDT(account.getId(), cheapestPool.poolAddress, '1', price, maxPrice)
|
Decimal.set({ precision: 5 })
|
||||||
|
const price = new Decimal(cheapestPool.poolPrice).times(1.05).toString()
|
||||||
|
const maxPrice = new Decimal(cheapestPool.poolPrice).times(2).toString()
|
||||||
|
Logger.log('Buying token', cheapestPool, account.getId(), price)
|
||||||
|
const buyResponse = await ocean.pool.buyDT(
|
||||||
|
account.getId(),
|
||||||
|
cheapestPool.poolAddress,
|
||||||
|
'1',
|
||||||
|
price,
|
||||||
|
maxPrice
|
||||||
|
)
|
||||||
Logger.log('DT buy response', buyResponse)
|
Logger.log('DT buy response', buyResponse)
|
||||||
|
return buyResponse
|
||||||
if (buyResponse === null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user