Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class DataFeedsExplorer {
}
this.repositories.feedRepository.setLegacyFeeds(legacyFeeds)
this.repositories.feedRepository.setV2Feeds(v2Feeds)
this.repositories.feedRepository.setConfiguration(this.configuration)

const web3Middleware = new Web3Middleware(
this.configuration,
Expand Down
118 changes: 101 additions & 17 deletions packages/api/src/repository/Feed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { FeedsState } from './feedState'
import { PaginatedFeedsObject, FeedInfo, ConfigByFullName } from '../../types'
import {
PaginatedFeedsObject,
FeedInfo,
ConfigByFullName,
Network,
FeedsFilters,
} from '../../types'
import { Configuration } from '../web3Middleware/Configuration'

export class FeedRepository {
feedsState: FeedsState
// TODO: replace string with Network
dataFeeds: Record<string, Record<Network, Array<FeedInfo>>>
dataFeedsByNetwork: Record<string, Array<FeedInfo>>
configByFullName: ConfigByFullName

Expand All @@ -15,15 +22,43 @@ export class FeedRepository {
initialize() {
const feeds = this.feedsState.listFeeds()

this.dataFeeds = feeds.reduce(
(
acc: Record<string, Record<Network, Array<FeedInfo>>>,
feedInfo: FeedInfo,
) => {
let value
const isPairKeyPresent: boolean = !!acc[feedInfo.name]
const isNetworkAndPairKeyPresent: boolean =
isPairKeyPresent && !!acc[feedInfo.name][feedInfo.network]
if (isNetworkAndPairKeyPresent) {
value = [...acc[feedInfo.name][feedInfo.network], feedInfo]
} else {
value = [feedInfo]
}
return {
...acc,
[feedInfo.name]: {
...acc[feedInfo.name],
[feedInfo.network]: value,
},
}
},
{},
)

this.dataFeedsByNetwork = feeds.reduce(
(acc: Record<string, Array<FeedInfo>>, feedInfo: FeedInfo) => ({
...acc,
[feedInfo.network]: acc[feedInfo.network]
? [...acc[feedInfo.network], feedInfo]
: [feedInfo],
}),
(acc: Record<string, Array<FeedInfo>>, feedInfo: FeedInfo) => {
return {
...acc,
[feedInfo.network]: acc[feedInfo.network]
? [...acc[feedInfo.network], feedInfo]
: [feedInfo],
}
},
{},
)

this.configByFullName = feeds.reduce(
(acc, feedInfo) => ({
...acc,
Expand All @@ -43,19 +78,63 @@ export class FeedRepository {
.find((feed) => feed.feedFullName === feedFullName)
}

async getFeedsByNetwork(
// starts in 1
network: string,
): Promise<PaginatedFeedsObject> {
let feeds: Array<FeedInfo>
async getFilteredFeeds({
network,
pair,
mainnet,
}: FeedsFilters): Promise<PaginatedFeedsObject> {
let feeds: Array<FeedInfo> = []
if (network === 'all') {
feeds = Object.values(this.dataFeedsByNetwork).flat()
feeds = this.feedsState.listFeeds()
} else {
if (network && pair) {
feeds = this.dataFeeds[pair][network]
} else if (network) {
feeds = this.dataFeedsByNetwork[network]
} else if (pair) {
feeds = Object.values(this.dataFeeds[pair]).flat()
}
}
return this.getPaginatedFeedsByEnv(feeds, mainnet)
}

getPaginatedFeedsByEnv(feeds: FeedInfo[], mainnet: boolean | null) {
if (mainnet === null) {
return {
feeds: feeds || [],
total: feeds ? feeds.length : 0,
}
}
if (mainnet) {
return this.getMainnetFeeds(feeds)
} else {
feeds = this.dataFeedsByNetwork[network]
return this.getTestnetFeeds(feeds)
}
}

getConfigurationFromNetwork(network: Network) {
return this.feedsState.getConfiguration().getNetworkConfiguration(network)
}

getTestnetFeeds(feeds: Array<FeedInfo>): PaginatedFeedsObject {
const filteredFeeds: FeedInfo[] =
feeds?.filter(
(feed) => !this.getConfigurationFromNetwork(feed.network).mainnet,
) ?? []
return {
feeds: feeds || [],
total: feeds ? feeds.length : 0,
feeds: filteredFeeds,
total: filteredFeeds.length,
}
}

getMainnetFeeds(feeds: Array<FeedInfo>): PaginatedFeedsObject {
const filteredFeeds: FeedInfo[] =
feeds?.filter(
(feed) => this.getConfigurationFromNetwork(feed.network).mainnet,
) ?? []
return {
feeds: filteredFeeds,
total: filteredFeeds.length,
}
}

Expand Down Expand Up @@ -98,4 +177,9 @@ export class FeedRepository {
this.feedsState.setV2Feeds(v2Feeds)
this.initialize()
}

setConfiguration(configuration: Configuration) {
this.feedsState.setConfiguration(configuration)
this.initialize()
}
}
51 changes: 40 additions & 11 deletions packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Db,
Collection,
WithoutId,
PaginatedRequests,
} from '../../types'
import { containFalsyValues } from './containFalsyValues'

Expand Down Expand Up @@ -47,21 +48,49 @@ export class ResultRequestRepository {
.map(this.normalizeId)
}

async getFeedRequestsPageByPair(
pair: string,
page: number,
size: number,
): Promise<PaginatedRequests> {
const query = { feedFullName: { $regex: pair } }
return {
requests: (
await this.collection
.find(query)
.sort({ timestamp: -1 })
.skip(size * (page - 1))
.limit(size)
.toArray()
).map(this.normalizeId),
total: (await this.collection.find(query).toArray()).length,
}
}

async getFeedRequestsPage(
feedFullName: string,
page: number,
size: number,
): Promise<Array<ResultRequestDbObjectNormalized>> {
return (
await this.collection
.find({
feedFullName,
})
.sort({ timestamp: -1 })
.skip(size * (page - 1))
.limit(size)
.toArray()
).map(this.normalizeId)
): Promise<PaginatedRequests> {
return {
requests: (
await this.collection
.find({
feedFullName,
})
.sort({ timestamp: -1 })
.skip(size * (page - 1))
.limit(size)
.toArray()
).map(this.normalizeId),
total: (
await this.collection
.find({
feedFullName,
})
.toArray()
).length,
}
}

async getLastResult(
Expand Down
10 changes: 10 additions & 0 deletions packages/api/src/repository/feedState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FeedInfo } from '../../types'
import { Configuration } from '../web3Middleware/Configuration'

export class FeedsState {
private legacyFeeds: Array<FeedInfo>
private v2Feeds: Array<FeedInfo>
private configuration: Configuration

constructor() {
this.legacyFeeds = []
Expand All @@ -17,10 +19,18 @@ export class FeedsState {
this.legacyFeeds = legacyFeeds
}

setConfiguration(configuration: Configuration) {
this.configuration = configuration
}

getV2Feeds(): Array<FeedInfo> {
return this.v2Feeds
}

getConfiguration(): Configuration {
return this.configuration
}

getLegacyFeeds(): Array<FeedInfo> {
return this.legacyFeeds
}
Expand Down
6 changes: 5 additions & 1 deletion packages/api/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Context } from '../types'
const resolvers = {
Query: {
feeds: async (_parent, args, { feedRepository }: Context) => {
return await feedRepository.getFeedsByNetwork(args.network)
return await feedRepository.getFilteredFeeds({
network: args.network,
pair: args.pair,
mainnet: args.mainnet,
})
},

networks: (_parent, _args, { config }: Context) => {
Expand Down
13 changes: 11 additions & 2 deletions packages/api/src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const typeDefs = gql`
timestamp: String! @column
}

type PaginatedResultRequest {
requests: [ResultRequest]
total: Int!
}

# type DataRequest @entity(embedded: true) {
# retrieval: String! @column
# aggregation: String! @column
Expand All @@ -58,8 +63,12 @@ const typeDefs = gql`

type Query {
feed(feedFullName: String!): Feed
feeds(network: String): FeedsPage!
requests(feedFullName: String!, page: Int!, size: Int!): [ResultRequest]!
feeds(network: String, mainnet: Boolean, pair: String): FeedsPage!
requests(
feedFullName: String!
page: Int!
size: Int!
): PaginatedResultRequest!
networks: [NetworksConfig]!
}
`
Expand Down
15 changes: 15 additions & 0 deletions packages/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export type ConfigByFullName = {
[key: string]: FeedInfo
}

export type NetworkConfigByNetwork = {
[key: string]: Configuration
}

export enum Network {
ArbitrumOne = 'arbitrum-one',
ArbitrumGoerli = 'arbitrum-goerli',
Expand Down Expand Up @@ -150,6 +154,11 @@ export type ResultRequestDbObjectNormalized = ResultRequestDbObject & {
id: string
}

export type PaginatedRequests = {
requests: Array<ResultRequestDbObjectNormalized>
total: number
}

export type Repositories = {
feedRepository: FeedRepository
resultRequestRepository: ResultRequestRepository
Expand Down Expand Up @@ -254,6 +263,12 @@ export type LegacyRouterDataFeedsConfig = {
chains: Record<string, Chain>
}

export type FeedsFilters = {
network: string | null
pair: string | null
mainnet: boolean
}

export type FeedInfosWithoutAbis = Array<
Omit<FeedInfoConfig, 'abi' | 'routerAbi'>
>
27 changes: 21 additions & 6 deletions packages/ui/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@ import feedQuery from './queries/feed'
import feedsQuery from './queries/feeds'
import networksQuery from './queries/networks'
import feedRequestsQuery from './queries/requests'
import type { Ecosystem, Feed, FeedRequests, Network } from '~/types'
import type { Ecosystem, Feed, FeedRequest, Network } from '~/types'

function getApiEndpoint() {
return useRuntimeConfig().public.apiBase
}

export const getAllFeedsRequests = async ({ network }: { network: string }) =>
export const getAllFeedsRequests = async ({
network,
mainnet,
pair,
}: {
network: string | null
mainnet: boolean | null
pair: string | null
}) =>
(await request(
getApiEndpoint(),
feedsQuery,
{
network,
mainnet,
pair,
},
{ accept: 'application/json' },
)) as {
feeds: FeedRequests[]
feeds: FeedRequest[]
total: number
}

Expand Down Expand Up @@ -68,8 +78,8 @@ export const getFeedRequests = async ({
feedFullName: string
page: number
size: number
}) =>
(await request(
}) => {
const result = (await request(
getApiEndpoint(),
feedRequestsQuery,
{
Expand All @@ -79,5 +89,10 @@ export const getFeedRequests = async ({
},
{ accept: 'application/json' },
)) as {
requests: FeedRequests[]
requests: {
requests: FeedRequest[]
total: number
}
}
return result.requests
}
Loading