Skip to content
Merged
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
11 changes: 9 additions & 2 deletions lib/ranking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Address, formatUnits } from "viem";
import { get24hPriceChange, getTokenTVL } from "./price";
import { STORY_FACTORY } from "./contracts/constants";
import type { Storyline } from "./supabase";
import type { SupabaseClient } from "@supabase/supabase-js";

Expand Down Expand Up @@ -62,7 +63,8 @@ async function fetchCandidatesAndRatings(supabase: SupabaseClient, writerType?:
.select("*")
.eq("hidden", false)
.eq("sunset", false)
.neq("token_address", "");
.neq("token_address", "")
.eq("contract_address", STORY_FACTORY.toLowerCase());
if (writerType !== undefined) q = q.eq("writer_type", writerType);
const { data } = await q
.order("block_timestamp", { ascending: false })
Expand All @@ -76,7 +78,8 @@ async function fetchCandidatesAndRatings(supabase: SupabaseClient, writerType?:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { data: allRatings } = await (supabase.from("ratings") as any)
.select("storyline_id, rating")
.in("storyline_id", storylineIds);
.in("storyline_id", storylineIds)
.eq("contract_address", STORY_FACTORY.toLowerCase());

const ratingMap = new Map<number, number>();
if (allRatings) {
Expand Down Expand Up @@ -182,12 +185,14 @@ export async function getRisingStorylines(
const { data: recentRatings } = await (supabase.from("ratings") as any)
.select("storyline_id, rating")
.in("storyline_id", storylineIds)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.gte("updated_at", threeDaysAgo);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { data: priorRatings } = await (supabase.from("ratings") as any)
.select("storyline_id, rating")
.in("storyline_id", storylineIds)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.gte("updated_at", sixDaysAgo)
.lt("updated_at", threeDaysAgo);

Expand All @@ -196,12 +201,14 @@ export async function getRisingStorylines(
const { data: recentPlots } = await (supabase.from("plots") as any)
.select("storyline_id")
.in("storyline_id", storylineIds)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.gte("block_timestamp", threeDaysAgo);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { data: priorPlots } = await (supabase.from("plots") as any)
.select("storyline_id")
.in("storyline_id", storylineIds)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.gte("block_timestamp", sixDaysAgo)
.lt("block_timestamp", threeDaysAgo);

Expand Down
18 changes: 18 additions & 0 deletions lib/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface Database {
block_timestamp: string | null;
indexed_at: string;
view_count: number;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -79,6 +80,7 @@ export interface Database {
block_timestamp?: string | null;
indexed_at?: string;
view_count?: number;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -97,6 +99,7 @@ export interface Database {
block_timestamp?: string | null;
indexed_at?: string;
view_count?: number;
contract_address?: string;
};
};
page_views: {
Expand All @@ -107,6 +110,7 @@ export interface Database {
viewer_address: string | null;
session_id: string;
viewed_at: string;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -115,6 +119,7 @@ export interface Database {
viewer_address?: string | null;
session_id: string;
viewed_at?: string;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -123,6 +128,7 @@ export interface Database {
viewer_address?: string | null;
session_id?: string;
viewed_at?: string;
contract_address?: string;
};
};
plots: {
Expand All @@ -140,6 +146,7 @@ export interface Database {
log_index: number;
block_timestamp: string | null;
indexed_at: string;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -155,6 +162,7 @@ export interface Database {
log_index: number;
block_timestamp?: string | null;
indexed_at?: string;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -170,6 +178,7 @@ export interface Database {
log_index?: number;
block_timestamp?: string | null;
indexed_at?: string;
contract_address?: string;
};
};
comments: {
Expand All @@ -181,6 +190,7 @@ export interface Database {
content: string;
created_at: string;
hidden: boolean;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -190,6 +200,7 @@ export interface Database {
content: string;
created_at?: string;
hidden?: boolean;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -199,6 +210,7 @@ export interface Database {
content?: string;
created_at?: string;
hidden?: boolean;
contract_address?: string;
};
};
donations: {
Expand All @@ -211,6 +223,7 @@ export interface Database {
log_index: number;
block_timestamp: string | null;
indexed_at: string;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -221,6 +234,7 @@ export interface Database {
log_index: number;
block_timestamp?: string | null;
indexed_at?: string;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -231,6 +245,7 @@ export interface Database {
log_index?: number;
block_timestamp?: string | null;
indexed_at?: string;
contract_address?: string;
};
};
ratings: {
Expand All @@ -242,6 +257,7 @@ export interface Database {
comment: string | null;
created_at: string;
updated_at: string;
contract_address: string;
};
Insert: {
id?: never;
Expand All @@ -251,6 +267,7 @@ export interface Database {
comment?: string | null;
created_at?: string;
updated_at?: string;
contract_address: string;
};
Update: {
id?: never;
Expand All @@ -260,6 +277,7 @@ export interface Database {
comment?: string | null;
created_at?: string;
updated_at?: string;
contract_address?: string;
};
};
};
Expand Down
7 changes: 6 additions & 1 deletion src/app/api/comments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
import { type Address } from "viem";
import { publicClient } from "../../../../lib/rpc";
import { createServerClient, supabase } from "../../../../lib/supabase";
import { STORY_FACTORY } from "../../../../lib/contracts/constants";

const MAX_COMMENT_LENGTH = 1000;
const DEFAULT_LIMIT = 20;
Expand Down Expand Up @@ -38,6 +39,7 @@ export async function GET(req: NextRequest) {
.eq("storyline_id", sid)
.eq("plot_index", pidx)
.eq("hidden", false)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.order("created_at", { ascending: false })
.range(offset, offset + limit - 1);

Expand All @@ -49,7 +51,8 @@ export async function GET(req: NextRequest) {
.select("id", { count: "exact", head: true })
.eq("storyline_id", sid)
.eq("plot_index", pidx)
.eq("hidden", false);
.eq("hidden", false)
.eq("contract_address", STORY_FACTORY.toLowerCase());

return NextResponse.json({
comments: data ?? [],
Expand Down Expand Up @@ -120,6 +123,7 @@ export async function POST(req: NextRequest) {
.eq("storyline_id", storylineId)
.eq("plot_index", plotIndex)
.eq("commenter_address", commenterAddress.toLowerCase())
.eq("contract_address", STORY_FACTORY.toLowerCase())
.gte("created_at", oneMinuteAgo)
.limit(1);

Expand All @@ -137,6 +141,7 @@ export async function POST(req: NextRequest) {
plot_index: plotIndex,
commenter_address: commenterAddress.toLowerCase(),
content,
contract_address: STORY_FACTORY.toLowerCase(),
});

if (insertError) return error(`Database error: ${insertError.message}`, 500);
Expand Down
4 changes: 4 additions & 0 deletions src/app/api/cron/backfill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async function processStorylineCreated(
block_timestamp: timestampISO,
tx_hash: txHash,
log_index: logIndex,
contract_address: STORY_FACTORY.toLowerCase(),
};

const { error: storylineError } = await supabase
Expand All @@ -241,6 +242,7 @@ async function processStorylineCreated(
block_timestamp: timestampISO,
tx_hash: txHash,
log_index: logIndex,
contract_address: STORY_FACTORY.toLowerCase(),
};
const { error: plotError } = await supabase
.from("plots")
Expand Down Expand Up @@ -278,6 +280,7 @@ async function processPlotChained(
block_timestamp: timestampISO,
tx_hash: txHash,
log_index: logIndex,
contract_address: STORY_FACTORY.toLowerCase(),
};

const { error: plotError } = await supabase
Expand Down Expand Up @@ -306,6 +309,7 @@ async function processDonation(
block_timestamp: timestampISO,
tx_hash: txHash,
log_index: logIndex,
contract_address: STORY_FACTORY.toLowerCase(),
};

const { error: donationError } = await supabase
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/index/donation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
storyFactoryAbi,
donationEvent,
} from "../../../../../lib/contracts/abi";
import { STORY_FACTORY } from "../../../../../lib/contracts/constants";
import type { Database } from "../../../../../lib/supabase";

/** Donation event topic0 */
Expand Down Expand Up @@ -89,6 +90,7 @@ export async function POST(req: Request) {
block_timestamp: new Date(Number(blockTimestamp) * 1000).toISOString(),
tx_hash: txHash.toLowerCase(),
log_index: donationLog.logIndex!,
contract_address: STORY_FACTORY.toLowerCase(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/index/plot/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
storyFactoryAbi,
plotChainedEvent,
} from "../../../../../lib/contracts/abi";
import { STORY_FACTORY } from "../../../../../lib/contracts/constants";
import { hashContent } from "../../../../../lib/content";
import type { Database } from "../../../../../lib/supabase";

Expand Down Expand Up @@ -120,6 +121,7 @@ export async function POST(req: Request) {
block_timestamp: new Date(Number(blockTimestamp) * 1000).toISOString(),
tx_hash: txHash.toLowerCase(),
log_index: plotChainedLog.logIndex!,
contract_address: STORY_FACTORY.toLowerCase(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
3 changes: 3 additions & 0 deletions src/app/api/index/storyline/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
storyFactoryAbi,
storylineCreatedEvent,
} from "../../../../../lib/contracts/abi";
import { STORY_FACTORY } from "../../../../../lib/contracts/constants";
import { detectWriterType } from "../../../../../lib/contracts/erc8004";
import { hashContent } from "../../../../../lib/content";
import type { Database } from "../../../../../lib/supabase";
Expand Down Expand Up @@ -137,6 +138,7 @@ export async function POST(req: Request) {
block_timestamp: timestampISO,
tx_hash: txHash.toLowerCase(),
log_index: storylineLog.logIndex!,
contract_address: STORY_FACTORY.toLowerCase(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -160,6 +162,7 @@ export async function POST(req: Request) {
block_timestamp: timestampISO,
tx_hash: txHash.toLowerCase(),
log_index: storylineLog.logIndex!,
contract_address: STORY_FACTORY.toLowerCase(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
10 changes: 8 additions & 2 deletions src/app/api/ratings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Address } from "viem";
import { publicClient } from "../../../../lib/rpc";
import { createServerClient, supabase } from "../../../../lib/supabase";
import { erc20Abi } from "../../../../lib/price";
import { STORY_FACTORY } from "../../../../lib/contracts/constants";

const MAX_COMMENT_LENGTH = 500;

Expand Down Expand Up @@ -33,7 +34,8 @@ export async function GET(req: NextRequest) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { data: allData, error: allError } = await (db.from("ratings") as any)
.select("rating")
.eq("storyline_id", sid);
.eq("storyline_id", sid)
.eq("contract_address", STORY_FACTORY.toLowerCase());

if (allError) {
return error(`Database error: ${allError.message}`, 500);
Expand All @@ -51,6 +53,7 @@ export async function GET(req: NextRequest) {
const { data, error: dbError } = await (db.from("ratings") as any)
.select("*")
.eq("storyline_id", sid)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.order("updated_at", { ascending: false })
.range(offset, offset + limit - 1);

Expand All @@ -69,6 +72,7 @@ export async function GET(req: NextRequest) {
.select("*")
.eq("storyline_id", sid)
.eq("rater_address", raterAddress.toLowerCase())
.eq("contract_address", STORY_FACTORY.toLowerCase())
.single();
myRating = mine ?? null;
}
Expand Down Expand Up @@ -147,6 +151,7 @@ export async function POST(req: NextRequest) {
const { data: storyline, error: slError } = await (serverClient.from("storylines") as any)
.select("token_address")
.eq("storyline_id", storylineId)
.eq("contract_address", STORY_FACTORY.toLowerCase())
.single();

if (slError || !storyline) {
Expand Down Expand Up @@ -180,8 +185,9 @@ export async function POST(req: NextRequest) {
rating,
comment: comment ?? null,
updated_at: new Date().toISOString(),
contract_address: STORY_FACTORY.toLowerCase(),
},
{ onConflict: "storyline_id,rater_address" },
{ onConflict: "storyline_id,rater_address,contract_address" },
);

if (upsertError) {
Expand Down
Loading
Loading