Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow updating address list of a wallet activity webhook #279

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -2878,7 +2878,7 @@ export interface UpdateWebhookRequest {
* @type {string}
* @memberof UpdateWebhookRequest
*/
'notification_uri': string;
'notification_uri'?: string;
}
/**
*
Expand Down
9 changes: 9 additions & 0 deletions src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,15 @@ export type CreateWebhookOptions = {
signatureHeader?: string;
};

/**
* Options for updating a Webhook.
*/
export type UpdateWebhookOptions = {
notificationUri?: string;
eventFilters?: Array<WebhookEventFilter>;
eventTypeFilter?: { addresses: string[] };
};

/**
* ContractInvocationAPI client type definition.
*/
Expand Down
1 change: 1 addition & 0 deletions src/coinbase/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@
*
* @param notificationUri - The URI to which the webhook notifications will be sent.
*
* @param addresses

Check failure on line 759 in src/coinbase/wallet.ts

View workflow job for this annotation

GitHub Actions / lint

@param "addresses" does not match an existing function parameter

Check failure on line 759 in src/coinbase/wallet.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "addresses" description

Check failure on line 759 in src/coinbase/wallet.ts

View workflow job for this annotation

GitHub Actions / lint

@param "addresses" does not match an existing function parameter

Check failure on line 759 in src/coinbase/wallet.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "addresses" description
* @returns The newly created webhook instance.
*/
public async createWebhook(notificationUri: string): Promise<Webhook> {
Expand Down
14 changes: 10 additions & 4 deletions src/coinbase/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Webhook> {
public async update({
notificationUri,
eventTypeFilter,
}: UpdateWebhookOptions): Promise<Webhook> {
const result = await Coinbase.apiClients.webhook!.updateWebhook(this.getId()!, {
notification_uri: notificationUri,
event_filters: this.getEventFilters()!,
event_type_filter: eventTypeFilter,
});

this.model = result.data;
Expand Down
29 changes: 28 additions & 1 deletion src/tests/webhook_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Webhook", () => {
data: {
...mockModel,
notification_uri: updateRequest.notification_uri,
event_type_filter: updateRequest.event_type_filter,
},
});
}),
Expand Down Expand Up @@ -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",
Expand All @@ -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", () => {
Expand Down
Loading