denotify-client 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ import { NotifyDiscordWebhook } from "./notifications/notify_discord_webhook.js";
2
+ import { HandlerFunctionCall } from "./triggers/handler_function_call.js";
3
+ import { HandlerFunctionCallV2 } from "./triggers/handler_function_call_v2.js";
4
+ import { HandlerOnchainEvent } from "./triggers/handler_onchain_event.js";
1
5
  export class AlertBuilder {
2
6
  name;
3
7
  network;
@@ -15,6 +19,13 @@ export class AlertBuilder {
15
19
  this.network = network;
16
20
  return this;
17
21
  }
22
+ /**
23
+ * Call withTrigger with one of the TriggerConfig types:
24
+ * PollFunctionV2 | PollFunctionV1 | OnchainEventV1
25
+ * @param id Simple ID
26
+ * @param options Desired trigger configuration
27
+ * @returns self for piping
28
+ */
18
29
  withTrigger(id, options) {
19
30
  this.triggerId = id;
20
31
  this.trigger = options;
@@ -25,13 +36,25 @@ export class AlertBuilder {
25
36
  this.notification = options;
26
37
  return this;
27
38
  }
28
- config() {
39
+ async validate() {
40
+ // Validate trigger
41
+ switch (this.triggerId) {
42
+ case 'OnchainEventV1': return HandlerOnchainEvent.validateCreate(this.trigger);
43
+ case 'PollFunctionV1': return HandlerFunctionCall.validateCreate(this.trigger);
44
+ case 'PollFunctionV2': return HandlerFunctionCallV2.validateCreate(this.trigger);
45
+ }
46
+ switch (this.notificationId) {
47
+ case 'Discord': return NotifyDiscordWebhook.validateCreate(this.notification);
48
+ }
49
+ }
50
+ async config() {
29
51
  if (this.trigger === undefined || this.triggerId === undefined)
30
52
  throw new Error('Trigger not configured');
31
53
  if (this.notification === undefined || this.notificationId === undefined)
32
54
  throw new Error('Notification not configured');
33
55
  if (this.network === undefined)
34
56
  throw new Error('Network not configured');
57
+ await this.validate();
35
58
  return {
36
59
  name: this.name,
37
60
  network: this.network,
@@ -47,10 +47,19 @@ export class DeNotifyClient {
47
47
  throw new Error('Authentication Required. DeNotify supports either username/password or API key authentication');
48
48
  }
49
49
  }
50
- async alertHistory(id, pagination = {}) {
51
- const alerts = await this.request('get', `alert-history${id ? '/' + id : ''}`, { params: pagination });
52
- return alerts;
50
+ async alertHistory(id, pagination) {
51
+ const url = `alert-history`;
52
+ const params = pagination ? pagination : {};
53
+ if (id)
54
+ params.id = id;
55
+ if (Object.keys(params).length > 0) {
56
+ return await this.request('get', url, { params: pagination });
57
+ }
58
+ else {
59
+ return await this.request('get', url);
60
+ }
53
61
  }
62
+ // TODO - Beutify the reponse
54
63
  async getAlert(id) {
55
64
  const alerts = await this.request('get', `alerts/${id}`);
56
65
  return alerts[0];
@@ -77,6 +86,7 @@ export class DeNotifyClient {
77
86
  url.searchParams.append(param, options.params[param]);
78
87
  }
79
88
  }
89
+ console.log(url.toString());
80
90
  const payload = {
81
91
  method,
82
92
  url: url.toString(),
@@ -0,0 +1,57 @@
1
+ import { DeNotifyClient } from "../denotifyclient.js";
2
+ // Simple App to demonstrate usage. Created a balance monitoring alert, updates it and deletes it
3
+ async function main() {
4
+ const api = await DeNotifyClient.create({
5
+ email: 's.battenally@gmail.com',
6
+ password: 'Password',
7
+ });
8
+ // const alerts = await api.getAlerts()
9
+ const size = 100;
10
+ let page = 1;
11
+ let len = size;
12
+ while (len === size) {
13
+ console.log(`fetching page: ${page}, size: ${size}, id: ${123}`);
14
+ const ret = await api.alertHistory(123, { page, size });
15
+ console.log('success!');
16
+ console.log('length:', ret.history.length);
17
+ len = ret.history.length;
18
+ page++;
19
+ }
20
+ // const network = 'avalanche'
21
+ // const address = '0x26985888d5b7019ff2A7444fB567D8F386c3b538'
22
+ // const myAddress = '0x7601630eC802952ba1ED2B6e4db16F699A0a5A87'
23
+ // const { abi } = await api.getAbi(network, address)
24
+ // const webhook = process.env.DISCORD_WEBHOOK as string
25
+ // const builder = FunctionBuilder.create(api)
26
+ // await builder.addFunction(address, 'getBalance', [myAddress], abi)
27
+ // // Create the Balance Monitor alert
28
+ // const alert = await AlertBuilder.create('Test Alert')
29
+ // .onNetwork('avalanche')
30
+ // .withTrigger<PollFunctionV2>('PollFunctionV2', {
31
+ // timeBase: 'time',
32
+ // timePeriod: '100s',
33
+ // functions: builder.get(),
34
+ // triggerOn: 'always',
35
+ // })
36
+ // .withNotification<DiscordWebhook>('Discord', {
37
+ // url: webhook,
38
+ // message:
39
+ // `Your avax balance is [{func_0_ret_0 / 1e18}](https://snowtrace.io/address/${myAddress})`,
40
+ // })
41
+ // .config()
42
+ // // Create the alert with the API
43
+ // const triggerId = await api.createAlert(alert)
44
+ // console.log(triggerId)
45
+ // // Update the period to every 10s
46
+ // await api.updateTrigger(triggerId, { timePeriod: '10s' })
47
+ // // Update the Filter using the filter builder
48
+ // const filter = FilterBuilder.new()
49
+ // .addCondition('WHERE', 'func_0_ret_0', 'Number', 'gt', 3)
50
+ // .finalise()
51
+ // await api.updateTrigger(13, { triggerOn: 'filter', filter, filterVersion: FilterBuilder.version() })
52
+ // // Delete the filter in 10s
53
+ // setTimeout(async () => {
54
+ // await api.deleteAlert(triggerId)
55
+ // }, 10 * 1000)
56
+ }
57
+ main();
@@ -1,4 +1,5 @@
1
1
  import { ethers } from "ethers";
2
+ import * as yup from 'yup';
2
3
  export class FunctionBuilder {
3
4
  api;
4
5
  data = [];
@@ -27,4 +28,12 @@ export class FunctionBuilder {
27
28
  get() {
28
29
  return this.data;
29
30
  }
31
+ static schema() {
32
+ return yup.array().of(yup.object({
33
+ address: yup.string().required(),
34
+ bytecode: yup.string().matches(/^(0x)?([0-9a-fA-F]{2})*$/).required(),
35
+ abiHash: yup.string().matches(/^[A-Fa-f0-9]{64}/).required(),
36
+ function: yup.string().required()
37
+ }));
38
+ }
30
39
  }