mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
reafactor, checks,
This commit is contained in:
parent
a3b6da7530
commit
6146fb00c6
48780
example/package-lock.json
generated
48780
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,18 +1,18 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useOcean, usePublish } from '@oceanprotocol/react'
|
import { useOcean } from '@oceanprotocol/react'
|
||||||
import { 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'
|
||||||
import { MetadataExample } from './MetadataExample'
|
import { MetadataExample } from './MetadataExample'
|
||||||
export function AllDdos() {
|
export function AllDdos() {
|
||||||
const { accountId, chainId, account, ocean } = useOcean()
|
const { chainId, 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 || account === undefined) return
|
if (!ocean || !account) return
|
||||||
|
|
||||||
const assets = await ocean.assets.query({
|
const assets = await ocean.assets.query({
|
||||||
page: 1,
|
page: 1,
|
||||||
|
@ -3,17 +3,10 @@ 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, ConfigHelper } from '@oceanprotocol/lib'
|
import { ConfigHelper } from '@oceanprotocol/lib'
|
||||||
imprt { ConsumeDdo } from './Consume'
|
import { NetworkMonitor } from './NetworkMonitor'
|
||||||
import { NetworkMonitor } from './NetworkMonadataExample from './MetadataExample'
|
import { AllDdos } from './AllDdos'
|
||||||
|
import { ConsumeDdo } from './ConsumeDdo'
|
||||||
// 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
|
|
||||||
|
|
||||||
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
|
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
|
||||||
|
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {
|
import {
|
||||||
useOcean,
|
useOcean,
|
||||||
usePublish,
|
|
||||||
useConsume,
|
useConsume,
|
||||||
useCompute,
|
useCompute,
|
||||||
computeOptions
|
computeOptions
|
||||||
} from '@oceanprotocol/react'
|
} from '@oceanprotocol/react'
|
||||||
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 { ocean } = useOcean()
|
||||||
const { consumeStepText, consume, consumeError } = useConsume()
|
const { consumeStepText, consume, consumeError } = useConsume()
|
||||||
const { compute, computeStepText } = useCompute()
|
const { compute, computeStepText } = useCompute()
|
||||||
const [did, setDid] = useState<string | undefined>()
|
const [did, setDid] = useState<string | undefined>()
|
||||||
@ -21,14 +19,14 @@ export function ConsumeDdo() {
|
|||||||
}, [ocean])
|
}, [ocean])
|
||||||
|
|
||||||
const consumeDid = async () => {
|
const consumeDid = async () => {
|
||||||
if (did === undefined) return
|
if (!did) return
|
||||||
const ddo = await ocean.assets.resolve(did)
|
const ddo = await ocean.assets.resolve(did)
|
||||||
|
|
||||||
await consume(did, ddo.dataToken, 'access')
|
await consume(did, ddo.dataToken, 'access')
|
||||||
}
|
}
|
||||||
|
|
||||||
const computeDid = async () => {
|
const computeDid = async () => {
|
||||||
if (did === undefined) return
|
if (!did) return
|
||||||
const ddo = await ocean.assets.resolve(did)
|
const ddo = await ocean.assets.resolve(did)
|
||||||
console.log(ddo)
|
console.log(ddo)
|
||||||
console.log('ocean dt', ocean.datatokens)
|
console.log('ocean dt', ocean.datatokens)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useState } from 'react'
|
import React from 'react'
|
||||||
import { useMetadata } from '@oceanprotocol/react'
|
import { useMetadata } from '@oceanprotocol/react'
|
||||||
import { DDO } from '@oceanprotocol/lib'
|
import { DDO } from '@oceanprotocol/lib'
|
||||||
|
|
||||||
|
@ -7,18 +7,7 @@ export function NetworkMonitor() {
|
|||||||
const { connect, web3Provider } = useOcean()
|
const { connect, web3Provider } = useOcean()
|
||||||
|
|
||||||
const handleNetworkChanged = (chainId: number) => {
|
const handleNetworkChanged = (chainId: number) => {
|
||||||
// const config = getOceanConfig(chainId)
|
const config = new ConfigHelper().getConfigById(chainId)
|
||||||
// temp hack
|
|
||||||
let network = ''
|
|
||||||
switch (chainId) {
|
|
||||||
case 1: {
|
|
||||||
network = 'mainnet'
|
|
||||||
}
|
|
||||||
case 4: {
|
|
||||||
network = 'rinkeby'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const config = new ConfigHelper().getConfig(network)
|
|
||||||
connect(config)
|
connect(config)
|
||||||
}
|
}
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -5,7 +5,6 @@ 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 { publish, publishStepText, isLoading } = usePublish()
|
const { publish, publishStepText, isLoading } = usePublish()
|
||||||
const [ddo, setDdo] = useState<DDO | undefined>()
|
const [ddo, setDdo] = useState<DDO | undefined>()
|
||||||
|
|
||||||
@ -34,8 +33,8 @@ export function Publish() {
|
|||||||
console.log(isLoading)
|
console.log(isLoading)
|
||||||
|
|
||||||
const priceOptions = {
|
const priceOptions = {
|
||||||
price:10,
|
price: 10,
|
||||||
tokensToMint:10,
|
tokensToMint: 10,
|
||||||
type: 'simple',
|
type: 'simple',
|
||||||
weightOnDataToken: '',
|
weightOnDataToken: '',
|
||||||
liquidityProviderFee: ''
|
liquidityProviderFee: ''
|
||||||
@ -44,13 +43,6 @@ export function Publish() {
|
|||||||
const ddo = await publish(asset as Metadata, priceOptions, 'access', '', '')
|
const ddo = await publish(asset as Metadata, priceOptions, 'access', '', '')
|
||||||
console.log(ddo)
|
console.log(ddo)
|
||||||
console.log(isLoading)
|
console.log(isLoading)
|
||||||
const pool = ocean.pool.createDTPool(
|
|
||||||
accountId,
|
|
||||||
ddo.dataToken,
|
|
||||||
'90',
|
|
||||||
'9',
|
|
||||||
'0.03'
|
|
||||||
)
|
|
||||||
setDdo(ddo)
|
setDdo(ddo)
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
@ -10,7 +10,7 @@ export function Wallet() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
if (ocean === undefined || accountId === undefined) return
|
if (!ocean || !accountId) return
|
||||||
|
|
||||||
const assets = await ocean.assets.ownerAssets(accountId)
|
const assets = await ocean.assets.ownerAssets(accountId)
|
||||||
console.log(assets)
|
console.log(assets)
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
export interface PriceOptions {
|
export interface PriceOptions {
|
||||||
price?: number
|
price?: number
|
||||||
tokensToMint: number
|
tokensToMint: number
|
||||||
type: 'simple' | 'advanced' | string
|
type: 'simple' | 'advanced' | string
|
||||||
weightOnDataToken: string
|
weightOnDataToken: string
|
||||||
liquidityProviderFee: string
|
liquidityProviderFee: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,11 +55,8 @@ function usePublish(): UsePublish {
|
|||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setPublishError(undefined)
|
setPublishError(undefined)
|
||||||
try {
|
try {
|
||||||
<<<<<<< HEAD
|
|
||||||
const tokensToMint = priceOptions.tokensToMint.toString()
|
const tokensToMint = priceOptions.tokensToMint.toString()
|
||||||
|
|
||||||
=======
|
|
||||||
>>>>>>> main
|
|
||||||
const publishedDate =
|
const publishedDate =
|
||||||
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
|
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
|
||||||
const timeout = 0
|
const timeout = 0
|
||||||
@ -135,7 +132,6 @@ function usePublish(): UsePublish {
|
|||||||
setStep(7)
|
setStep(7)
|
||||||
await mint(ddo.dataToken, tokensToMint)
|
await mint(ddo.dataToken, tokensToMint)
|
||||||
Logger.log(`minted ${tokensToMint} tokens`)
|
Logger.log(`minted ${tokensToMint} tokens`)
|
||||||
setStep(8)
|
|
||||||
|
|
||||||
await createPricing(priceOptions, ddo.dataToken)
|
await createPricing(priceOptions, ddo.dataToken)
|
||||||
setStep(8)
|
setStep(8)
|
||||||
|
@ -19,7 +19,13 @@ export async function getCheapestPool(
|
|||||||
address: '',
|
address: '',
|
||||||
price: ''
|
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,
|
accountId,
|
||||||
tokenPools[i],
|
tokenPools[i],
|
||||||
'1'
|
'1'
|
||||||
@ -55,6 +61,11 @@ export async function getCheapestExchange(
|
|||||||
if (!ocean || !dataTokenAddress) return
|
if (!ocean || !dataTokenAddress) return
|
||||||
|
|
||||||
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||||
|
dataTokenAddress,
|
||||||
|
'1'
|
||||||
|
)
|
||||||
|
Logger.log('Exchanges found', tokenExchanges)
|
||||||
|
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
||||||
return {
|
return {
|
||||||
address: '',
|
address: '',
|
||||||
price: ''
|
price: ''
|
||||||
@ -104,14 +115,29 @@ export async function checkAndBuyDT(
|
|||||||
)
|
)
|
||||||
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
||||||
Decimal.set({ precision: 5 })
|
Decimal.set({ precision: 5 })
|
||||||
Logger.log('yResponse = await ocean.pool.buyDT(
|
const cheapestPoolPrice = new Decimal(cheapestPool.price)
|
||||||
account.getId(),
|
const cheapestExchangePrice = new Decimal(cheapestExchange.price)
|
||||||
cheapestPool.address,
|
|
||||||
'1',
|
if (cheapestExchangePrice > cheapestPoolPrice) {
|
||||||
price,
|
const price = new Decimal(cheapestPool.price).times(1.05).toString()
|
||||||
maxPrice
|
const maxPrice = new Decimal(cheapestPool.price).times(2).toString()
|
||||||
)
|
Logger.log('Buying token', cheapestPool, account.getId(), price)
|
||||||
Logger.log('DT buy response', buyResponse)
|
const buyResponse = await ocean.pool.buyDT(
|
||||||
return buyResponse
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user