1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

reafactor, checks,

This commit is contained in:
mihaisc 2020-09-07 12:32:34 +03:00
parent a3b6da7530
commit 6146fb00c6
11 changed files with 57 additions and 48844 deletions

48780
example/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,18 @@
import React from 'react'
import { useOcean, usePublish } from '@oceanprotocol/react'
import { useOcean } from '@oceanprotocol/react'
import { DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { useEffect } from 'react'
import shortid from 'shortid'
import { MetadataExample } from './MetadataExample'
export function AllDdos() {
const { accountId, chainId, account, ocean } = useOcean()
const { chainId, account, ocean } = useOcean()
const [ddos, setDdos] = useState<DDO[] | undefined>()
useEffect(() => {
async function init() {
if (ocean === undefined || account === undefined) return
if (!ocean || !account) return
const assets = await ocean.assets.query({
page: 1,

View File

@ -3,17 +3,10 @@ import './App.css'
import { OceanProvider } from '@oceanprotocol/react'
import { Wallet } from './Wallet'
import { Publish } from './Publish'
import { Config, ConfigHelper } from '@oceanprotocol/lib'
imprt { ConsumeDdo } from './Consume'
import { NetworkMonitor } from './NetworkMonadataExample from './MetadataExample'
// factory Address needs to be updated each time you deploy the contract on local network
const config = {
metadataStoreUri: 'http://aquarius:5000',
providerUri: 'http://localhost:8030',
nodeUri: `http://localhost:8545`,
factoryAddress: '0x2fC1fd21cb222Dc180Ef817dE4c426fd9230b5A5'
} as Config
import { ConfigHelper } from '@oceanprotocol/lib'
import { NetworkMonitor } from './NetworkMonitor'
import { AllDdos } from './AllDdos'
import { ConsumeDdo } from './ConsumeDdo'
const configRinkeby = new ConfigHelper().getConfig('rinkeby')

View File

@ -1,17 +1,15 @@
import React from 'react'
import {
useOcean,
usePublish,
useConsume,
useCompute,
computeOptions
} from '@oceanprotocol/react'
import { Metadata, DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { useEffect } from 'react'
export function ConsumeDdo() {
const { accountId, ocean } = useOcean()
const { ocean } = useOcean()
const { consumeStepText, consume, consumeError } = useConsume()
const { compute, computeStepText } = useCompute()
const [did, setDid] = useState<string | undefined>()
@ -21,14 +19,14 @@ export function ConsumeDdo() {
}, [ocean])
const consumeDid = async () => {
if (did === undefined) return
if (!did) return
const ddo = await ocean.assets.resolve(did)
await consume(did, ddo.dataToken, 'access')
}
const computeDid = async () => {
if (did === undefined) return
if (!did) return
const ddo = await ocean.assets.resolve(did)
console.log(ddo)
console.log('ocean dt', ocean.datatokens)

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React from 'react'
import { useMetadata } from '@oceanprotocol/react'
import { DDO } from '@oceanprotocol/lib'

View File

@ -7,18 +7,7 @@ export function NetworkMonitor() {
const { connect, web3Provider } = useOcean()
const handleNetworkChanged = (chainId: number) => {
// const config = getOceanConfig(chainId)
// temp hack
let network = ''
switch (chainId) {
case 1: {
network = 'mainnet'
}
case 4: {
network = 'rinkeby'
}
}
const config = new ConfigHelper().getConfig(network)
const config = new ConfigHelper().getConfigById(chainId)
connect(config)
}
useEffect(() => {

View File

@ -5,7 +5,6 @@ import { useState } from 'react'
import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata'
export function Publish() {
const { accountId, ocean } = useOcean()
const { publish, publishStepText, isLoading } = usePublish()
const [ddo, setDdo] = useState<DDO | undefined>()
@ -34,8 +33,8 @@ export function Publish() {
console.log(isLoading)
const priceOptions = {
price:10,
tokensToMint:10,
price: 10,
tokensToMint: 10,
type: 'simple',
weightOnDataToken: '',
liquidityProviderFee: ''
@ -44,13 +43,6 @@ export function Publish() {
const ddo = await publish(asset as Metadata, priceOptions, 'access', '', '')
console.log(ddo)
console.log(isLoading)
const pool = ocean.pool.createDTPool(
accountId,
ddo.dataToken,
'90',
'9',
'0.03'
)
setDdo(ddo)
}
return (

View File

@ -10,7 +10,7 @@ export function Wallet() {
}
const init = async () => {
if (ocean === undefined || accountId === undefined) return
if (!ocean || !accountId) return
const assets = await ocean.assets.ownerAssets(accountId)
console.log(assets)

View File

@ -1,8 +1,7 @@
export interface PriceOptions {
price?: number
tokensToMint: number
type: 'simple' | 'advanced' | string
weightOnDataToken: string
liquidityProviderFee: string
}
price?: number
tokensToMint: number
type: 'simple' | 'advanced' | string
weightOnDataToken: string
liquidityProviderFee: string
}

View File

@ -55,11 +55,8 @@ function usePublish(): UsePublish {
setIsLoading(true)
setPublishError(undefined)
try {
<<<<<<< HEAD
const tokensToMint = priceOptions.tokensToMint.toString()
=======
>>>>>>> main
const publishedDate =
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0
@ -135,7 +132,6 @@ function usePublish(): UsePublish {
setStep(7)
await mint(ddo.dataToken, tokensToMint)
Logger.log(`minted ${tokensToMint} tokens`)
setStep(8)
await createPricing(priceOptions, ddo.dataToken)
setStep(8)

View File

@ -19,7 +19,13 @@ export async function getCheapestPool(
address: '',
price: ''
}
let cheapestPoolAddresspoolPrice = await ocean.pool.getOceanNeeded(
}
let cheapestPoolAddress
let cheapestPoolPrice = new Decimal(999999999999)
if (tokenPools) {
for (let i = 0; i < tokenPools.length; i++) {
const poolPrice = await ocean.pool.getOceanNeeded(
accountId,
tokenPools[i],
'1'
@ -55,6 +61,11 @@ export async function getCheapestExchange(
if (!ocean || !dataTokenAddress) return
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
dataTokenAddress,
'1'
)
Logger.log('Exchanges found', tokenExchanges)
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
return {
address: '',
price: ''
@ -104,14 +115,29 @@ export async function checkAndBuyDT(
)
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
Decimal.set({ precision: 5 })
Logger.log('yResponse = await ocean.pool.buyDT(
account.getId(),
cheapestPool.address,
'1',
price,
maxPrice
)
Logger.log('DT buy response', buyResponse)
return buyResponse
const cheapestPoolPrice = new Decimal(cheapestPool.price)
const cheapestExchangePrice = new Decimal(cheapestExchange.price)
if (cheapestExchangePrice > cheapestPoolPrice) {
const price = new Decimal(cheapestPool.price).times(1.05).toString()
const maxPrice = new Decimal(cheapestPool.price).times(2).toString()
Logger.log('Buying token', cheapestPool, account.getId(), price)
const buyResponse = await ocean.pool.buyDT(
account.getId(),
cheapestPool.address,
'1',
price,
maxPrice
)
Logger.log('DT buy response', buyResponse)
return buyResponse
} else {
const exchange = await ocean.fixedRateExchange.buyDT(
cheapestExchange.address,
'1',
account.getId()
)
return exchange
}
}
}