mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
optimize price calculation
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
parent
1532de813e
commit
312b62ced3
12
package-lock.json
generated
12
package-lock.json
generated
@ -1521,9 +1521,9 @@
|
|||||||
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
|
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
|
||||||
},
|
},
|
||||||
"@oceanprotocol/lib": {
|
"@oceanprotocol/lib": {
|
||||||
"version": "0.9.16",
|
"version": "0.9.18",
|
||||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.16.tgz",
|
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.18.tgz",
|
||||||
"integrity": "sha512-7uHnVaykabT3cEPUGRq3ITpiw9+mhS29Pq6rDTi5mtW4dD8DX7jdA7ddkxyHsZe/uQY9I4FLEuCmeTPcaFXPJw==",
|
"integrity": "sha512-RsP4CjAnauI2kDH0923LOO3NhdKNB1y8WwpAviVwIwz9sYqsIIcac6MKXIm5oDeLNhmCIhJXbwvQehf17wRL5Q==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@ethereum-navigator/navigator": "^0.5.0",
|
"@ethereum-navigator/navigator": "^0.5.0",
|
||||||
"@oceanprotocol/contracts": "^0.5.7",
|
"@oceanprotocol/contracts": "^0.5.7",
|
||||||
@ -5011,9 +5011,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"file-saver": {
|
"file-saver": {
|
||||||
"version": "2.0.2",
|
"version": "2.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.5.tgz",
|
||||||
"integrity": "sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw=="
|
"integrity": "sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA=="
|
||||||
},
|
},
|
||||||
"filesize": {
|
"filesize": {
|
||||||
"version": "6.1.0",
|
"version": "6.1.0",
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
"dist/"
|
"dist/"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@oceanprotocol/lib": "^0.9.14",
|
"@oceanprotocol/lib": "^0.9.18",
|
||||||
"axios": "^0.21.0",
|
"axios": "^0.21.0",
|
||||||
"decimal.js": "^10.2.1",
|
"decimal.js": "^10.2.1",
|
||||||
"web3": "^1.3.0",
|
"web3": "^1.3.0",
|
||||||
|
@ -6,7 +6,7 @@ import { TransactionReceipt } from 'web3-core'
|
|||||||
import { Decimal } from 'decimal.js'
|
import { Decimal } from 'decimal.js'
|
||||||
import {
|
import {
|
||||||
getBestDataTokenPrice,
|
getBestDataTokenPrice,
|
||||||
getFirstPool,
|
getFirstPoolPrice,
|
||||||
getCreatePricingPoolFeedback,
|
getCreatePricingPoolFeedback,
|
||||||
getCreatePricingExchangeFeedback,
|
getCreatePricingExchangeFeedback,
|
||||||
getBuyDTFeedback,
|
getBuyDTFeedback,
|
||||||
@ -185,9 +185,9 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setStep(1, 'sell')
|
setStep(1, 'sell')
|
||||||
const pool = await getFirstPool(ocean, dataToken)
|
const pool = await getFirstPoolPrice(ocean, dataToken)
|
||||||
if (!pool || pool.price === 0) return
|
if (!pool || pool.value === 0) return
|
||||||
const price = new Decimal(pool.price).times(0.95).toString()
|
const price = new Decimal(pool.value).times(0.95).toString()
|
||||||
setStep(2, 'sell')
|
setStep(2, 'sell')
|
||||||
Logger.log('Selling token to pool', pool, accountId, price)
|
Logger.log('Selling token to pool', pool, accountId, price)
|
||||||
const tx = await ocean.pool.sellDT(
|
const tx = await ocean.pool.sellDT(
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib'
|
import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib'
|
||||||
import { Decimal } from 'decimal.js'
|
import { Decimal } from 'decimal.js'
|
||||||
import Pool from 'hooks/useMetadata/Pool'
|
|
||||||
|
|
||||||
export async function getCheapestPool(
|
export async function getCheapestPoolPrice(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
dataTokenAddress: string
|
dataTokenAddress: string
|
||||||
): Promise<Pool> {
|
): Promise<BestPrice> {
|
||||||
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
|
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
|
||||||
|
|
||||||
if (tokenPools === undefined || tokenPools.length === 0) {
|
if (tokenPools === undefined || tokenPools.length === 0) {
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cheapestPoolAddress = tokenPools[0]
|
let cheapestPoolAddress = tokenPools[0]
|
||||||
@ -19,7 +22,12 @@ export async function getCheapestPool(
|
|||||||
|
|
||||||
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(tokenPools[i], '1')
|
const poolPrice = await ocean.pool.calcInGivenOut(
|
||||||
|
tokenPools[i],
|
||||||
|
dataTokenAddress,
|
||||||
|
ocean.pool.oceanAddress,
|
||||||
|
'1'
|
||||||
|
)
|
||||||
const decimalPoolPrice = new Decimal(poolPrice)
|
const decimalPoolPrice = new Decimal(poolPrice)
|
||||||
|
|
||||||
if (decimalPoolPrice < cheapestPoolPrice) {
|
if (decimalPoolPrice < cheapestPoolPrice) {
|
||||||
@ -29,21 +37,25 @@ export async function getCheapestPool(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const usePrice = await ocean.pool.getOceanNeeded(cheapestPoolAddress, '1')
|
||||||
const oceanReserve = await ocean.pool.getOceanReserve(cheapestPoolAddress)
|
const oceanReserve = await ocean.pool.getOceanReserve(cheapestPoolAddress)
|
||||||
const dtReserve = await ocean.pool.getDTReserve(cheapestPoolAddress)
|
const dtReserve = await ocean.pool.getDTReserve(cheapestPoolAddress)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type: 'pool',
|
||||||
|
pools: [cheapestPoolAddress],
|
||||||
address: cheapestPoolAddress,
|
address: cheapestPoolAddress,
|
||||||
price: Number(cheapestPoolPrice),
|
value: Number(cheapestPoolPrice),
|
||||||
ocean: Number(oceanReserve),
|
ocean: Number(oceanReserve),
|
||||||
datatoken: Number(dtReserve)
|
datatoken: Number(dtReserve),
|
||||||
|
isConsumable: Number(usePrice) > 0 ? 'true' : 'false'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCheapestExchange(
|
export async function getCheapestExchangePrice(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
dataTokenAddress: string
|
dataTokenAddress: string
|
||||||
): Promise<Pool> {
|
): Promise<BestPrice> {
|
||||||
try {
|
try {
|
||||||
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||||
dataTokenAddress,
|
dataTokenAddress,
|
||||||
@ -51,8 +63,12 @@ export async function getCheapestExchange(
|
|||||||
)
|
)
|
||||||
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cheapestExchangeAddress = tokenExchanges[0].exchangeID
|
let cheapestExchangeAddress = tokenExchanges[0].exchangeID
|
||||||
@ -67,20 +83,35 @@ export async function getCheapestExchange(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dtReserve = cheapestExchangeAddress
|
||||||
|
? await ocean.fixedRateExchange.getSupply(cheapestExchangeAddress)
|
||||||
|
: '0'
|
||||||
return {
|
return {
|
||||||
|
type: 'pool',
|
||||||
|
pools: [],
|
||||||
address: cheapestExchangeAddress || '',
|
address: cheapestExchangeAddress || '',
|
||||||
price: Number(cheapestExchangePrice)
|
value: Number(cheapestExchangePrice),
|
||||||
|
ocean: 0,
|
||||||
|
datatoken: Number(dtReserve),
|
||||||
|
isConsumable: Number(dtReserve) > 0 ? 'true' : 'false'
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.log(err)
|
Logger.log(err)
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getFirstExchange(ocean: Ocean, dataTokenAddress: string) {
|
export async function getFirstExchangePrice(
|
||||||
|
ocean: Ocean,
|
||||||
|
dataTokenAddress: string
|
||||||
|
): Promise<BestPrice> {
|
||||||
try {
|
try {
|
||||||
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||||
dataTokenAddress,
|
dataTokenAddress,
|
||||||
@ -88,103 +119,116 @@ export async function getFirstExchange(ocean: Ocean, dataTokenAddress: string) {
|
|||||||
)
|
)
|
||||||
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [tokenExchange] = tokenExchanges
|
const [tokenExchange] = tokenExchanges
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type: 'pool',
|
||||||
|
pools: [],
|
||||||
address: tokenExchange.exchangeID || '',
|
address: tokenExchange.exchangeID || '',
|
||||||
price: Number(tokenExchange.fixedRate)
|
value: Number(tokenExchange.fixedRate),
|
||||||
|
ocean: 0,
|
||||||
|
datatoken: Number(tokenExchange.supply),
|
||||||
|
isConsumable: Number(tokenExchange.supply) > 0 ? 'true' : 'false'
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.log(err)
|
Logger.log(err)
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getFirstPool(
|
export async function getFirstPoolPrice(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
dataTokenAddress: string,
|
dataTokenAddress: string,
|
||||||
poolAddress?: string
|
poolAddress?: string
|
||||||
): Promise<Pool> {
|
): Promise<BestPrice> {
|
||||||
let firstPoolAddress = poolAddress
|
let firstPoolAddress = poolAddress
|
||||||
if (!poolAddress) {
|
if (!poolAddress) {
|
||||||
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
|
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
|
||||||
|
|
||||||
if (tokenPools === undefined || tokenPools.length === 0) {
|
if (tokenPools === undefined || tokenPools.length === 0) {
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
;[firstPoolAddress] = tokenPools
|
;[firstPoolAddress] = tokenPools
|
||||||
}
|
}
|
||||||
if (!firstPoolAddress) {
|
if (!firstPoolAddress) {
|
||||||
return {
|
return {
|
||||||
|
type: '',
|
||||||
address: '',
|
address: '',
|
||||||
price: 0
|
pools: [],
|
||||||
|
datatoken: 0,
|
||||||
|
value: 0,
|
||||||
|
isConsumable: ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
|
let firstPoolPrice = await ocean.pool.calcInGivenOut(
|
||||||
|
firstPoolAddress,
|
||||||
|
dataTokenAddress,
|
||||||
|
ocean.pool.oceanAddress,
|
||||||
|
'1'
|
||||||
|
)
|
||||||
|
const usePrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
|
||||||
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
|
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
|
||||||
|
|
||||||
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
|
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
|
||||||
|
|
||||||
if (firstPoolPrice) {
|
|
||||||
const priceChars = firstPoolPrice.split('.')
|
|
||||||
const numberOfCharsOfPrice = priceChars[0].length
|
|
||||||
|
|
||||||
if (numberOfCharsOfPrice > 8) firstPoolPrice = '0'
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type: 'pool',
|
||||||
|
pools: [firstPoolAddress],
|
||||||
address: firstPoolAddress,
|
address: firstPoolAddress,
|
||||||
price: Number(firstPoolPrice),
|
value: Number(firstPoolPrice),
|
||||||
ocean: Number(oceanReserve),
|
ocean: Number(oceanReserve),
|
||||||
datatoken: Number(dtReserve)
|
datatoken: Number(dtReserve),
|
||||||
|
isConsumable: Number(usePrice) > 0 ? 'true' : 'false'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBestDataTokenPrice(
|
export async function getBestDataTokenPrice(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
dataTokenAddress: string,
|
dataTokenAddress: string
|
||||||
poolAddress?: string
|
|
||||||
): Promise<BestPrice> {
|
): Promise<BestPrice> {
|
||||||
const cheapestPool = await getFirstPool(ocean, dataTokenAddress, poolAddress)
|
const cheapestPool = await getCheapestPoolPrice(ocean, dataTokenAddress)
|
||||||
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
const cheapestExchange = await getCheapestExchangePrice(
|
||||||
|
ocean,
|
||||||
|
dataTokenAddress
|
||||||
|
)
|
||||||
Decimal.set({ precision: 5 })
|
Decimal.set({ precision: 5 })
|
||||||
|
|
||||||
const cheapestPoolPrice = new Decimal(
|
const cheapestPoolPrice = new Decimal(
|
||||||
cheapestPool && cheapestPool.price !== 0 ? cheapestPool.price : 999999999999
|
cheapestPool && cheapestPool.value !== 0 ? cheapestPool.value : 999999999999
|
||||||
)
|
)
|
||||||
const cheapestExchangePrice = new Decimal(
|
const cheapestExchangePrice = new Decimal(
|
||||||
cheapestExchange && cheapestExchange?.price !== 0
|
cheapestExchange && cheapestExchange?.value !== 0
|
||||||
? cheapestExchange.price
|
? cheapestExchange.value
|
||||||
: 999999999999
|
: 999999999999
|
||||||
)
|
)
|
||||||
|
|
||||||
if (cheapestPoolPrice < cheapestExchangePrice) {
|
return cheapestPoolPrice < cheapestExchangePrice
|
||||||
return {
|
? cheapestPool
|
||||||
type: 'pool',
|
: cheapestExchange
|
||||||
address: cheapestPool?.address,
|
|
||||||
value: cheapestPool?.price,
|
|
||||||
ocean: cheapestPool?.ocean,
|
|
||||||
datatoken: cheapestPool?.datatoken
|
|
||||||
} as BestPrice
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
type: 'exchange',
|
|
||||||
address: cheapestExchange?.address,
|
|
||||||
value: Number(cheapestExchange?.price)
|
|
||||||
} as BestPrice
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDataTokenPrice(
|
export async function getDataTokenPrice(
|
||||||
@ -192,32 +236,16 @@ export async function getDataTokenPrice(
|
|||||||
dataTokenAddress: string,
|
dataTokenAddress: string,
|
||||||
type?: string,
|
type?: string,
|
||||||
poolAddress?: string
|
poolAddress?: string
|
||||||
) {
|
): Promise<BestPrice> {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'pool': {
|
case 'pool': {
|
||||||
const cheapestPool = await getFirstPool(
|
return await getFirstPoolPrice(ocean, dataTokenAddress, poolAddress)
|
||||||
ocean,
|
|
||||||
dataTokenAddress,
|
|
||||||
poolAddress
|
|
||||||
)
|
|
||||||
return {
|
|
||||||
type: 'pool',
|
|
||||||
address: cheapestPool?.address,
|
|
||||||
value: cheapestPool?.price,
|
|
||||||
ocean: cheapestPool?.ocean,
|
|
||||||
datatoken: cheapestPool?.datatoken
|
|
||||||
} as BestPrice
|
|
||||||
}
|
}
|
||||||
case 'exchange': {
|
case 'exchange': {
|
||||||
const cheapestExchange = await getFirstExchange(ocean, dataTokenAddress)
|
return await getFirstExchangePrice(ocean, dataTokenAddress)
|
||||||
return {
|
|
||||||
type: 'exchange',
|
|
||||||
address: cheapestExchange?.address,
|
|
||||||
value: Number(cheapestExchange?.price)
|
|
||||||
} as BestPrice
|
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
return await getBestDataTokenPrice(ocean, dataTokenAddress, poolAddress)
|
return await getBestDataTokenPrice(ocean, dataTokenAddress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user