Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Safe Actions (Create Tx, Sign Tx, and Execute Tx) #2

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { z } from "zod";
import {
saveUserInformation,
getUserInformation,
deleteUserInformationTool,
} from "@/lib/ai/tools/user-information";
import { setupAgentKit } from "@/lib/web3/agentkit/setup";
import { generateUserProfile } from "@/lib/ai/prompts/user";
Expand Down
156 changes: 0 additions & 156 deletions lib/db/migrations/0000_absurd_hobgoblin.sql

This file was deleted.

2 changes: 1 addition & 1 deletion lib/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "d676d783-e2de-417e-8956-8efd3b9af1b0",
"id": "a3b394b5-485a-4c1b-a4cb-8097d87871df",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down
4 changes: 2 additions & 2 deletions lib/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1739109815400,
"tag": "0000_absurd_hobgoblin",
"when": 1739101065473,
"tag": "0000_dizzy_madame_masque",
"breakpoints": true
}
]
Expand Down
72 changes: 71 additions & 1 deletion lib/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { and, asc, desc, eq, gt, gte, inArray, isNull, sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

import {
user,
chat,
Expand All @@ -18,8 +17,11 @@ import {
type UserKnowledge,
charge,
type UserWithRelations,
safeTransaction
} from "./schema";
import type { BlockKind } from "@/components/block";
import { SafeTransaction as SafeTxType } from '@safe-global/types-kit';


// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
Expand Down Expand Up @@ -636,6 +638,74 @@ export async function getUserCharges(userId: string) {
}
}

export async function saveSafeTransaction({
transactionHash,
safeAddress,
transaction,
}: {
transactionHash: string;
safeAddress: string;
transaction: SafeTxType;
}) {
try {
const existingTx = await getSafeTransactionByHash({ transactionHash });

if (existingTx) {
// Merge signatures from existing and new transaction data - this is an object since it came from db
const existingSignatures = new Map(Object.entries((existingTx.transaction as SafeTxType).signatures));
// This is already a map
const newSignatures = transaction.signatures;
const mergedSignatures = Object.fromEntries(new Map([
...existingSignatures,
...newSignatures
]));
const mergedTransactionData = {
data:transaction.data,
signatures: mergedSignatures,
signatureCount: mergedSignatures.size
};

// Update existing transaction with merged data
return await db
.update(safeTransaction)
.set({
transaction: mergedTransactionData,
})
.where(eq(safeTransaction.transactionHash, transactionHash));
}

// Insert new transaction if it doesn't exist
return await db.insert(safeTransaction).values({
transactionHash,
safeAddress,
signatureCount: transaction.signatures.size,
transaction,
createdAt: new Date(),
});
} catch (error) {
console.error('Failed to save safe transaction in database');
throw error;
}
}

export async function getSafeTransactionByHash({
transactionHash,
}: {
transactionHash: string;
}) {
try {
const [transaction] = await db
.select()
.from(safeTransaction)
.where(eq(safeTransaction.transactionHash, transactionHash))
.limit(1);

return transaction;
} catch (error) {
console.error('Failed to get safe transaction by hash from database');
}
}

export async function getAvailableStarterKits() {
try {
return await db
Expand Down
14 changes: 13 additions & 1 deletion lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
foreignKey,
boolean,
bigint,
} from "drizzle-orm/pg-core";
integer,
} from 'drizzle-orm/pg-core';

export const user = pgTable("User", {
id: varchar("id", { length: 42 }).primaryKey().notNull(), // Ethereum address
Expand Down Expand Up @@ -165,10 +166,21 @@ export const suggestion = pgTable(
})
);

export const safeTransaction = pgTable("SafeTransaction", {
id: uuid("id").primaryKey().notNull().defaultRandom(),
transactionHash: text("transactionHash").notNull().unique(),
safeAddress: text("safeAddress").notNull(),
transaction: json("transaction").notNull(),
signatureCount: integer("signatureCount").notNull().default(0),
createdAt: timestamp("createdAt").notNull().defaultNow(),
});

export type Suggestion = InferSelectModel<typeof suggestion>;

export type UserKnowledge = InferSelectModel<typeof userKnowledge>;

export type StarterKit = InferSelectModel<typeof starterKit>;

export type Charge = InferSelectModel<typeof charge>;

export type SafeTransaction = InferSelectModel<typeof safeTransaction>;
Loading