From 8d473f242b67d3d5c1b89bcfa70bc2a0a7ee14cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C3=96hrlund?= Date: Thu, 24 Aug 2023 14:06:43 +0200 Subject: [PATCH] garage: update ft checker function to support single & multiple wallets --- garage/api/ft/[from]/[to]/index.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/garage/api/ft/[from]/[to]/index.ts b/garage/api/ft/[from]/[to]/index.ts index 68c36f42..c849ec1d 100644 --- a/garage/api/ft/[from]/[to]/index.ts +++ b/garage/api/ft/[from]/[to]/index.ts @@ -4,13 +4,19 @@ export const config = { runtime: "edge" }; const { pilotSessionsTable, ftRewardsTable } = deployments.ethereum; -const getFtQuery = (address: string) => { +const isInvalidAddress = (address?: string): boolean => { + return !/0x[0-9a-z]{40,40}/i.test(address || ""); +}; + +const getFtQuery = (wallets: string[]) => { + const walletsList = wallets.map((v) => `'${v.toLowerCase()}'`).join(", "); + return ` SELECT SUM(ft) as "ft" FROM ( - SELECT (coalesce(end_time, BLOCK_NUM(1)) - start_time) as "ft" FROM ${pilotSessionsTable} WHERE lower(owner) = lower('${address}') + SELECT (coalesce(end_time, BLOCK_NUM(1)) - start_time) as "ft" FROM ${pilotSessionsTable} WHERE lower(owner) IN (${walletsList}) UNION ALL - SELECT amount as "ft" FROM ${ftRewardsTable} WHERE lower(recipient) = lower('${address}') + SELECT amount as "ft" FROM ${ftRewardsTable} WHERE lower(recipient) IN (${walletsList}) )`; }; @@ -24,9 +30,14 @@ export default async function (request: Request) { if (isNaN(min)) return new Response(null, { status: 400 }); - const { wallet } = await request.json(); + let { wallet, wallets } = await request.json(); + + if (!wallets) wallets = [wallet]; + if (wallets.length === 0 || wallets.some(isInvalidAddress)) { + return new Response(null, { status: 422 }); + } - const query = getFtQuery(wallet); + const query = getFtQuery(wallets); const apiUrl = new URL("https://tableland.network/api/v1/query"); apiUrl.searchParams.set("statement", query); apiUrl.searchParams.set("unwrap", "true");