From 07216abc0b12d865340dbb96f939254671e9ef78 Mon Sep 17 00:00:00 2001 From: Koen Rijpstra Date: Tue, 20 Feb 2024 10:16:39 +0100 Subject: [PATCH] Add removeAddressesFromWebhook method (#66) --- README.md | 16 ++++++++++++++++ src/Helius.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/README.md b/README.md index aca28d0..6100627 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ Provides methods for setting up, editing, and managing webhooks, crucial for lis - [`createWebhook()`](#createWebhook): Creates a new webhook with the provided request. - [`editWebhook()`](#editWebhook): Edits an existing webhook by its ID with the provided request. - [`appendAddressesToWebhook()`](#appendAddressesToWebhook): Append new addresses to an existing webhook. +- [`removeAddressesFromWebhook()`](#removeAddressesFromWebhook): Remove addresses from an existing webhook. - [`deleteWebhook()`](#deleteWebhook): Deletes a webhook by its ID. - [`getWebhookByID()`](#getWebhookByID): Retrieves a single webhook by its ID. - [`getAllWebhooks()`](#getAllWebhooks): Retrieves a list of all webhooks. @@ -497,6 +498,21 @@ helius.appendAddressesToWebhook("your-webhook-id-here", [ ]); ``` +### removeAddressesFromWebhook() + +For convenience, we've added a method to let you simply remove addresses from an existing webhook: + +```ts +import { Helius, Address } from "helius-sdk"; + +const helius = new Helius("YOUR_API_KEY"); + +helius.removeAddressesFromWebhook("your-webhook-id-here", [ + Address.SQUADS, + Address.JUPITER_V3, +]); +``` + ### getAllWebhooks() ```ts diff --git a/src/Helius.ts b/src/Helius.ts index 08fa2ee..ce73afc 100644 --- a/src/Helius.ts +++ b/src/Helius.ts @@ -236,6 +236,38 @@ export class Helius { } } + /** + * Removes an array of addresses from an existing webhook by its ID + * @param {string} webhookID - the ID of the webhook to edit + * @param {string[]} addressesToRemove - the array of addresses to be removed from the webhook + * @returns {Promise} a promise that resolves to the edited webhook object + * @throws {Error} if there is an error calling the webhooks endpoint, if the response contains an error + */ + async removeAddressesFromWebhook( + webhookID: string, + addressesToRemove: string[] + ): Promise { + try { + const webhook = await this.getWebhookByID(webhookID); + // Filter out the addresses to be removed + const accountAddresses = webhook.accountAddresses.filter( + address => !addressesToRemove.includes(address) + ); + const editRequest: EditWebhookRequest = { accountAddresses }; + return this._editWebhook(webhookID, webhook, editRequest); + } catch (err: any | AxiosError) { + if (axios.isAxiosError(err)) { + throw new Error( + `error during removeAddressesFromWebhook: ${ + err.response?.data.error || err + }` + ); + } else { + throw new Error(`error during removeAddressesFromWebhook: ${err}`); + } + } + } + async createCollectionWebhook( request: CreateCollectionWebhookRequest ): Promise {