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
25 changes: 20 additions & 5 deletions src/app/api/backfill-user-address/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { decodeEventLog } from "viem";
import { publicClient } from "../../../../lib/rpc";
import { createServerClient } from "../../../../lib/supabase";
import { mcv2BondEventAbi } from "../../../../lib/contracts/abi";
import { MCV2_BOND } from "../../../../lib/contracts/constants";
import { MCV2_BOND, ZAP_PLOTLINK } from "../../../../lib/contracts/constants";

/** Fail closed in production when CRON_SECRET is unset */
function verifyCron(req: Request): boolean {
Expand All @@ -25,11 +25,11 @@ export async function POST(req: Request) {
return NextResponse.json({ error: "Supabase not configured" }, { status: 500 });
}

// Fetch all trade_history rows missing user_address
// Fetch trade_history rows missing user_address OR attributed to the Zap contract
const { data: rows, error: fetchError } = await supabase
.from("trade_history")
.select("id, tx_hash, log_index")
.is("user_address", null)
.or(`user_address.is.null,user_address.eq.${ZAP_PLOTLINK.toLowerCase()}`)
.order("id", { ascending: true })
.limit(500);

Expand Down Expand Up @@ -79,8 +79,23 @@ export async function POST(req: Request) {
topics: log.topics,
});

const args = decoded.args as { user: `0x${string}` };
const userAddress = args.user.toLowerCase();
const args = decoded.args as { user: `0x${string}`; receiver: `0x${string}` };
const userAddress = args.receiver.toLowerCase();

// Delete intermediate Zap self-mints (receiver is the Zap contract)
if (userAddress === ZAP_PLOTLINK.toLowerCase()) {
const { error: deleteError } = await supabase
.from("trade_history")
.delete()
.eq("id", row.id);
if (deleteError) {
errorDetails.push({ id: row.id, tx_hash: txHash, reason: deleteError.message });
errors++;
} else {
updated++;
}
continue;
}

const { error: updateError } = await supabase
.from("trade_history")
Expand Down
8 changes: 6 additions & 2 deletions src/app/api/cron/trade-history/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { decodeEventLog, formatUnits, type Log } from "viem";
import { publicClient } from "../../../../../lib/rpc";
import { createServerClient } from "../../../../../lib/supabase";
import { mcv2BondEventAbi } from "../../../../../lib/contracts/abi";
import { MCV2_BOND } from "../../../../../lib/contracts/constants";
import { MCV2_BOND, ZAP_PLOTLINK } from "../../../../../lib/contracts/constants";
import { erc20Abi } from "../../../../../lib/price";
import type { Database } from "../../../../../lib/supabase";

Expand Down Expand Up @@ -172,12 +172,16 @@ async function processTradeEvent(
const args = decoded.args as {
token: `0x${string}`;
user: `0x${string}`;
receiver: `0x${string}`;
amountMinted?: bigint;
amountBurned?: bigint;
reserveAmount?: bigint;
refundAmount?: bigint;
};

// Skip intermediate Zap self-mints (HUNT→PLOT conversion where receiver is the Zap contract)
if (args.receiver.toLowerCase() === ZAP_PLOTLINK.toLowerCase()) return;

const isMint = decoded.eventName === "Mint";
const reserveAmount = isMint ? args.reserveAmount! : args.refundAmount!;
const tokenAmount = isMint ? args.amountMinted! : args.amountBurned!;
Expand Down Expand Up @@ -215,7 +219,7 @@ async function processTradeEvent(
tx_hash: log.transactionHash!.toLowerCase(),
log_index: log.logIndex!,
contract_address: MCV2_BOND.toLowerCase(),
user_address: args.user.toLowerCase(),
user_address: args.receiver.toLowerCase(),
};

const { error } = await supabase
Expand Down
8 changes: 6 additions & 2 deletions src/app/api/index/trade/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Hex, decodeEventLog, formatUnits } from "viem";
import { publicClient, getReceiptWithRetry } from "../../../../../lib/rpc";
import { createServerClient } from "../../../../../lib/supabase";
import { mcv2BondEventAbi, priceForNextMintFunction } from "../../../../../lib/contracts/abi";
import { MCV2_BOND } from "../../../../../lib/contracts/constants";
import { MCV2_BOND, ZAP_PLOTLINK } from "../../../../../lib/contracts/constants";
import { erc20Abi } from "../../../../../lib/price";
import type { Database } from "../../../../../lib/supabase";

Expand Down Expand Up @@ -66,6 +66,7 @@ export async function POST(req: Request) {
const args = decoded.args as {
token: `0x${string}`;
user: `0x${string}`;
receiver: `0x${string}`;
amountMinted?: bigint;
amountBurned?: bigint;
reserveAmount?: bigint;
Expand All @@ -74,6 +75,9 @@ export async function POST(req: Request) {

if (args.token.toLowerCase() !== tokenAddress) continue;

// Skip intermediate Zap self-mints (HUNT→PLOT conversion where receiver is the Zap contract)
if (args.receiver.toLowerCase() === ZAP_PLOTLINK.toLowerCase()) continue;

const isMint = decoded.eventName === "Mint";
const reserveAmount = isMint ? args.reserveAmount! : args.refundAmount!;
const tokenAmount = isMint ? args.amountMinted! : args.amountBurned!;
Expand Down Expand Up @@ -121,7 +125,7 @@ export async function POST(req: Request) {
tx_hash: txHash.toLowerCase(),
log_index: log.logIndex!,
contract_address: MCV2_BOND.toLowerCase(),
user_address: args.user.toLowerCase(),
user_address: args.receiver.toLowerCase(),
};

const { error: dbError } = await supabase
Expand Down
Loading