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

Replace profile sold statistic value number with subgraph value (#926)

* replace sold number with subgraph value

* remove unused function
This commit is contained in:
claudiaHash 2021-10-18 17:43:32 +03:00 committed by GitHub
parent 2961c5e631
commit e993114f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 46 deletions

View File

@ -4,8 +4,8 @@ import { ReactElement } from 'react-markdown'
import { useUserPreferences } from '../../../../providers/UserPreferences'
import {
getAccountLiquidityInOwnAssets,
getAccountNumberOfOrders,
getAssetsBestPrices,
getUserSales,
UserLiquidity,
calculateUserLiquidity
} from '../../../../utils/subgraph'
@ -34,32 +34,17 @@ export default function Stats({
accountId: string
}): ReactElement {
const { chainIds } = useUserPreferences()
const { poolShares, assets, assetsTotal, downloadsTotal } = useProfile()
const { poolShares, assets, assetsTotal, sales } = useProfile()
const [sold, setSold] = useState(0)
const [publisherLiquidity, setPublisherLiquidity] = useState<UserLiquidity>()
const [totalLiquidity, setTotalLiquidity] = useState(0)
useEffect(() => {
if (!accountId) {
setSold(0)
if (!accountId || chainIds.length === 0) {
setPublisherLiquidity({ price: '0', oceanBalance: '0' })
setTotalLiquidity(0)
return
}
async function getSales() {
if (!assets) return
try {
const nrOrders = await getAccountNumberOfOrders(assets, chainIds)
setSold(nrOrders)
} catch (error) {
Logger.error(error.message)
}
}
getSales()
}, [accountId, assets])
}, [accountId, chainIds])
useEffect(() => {
if (!assets || !accountId || !chainIds) return
@ -84,7 +69,7 @@ export default function Stats({
}
}
getPublisherLiquidity()
}, [assets, accountId])
}, [assets, accountId, chainIds])
useEffect(() => {
if (!poolShares) return
@ -112,7 +97,7 @@ export default function Stats({
label="Total Liquidity"
value={<Conversion price={`${totalLiquidity}`} hideApproximateSymbol />}
/>
<NumberUnit label={`Sale${sold === 1 ? '' : 's'}`} value={sold} />
<NumberUnit label={`Sale${sales === 1 ? '' : 's'}`} value={sales} />
<NumberUnit label="Published" value={assetsTotal} />
</div>
)

View File

@ -7,7 +7,11 @@ import React, {
useCallback,
ReactNode
} from 'react'
import { getPoolSharesData, getUserTokenOrders } from '../utils/subgraph'
import {
getPoolSharesData,
getUserSales,
getUserTokenOrders
} from '../utils/subgraph'
import { useUserPreferences } from './UserPreferences'
import { PoolShares_poolShares as PoolShare } from '../@types/apollo/PoolShares'
import { DDO, Logger } from '@oceanprotocol/lib'
@ -34,6 +38,7 @@ interface ProfileProviderValue {
downloads: DownloadedAsset[]
downloadsTotal: number
isDownloadsLoading: boolean
sales: number
}
const ProfileContext = createContext({} as ProfileProviderValue)
@ -273,6 +278,27 @@ function ProfileProvider({
}
}, [fetchDownloads, appConfig.metadataCacheUri, downloadsInterval])
//
// SALES NUMBER
//
const [sales, setSales] = useState(0)
useEffect(() => {
if (!accountId || chainIds.length === 0) {
setSales(0)
return
}
async function getUserSalesNumber() {
try {
const result = await getUserSales(accountId, chainIds)
setSales(result)
Logger.log(`[profile] Fetched sales number: ${result}.`, result)
} catch (error) {
Logger.error(error.message)
}
}
getUserSalesNumber()
}, [accountId, chainIds])
return (
<ProfileContext.Provider
value={{
@ -284,7 +310,8 @@ function ProfileProvider({
isEthAddress,
downloads,
downloadsTotal,
isDownloadsLoading
isDownloadsLoading,
sales
}}
>
{children}

View File

@ -252,6 +252,14 @@ const UserTokenOrders = gql`
}
}
`
const UserSalesQuery = gql`
query UserSalesQuery($userSalesId: String) {
users(where: { id: $userSalesId }) {
id
nrSales
}
}
`
export function getSubgraphUri(chainId: number): string {
const config = getOceanConfig(chainId)
@ -613,29 +621,6 @@ export async function getHighestLiquidityDIDs(
return [searchDids, didList.length]
}
export async function getAccountNumberOfOrders(
assets: DDO[],
chainIds: number[]
): Promise<number> {
const datatokens: string[] = []
assets.forEach((ddo) => {
datatokens.push(ddo?.dataToken?.toLowerCase())
})
const queryVariables = {
datatokenId_in: datatokens
}
const results = await fetchDataForMultipleChains(
TotalAccountOrders,
queryVariables,
chainIds
)
let numberOfOrders = 0
for (const result of results) {
numberOfOrders += result?.tokenOrders?.length
}
return numberOfOrders
}
export function calculateUserLiquidity(poolShare: PoolShare): number {
const ocean =
(poolShare.balance / poolShare.poolId.totalShares) *
@ -722,3 +707,26 @@ export async function getUserTokenOrders(
Logger.error(error.message)
}
}
export async function getUserSales(
accountId: string,
chainIds: number[]
): Promise<number> {
const variables = { userSalesId: accountId?.toLowerCase() }
try {
const userSales = await fetchDataForMultipleChains(
UserSalesQuery,
variables,
chainIds
)
let salesSum = 0
for (let i = 0; i < userSales.length; i++) {
if (userSales[i].users.length > 0) {
salesSum += userSales[i].users[0].nrSales
}
}
return salesSum
} catch (error) {
Logger.log(error.message)
}
}