Skip to content

Commit 77d3cf8

Browse files
authored
Configuration + Webhooks : PG Notify/Trigger (#361)
* updated implementation to use configuration cache * changed log level for cache to debug * removed extra export fr cache * updated config end-points to pull latest & return latest in response * removed extra logging for webhooks * cleaned webhooks cache debug logs
1 parent ee05cf9 commit 77d3cf8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+510
-116
lines changed

src/db/transactions/getQueuedTxs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Transactions } from "@prisma/client";
22
import { Static } from "@sinclair/typebox";
33
import { PrismaTransaction } from "../../schema/prisma";
44
import { transactionResponseSchema } from "../../server/schemas/transaction";
5+
import { getConfig } from "../../utils/cache/getConfig";
56
import { getPrismaWithPostgresTx } from "../client";
6-
import { getConfiguration } from "../configuration/getConfiguration";
77
import { cleanTxs } from "./cleanTxs";
88

99
interface GetQueuedTxsParams {
@@ -14,7 +14,7 @@ export const getQueuedTxs = async ({ pgtx }: GetQueuedTxsParams = {}): Promise<
1414
Static<typeof transactionResponseSchema>[]
1515
> => {
1616
const prisma = getPrismaWithPostgresTx(pgtx);
17-
const config = await getConfiguration();
17+
const config = await getConfig();
1818

1919
// TODO: Don't use env var for transactions to batch
2020
const txs = await prisma.$queryRaw<Transactions[]>`

src/db/transactions/getSentTxs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Transactions } from "@prisma/client";
22
import { PrismaTransaction } from "../../schema/prisma";
3+
import { getConfig } from "../../utils/cache/getConfig";
34
import { getPrismaWithPostgresTx } from "../client";
4-
import { getConfiguration } from "../configuration/getConfiguration";
55

66
interface GetSentTxsParams {
77
pgtx?: PrismaTransaction;
@@ -11,7 +11,7 @@ export const getSentTxs = async ({ pgtx }: GetSentTxsParams = {}): Promise<
1111
Transactions[]
1212
> => {
1313
const prisma = getPrismaWithPostgresTx(pgtx);
14-
const config = await getConfiguration();
14+
const config = await getConfig();
1515

1616
return prisma.$queryRaw<Transactions[]>`
1717
SELECT * FROM "transactions"

src/db/transactions/getSentUserOps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Transactions } from "@prisma/client";
22
import { PrismaTransaction } from "../../schema/prisma";
3+
import { getConfig } from "../../utils/cache/getConfig";
34
import { getPrismaWithPostgresTx } from "../client";
4-
import { getConfiguration } from "../configuration/getConfiguration";
55

66
interface GetSentUserOpsParams {
77
pgtx?: PrismaTransaction;
@@ -11,7 +11,7 @@ export const getSentUserOps = async ({
1111
pgtx,
1212
}: GetSentUserOpsParams = {}): Promise<Transactions[]> => {
1313
const prisma = getPrismaWithPostgresTx(pgtx);
14-
const config = await getConfiguration();
14+
const config = await getConfig();
1515

1616
return prisma.$queryRaw<Transactions[]>`
1717
SELECT * FROM "transactions"

src/db/transactions/getTxToRetry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Transactions } from "@prisma/client";
22
import type { PrismaTransaction } from "../../schema/prisma";
3+
import { getConfig } from "../../utils/cache/getConfig";
34
import { getPrismaWithPostgresTx } from "../client";
4-
import { getConfiguration } from "../configuration/getConfiguration";
55

66
interface GetTxToRetryParams {
77
pgtx?: PrismaTransaction;
@@ -11,7 +11,7 @@ export const getTxToRetry = async ({ pgtx }: GetTxToRetryParams = {}): Promise<
1111
Transactions | undefined
1212
> => {
1313
const prisma = getPrismaWithPostgresTx(pgtx);
14-
const config = await getConfiguration();
14+
const config = await getConfig();
1515

1616
// TODO: Remove transactionHash
1717
// TODO: For now, we're not retrying user ops

src/db/webhooks/createWebhook.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createHash, randomBytes } from "crypto";
22
import { WebhooksEventTypes } from "../../schema/webhooks";
3-
import { webhookCache } from "../../utils/cache/getWebhook";
43
import { prisma } from "../client";
54

65
interface CreateWebhooksParams {
@@ -19,9 +18,6 @@ export const insertWebhook = async ({
1918
// hash the bytes to create the secret (this will not be stored by itself)
2019
const secret = createHash("sha512").update(bytes).digest("base64url");
2120

22-
// Clear Cache
23-
webhookCache.clear();
24-
2521
return prisma.webhooks.create({
2622
data: {
2723
url,

src/db/webhooks/revokeWebhook.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { StatusCodes } from "http-status-codes";
22
import { createCustomError } from "../../server/middleware/error";
3-
import { webhookCache } from "../../utils/cache/getWebhook";
43
import { prisma } from "../client";
54

65
interface RevokeWebhooksParams {
76
id: number;
87
}
98

109
export const markWebhookAsRevoked = async ({ id }: RevokeWebhooksParams) => {
11-
// Clear Cache
12-
webhookCache.clear();
1310
const currentTimestamp = new Date();
1411

1512
const exists = await prisma.webhooks.findUnique({
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- Configuration Triggers
2+
CREATE OR REPLACE FUNCTION notify_configuration_insert()
3+
RETURNS TRIGGER
4+
LANGUAGE plpgsql
5+
AS $function$
6+
BEGIN
7+
PERFORM pg_notify('new_configuration_data', row_to_json(NEW)::text);
8+
RETURN NEW;
9+
END;
10+
$function$;
11+
12+
CREATE OR REPLACE FUNCTION notify_configuration_update()
13+
RETURNS TRIGGER
14+
LANGUAGE plpgsql
15+
AS $function$
16+
BEGIN
17+
PERFORM pg_notify('updated_configuration_data', json_build_object(
18+
'id', NEW.id
19+
)::text);
20+
RETURN NEW;
21+
END;
22+
$function$;
23+
24+
CREATE OR REPLACE TRIGGER configuration_insert_trigger
25+
AFTER INSERT ON configuration
26+
FOR EACH ROW
27+
EXECUTE FUNCTION notify_configuration_insert();
28+
29+
CREATE OR REPLACE TRIGGER configuration_update_trigger
30+
AFTER UPDATE ON configuration
31+
FOR EACH ROW
32+
EXECUTE FUNCTION notify_configuration_update();
33+
34+
-- Webhooks Triggers
35+
CREATE OR REPLACE FUNCTION notify_webhooks_insert()
36+
RETURNS TRIGGER
37+
LANGUAGE plpgsql
38+
AS $function$
39+
BEGIN
40+
PERFORM pg_notify('new_webhook_data', row_to_json(NEW)::text);
41+
RETURN NEW;
42+
END;
43+
$function$;
44+
45+
CREATE OR REPLACE FUNCTION notify_webhooks_update()
46+
RETURNS TRIGGER
47+
LANGUAGE plpgsql
48+
AS $function$
49+
BEGIN
50+
PERFORM pg_notify('updated_webhook_data', json_build_object(
51+
'id', NEW.id
52+
)::text);
53+
RETURN NEW;
54+
END;
55+
$function$;
56+
57+
CREATE OR REPLACE TRIGGER webhooks_insert_trigger
58+
AFTER INSERT ON webhooks
59+
FOR EACH ROW
60+
EXECUTE FUNCTION notify_webhooks_insert();
61+
62+
CREATE OR REPLACE TRIGGER webhooks_update_trigger
63+
AFTER UPDATE ON webhooks
64+
FOR EACH ROW
65+
EXECUTE FUNCTION notify_webhooks_update();

src/server/middleware/auth.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { GenericAuthWallet, LocalWallet } from "@thirdweb-dev/wallets";
88
import { AsyncWallet } from "@thirdweb-dev/wallets/evm/wallets/async";
99
import { utils } from "ethers";
1010
import { FastifyInstance } from "fastify";
11-
import { getConfiguration } from "../../db/configuration/getConfiguration";
1211
import { updateConfiguration } from "../../db/configuration/updateConfiguration";
1312
import { getPermissions } from "../../db/permissions/getPermissions";
1413
import { createToken } from "../../db/tokens/createToken";
1514
import { getToken } from "../../db/tokens/getToken";
1615
import { revokeToken } from "../../db/tokens/revokeToken";
1716
import { WebhooksEventTypes } from "../../schema/webhooks";
18-
import { getWebhookConfig } from "../../utils/cache/getWebhook";
17+
import { getConfig } from "../../utils/cache/getConfig";
18+
import { getWebhook } from "../../utils/cache/getWebhook";
1919
import { env } from "../../utils/env";
2020
import { logger } from "../../utils/logger";
2121
import { Permission } from "../schemas/auth";
@@ -73,7 +73,7 @@ const authWithApiServer = async (jwt: string, domain: string) => {
7373
};
7474

7575
export const withAuth = async (server: FastifyInstance) => {
76-
const config = await getConfiguration();
76+
const config = await getConfig();
7777

7878
// Configure the ThirdwebAuth fastify plugin
7979
const { authRouter, authMiddleware, getUser } = ThirdwebAuth<
@@ -86,7 +86,7 @@ export const withAuth = async (server: FastifyInstance) => {
8686
wallet: new AsyncWallet({
8787
// TODO: Use caching for the signer
8888
getSigner: async () => {
89-
const config = await getConfiguration();
89+
const config = await getConfig();
9090
const wallet = new LocalWallet();
9191

9292
try {
@@ -209,7 +209,7 @@ export const withAuth = async (server: FastifyInstance) => {
209209
const thirdwebApiSecretKey = req.headers.authorization?.split(" ")[1];
210210
if (thirdwebApiSecretKey === env.THIRDWEB_API_SECRET_KEY) {
211211
// If the secret key is being used, treat the user as the auth wallet
212-
const config = await getConfiguration();
212+
const config = await getConfig();
213213
const wallet = new LocalWallet();
214214

215215
try {
@@ -300,7 +300,7 @@ export const withAuth = async (server: FastifyInstance) => {
300300
// no-op
301301
}
302302

303-
const authWebhooks = await getWebhookConfig(WebhooksEventTypes.AUTH);
303+
const authWebhooks = await getWebhook(WebhooksEventTypes.AUTH);
304304
if (authWebhooks) {
305305
const authResponses = await Promise.all(
306306
authWebhooks.map((webhook) =>

src/server/routes/auth/access-tokens/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { buildJWT } from "@thirdweb-dev/auth";
33
import { LocalWallet } from "@thirdweb-dev/wallets";
44
import { FastifyInstance } from "fastify";
55
import { StatusCodes } from "http-status-codes";
6-
import { getConfiguration } from "../../../../db/configuration/getConfiguration";
76
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
87
import { createToken } from "../../../../db/tokens/createToken";
8+
import { getConfig } from "../../../../utils/cache/getConfig";
99
import { env } from "../../../../utils/env";
1010
import { AccessTokenSchema } from "./getAll";
1111

@@ -42,7 +42,7 @@ export async function createAccessToken(fastify: FastifyInstance) {
4242
handler: async (req, res) => {
4343
const { label } = req.body;
4444

45-
const config = await getConfiguration();
45+
const config = await getConfig();
4646
const wallet = new LocalWallet();
4747

4848
// TODO: Remove this with next breaking change

src/server/routes/backend-wallet/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { getConfiguration } from "../../../db/configuration/getConfiguration";
54
import { WalletType } from "../../../schema/wallet";
5+
import { getConfig } from "../../../utils/cache/getConfig";
66
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
77
import { createAwsKmsWallet } from "../../utils/wallets/createAwsKmsWallet";
88
import { createGcpKmsWallet } from "../../utils/wallets/createGcpKmsWallet";
@@ -48,7 +48,7 @@ export const createBackendWallet = async (fastify: FastifyInstance) => {
4848
const { label } = req.body;
4949

5050
let walletAddress: string;
51-
const config = await getConfiguration();
51+
const config = await getConfig();
5252
switch (config.walletConfiguration.type) {
5353
case WalletType.local:
5454
walletAddress = await createLocalWallet({ label });

0 commit comments

Comments
 (0)