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",
|
||||
"react": "^16.9.41",
|
||||
"web3": "^1.2.11",
|
||||
"web3modal": "^1.8.0"
|
||||
"web3modal": "^1.8.0",
|
||||
"@ethereum-navigator/navigator": "^0.5.0",
|
||||
"@oceanprotocol/contracts": "^0.3.1",
|
||||
"bignumber.js": "^9.0.0",
|
||||
|
@ -13,7 +13,6 @@ export function AllDdos() {
|
||||
async function init() {
|
||||
if (ocean === undefined || account === undefined) return
|
||||
|
||||
|
||||
const assets = await ocean.assets.query({
|
||||
page: 1,
|
||||
offset: 10,
|
||||
|
@ -10,7 +10,6 @@ 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()
|
||||
@ -28,7 +27,6 @@ export function ConsumeDdo() {
|
||||
await consume(did, ddo.dataToken, 'access')
|
||||
}
|
||||
|
||||
|
||||
const computeDid = async () => {
|
||||
if (did === undefined) return
|
||||
const ddo = await ocean.assets.resolve(did)
|
||||
|
@ -33,7 +33,13 @@ export function Publish() {
|
||||
const publishAsset = async () => {
|
||||
const ddo = await publish(asset as Metadata, '90', 'access', '', '')
|
||||
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)
|
||||
}
|
||||
return (
|
||||
|
@ -2,8 +2,7 @@ import { useState } from 'react'
|
||||
import { useOcean } from '../../providers'
|
||||
import { feedback } from '../../utils'
|
||||
import { DID, Logger, ServiceType } from '@oceanprotocol/lib'
|
||||
import { getCheapestPool, checkAndBuyDT } from '../../utils/dtUtils'
|
||||
const Decimal = require('decimal.js')
|
||||
import { checkAndBuyDT } from '../../utils/dtUtils'
|
||||
interface UseConsume {
|
||||
consume: (
|
||||
did: DID | string,
|
||||
@ -46,11 +45,9 @@ function useConsume(): UseConsume {
|
||||
setConsumeError(undefined)
|
||||
|
||||
try {
|
||||
|
||||
setStep(0)
|
||||
await checkAndBuyDT(ocean, dataTokenAddress, account)
|
||||
|
||||
|
||||
setStep(1)
|
||||
const order = await ocean.assets.order(did, serviceType, accountId)
|
||||
Logger.log('order created', order)
|
||||
|
@ -1,11 +1,5 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import {
|
||||
DID,
|
||||
DDO,
|
||||
Metadata,
|
||||
MetadataStore,
|
||||
Logger,
|
||||
} from '@oceanprotocol/lib'
|
||||
import { DID, DDO, Metadata, MetadataStore, Logger } from '@oceanprotocol/lib'
|
||||
import { useOcean } from '../../providers'
|
||||
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
|
||||
import { getBestDataTokenPrice } from '../../utils/dtUtils'
|
||||
|
@ -73,7 +73,6 @@ function usePublish(): UsePublish {
|
||||
const timeout = 0
|
||||
const services: Service[] = []
|
||||
|
||||
|
||||
const price = ocean.datatokens.toWei('1')
|
||||
switch (serviceType) {
|
||||
case 'access': {
|
||||
@ -156,10 +155,6 @@ function usePublish(): UsePublish {
|
||||
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
||||
}
|
||||
|
||||
async function createBalancerPool() {
|
||||
ocean
|
||||
}
|
||||
|
||||
return {
|
||||
publish,
|
||||
mint,
|
||||
|
@ -1,14 +1,25 @@
|
||||
import { Logger, Ocean, Account } from '@oceanprotocol/lib'
|
||||
const Decimal = require('decimal.js')
|
||||
export async function getCheapestPool(ocean: Ocean, accountId: string, dataTokenAddress: string): Promise<{ poolAddress: string, poolPrice: string }> {
|
||||
const tokenPools = await ocean.pool.searchPoolforDT(accountId, dataTokenAddress)
|
||||
const Decimal = await require('decimal.js')
|
||||
export async function getCheapestPool(
|
||||
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)
|
||||
let cheapestPoolAddress
|
||||
let cheapestPoolPrice = 999999
|
||||
|
||||
if (tokenPools) {
|
||||
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)
|
||||
if (Decimal(poolPrice) < cheapestPoolPrice) {
|
||||
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)
|
||||
|
||||
return bestPool.poolPrice
|
||||
}
|
||||
|
||||
export async function checkAndBuyDT(ocean: Ocean, dataTokenAddress: string, account: Account) {
|
||||
const userOwnedTokens = await ocean.accounts.getTokenBalance(dataTokenAddress, account)
|
||||
export async function checkAndBuyDT(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string,
|
||||
account: Account
|
||||
) {
|
||||
const userOwnedTokens = await ocean.accounts.getTokenBalance(
|
||||
dataTokenAddress,
|
||||
account
|
||||
)
|
||||
Logger.log(`User has ${userOwnedTokens} tokens`)
|
||||
let cheapestPool
|
||||
if (userOwnedTokens === '0') {
|
||||
cheapestPool = await getCheapestPool(ocean,account.getId(),dataTokenAddress)
|
||||
cheapestPool = await getCheapestPool(
|
||||
ocean,
|
||||
account.getId(),
|
||||
dataTokenAddress
|
||||
)
|
||||
Decimal.set({ precision: 5 })
|
||||
const price = (new Decimal(cheapestPool.poolPrice)).times(1.05).toString()
|
||||
const maxPrice = (new Decimal(cheapestPool.poolPrice)).times(2).toString()
|
||||
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)
|
||||
let buyResponse = await ocean.pool.buyDT(account.getId(), cheapestPool.poolAddress, '1', price, maxPrice)
|
||||
const buyResponse = await ocean.pool.buyDT(
|
||||
account.getId(),
|
||||
cheapestPool.poolAddress,
|
||||
'1',
|
||||
price,
|
||||
maxPrice
|
||||
)
|
||||
Logger.log('DT buy response', buyResponse)
|
||||
|
||||
if (buyResponse === null) {
|
||||
return
|
||||
}
|
||||
return buyResponse
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user