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 #280

Merged
merged 5 commits into from
Oct 3, 2024
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

## [0.8.0] - 2024-10-03

### Breaking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also add the instructions to docs

 wh1.update({eventTypeFilter: {addresses:["0x40A28c0fCc0BE09400bB89CdF556Cd8C4eF1c165","0x6f03b3Df22F0C57A4477EEAc3a49c2Bc4EAe2206"]}})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if that is the place to put documentation then this change is documented. cc: @jazz-cb

- `Webhook#update` now takes an Object of `{notificationUri, eventTypeFilter}` instead of `notificationUri` string

## [0.7.0] - 2024-09-26

### Added
Expand Down
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
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.
Copy link
Contributor

@jazz-cb jazz-cb Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if eventTypeFilter can only contain replacement addresses, should this be called just addresses?

Also replacement is confusing. Aren't you adding new addresses to the webhook and not replacing the old ones?

@chaoyaji-cb @howard-at-cb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I thought about the naming eventTypeFilter, my thought process is that in future we might add more stuff into eventTypeFilter, and we might want to allow clients to update them, so keeping this name for the purpose of extensibility.
  2. It is actually a PUT not a PATCH, so address array passed in will replace the existing addresses in the webhook. I clarified it in this doc PR https://github.cbhq.net/solutions-architecture/docs/pull/715/files#diff-e948e3fa52983fe8dee3cacd990a72989a39f539973f07fca0f2065dc7a0577dR15
    This might not be the ideal solution, we can follow up with changes to this behavior.

* @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> {
Comment on lines +173 to +176
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that we're breaking back compat by changing the params here. Not sure if this would be an issue at this point, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good call out cc @jazz-cb not sure if it matters at this point

or maybe we can make the param like this: notificationUriOrOptions: string | UpdateWebhookOptions
then we can check their input: typeof notificationUriOrOptions === 'string'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's note it in the release logs.

For update functions lets make sure to use Objects for this forward-compatibility!

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