diff --git a/CHANGELOG.md b/CHANGELOG.md index add44b41..f7de1af4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +## [0.8.1] - 2024-10-09 + +### Fixed +- Fixed a bug that overwrites existing webhook notification URI when only updating a webhook's addresses + ## [0.8.0] - 2024-10-04 ### Added diff --git a/quickstart-template/webhook/README.md b/quickstart-template/webhook/README.md index 65308036..0b79a535 100644 --- a/quickstart-template/webhook/README.md +++ b/quickstart-template/webhook/README.md @@ -3,38 +3,16 @@ This is a template repository for quickly getting started with a Webhook App, which can be used for setting up Webhooks for listening to blockchain event notifications. -## Local Webhook App - -To set up the template, run the following commands from this `webhook` folder: - -```bash -npm install -npm run start-webhook-app -``` - -This command will set up a webhook app locally. - -Once the local webhook app is setup, in another terminal window, you can test it with: - -```bash -curl -X POST -H "Content-Type:application/json" -d '{"app": "webhook"}' http://localhost:3000/callback -``` - ## Public Webhook App -To setup a temporary public URL that points to this local webhook app, -you can use [Pinggy](https://pinggy.io/) in another terminal window: - -```bash -ssh -p 443 -R0:localhost:3000 -L4300:localhost:4300 qr@a.pinggy.io -``` - -You can also use [Vercel](https://vercel.com/) or other hosting solutions for your webhook app. +You can use [webhook.site](https://webhook.site/) or [Vercel](https://vercel.com/) or other hosting solutions to create a simple webapp with public facing URL which can receive data from webhook POST calls. -Once the public webhook app is setup, copy the URL provided and test it with: +Once the public webapp is setup, copy the URL provided and test it locally. Below is an example from webhook.site: ```bash -curl -X POST -H "Content-Type:application/json" -d '{"app": "webhook"}' {url_copied_from_pinggy_io}/callback +curl -X POST 'https://webhook.site/00000000-0000-0000-0000-000000000000' \ + -H 'content-type: application/json' \ + -d $'{"id": 7, "name": "Jack Daniels", "position": "Assistant"}' ``` This URL (ie Notification URL) can now be used to create Webhook either diff --git a/src/coinbase/webhook.ts b/src/coinbase/webhook.ts index c9bf3d15..b6a109d1 100644 --- a/src/coinbase/webhook.ts +++ b/src/coinbase/webhook.ts @@ -185,10 +185,13 @@ export class Webhook { notificationUri, eventTypeFilter, }: UpdateWebhookOptions): Promise { + const finalNotificationUri = notificationUri ?? this.getNotificationURI(); + const finalEventTypeFilter = eventTypeFilter ?? this.getEventTypeFilter(); + const result = await Coinbase.apiClients.webhook!.updateWebhook(this.getId()!, { - notification_uri: notificationUri, + notification_uri: finalNotificationUri, event_filters: this.getEventFilters()!, - event_type_filter: eventTypeFilter, + event_type_filter: finalEventTypeFilter, }); this.model = result.data; diff --git a/src/tests/webhook_test.ts b/src/tests/webhook_test.ts index 10139643..fc171d51 100644 --- a/src/tests/webhook_test.ts +++ b/src/tests/webhook_test.ts @@ -206,10 +206,27 @@ describe("Webhook", () => { expect(Coinbase.apiClients.webhook!.updateWebhook).toHaveBeenCalledWith("test-id", { notification_uri: "https://new-url.com/callback", event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }], + event_type_filter: { + addresses: ["0xa55C5950F7A3C42Fa5799B2Cac0e455774a07382"], + wallet_id: "w1", + }, }); expect(webhook.getNotificationURI()).toBe("https://new-url.com/callback"); }); + it("should update the webhook address list only", async () => { + const webhook = Webhook.init(mockModel); + await webhook.update({ eventTypeFilter: { addresses: ["0x1..", "0x2.."] } }); + + expect(Coinbase.apiClients.webhook!.updateWebhook).toHaveBeenCalledWith("test-id", { + notification_uri: "https://example.com/callback", + event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }], + event_type_filter: { addresses: ["0x1..", "0x2.."] }, + }); + + expect(webhook.getNotificationURI()).toBe("https://example.com/callback"); + expect(webhook.getEventTypeFilter()?.addresses).toEqual(["0x1..", "0x2.."]); + }); it("should update both the webhook notification URI and the list of addresses monitoring", async () => { const mockModel: WebhookModel = { id: "test-id",