Skip to content

Commit

Permalink
Webhook Docs Update (#228)
Browse files Browse the repository at this point in the history
* updated webhook docs & cleanup

* docs Table of contents udpate. New guides folder

* image path update
  • Loading branch information
farhanW3 authored Oct 17, 2023
1 parent 47b0ee3 commit ca6f6e0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 74 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
- [Setup environment variables](#setup-environment-variables)
- [Run the server](#run-the-server)
- [Using the server](#using-the-server)
- [Local development](#local-development)
- [User guide](#user-guide)
- [Documentation](#api-documentation)
- [Contributing](#contributing)
- [Resources](#resources)
- [Using Engine](#using-engine)
- [Other Resources](#other-resources)
- [Security](#security)

## Introduction

Expand Down Expand Up @@ -87,7 +87,7 @@ docker run \

## Resources

**Using Engine**
#### Using Engine

- [User Guide](./docs/1-user-guide.md) - Setup an engine instance.
- [Smart Wallets Guide](./docs/2-smart-wallets.md) - Deploy, manage, and transact with smart wallets with engine.
Expand All @@ -97,7 +97,7 @@ docker run \
- [AWS KMS Setup](./docs/kms/aws_kms_how_to.md) - Setup engine to use AWS KMS to manage wallets.
- [Google KMS Setup](./docs/kms/google_kms_how_to.md) - Setup engine to use Google KMS to manage wallets.

**Other Resources**
#### Other Resources

- [Frequently Asked Questions](./docs/addons/faqs.md) - The most common questions & answers.
- [Contributing to Engine](./docs/addons/contributing.md) - Contribute to this project!
Expand Down
27 changes: 2 additions & 25 deletions docs/1-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,9 @@ For updates on your requests, you can either poll using the `get` (`/transaction

### Run on Cloud

#### Zeet Example:

1. Goto https://zeet.co/ and create an account
2. Create a new project
3. Select `Docker Image` as the deployment method
4. Under `Docker Image`, search for `thirdweb/engine` and select it
5. Under `Docker Image Tag`, select either `latest` or `nightly`
![Alt text](./images/Zeet-Docker-Source-Setting.png)
6. Select the Target cluster you want the above image to be deployed to
7. Choose your `Compute` settings
8. Update the `port` under `Networking` Tab to `3005`
9. Under `Environment Vriables` add the below vars with values:
Check the below Deployment Guides for more details:

```
POSTGRES_CONNECTION_URL
THIRDWEB_API_SECRET_KEY
```

10. Under `Organize` Tab,

- you can select an existing `group`` or create a new one
- you can select an existing `sub-group` or create a new one
- Add a Project Name

11. Click on `Deploy` button
12. Once the deployment is complete, you can click on the URL given by zeet to access the API & Swagger UI
1. [Zeet Deployment Guide](./guides//deployment/zeet-deployment.md)

### Note:

Expand Down
9 changes: 9 additions & 0 deletions docs/3-webhook-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ WEBHOOK_URL=<your_web_hook_url>

The webhook URL needs to be a `POST` method to accept the data being sent.

### Authentication

The webhook URL will be sent with a 'Authorization' header with the value as `Bearer <webhook_auth_bearer_token>`. The value of the token will be the same as the `WEBHOOK_AUTH_BEARER_TOKEN` environment variable. You can set this variable to any value you want.

```
# env variable
WEBHOOK_AUTH_BEARER_TOKEN=<your_web_hook_auth_token>
```

### Payload

The payload sent to the webhook URL will be in the below format:
Expand Down
26 changes: 26 additions & 0 deletions docs/guides/deployment/zeet-deployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#### Zeet Deployment Guide:

1. Goto https://zeet.co/ and create an account
2. Create a new project
3. Select `Docker Image` as the deployment method
4. Under `Docker Image`, search for `thirdweb/engine` and select it
5. Under `Docker Image Tag`, select either `latest` or `nightly`
![Alt text](../../images/Zeet-Docker-Source-Setting.png)
6. Select the Target cluster you want the above image to be deployed to
7. Choose your `Compute` settings
8. Update the `port` under `Networking` Tab to `3005`
9. Under `Environment Vriables` add the below vars with values:

```
POSTGRES_CONNECTION_URL
THIRDWEB_API_SECRET_KEY
```

10. Under `Organize` Tab,

- you can select an existing `group`` or create a new one
- you can select an existing `sub-group` or create a new one
- Add a Project Name

11. Click on `Deploy` button
12. Once the deployment is complete, you can click on the URL given by zeet to access the API & Swagger UI
23 changes: 2 additions & 21 deletions server/controller/tx-update-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getStatusMessageAndConnectionStatus,
} from "../helpers/websocket";
import { subscriptionsData } from "../schemas/websocket";
import { sendWebhook } from "../utilities/webhook";

export const startTxUpdatesNotificationListener = async (): Promise<void> => {
try {
Expand All @@ -23,27 +24,7 @@ export const startTxUpdatesNotificationListener = async (): Promise<void> => {

// Send webhook
if (env.WEBHOOK_URL.length > 0) {
const txData = await getTxById({ queueId: parsedPayload.id });
let headers: { Accept: string, "Content-Type": string, Authorization?: string } = {
Accept: "application/json",
"Content-Type": "application/json",
};

if (process.env.WEBHOOK_AUTH_BEARER_TOKEN) {
headers["Authorization"] = `Bearer ${process.env.WEBHOOK_AUTH_BEARER_TOKEN}`;
}

const response = await fetch(env.WEBHOOK_URL, {
method: "POST",
headers,
body: JSON.stringify(txData),
});

if (!response.ok) {
logger.server.error(
`Webhook error: ${response.status} ${response.statusText}`,
);
}
await sendWebhook(parsedPayload);
} else {
logger.server.debug(
`Webhooks are disabled or no URL is provided. Skipping webhook update`,
Expand Down
37 changes: 37 additions & 0 deletions server/utilities/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getTxById } from "../../src/db/transactions/getTxById";
import { env } from "../../src/utils/env";
import { logger } from "../../src/utils/logger";

export const sendWebhook = async (data: any): Promise<void> => {
try {
const txData = await getTxById({ queueId: data.id });
const headers: {
Accept: string;
"Content-Type": string;
Authorization?: string;
} = {
Accept: "application/json",
"Content-Type": "application/json",
};

if (process.env.WEBHOOK_AUTH_BEARER_TOKEN) {
headers[
"Authorization"
] = `Bearer ${process.env.WEBHOOK_AUTH_BEARER_TOKEN}`;
}

const response = await fetch(env.WEBHOOK_URL, {
method: "POST",
headers,
body: JSON.stringify(txData),
});

if (!response.ok) {
logger.server.error(
`[sendWebhook] Webhook Request error: ${response.status} ${response.statusText}`,
);
}
} catch (error) {
logger.server.error(`[sendWebhook] error: ${error}`);
}
};
24 changes: 2 additions & 22 deletions src/worker/listeners/queuedTxListener.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PQueue from "p-queue";
import { sendWebhook } from "../../../server/utilities/webhook";
import { knex } from "../../db/client";
import { getTxById } from "../../db/transactions/getTxById";
import { env } from "../../utils/env";
import { logger } from "../../utils/logger";
import { processTx } from "../tasks/processTx";
Expand Down Expand Up @@ -33,27 +33,7 @@ export const queuedTxListener = async (): Promise<void> => {
async (msg: { channel: string; payload: string }) => {
if (env.WEBHOOK_URL.length > 0) {
const parsedPayload = JSON.parse(msg.payload);
const txData = await getTxById({ queueId: parsedPayload.id });

let headers: { Accept: string, "Content-Type": string, Authorization?: string } = {
Accept: "application/json",
"Content-Type": "application/json",
};

if (process.env.WEBHOOK_AUTH_BEARER_TOKEN) {
headers["Authorization"] = `Bearer ${process.env.WEBHOOK_AUTH_BEARER_TOKEN}`;
}
const response = await fetch(env.WEBHOOK_URL, {
method: "POST",
headers,
body: JSON.stringify(txData),
});

if (!response.ok) {
logger.server.error(
`Webhook error: ${response.status} ${response.statusText}`,
);
}
await sendWebhook(parsedPayload);
} else {
logger.server.debug(
`Webhooks are disabled or no URL is provided. Skipping webhook update`,
Expand Down

0 comments on commit ca6f6e0

Please sign in to comment.