Skip to content

Commit

Permalink
Merge pull request #1 from citizenwallet/interaction-for-transaction
Browse files Browse the repository at this point in the history
upsert interaction between from-to for transaction
  • Loading branch information
kevtechi authored Jan 28, 2025
2 parents bd68c00 + 438a028 commit 0c73260
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
],
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
},
"deno.enable": true
}
48 changes: 48 additions & 0 deletions supabase/functions/_db/interactions.ts
Original file line number Diff line number Diff line change
@@ -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<Transaction, "id" | "from" | "to">,
) => {
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();
};
17 changes: 14 additions & 3 deletions supabase/functions/_db/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface TransactionWithDescription {

const TRANSACTIONS_TABLE = "a_transactions";

export const upsertTransaction = async (
export const upsertTransaction = (
client: SupabaseClient,
transaction: Transaction,
) => {
Expand All @@ -35,15 +35,26 @@ export const upsertTransactionWithDescription = async (
client: SupabaseClient,
transaction: TransactionWithDescription,
) => {
return client.from(TRANSACTIONS_TABLE).upsert(transaction, {
onConflict: "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 = (
client: SupabaseClient,
hash: string,
): Promise<PostgrestSingleResponse<Transaction>> => {
// @ts-ignore: cryptic error
return client.from(TRANSACTIONS_TABLE).select("*").eq("hash", hash)
.maybeSingle();
};
6 changes: 3 additions & 3 deletions supabase/functions/process-tx-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions supabase/functions/process-tx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
});

Expand Down

0 comments on commit 0c73260

Please sign in to comment.