diff --git a/src/client/api.ts b/src/client/api.ts index 477ff9c0..7c1be6da 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -790,7 +790,7 @@ export interface CreateWalletRequestWallet { */ export interface CreateWalletWebhookRequest { /** - * The URL to which the notifications will be sent + * The URL to which the notifications will be sent. * @type {string} * @memberof CreateWalletWebhookRequest */ @@ -2186,7 +2186,7 @@ export interface SmartContractList { export type SmartContractOptions = MultiTokenContractOptions | NFTContractOptions | TokenContractOptions; /** - * The type of the smart contract + * The type of the smart contract. * @export * @enum {string} */ @@ -2878,7 +2878,7 @@ export interface UpdateWebhookRequest { * @type {string} * @memberof UpdateWebhookRequest */ - 'notification_uri': string; + 'notification_uri'?: string; } /** * diff --git a/src/coinbase/types.ts b/src/coinbase/types.ts index abf830db..6b9046d2 100644 --- a/src/coinbase/types.ts +++ b/src/coinbase/types.ts @@ -1209,6 +1209,15 @@ export type CreateWebhookOptions = { signatureHeader?: string; }; +/** + * Options for updating a Webhook. + */ +export type UpdateWebhookOptions = { + notificationUri?: string; + eventFilters?: Array; + eventTypeFilter?: { addresses: string[] }; +}; + /** * ContractInvocationAPI client type definition. */ diff --git a/src/coinbase/wallet.ts b/src/coinbase/wallet.ts index bd9c1baf..8f6d41a6 100644 --- a/src/coinbase/wallet.ts +++ b/src/coinbase/wallet.ts @@ -756,6 +756,7 @@ export class Wallet { * * @param notificationUri - The URI to which the webhook notifications will be sent. * + * @param addresses * @returns The newly created webhook instance. */ public async createWebhook(notificationUri: string): Promise { diff --git a/src/coinbase/webhook.ts b/src/coinbase/webhook.ts index bbaf1252..d60d672e 100644 --- a/src/coinbase/webhook.ts +++ b/src/coinbase/webhook.ts @@ -5,7 +5,7 @@ import { WebhookEventTypeFilter, } from "../client/api"; import { Coinbase } from "./coinbase"; -import { CreateWebhookOptions } from "./types"; +import { CreateWebhookOptions, UpdateWebhookOptions } from "./types"; /** * A representation of a Webhook, @@ -163,15 +163,21 @@ export class Webhook { } /** - * Updates the webhook with a new notification URI. + * Updates the webhook with a new notification URI, and optionally a new list of addresses to monitor. * - * @param notificationUri - The new URI for webhook notifications. + * @param options - The options to update webhook. + * @param options.notificationUri - The new URI for webhook notifications. + * @param options.eventTypeFilter - The new eventTypeFilter that contains a new list (replacement) of addresses to monitor for the webhook. * @returns A promise that resolves to the updated Webhook object. */ - public async update(notificationUri: string): Promise { + public async update({ + notificationUri, + eventTypeFilter, + }: UpdateWebhookOptions): Promise { const result = await Coinbase.apiClients.webhook!.updateWebhook(this.getId()!, { notification_uri: notificationUri, event_filters: this.getEventFilters()!, + event_type_filter: eventTypeFilter, }); this.model = result.data; diff --git a/src/tests/webhook_test.ts b/src/tests/webhook_test.ts index 90dc79e3..5e81005b 100644 --- a/src/tests/webhook_test.ts +++ b/src/tests/webhook_test.ts @@ -31,6 +31,7 @@ describe("Webhook", () => { data: { ...mockModel, notification_uri: updateRequest.notification_uri, + event_type_filter: updateRequest.event_type_filter, }, }); }), @@ -214,7 +215,7 @@ describe("Webhook", () => { describe("#update", () => { it("should update the webhook notification URI", async () => { const webhook = Webhook.init(mockModel); - await webhook.update("https://new-url.com/callback"); + await webhook.update({ notificationUri: "https://new-url.com/callback" }); expect(Coinbase.apiClients.webhook!.updateWebhook).toHaveBeenCalledWith("test-id", { notification_uri: "https://new-url.com/callback", @@ -223,6 +224,32 @@ describe("Webhook", () => { expect(webhook.getNotificationURI()).toBe("https://new-url.com/callback"); }); + it("should update both the webhook notification URI and the list of addresses monitoring", async () => { + const mockModel: WebhookModel = { + id: "test-id", + network_id: "test-network", + notification_uri: "https://example.com/callback", + event_type: "erc20_transfer", + event_type_filter: { + addresses: ["0xa55C5950F7A3C42Fa5799B2Cac0e455774a07382"], + }, + event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }], + }; + const webhook = Webhook.init(mockModel); + await webhook.update({ + notificationUri: "https://new-url.com/callback", + eventTypeFilter: { addresses: ["0x1..", "0x2.."] }, + }); + + 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: ["0x1..", "0x2.."] }, + }); + + expect(webhook.getNotificationURI()).toBe("https://new-url.com/callback"); + expect(webhook.getEventTypeFilter()).toEqual({ addresses: ["0x1..", "0x2.."] }); + }); }); describe("#delete", () => {