Skip to content

Commit

Permalink
Merge pull request #2233 from zeitgeistpm/tr-better-market-reccomenda…
Browse files Browse the repository at this point in the history
…tions

Better market recommendations
  • Loading branch information
Robiquet authored Feb 7, 2024
2 parents feaef12 + 531172d commit 3ae81c0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 62 deletions.
95 changes: 48 additions & 47 deletions lib/hooks/queries/useMarketSearch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { MarketOrderByInput, ZeitgeistIndexer } from "@zeitgeistpm/indexer";
import { isIndexedSdk } from "@zeitgeistpm/sdk";
import Fuse from "fuse.js";
import { isWSX, wsxID } from "lib/constants";
import { useDebounce } from "use-debounce";
import { useSdkv2 } from "../useSdkv2";
import { MarketOrderByInput } from "@zeitgeistpm/indexer";
import { isWSX, wsxID } from "lib/constants";

export const marketSearchKey = "market-search";

Expand All @@ -20,51 +20,7 @@ export const useMarketSearch = (searchTerm: string) => {
[id, marketSearchKey, debouncedSearchTerm],
async () => {
if (enabled) {
const search = buildSearch(debouncedSearchTerm);
const { markets } = await sdk.indexer.markets({
where: {
AND: [
{
baseAsset_eq: isWSX ? `{"foreignAsset":${wsxID}}` : undefined,
baseAsset_not_eq: !isWSX
? `{"foreignAsset":${wsxID}}`
: undefined,
},
{
OR: search,
},
],
},
order: MarketOrderByInput.IdDesc,
limit: 100,
});

const fuse = new Fuse(markets, {
includeScore: true,
threshold: 0.9,
keys: [
//matches in the question are consisdered more important than description, slightly favour active markets
{
name: "question",
weight: 3,
},
{
name: "description",
weight: 1,
},
{ name: "status", weight: 0.2 },
],
});

const result = fuse.search({
$or: [
{ question: debouncedSearchTerm },
{ description: debouncedSearchTerm },
{ status: "Active" },
],
});

return result.map((r) => r.item);
return searchMarketsText(sdk.indexer, debouncedSearchTerm);
}
},
{
Expand All @@ -76,6 +32,51 @@ export const useMarketSearch = (searchTerm: string) => {
return query;
};

export const searchMarketsText = async (
indexer: ZeitgeistIndexer,
query: string,
) => {
const search = buildSearch(query);
const { markets } = await indexer.markets({
where: {
AND: [
{
baseAsset_eq: isWSX ? `{"foreignAsset":${wsxID}}` : undefined,
baseAsset_not_eq: !isWSX ? `{"foreignAsset":${wsxID}}` : undefined,
},
{
OR: search,
},
],
},
order: MarketOrderByInput.IdDesc,
limit: 100,
});

const fuse = new Fuse(markets, {
includeScore: true,
threshold: 0.9,
keys: [
//matches in the question are consisdered more important than description, slightly favour active markets
{
name: "question",
weight: 3,
},
{
name: "description",
weight: 1,
},
{ name: "status", weight: 0.2 },
],
});

const result = fuse.search({
$or: [{ question: query }, { description: query }, { status: "Active" }],
});

return result.map((r) => r.item);
};

const buildSearch = (searchTerm: string) => {
const search = searchTerm
.trim()
Expand Down
31 changes: 16 additions & 15 deletions lib/hooks/queries/useRecommendedMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getCurrentPrediction } from "lib/util/assets";
import { useSdkv2 } from "../useSdkv2";
import { QueryMarketData } from "./useInfiniteMarkets";
import { useMarket } from "./useMarket";
import { searchMarketsText } from "./useMarketSearch";

export const recommendedMarketsRootKey = "recommended-markets";

Expand All @@ -24,20 +25,19 @@ export const useRecommendedMarkets = (marketId?: number, limit = 2) => {
[id, recommendedMarketsRootKey, market?.marketId],
async () => {
if (enabled) {
const { markets: similarMarkets } = await sdk.indexer.markets({
limit,
order: [MarketOrderByInput.VolumeDesc],
where: {
tags_containsAny: market?.tags,
status_eq: MarketStatus.Active,
marketId_not_eq: marketId,
volume_gt: "0",
},
});
const similarMarkets = await searchMarketsText(
sdk.indexer,
market.question ?? "",
);

if (similarMarkets.length > 0) {
if (market.question && similarMarkets.length > 0) {
return {
markets: await mapMarkets(sdk.indexer, similarMarkets),
markets: await mapMarkets(
sdk.indexer,
similarMarkets
.filter((m) => m.question !== market.question)
.slice(0, 2),
),
type: "similar" as const,
};
} else {
Expand Down Expand Up @@ -76,9 +76,10 @@ const mapMarkets = async (

for (const market of markets) {
const marketOutcomes = outcomes[market.marketId];
const prediction = market
? getCurrentPrediction(marketOutcomes, market)
: { name: "None", price: 0 };
const prediction =
market && market.assets.length > 0
? getCurrentPrediction(market.assets, market)
: { name: "None", price: 0 };

resMarkets = [
...resMarkets,
Expand Down

0 comments on commit 3ae81c0

Please sign in to comment.