Skip to content

Commit

Permalink
fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
howard-at-cb committed Nov 26, 2024
1 parent fe33721 commit 509c620
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ export type CreateWebhookOptions = {
export type UpdateWebhookOptions = {
notificationUri?: string;
eventFilters?: Array<WebhookEventFilter>;
eventTypeFilter?: { addresses: string[] };
eventTypeFilter?: WebhookEventTypeFilter;
};

/**
Expand Down
11 changes: 9 additions & 2 deletions src/coinbase/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Webhook as WebhookModel,
WebhookEventType,
WebhookEventFilter,
WebhookEventTypeFilter,
WebhookEventTypeFilter, WebhookWalletActivityFilter,

Check failure on line 5 in src/coinbase/webhook.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·`
} from "../client/api";
import { Coinbase } from "./coinbase";
import {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class Webhook {
* @param options.networkId - The network ID for which the webhook is created.
* @param options.notificationUri - The URI where notifications should be sent.
* @param options.eventType - The type of event for the webhook.
* @param options.eventTypeFilter - Filter for wallet activity event type.
* @param options.eventTypeFilter - Filter for wallet or smart contract activity event types.
* @param options.eventFilters - Filters applied to the events that determine which specific events trigger the webhook.
* @returns A promise that resolves to a new instance of Webhook.
*/
Expand Down Expand Up @@ -188,6 +188,13 @@ export class Webhook {
const finalNotificationUri = notificationUri ?? this.getNotificationURI();
const finalEventTypeFilter = eventTypeFilter ?? this.getEventTypeFilter();

// wallet ID is required for wallet activity event type filter, but we do not support updating it just yet
if (this.getEventType() === WebhookEventType.WalletActivity) {
(finalEventTypeFilter as WebhookWalletActivityFilter).wallet_id = <string>(
(this.getEventTypeFilter() as WebhookWalletActivityFilter)?.wallet_id
);
}

const result = await Coinbase.apiClients.webhook!.updateWebhook(this.getId()!, {
notification_uri: finalNotificationUri,
event_filters: this.getEventFilters()!,
Expand Down
6 changes: 3 additions & 3 deletions src/tests/wallet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
FetchHistoricalStakingBalances200Response,
StakingRewardStateEnum,
StakingRewardFormat,
FeatureSet,
FeatureSet, WebhookWalletActivityFilter,
} from "./../client";
import {
VALID_ADDRESS_MODEL,
Expand Down Expand Up @@ -1442,8 +1442,8 @@ describe("Wallet Class", () => {
jest.spyOn(Wallet.prototype, "createWebhook").mockReturnValue(wh);
const result = await wallet.createWebhook("https://example.com/callback");
expect(result).toBeInstanceOf(Webhook);
expect(result.getEventTypeFilter()?.wallet_id).toBe(walletModel.id);
expect(result.getEventTypeFilter()?.addresses).toStrictEqual([address1]);
expect((result.getEventTypeFilter() as WebhookWalletActivityFilter)?.wallet_id).toBe(walletModel.id);
expect((result.getEventTypeFilter() as WebhookWalletActivityFilter)?.addresses).toStrictEqual([address1]);
expect(result.getEventType()).toBe("wallet_activity");
});
});
Expand Down
20 changes: 4 additions & 16 deletions src/tests/webhook_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Webhook } from "../coinbase/webhook";
import { Coinbase } from "../coinbase/coinbase";
import { Webhook as WebhookModel } from "../client/api";
import { Webhook as WebhookModel, WebhookWalletActivityFilter } from "../client/api";
import { mockReturnRejectedValue } from "./utils";
import { APIError } from "../coinbase/api_error";

Expand All @@ -10,10 +10,6 @@ describe("Webhook", () => {
network_id: "test-network",
notification_uri: "https://example.com/callback",
event_type: "erc20_transfer",
event_type_filter: {
addresses: ["0xa55C5950F7A3C42Fa5799B2Cac0e455774a07382"],
wallet_id: "w1",
},
event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }],
};

Expand Down Expand Up @@ -151,15 +147,13 @@ describe("Webhook", () => {
networkId: "test-network",
notificationUri: "https://example.com/callback",
eventType: "erc20_transfer",
eventTypeFilter: { addresses: ["0x1..", "0x2.."] },
eventFilters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }],
});

expect(Coinbase.apiClients.webhook!.createWebhook).toHaveBeenCalledWith({
network_id: "test-network",
notification_uri: "https://example.com/callback",
event_type: "erc20_transfer",
event_type_filter: { addresses: ["0x1..", "0x2.."] },
event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }],
});
expect(webhook).toBeInstanceOf(Webhook);
Expand Down Expand Up @@ -206,10 +200,6 @@ 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");
Expand All @@ -225,18 +215,17 @@ describe("Webhook", () => {
});

expect(webhook.getNotificationURI()).toBe("https://example.com/callback");
expect(webhook.getEventTypeFilter()?.addresses).toEqual(["0x1..", "0x2.."]);
expect((webhook.getEventTypeFilter() as WebhookWalletActivityFilter)?.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",
network_id: "test-network",
notification_uri: "https://example.com/callback",
event_type: "erc20_transfer",
event_type: "wallet_activity",
event_type_filter: {
addresses: ["0xa55C5950F7A3C42Fa5799B2Cac0e455774a07382"],
},
event_filters: [{ contract_address: "0x...", from_address: "0x...", to_address: "0x..." }],
};
const webhook = Webhook.init(mockModel);
await webhook.update({
Expand All @@ -246,7 +235,6 @@ 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: ["0x1..", "0x2.."] },
});

Expand All @@ -271,7 +259,7 @@ describe("Webhook", () => {
const webhook = Webhook.init(mockModel);
const stringRepresentation = webhook.toString();
expect(stringRepresentation).toBe(
`Webhook { id: 'test-id', networkId: 'test-network', eventType: 'erc20_transfer', eventFilter: [{"contract_address":"0x...","from_address":"0x...","to_address":"0x..."}], eventTypeFilter: {"addresses":["0xa55C5950F7A3C42Fa5799B2Cac0e455774a07382"],"wallet_id":"w1"}, notificationUri: 'https://example.com/callback', signatureHeader: 'undefined' }`,
`Webhook { id: 'test-id', networkId: 'test-network', eventType: 'erc20_transfer', eventFilter: [{"contract_address":"0x...","from_address":"0x...","to_address":"0x..."}], eventTypeFilter: undefined, notificationUri: 'https://example.com/callback', signatureHeader: 'undefined' }`,
);
});
});
Expand Down

0 comments on commit 509c620

Please sign in to comment.