Skip to content
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
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"dependencies": {
"@amplitude/analytics-browser": "^1.9.1",
"@dynamic-labs/sdk-react": "^0.15.16",
"@dynamic-labs/wagmi-connector": "^0.15.16",
"@amplitude/analytics-browser": "^1.9.1",
"@ethersproject/hash": "^5.7.0",
"@headlessui/react": "^1.7.10",
"@heroicons/react": "^2.0.14",
Expand All @@ -41,6 +41,7 @@
"graphql": "^16.6.0",
"graphql-request": "^5.2.0",
"graphql-tag": "^2.12.6",
"json2csv": "^6.0.0-alpha.2",
"lodash.debounce": "^4.0.8",
"lodash.get": "^4.4.2",
"lodash.isequal": "^4.5.0",
Expand Down Expand Up @@ -74,6 +75,7 @@
},
"devDependencies": {
"@faker-js/faker": "^7.6.0",
"@types/json2csv": "^5.0.3",
"@types/jsonwebtoken": "^9.0.1",
"@types/lodash.debounce": "^4.0.7",
"@types/lodash.get": "^4.4.7",
Expand Down
82 changes: 82 additions & 0 deletions pages/api/v1/tokenTransfer/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import db from "db"
import { parse } from "json2csv"
import { isAuthenticated } from "lib/api/auth/isAuthenticated"
import { NextApiRequest, NextApiResponse } from "next"
import {
alchemyFetcher,
TransferDirection,
} from "../../../../src/hooks/useAssetTransfers"

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
// should only be able to export if you are authed
await isAuthenticated(req, res)

const { query } = req
const {
safeAddress: safeAddressQuery,
chainId: chainIdQuery,
filter: filterQuery,
} = query as {
safeAddress: string
chainId: string
filter: string
}

const databaseTransfers = await db.tokenTransfer.findMany({
where: {
chainId: parseInt(chainIdQuery),
terminalAddress: safeAddressQuery,
},
})

const alchemyToTransfers = await alchemyFetcher([
safeAddressQuery,
parseInt(chainIdQuery),
TransferDirection.INBOUND,
])

const alchemyFromTransfers = await alchemyFetcher([
safeAddressQuery,
parseInt(chainIdQuery),
TransferDirection.OUTBOUND,
])

const alchemyTransfers =
filterQuery === TransferDirection.ALL
? [...alchemyToTransfers, ...alchemyFromTransfers]
: filterQuery === TransferDirection.INBOUND
? alchemyToTransfers
: alchemyFromTransfers

const joinedData = [] as any[]
alchemyTransfers.forEach((alchemyItem: any) => {
const databaseItem = databaseTransfers.find(
(item: any) => item.txHash === alchemyItem.hash,
)
if (databaseItem) {
let databaseMeta = databaseItem?.data as {
note: string
category: string
}
joinedData.push({
...alchemyItem,
message: databaseMeta.note ?? "",
category: databaseMeta.category ?? "",
})
} else {
joinedData.push(alchemyItem)
}
})

const csv = parse(joinedData)

res.setHeader("Content-Type", "text/csv")
res.setHeader(
"Content-Disposition",
`attachment; filename=${chainIdQuery}-${safeAddressQuery}-${filterQuery}.csv`,
)
res.status(200).send(csv)
}
74 changes: 74 additions & 0 deletions pages/api/v1/tokenTransfer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import db from "db"
import { isAuthenticated } from "lib/api/auth/isAuthenticated"
import { NextApiRequest, NextApiResponse } from "next"

export async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method, query, body } = req
const { safeAddress: safeAddressQuery, chainId: chainIdQuery } = query as {
safeAddress: string
chainId: string
}

switch (method) {
case "PUT":
const { note, category, txHash } = body as {
note: string
category: string
txHash: string
}
// no try/catch since isAuthenticated will return the response
await isAuthenticated(req, res)

try {
const transfer = await db.tokenTransfer.upsert({
where: {
txHash: txHash,
},
update: {
data: {
note: note,
category: category,
},
},
create: {
txHash: txHash,
chainId: parseInt(chainIdQuery),
terminalAddress: safeAddressQuery,
data: {
note: note,
category: category,
},
},
})
res.status(200).json(transfer)
} catch (err) {
console.error("Error creating terminal", err)
res.statusCode = 500
return res.end(JSON.stringify("Error creating terminal"))
}
break

case "GET":
try {
const transfers = await db.tokenTransfer.findMany({
where: {
chainId: parseInt(chainIdQuery),
terminalAddress: safeAddressQuery,
},
})

res.status(200).json(transfers)
} catch (err) {
console.error("Error fetching transfers", err)
res.statusCode = 500
return res.end(JSON.stringify("Error fetching transfers"))
}
break

default:
res.setHeader("Allow", ["GET", "PUT"])
res.status(405).end(`Method ${method} Not Allowed`)
}
}

export default handler
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "TokenTransfer" (
"id" TEXT NOT NULL,
"chainId" INTEGER NOT NULL,
"terminalAddress" TEXT NOT NULL,
"data" JSONB NOT NULL,

CONSTRAINT "TokenTransfer_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "TokenTransfer" ADD CONSTRAINT "TokenTransfer_chainId_terminalAddress_fkey" FOREIGN KEY ("chainId", "terminalAddress") REFERENCES "Terminal"("chainId", "safeAddress") ON DELETE RESTRICT ON UPDATE CASCADE;
12 changes: 12 additions & 0 deletions prisma/migrations/20230406023450_adding_tx_hash/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:

- A unique constraint covering the columns `[txHash]` on the table `TokenTransfer` will be added. If there are existing duplicate values, this will fail.
- Added the required column `txHash` to the `TokenTransfer` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "TokenTransfer" ADD COLUMN "txHash" TEXT NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "TokenTransfer_txHash_key" ON "TokenTransfer"("txHash");
11 changes: 11 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ model Terminal {
requests Request[]
automations Automation[]
invoices Invoice[]
tokenTransfers TokenTransfer[]

@@unique([chainId, safeAddress])
}
Expand Down Expand Up @@ -136,6 +137,16 @@ model Invoice {
@@unique([terminalAddress, chainId, number(sort: Desc)])
}

model TokenTransfer {
id String @id @default(uuid())
chainId Int
terminalAddress String
txHash String @unique
data Json

terminal Terminal @relation(fields: [chainId, terminalAddress], references: [chainId, safeAddress])
}

enum ActivityVariant {
CREATE_REQUEST
CREATE_AND_APPROVE_REQUEST
Expand Down
Loading