Skip to content

Commit

Permalink
feat: filter by witnet network and pair
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Jan 30, 2025
1 parent 7efcfc9 commit 36194c9
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 25 deletions.
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()
}
}
19 changes: 19 additions & 0 deletions packages/api/src/repository/ResultRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ 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,
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
2 changes: 1 addition & 1 deletion packages/api/src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const typeDefs = gql`
type Query {
feed(feedFullName: String!): Feed
feeds(network: String): FeedsPage!
feeds(network: String, mainnet: Boolean, pair: String): FeedsPage!
requests(
feedFullName: String!
page: Int!
Expand Down
10 changes: 10 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 @@ -259,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'>
>
12 changes: 11 additions & 1 deletion packages/ui/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ 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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/api/queries/feeds.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from 'graphql-request'

export default gql`
query feeds($network: String!) {
feeds(network: $network) {
query feeds($network: String, $mainnet: Boolean, $pair: String) {
feeds(network: $network, mainnet: $mainnet, pair: $pair) {
feeds {
isRouted
feedFullName
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/components/DataFeeds.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ watch(networkFeeds, () => {
}
})
onMounted(async () => {
networkFeeds.value = await store.fetchFeeds({
const fetchedNetworks = await store.fetchFeeds({
network: props.network.key.toLowerCase(),
mainnet: null,
})
networkFeeds.value = fetchedNetworks
})
</script>

Expand Down
12 changes: 10 additions & 2 deletions packages/ui/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ export const useStore = defineStore('data', {
this.networks = (await getNetworks()).networks
return this.networks
},
async fetchFeeds({ network }: { network: any }) {
return (await getAllFeedsRequests({ network })).feeds
async fetchFeeds({
network = null,
mainnet,
pair = null,
}: {
network?: string | null
mainnet: boolean | null
pair?: string | null
}) {
return (await getAllFeedsRequests({ network, mainnet, pair })).feeds
},
async fetchFeedInfo({
feedFullName,
Expand Down

0 comments on commit 36194c9

Please sign in to comment.