From 28097bfd34ef06d6c229e442bad836b69f61ca2d Mon Sep 17 00:00:00 2001 From: sajee_techi Date: Mon, 6 Jan 2025 17:39:57 +0530 Subject: [PATCH 1/3] upsert interaction between from-to for transaction --- .vscode/settings.json | 3 +- supabase/functions/_db/interactions.ts | 48 ++++++++++++++++++++++++++ supabase/functions/process-tx/index.ts | 7 ++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 supabase/functions/_db/interactions.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 3e31b5e..46dedec 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,5 +18,6 @@ ], "[typescript]": { "editor.defaultFormatter": "denoland.vscode-deno" - } + }, + "deno.enable": true } diff --git a/supabase/functions/_db/interactions.ts b/supabase/functions/_db/interactions.ts new file mode 100644 index 0000000..3cbf5f2 --- /dev/null +++ b/supabase/functions/_db/interactions.ts @@ -0,0 +1,48 @@ +import type { SupabaseClient } from "jsr:@supabase/supabase-js@2"; +import { Transaction } from "./transactions.ts"; + +const INTERACTIONS_TABLE = "a_interactions"; + +export const upsertInteraction = async ( + client: SupabaseClient, + transaction: Pick, +) => { + const timestamp = new Date().toISOString(); + + // First direction: from->to + await client + .from(INTERACTIONS_TABLE) + .upsert( + { + transaction_id: transaction.id, + account: transaction.from, + with: transaction.to, + updated_at: timestamp, + created_at: timestamp, + new_interaction: true, + }, + { + onConflict: "account,with", + ignoreDuplicates: false, + }, + ) + .select() + .single(); + + // Second direction: to->from + await client + .from(INTERACTIONS_TABLE) + .upsert({ + transaction_id: transaction.id, + account: transaction.to, + with: transaction.from, + updated_at: timestamp, + created_at: timestamp, + new_interaction: true, + }, { + onConflict: "account,with", + ignoreDuplicates: false, + }) + .select() + .single(); +}; diff --git a/supabase/functions/process-tx/index.ts b/supabase/functions/process-tx/index.ts index 4c104c3..7a26798 100644 --- a/supabase/functions/process-tx/index.ts +++ b/supabase/functions/process-tx/index.ts @@ -12,6 +12,7 @@ import { } from "../_citizen-wallet/index.ts"; import { getServiceRoleClient } from "../_db/index.ts"; import { type Transaction, upsertTransaction } from "../_db/transactions.ts"; +import { upsertInteraction } from "../_db/interactions.ts"; import { ensureProfileExists } from "../_citizen-wallet/profiles.ts"; import { createOrder, @@ -108,10 +109,16 @@ Deno.serve(async (req) => { } const { error } = await upsertTransaction(supabaseClient, transaction); + if (error) { console.error("Error inserting transaction:", error); } + await upsertInteraction( + supabaseClient, + transaction, + ); + return new Response("notification sent", { status: 200 }); }); From e6ed33e05d01b94f25de5825e6b434385b4e90a0 Mon Sep 17 00:00:00 2001 From: kev-techi Date: Wed, 8 Jan 2025 10:27:31 +0100 Subject: [PATCH 2/3] fix missing tx descriptions --- supabase/functions/_db/transactions.ts | 12 +++++++----- supabase/functions/process-tx-data/index.ts | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/supabase/functions/_db/transactions.ts b/supabase/functions/_db/transactions.ts index 0166ea5..9c68255 100644 --- a/supabase/functions/_db/transactions.ts +++ b/supabase/functions/_db/transactions.ts @@ -22,7 +22,7 @@ export interface TransactionWithDescription { const TRANSACTIONS_TABLE = "a_transactions"; -export const upsertTransaction = async ( +export const upsertTransaction = ( client: SupabaseClient, transaction: Transaction, ) => { @@ -31,19 +31,21 @@ export const upsertTransaction = async ( }); }; -export const upsertTransactionWithDescription = async ( +export const updateTransaction = ( client: SupabaseClient, transaction: TransactionWithDescription, ) => { - return client.from(TRANSACTIONS_TABLE).upsert(transaction, { - onConflict: "id", - }); + return client.from(TRANSACTIONS_TABLE).update(transaction).eq( + "id", + transaction.id, + ); }; export const getTransactionByHash = ( client: SupabaseClient, hash: string, ): Promise> => { + // @ts-ignore: cryptic error return client.from(TRANSACTIONS_TABLE).select("*").eq("hash", hash) .maybeSingle(); }; diff --git a/supabase/functions/process-tx-data/index.ts b/supabase/functions/process-tx-data/index.ts index a01e176..9504eb6 100644 --- a/supabase/functions/process-tx-data/index.ts +++ b/supabase/functions/process-tx-data/index.ts @@ -10,7 +10,7 @@ import { getServiceRoleClient } from "../_db/index.ts"; import { getTransactionByHash, type TransactionWithDescription, - upsertTransactionWithDescription, + updateTransaction, } from "../_db/transactions.ts"; import { findOrdersWithTxHash, setOrderDescription } from "../_db/orders.ts"; @@ -64,7 +64,7 @@ Deno.serve(async (req) => { } } - const { error } = await upsertTransactionWithDescription( + const { error } = await updateTransaction( supabaseClient, transaction, ); From 438a0287ee9cce578be9bd5217b27a17e6acdc2b Mon Sep 17 00:00:00 2001 From: kev-techi Date: Thu, 9 Jan 2025 10:17:58 +0100 Subject: [PATCH 3/3] fix tx upsert --- supabase/functions/_db/transactions.ts | 19 ++++++++++++++----- supabase/functions/process-tx-data/index.ts | 10 +++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/supabase/functions/_db/transactions.ts b/supabase/functions/_db/transactions.ts index 9c68255..2efae2c 100644 --- a/supabase/functions/_db/transactions.ts +++ b/supabase/functions/_db/transactions.ts @@ -31,14 +31,23 @@ export const upsertTransaction = ( }); }; -export const updateTransaction = ( +export const upsertTransactionWithDescription = async ( client: SupabaseClient, transaction: TransactionWithDescription, ) => { - return client.from(TRANSACTIONS_TABLE).update(transaction).eq( - "id", - transaction.id, - ); + // check if exists + const { data: existingTransaction } = await client.from(TRANSACTIONS_TABLE) + .select("*").eq("id", transaction.id).maybeSingle(); + if (existingTransaction) { + return client.from(TRANSACTIONS_TABLE).update({ + description: transaction.description, + }).eq("id", transaction.id); + } + + return client.from(TRANSACTIONS_TABLE).insert({ + ...transaction, + hash: "", + }); }; export const getTransactionByHash = ( diff --git a/supabase/functions/process-tx-data/index.ts b/supabase/functions/process-tx-data/index.ts index 9504eb6..5e73756 100644 --- a/supabase/functions/process-tx-data/index.ts +++ b/supabase/functions/process-tx-data/index.ts @@ -10,7 +10,7 @@ import { getServiceRoleClient } from "../_db/index.ts"; import { getTransactionByHash, type TransactionWithDescription, - updateTransaction, + upsertTransactionWithDescription, } from "../_db/transactions.ts"; import { findOrdersWithTxHash, setOrderDescription } from "../_db/orders.ts"; @@ -25,15 +25,15 @@ Deno.serve(async (req) => { const { hash, - extra_data, + data, } = record; // Initialize Supabase client const supabaseClient = getServiceRoleClient(); let erc20TransferExtraData: ERC20TransferExtraData = { description: "" }; - if (extra_data) { - erc20TransferExtraData = extra_data as ERC20TransferExtraData; + if (data) { + erc20TransferExtraData = data as ERC20TransferExtraData; } // insert transaction into db @@ -64,7 +64,7 @@ Deno.serve(async (req) => { } } - const { error } = await updateTransaction( + const { error } = await upsertTransactionWithDescription( supabaseClient, transaction, );