2021-06-25 09:50:56 +02:00
|
|
|
import { gql, OperationResult, TypedDocumentNode, OperationContext } from 'urql'
|
2022-08-02 11:53:22 +02:00
|
|
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
2021-10-13 18:48:59 +02:00
|
|
|
import { getUrqlClientInstance } from '@context/UrqlProvider'
|
2021-06-25 09:50:56 +02:00
|
|
|
import { getOceanConfig } from './ocean'
|
2022-01-14 00:48:05 +01:00
|
|
|
import { AssetPreviousOrder } from '../@types/subgraph/AssetPreviousOrder'
|
2022-03-09 13:58:54 +01:00
|
|
|
import { OrdersData_orders as OrdersData } from '../@types/subgraph/OrdersData'
|
2022-02-24 16:21:45 +01:00
|
|
|
import { OpcFeesQuery as OpcFeesData } from '../@types/subgraph/OpcFeesQuery'
|
2022-08-02 11:53:22 +02:00
|
|
|
|
2022-07-05 14:49:35 +02:00
|
|
|
import { getPublishedAssets, getTopPublishers } from '@utils/aquarius'
|
2021-09-13 16:39:32 +02:00
|
|
|
export interface UserLiquidity {
|
Account metadata header (#776)
* get all neded data for the header from 3box, aqua and subgraph
* fix tvl display error
* WIP metadata header styling
* added more styling for the header
* make page title optional so we can remove it on account page
* stroke change for svg images and default values
* more styling added to the header
* fixed linter
* added ocean balance to tvl
* update styling for statistcs
* fixed eror for go to my account from another account page
* updated styling for mobile use
* wip show more on explorer links and description
* properly display read more for explorer links and description
* replaced show more with 3box redirect on description
* change accounts default picture and check links length before display element
* use optional on links
* grid cleanup, new number unit, split up stats
* rename all the things, more profile header styling
* visual hierarchy, improve image loading experience
* layout flow & visual tweaks
* more description
* replaced account route with profile when accesing a profile by the eth address
* use account id from url if exists when fetching data
* bump @oceanprotocol/art to v3.2.0
* styling, fallbacks, edge case fixes
* clean up Publisher atom, link to profile page
* fixed issue when switching to my profile from another profile
* output accountId, make it copyable, remove stats icons
* render tweaks, markup cleanup
* add 3box reference
* mobile tabs spacing tweaks
* text flow and spacing tweaks
Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
|
|
|
price: string
|
|
|
|
oceanBalance: string
|
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
|
2021-04-14 13:41:17 +02:00
|
|
|
export interface PriceList {
|
|
|
|
[key: string]: string
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:44:18 +02:00
|
|
|
const PreviousOrderQuery = gql`
|
2021-04-11 01:13:43 +02:00
|
|
|
query AssetPreviousOrder($id: String!, $account: String!) {
|
2022-01-13 22:24:06 +01:00
|
|
|
orders(
|
2021-04-11 01:13:43 +02:00
|
|
|
first: 1
|
2022-01-31 13:41:58 +01:00
|
|
|
where: { datatoken: $id, payer: $account }
|
2022-01-13 22:24:06 +01:00
|
|
|
orderBy: createdTimestamp
|
2021-04-11 01:13:43 +02:00
|
|
|
orderDirection: desc
|
|
|
|
) {
|
2022-01-13 22:24:06 +01:00
|
|
|
createdTimestamp
|
2021-04-11 01:13:43 +02:00
|
|
|
tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2021-09-13 16:39:32 +02:00
|
|
|
|
|
|
|
const UserTokenOrders = gql`
|
|
|
|
query OrdersData($user: String!) {
|
2022-01-13 22:24:06 +01:00
|
|
|
orders(
|
|
|
|
orderBy: createdTimestamp
|
2021-09-13 16:39:32 +02:00
|
|
|
orderDirection: desc
|
|
|
|
where: { consumer: $user }
|
|
|
|
) {
|
2022-02-21 16:15:33 +01:00
|
|
|
consumer {
|
|
|
|
id
|
|
|
|
}
|
2022-01-31 13:41:58 +01:00
|
|
|
datatoken {
|
2022-02-21 16:15:33 +01:00
|
|
|
id
|
2022-01-31 13:41:58 +01:00
|
|
|
address
|
|
|
|
symbol
|
|
|
|
}
|
|
|
|
consumerMarketToken {
|
2021-09-13 16:39:32 +02:00
|
|
|
address
|
|
|
|
symbol
|
|
|
|
}
|
2022-01-13 22:24:06 +01:00
|
|
|
createdTimestamp
|
2021-09-13 16:39:32 +02:00
|
|
|
tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2022-01-13 22:24:06 +01:00
|
|
|
|
2022-02-24 16:21:45 +01:00
|
|
|
const OpcFeesQuery = gql`
|
|
|
|
query OpcFeesQuery($id: ID!) {
|
|
|
|
opc(id: $id) {
|
|
|
|
swapOceanFee
|
|
|
|
swapNonOceanFee
|
2022-05-18 15:38:08 +02:00
|
|
|
orderFee
|
2022-02-24 16:21:45 +01:00
|
|
|
providerFee
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-08-17 11:46:51 +02:00
|
|
|
export function getSubgraphUri(chainId: number): string {
|
2021-06-25 09:50:56 +02:00
|
|
|
const config = getOceanConfig(chainId)
|
|
|
|
return config.subgraphUri
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:01:30 +02:00
|
|
|
export function getQueryContext(chainId: number): OperationContext {
|
2022-03-09 13:58:54 +01:00
|
|
|
try {
|
|
|
|
const queryContext: OperationContext = {
|
|
|
|
url: `${getSubgraphUri(
|
|
|
|
Number(chainId)
|
|
|
|
)}/subgraphs/name/oceanprotocol/ocean-subgraph`,
|
2022-03-29 15:32:45 +02:00
|
|
|
requestPolicy: 'network-only'
|
2022-03-09 13:58:54 +01:00
|
|
|
}
|
|
|
|
return queryContext
|
|
|
|
} catch (error) {
|
|
|
|
LoggerInstance.error('Get query context error: ', error.message)
|
2021-07-22 14:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:02:32 +02:00
|
|
|
export async function fetchData(
|
2021-06-22 07:52:49 +02:00
|
|
|
query: TypedDocumentNode,
|
2021-06-25 09:50:56 +02:00
|
|
|
variables: any,
|
|
|
|
context: OperationContext
|
2021-07-22 13:07:52 +02:00
|
|
|
): Promise<any> {
|
2021-04-09 11:55:18 +02:00
|
|
|
try {
|
2021-06-22 07:52:49 +02:00
|
|
|
const client = getUrqlClientInstance()
|
2022-03-09 13:58:54 +01:00
|
|
|
|
2021-06-25 09:50:56 +02:00
|
|
|
const response = await client.query(query, variables, context).toPromise()
|
2021-04-09 11:55:18 +02:00
|
|
|
return response
|
|
|
|
} catch (error) {
|
2022-03-09 13:58:54 +01:00
|
|
|
LoggerInstance.error('Error fetchData: ', error.message)
|
2021-04-11 01:13:43 +02:00
|
|
|
}
|
2022-03-09 13:58:54 +01:00
|
|
|
return null
|
2021-04-11 01:13:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:46:25 +02:00
|
|
|
export async function fetchDataForMultipleChains(
|
|
|
|
query: TypedDocumentNode,
|
|
|
|
variables: any,
|
|
|
|
chainIds: number[]
|
|
|
|
): Promise<any[]> {
|
|
|
|
let datas: any[] = []
|
2022-03-09 13:58:54 +01:00
|
|
|
try {
|
|
|
|
for (const chainId of chainIds) {
|
|
|
|
const context: OperationContext = getQueryContext(chainId)
|
2021-07-23 14:46:25 +02:00
|
|
|
const response = await fetchData(query, variables, context)
|
2022-03-09 13:58:54 +01:00
|
|
|
if (!response || response.error) continue
|
|
|
|
datas = datas.concat(response?.data)
|
2021-07-23 14:46:25 +02:00
|
|
|
}
|
2022-03-09 13:58:54 +01:00
|
|
|
return datas
|
|
|
|
} catch (error) {
|
|
|
|
LoggerInstance.error('Error fetchDataForMultipleChains: ', error.message)
|
2021-07-23 14:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:21:45 +01:00
|
|
|
export async function getOpcFees(chainId: number) {
|
|
|
|
let opcFees
|
|
|
|
const variables = {
|
|
|
|
id: 1
|
|
|
|
}
|
|
|
|
const context = getQueryContext(chainId)
|
|
|
|
try {
|
|
|
|
const response: OperationResult<OpcFeesData> = await fetchData(
|
|
|
|
OpcFeesQuery,
|
|
|
|
variables,
|
|
|
|
context
|
|
|
|
)
|
|
|
|
opcFees = response?.data?.opc
|
|
|
|
} catch (error) {
|
2022-03-09 13:58:54 +01:00
|
|
|
LoggerInstance.error('Error getOpcFees: ', error.message)
|
2022-02-24 16:21:45 +01:00
|
|
|
throw Error(error.message)
|
|
|
|
}
|
|
|
|
return opcFees
|
|
|
|
}
|
|
|
|
|
2021-04-11 01:13:43 +02:00
|
|
|
export async function getPreviousOrders(
|
|
|
|
id: string,
|
|
|
|
account: string,
|
|
|
|
assetTimeout: string
|
|
|
|
): Promise<string> {
|
2022-05-12 12:35:07 +02:00
|
|
|
const variables = { id, account }
|
2021-06-22 07:52:49 +02:00
|
|
|
const fetchedPreviousOrders: OperationResult<AssetPreviousOrder> =
|
2021-06-25 09:50:56 +02:00
|
|
|
await fetchData(PreviousOrderQuery, variables, null)
|
2022-01-13 22:41:19 +01:00
|
|
|
if (fetchedPreviousOrders.data?.orders?.length === 0) return null
|
2021-04-12 16:09:41 +02:00
|
|
|
if (assetTimeout === '0') {
|
2022-01-13 22:41:19 +01:00
|
|
|
return fetchedPreviousOrders?.data?.orders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
2021-05-07 15:00:40 +02:00
|
|
|
const expiry =
|
2022-01-13 22:41:19 +01:00
|
|
|
fetchedPreviousOrders?.data?.orders[0]?.createdTimestamp * 1000 +
|
2021-05-07 15:00:40 +02:00
|
|
|
Number(assetTimeout) * 1000
|
|
|
|
if (Date.now() <= expiry) {
|
2022-01-13 22:41:19 +01:00
|
|
|
return fetchedPreviousOrders?.data?.orders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:39:32 +02:00
|
|
|
export async function getUserTokenOrders(
|
|
|
|
accountId: string,
|
|
|
|
chainIds: number[]
|
|
|
|
): Promise<OrdersData[]> {
|
|
|
|
const data: OrdersData[] = []
|
|
|
|
const variables = { user: accountId?.toLowerCase() }
|
|
|
|
|
|
|
|
try {
|
|
|
|
const tokenOrders = await fetchDataForMultipleChains(
|
|
|
|
UserTokenOrders,
|
|
|
|
variables,
|
|
|
|
chainIds
|
|
|
|
)
|
|
|
|
for (let i = 0; i < tokenOrders?.length; i++) {
|
2022-02-21 16:15:33 +01:00
|
|
|
tokenOrders[i].orders.forEach((tokenOrder: OrdersData) => {
|
2021-09-13 16:39:32 +02:00
|
|
|
data.push(tokenOrder)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
} catch (error) {
|
2022-03-09 13:58:54 +01:00
|
|
|
LoggerInstance.error('Error getUserTokenOrders', error.message)
|
2021-09-13 16:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-18 16:43:32 +02:00
|
|
|
|
|
|
|
export async function getUserSales(
|
|
|
|
accountId: string,
|
|
|
|
chainIds: number[]
|
|
|
|
): Promise<number> {
|
|
|
|
try {
|
2022-07-05 14:49:35 +02:00
|
|
|
const result = await getPublishedAssets(accountId, chainIds, null)
|
|
|
|
const { totalOrders } = result.aggregations
|
|
|
|
return totalOrders.value
|
2021-10-18 16:43:32 +02:00
|
|
|
} catch (error) {
|
2022-03-09 13:58:54 +01:00
|
|
|
LoggerInstance.error('Error getUserSales', error.message)
|
2021-10-18 16:43:32 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-10 10:50:00 +01:00
|
|
|
|
|
|
|
export async function getTopAssetsPublishers(
|
|
|
|
chainIds: number[],
|
|
|
|
nrItems = 9
|
|
|
|
): Promise<AccountTeaserVM[]> {
|
2022-07-05 14:49:35 +02:00
|
|
|
const publishers: AccountTeaserVM[] = []
|
2021-12-10 10:50:00 +01:00
|
|
|
|
2022-07-05 14:49:35 +02:00
|
|
|
const result = await getTopPublishers(chainIds, null)
|
|
|
|
const { topPublishers } = result.aggregations
|
|
|
|
|
|
|
|
for (let i = 0; i < topPublishers.buckets.length; i++) {
|
|
|
|
publishers.push({
|
|
|
|
address: topPublishers.buckets[i].key,
|
|
|
|
nrSales: parseInt(topPublishers.buckets[i].totalSales.value)
|
|
|
|
})
|
2021-12-10 10:50:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:49:35 +02:00
|
|
|
publishers.sort((a, b) => b.nrSales - a.nrSales)
|
2021-12-10 10:50:00 +01:00
|
|
|
|
2022-07-05 14:49:35 +02:00
|
|
|
return publishers.slice(0, nrItems)
|
2021-12-10 10:50:00 +01:00
|
|
|
}
|