1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

shorten getAccessDetailsFromTokenPrice() a bit

This commit is contained in:
Matthias Kretschmann 2022-06-22 12:05:35 +01:00
parent b09dc6b953
commit c8ab4b18c5
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 10 additions and 18 deletions

View File

@ -173,22 +173,21 @@ function getAccessDetailsFromTokenPrice(
): AccessDetails { ): AccessDetails {
const accessDetails = {} as AccessDetails const accessDetails = {} as AccessDetails
if (tokenPrice && tokenPrice.orders && tokenPrice.orders.length > 0) { if (tokenPrice?.orders?.length > 0) {
const order = tokenPrice.orders[0] const order = tokenPrice.orders[0]
const reusedOrder = const reusedOrder = order?.reuses?.length > 0 ? order.reuses[0] : null
order && order.reuses && order.reuses.length > 0 ? order.reuses[0] : null
// asset is owned if there is an order and asset has timeout 0 (forever) or if the condition is valid // asset is owned if there is an order and asset has timeout 0 (forever) or if the condition is valid
accessDetails.isOwned = accessDetails.isOwned =
timeout === 0 || Date.now() / 1000 - order.createdTimestamp < timeout timeout === 0 || Date.now() / 1000 - order?.createdTimestamp < timeout
// the last valid order should be the last reuse order tx id if there is one // the last valid order should be the last reuse order tx id if there is one
accessDetails.validOrderTx = reusedOrder ? reusedOrder.tx : order.tx accessDetails.validOrderTx = reusedOrder?.tx || order?.tx
} }
// TODO: fetch order fee from sub query // TODO: fetch order fee from sub query
accessDetails.publisherMarketOrderFee = tokenPrice.publishMarketFeeAmount accessDetails.publisherMarketOrderFee = tokenPrice?.publishMarketFeeAmount
// free is always the best price // free is always the best price
if (tokenPrice.dispensers && tokenPrice.dispensers.length > 0) { if (tokenPrice?.dispensers?.length > 0) {
const dispenser = tokenPrice.dispensers[0] const dispenser = tokenPrice.dispensers[0]
accessDetails.type = 'free' accessDetails.type = 'free'
accessDetails.addressOrId = dispenser.token.id accessDetails.addressOrId = dispenser.token.id
@ -203,10 +202,7 @@ function getAccessDetailsFromTokenPrice(
} }
// checking for fixed price // checking for fixed price
if ( if (tokenPrice?.fixedRateExchanges?.length > 0) {
tokenPrice.fixedRateExchanges &&
tokenPrice.fixedRateExchanges.length > 0
) {
const fixed = tokenPrice.fixedRateExchanges[0] const fixed = tokenPrice.fixedRateExchanges[0]
accessDetails.type = 'fixed' accessDetails.type = 'fixed'
accessDetails.addressOrId = fixed.exchangeId accessDetails.addressOrId = fixed.exchangeId
@ -227,7 +223,7 @@ function getAccessDetailsFromTokenPrice(
} }
// checking for pools // checking for pools
if (tokenPrice.pools && tokenPrice.pools.length > 0) { if (tokenPrice?.pools?.length > 0) {
const pool = tokenPrice.pools[0] const pool = tokenPrice.pools[0]
accessDetails.type = 'dynamic' accessDetails.type = 'dynamic'
accessDetails.addressOrId = pool.id accessDetails.addressOrId = pool.id
@ -256,7 +252,6 @@ function getAccessDetailsFromTokenPrice(
* @return {Promise<OrdePriceAndFee>} * @return {Promise<OrdePriceAndFee>}
*/ */
export async function getOrderPriceAndFees( export async function getOrderPriceAndFees(
web3: Web3,
asset: AssetExtended, asset: AssetExtended,
accountId?: string, accountId?: string,
paramsForPool?: CalcInGivenOutParams, paramsForPool?: CalcInGivenOutParams,
@ -283,10 +278,10 @@ export async function getOrderPriceAndFees(
!providerFees && !providerFees &&
(await ProviderInstance.initialize( (await ProviderInstance.initialize(
asset?.id, asset?.id,
asset.services[0].id, asset?.services[0].id,
0, 0,
accountId, accountId,
asset.services[0].serviceEndpoint asset?.services[0].serviceEndpoint
)) ))
orderPriceAndFee.providerFee = providerFees || initializeData.providerFee orderPriceAndFee.providerFee = providerFees || initializeData.providerFee

View File

@ -181,7 +181,6 @@ export default function Compute({
} }
: null : null
const datasetPriceAndFees = await getOrderPriceAndFees( const datasetPriceAndFees = await getOrderPriceAndFees(
web3,
asset, asset,
ZERO_ADDRESS, ZERO_ADDRESS,
poolParams, poolParams,
@ -229,7 +228,6 @@ export default function Compute({
} }
} }
const algorithmOrderPriceAndFees = await getOrderPriceAndFees( const algorithmOrderPriceAndFees = await getOrderPriceAndFees(
web3,
selectedAlgorithmAsset, selectedAlgorithmAsset,
ZERO_ADDRESS, ZERO_ADDRESS,
algoPoolParams, algoPoolParams,

View File

@ -83,7 +83,6 @@ export default function Download({
consumeMarketSwapFee: '0' consumeMarketSwapFee: '0'
} }
const _orderPriceAndFees = await getOrderPriceAndFees( const _orderPriceAndFees = await getOrderPriceAndFees(
web3,
asset, asset,
ZERO_ADDRESS, ZERO_ADDRESS,
paramsForPool paramsForPool