denotify-client 1.1.9 → 1.1.11
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.
- package/.env +9 -0
- package/.eslintignore +8 -0
- package/.eslintrc +18 -0
- package/.lintstagedrc +4 -0
- package/.prettierignore +8 -0
- package/.prettierrc +16 -0
- package/.stylelintignore +8 -0
- package/.stylelintrc +18 -0
- package/CHANGELOG.md +0 -0
- package/dts-bundle-generator.config.ts +18 -0
- package/favicon.svg +15 -0
- package/index.html +13 -0
- package/jest.config.ts +9 -0
- package/package.json +51 -62
- package/tsconfig.json +22 -0
- package/vite.config.ts +35 -0
- package/dist/alertbuilder.js +0 -67
- package/dist/denotifyclient.js +0 -125
- package/dist/examples/balance-monitor.js +0 -54
- package/dist/examples/delete-alert.js +0 -11
- package/dist/functionbuilder.js +0 -41
- package/dist/index.js +0 -569
- package/dist/index.min.js +0 -1
- package/dist/index.mjs +0 -539
- package/dist/notifications/index.js +0 -3
- package/dist/notifications/notification.js +0 -8
- package/dist/notifications/notify_discord_webhook.js +0 -21
- package/dist/triggers/handler_function_call.js +0 -26
- package/dist/triggers/handler_function_call_v2.js +0 -41
- package/dist/triggers/handler_onchain_event.js +0 -28
- package/dist/triggers/index.js +0 -5
- package/dist/triggers/trigger.js +0 -14
- package/dist/types/types.js +0 -1
- package/dist/util/filter.js +0 -160
- /package/{license → LICENSE.md} +0 -0
package/dist/index.mjs
DELETED
@@ -1,539 +0,0 @@
|
|
1
|
-
import { createClient } from '@supabase/supabase-js';
|
2
|
-
import * as yup from 'yup';
|
3
|
-
import { ethers } from 'ethers';
|
4
|
-
import fetch from 'node-fetch';
|
5
|
-
|
6
|
-
const NOTIFY_DISCORD_WEBHOOK_RAW_ID = 'notify_discord_webhook';
|
7
|
-
class NotifyDiscordWebhook {
|
8
|
-
static SimpleToRaw(config) {
|
9
|
-
return {
|
10
|
-
name: '',
|
11
|
-
notify_type: NOTIFY_DISCORD_WEBHOOK_RAW_ID,
|
12
|
-
notify: config
|
13
|
-
};
|
14
|
-
}
|
15
|
-
static validateCreate(options) {
|
16
|
-
const urlRegex = /^(https?|ftp):\/\/(-\.)?([^\s/?\.#]+\.?)+([^\s\.?#]+)?(\?\S*)?$/;
|
17
|
-
const schema = yup.object({
|
18
|
-
url: yup.string().matches(urlRegex, 'url is not a valid url').required(),
|
19
|
-
username: yup.string(),
|
20
|
-
avatar_url: yup.string().matches(urlRegex, 'url is not a valid url'),
|
21
|
-
message: yup.string().required()
|
22
|
-
});
|
23
|
-
return schema.validate(options);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
|
27
|
-
class Notification {
|
28
|
-
static SimpleToRaw(id, config) {
|
29
|
-
switch (id) {
|
30
|
-
case 'Discord': return NotifyDiscordWebhook.SimpleToRaw(config);
|
31
|
-
}
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
const HANDLER_FUNCTION_CALL_V1_RAW_ID = 'handler_function_call';
|
36
|
-
class HandlerFunctionCall {
|
37
|
-
static SimpleToRaw(name, network, config) {
|
38
|
-
return {
|
39
|
-
alertType: 'event',
|
40
|
-
network,
|
41
|
-
nickname: name,
|
42
|
-
type: HANDLER_FUNCTION_CALL_V1_RAW_ID,
|
43
|
-
handler: config
|
44
|
-
};
|
45
|
-
}
|
46
|
-
static validateCreate(options) {
|
47
|
-
const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
|
48
|
-
const schema = yup.object({
|
49
|
-
address: yup.string().required(),
|
50
|
-
abi: yup.array().required(),
|
51
|
-
nBlocks: yup.number().min(10),
|
52
|
-
condition: yup.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
|
53
|
-
constant: yup.number().min(0).when('condition', requiredWhenConditional),
|
54
|
-
responseArgIndex: yup.number().min(0).when('condition', requiredWhenConditional),
|
55
|
-
responseArgDecimals: yup.number().min(0).when('condition', requiredWhenConditional),
|
56
|
-
});
|
57
|
-
return schema.validate(options);
|
58
|
-
}
|
59
|
-
}
|
60
|
-
|
61
|
-
const HANDLER_ONCHAIN_EVENT_V1_RAW_ID = 'handler_onchain_event';
|
62
|
-
class HandlerOnchainEvent {
|
63
|
-
static SimpleToRaw(name, network, config) {
|
64
|
-
return {
|
65
|
-
alertType: 'event',
|
66
|
-
network,
|
67
|
-
nickname: name,
|
68
|
-
type: HANDLER_ONCHAIN_EVENT_V1_RAW_ID,
|
69
|
-
handler: config
|
70
|
-
};
|
71
|
-
}
|
72
|
-
static validateCreate(options) {
|
73
|
-
const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
|
74
|
-
const onchainEventSchema = yup.object({
|
75
|
-
address: yup.string().required(),
|
76
|
-
event: yup.string().required(),
|
77
|
-
abi: yup.array().required(),
|
78
|
-
condition: yup.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
|
79
|
-
constant: yup.number().min(0).when('condition', requiredWhenConditional),
|
80
|
-
paramsIndex: yup.number().min(0).when('condition', requiredWhenConditional),
|
81
|
-
paramsDecimals: yup.number().min(0).when('condition', requiredWhenConditional),
|
82
|
-
});
|
83
|
-
return onchainEventSchema.validate(options);
|
84
|
-
}
|
85
|
-
static validateUpdate(options) {
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
class FunctionBuilder {
|
90
|
-
api;
|
91
|
-
data = [];
|
92
|
-
constructor(api) {
|
93
|
-
this.api = api;
|
94
|
-
}
|
95
|
-
static create(api) {
|
96
|
-
return new FunctionBuilder(api);
|
97
|
-
}
|
98
|
-
getAbiHash(abi) {
|
99
|
-
if (this.api === undefined)
|
100
|
-
throw new Error('FunctionBuilder must be initialised with api');
|
101
|
-
return this.api.getAbiHash(abi);
|
102
|
-
}
|
103
|
-
async addFunction(address, func, args, abi) {
|
104
|
-
const contract = new ethers.Contract(address, abi);
|
105
|
-
contract.interface.encodeFunctionData(func, args);
|
106
|
-
this.data.push({
|
107
|
-
address,
|
108
|
-
bytecode: contract.interface.encodeFunctionData(func, args),
|
109
|
-
abiHash: await this.getAbiHash(abi),
|
110
|
-
function: func,
|
111
|
-
});
|
112
|
-
return this;
|
113
|
-
}
|
114
|
-
get() {
|
115
|
-
return this.data;
|
116
|
-
}
|
117
|
-
static schema() {
|
118
|
-
return yup.array().of(yup.object({
|
119
|
-
address: yup.string().required(),
|
120
|
-
bytecode: yup.string().matches(/^(0x)?([0-9a-fA-F]{2})*$/).required(),
|
121
|
-
abiHash: yup.string().matches(/^[A-Fa-f0-9]{64}/).required(),
|
122
|
-
function: yup.string().required()
|
123
|
-
}));
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
class Filter {
|
128
|
-
static version() {
|
129
|
-
return 'v1';
|
130
|
-
}
|
131
|
-
static execute(record, groupsIn, version = 'v1') {
|
132
|
-
// Deep copy to so we can edit the object during recursion
|
133
|
-
const groups = JSON.parse(JSON.stringify(groupsIn));
|
134
|
-
let lhs = true;
|
135
|
-
for (const group of groups) {
|
136
|
-
const rhs = Filter.process(record, group.conditions);
|
137
|
-
lhs = Filter.execLogic(lhs, group.logic, rhs);
|
138
|
-
}
|
139
|
-
return lhs;
|
140
|
-
}
|
141
|
-
static process(record, conditions, lhs) {
|
142
|
-
const condition = conditions.shift();
|
143
|
-
if (!condition) {
|
144
|
-
if (lhs === undefined)
|
145
|
-
return true;
|
146
|
-
return lhs;
|
147
|
-
}
|
148
|
-
// lhs <logic> rhs
|
149
|
-
const rhs = Filter.execCondition(record, condition);
|
150
|
-
if (lhs === undefined || condition.logic === 'WHERE')
|
151
|
-
return Filter.process(record, conditions, rhs);
|
152
|
-
if (condition.logic === undefined)
|
153
|
-
throw new Error('Invalid filter. Condition must have logic value');
|
154
|
-
const next = Filter.execLogic(lhs, condition.logic, rhs);
|
155
|
-
return Filter.process(record, conditions, next);
|
156
|
-
}
|
157
|
-
static execLogic(lhs, logic, rhs) {
|
158
|
-
switch (logic) {
|
159
|
-
case 'AND': return lhs && rhs;
|
160
|
-
case 'OR': return lhs || rhs;
|
161
|
-
case 'XOR': return (lhs || rhs) && !(lhs && rhs);
|
162
|
-
case 'NAND': return !(lhs && rhs);
|
163
|
-
case 'NOR': return !(lhs && rhs);
|
164
|
-
case 'WHERE': return rhs;
|
165
|
-
default: throw new Error(`Invalid Filter. Bad logic value: ${logic}`);
|
166
|
-
}
|
167
|
-
}
|
168
|
-
static execCondition(record, condition) {
|
169
|
-
const data = record[condition.key];
|
170
|
-
if (condition.type === 'Number') {
|
171
|
-
if (typeof condition.constant !== 'number' || typeof data !== 'number')
|
172
|
-
throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
|
173
|
-
const n = data;
|
174
|
-
const constant = condition.constant;
|
175
|
-
switch (condition.operation) {
|
176
|
-
case 'eq': return n === constant;
|
177
|
-
case '!eq': return n !== constant;
|
178
|
-
case 'gt': return n > constant;
|
179
|
-
case 'gte': return n >= constant;
|
180
|
-
case 'lt': return n < constant;
|
181
|
-
case 'lte': return n <= constant;
|
182
|
-
default: throw new Error('Invalid Filter. Operation does not match type');
|
183
|
-
}
|
184
|
-
}
|
185
|
-
if (condition.type === 'String') {
|
186
|
-
if (typeof condition.constant !== 'string' || typeof data !== 'string')
|
187
|
-
throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
|
188
|
-
const str = data;
|
189
|
-
const constant = condition.constant;
|
190
|
-
switch (condition.operation) {
|
191
|
-
case 'contains': return str.includes(constant);
|
192
|
-
case '!contains': return !str.includes(constant);
|
193
|
-
case 'is': return str === constant;
|
194
|
-
case '!is': return str !== constant;
|
195
|
-
case 'isEmpty': return str === '';
|
196
|
-
case '!isEmpty': return str !== '';
|
197
|
-
default: throw new Error('Invalid Filter. Operation does not match type');
|
198
|
-
}
|
199
|
-
}
|
200
|
-
if (condition.type === 'Address') {
|
201
|
-
if (typeof condition.constant !== 'string' || typeof data !== 'string')
|
202
|
-
throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
|
203
|
-
const str = data;
|
204
|
-
const constant = condition.constant;
|
205
|
-
switch (condition.operation) {
|
206
|
-
case 'contains': return str.toLowerCase().includes(constant.toLowerCase());
|
207
|
-
case '!contains': return !str.toLowerCase().includes(constant.toLowerCase());
|
208
|
-
case 'is': return str.toLowerCase() === constant.toLowerCase();
|
209
|
-
case '!is': return str.toLowerCase() !== constant.toLowerCase();
|
210
|
-
case 'isEmpty': return str === '';
|
211
|
-
case '!isEmpty': return str !== '';
|
212
|
-
default: throw new Error('Invalid Filter. Operation does not match type');
|
213
|
-
}
|
214
|
-
}
|
215
|
-
throw new Error('Invalid Filter. Unknown Type');
|
216
|
-
}
|
217
|
-
}
|
218
|
-
class FilterBuilder {
|
219
|
-
groups = [];
|
220
|
-
constructor() {
|
221
|
-
this.newGroup('WHERE');
|
222
|
-
}
|
223
|
-
static version() {
|
224
|
-
return Filter.version();
|
225
|
-
}
|
226
|
-
static new() {
|
227
|
-
return new FilterBuilder();
|
228
|
-
}
|
229
|
-
newGroup(logic) {
|
230
|
-
if (this.groups.length !== 0 && logic === 'WHERE')
|
231
|
-
throw new Error('Only the first groups can start with "WHERE"');
|
232
|
-
this.groups.push({ logic: 'WHERE', conditions: [] });
|
233
|
-
return this;
|
234
|
-
}
|
235
|
-
addCondition(logic, key, type, operation, constant) {
|
236
|
-
const group = this.groups[this.groups.length - 1];
|
237
|
-
if (group.conditions.length === 0 && logic !== 'WHERE')
|
238
|
-
throw new Error('Logic for the first condition of a group must be "WHERE"');
|
239
|
-
if (group.conditions.length > 0 && logic === 'WHERE')
|
240
|
-
throw new Error('Only the first condition of a group can be "WHERE"');
|
241
|
-
group.conditions.push({
|
242
|
-
logic,
|
243
|
-
key,
|
244
|
-
type,
|
245
|
-
operation,
|
246
|
-
constant
|
247
|
-
});
|
248
|
-
return this;
|
249
|
-
}
|
250
|
-
finalise() {
|
251
|
-
for (const group of this.groups) {
|
252
|
-
if (group.conditions.length === 0)
|
253
|
-
throw new Error('Bad Filter. All Groups must have atleast one condition');
|
254
|
-
}
|
255
|
-
return this.groups;
|
256
|
-
}
|
257
|
-
static schema() {
|
258
|
-
const logic = yup.string().oneOf(['AND', 'OR', 'XOR', 'NAND', 'NOR', 'WHERE']).required();
|
259
|
-
const condition = yup.object({
|
260
|
-
logic,
|
261
|
-
key: yup.string().required(),
|
262
|
-
type: yup.string().oneOf(['String', 'Address', 'Number']).required(),
|
263
|
-
operation: yup.string().when('type', ([type], schema) => {
|
264
|
-
switch (type) {
|
265
|
-
case 'String': return schema.oneOf(['contains', '!contains', 'is', '!is', 'isEmpty', '!isEmpty']);
|
266
|
-
case 'Address': return schema.oneOf(['is', '!is', 'isEmpty', '!isEmpty']);
|
267
|
-
case 'Number': return schema.oneOf(['String', 'Address', 'Number']);
|
268
|
-
default: throw new Error('Invalid Filter Data Type');
|
269
|
-
}
|
270
|
-
}),
|
271
|
-
constant: yup.mixed().when('type', ([type]) => {
|
272
|
-
switch (type) {
|
273
|
-
case 'String': return yup.string();
|
274
|
-
case 'Address': return yup.string();
|
275
|
-
case 'Number': return yup.number();
|
276
|
-
default: throw new Error('Invalid Filter Data Type');
|
277
|
-
}
|
278
|
-
}),
|
279
|
-
});
|
280
|
-
return yup.array().of(yup.object({
|
281
|
-
logic,
|
282
|
-
conditions: yup.array().of(condition)
|
283
|
-
}));
|
284
|
-
}
|
285
|
-
}
|
286
|
-
|
287
|
-
const HANDLER_FUNCTION_CALL_V2_RAW_ID = 'handler_function_call_v2';
|
288
|
-
class HandlerFunctionCallV2 {
|
289
|
-
static SimpleToRaw(name, network, config) {
|
290
|
-
return {
|
291
|
-
alertType: 'event',
|
292
|
-
network,
|
293
|
-
nickname: name,
|
294
|
-
type: HANDLER_FUNCTION_CALL_V2_RAW_ID,
|
295
|
-
handler: config
|
296
|
-
};
|
297
|
-
}
|
298
|
-
static validateCreate(options) {
|
299
|
-
const timePeriodRegex = /^(\d+)([SMHD])$/i;
|
300
|
-
const onchainEventSchema = yup.object({
|
301
|
-
timeBase: yup.string().oneOf(['blocks', 'time']).required(),
|
302
|
-
// Blocks config
|
303
|
-
nBlocks: yup.number()
|
304
|
-
.min(10)
|
305
|
-
.when('timeBase', ([timeBase], schema) => timeBase === 'blocks' ? schema.required() : schema),
|
306
|
-
// Or Time config
|
307
|
-
timePeriod: yup.string()
|
308
|
-
.matches(timePeriodRegex, 'timePeriod must be of the format n[s|m|h|d], eg 10s for 10 seconds')
|
309
|
-
.when('timeBase', ([timeBase], schema) => timeBase === 'time' ? schema.required() : schema),
|
310
|
-
startTime: yup.number().default(0),
|
311
|
-
// Debouncing. Default is 0
|
312
|
-
debounceCount: yup.number(),
|
313
|
-
// Functions
|
314
|
-
functions: FunctionBuilder.schema(),
|
315
|
-
// Trigger
|
316
|
-
triggerOn: yup.string().oneOf(['always', 'filter']).default('always'),
|
317
|
-
latch: yup.boolean().default(false),
|
318
|
-
// Filter
|
319
|
-
filterVersion: yup.string().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
|
320
|
-
filter: FilterBuilder.schema().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
|
321
|
-
});
|
322
|
-
return onchainEventSchema.validate(options);
|
323
|
-
}
|
324
|
-
}
|
325
|
-
|
326
|
-
class Trigger {
|
327
|
-
static SimpleToRaw(name, id, network, config) {
|
328
|
-
switch (id) {
|
329
|
-
case 'PollFunctionV2': return HandlerFunctionCallV2.SimpleToRaw(name, network, config);
|
330
|
-
case 'PollFunctionV1': return HandlerFunctionCall.SimpleToRaw(name, network, config);
|
331
|
-
case 'OnchainEventV1': return HandlerOnchainEvent.SimpleToRaw(name, network, config);
|
332
|
-
default:
|
333
|
-
throw new Error('Invalid Trigger ID');
|
334
|
-
}
|
335
|
-
}
|
336
|
-
}
|
337
|
-
|
338
|
-
const toFunctionsUrl = (id) => {
|
339
|
-
return `https://${id}.functions.supabase.co/`;
|
340
|
-
};
|
341
|
-
const toApiUrl = (id) => {
|
342
|
-
return `https://${id}.supabase.co/`;
|
343
|
-
};
|
344
|
-
const PROD_PROJECT_ID = 'fdgtrxmmrtlokhgkvcjz';
|
345
|
-
// const API_URL = ''
|
346
|
-
const ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZkZ3RyeG1tcnRsb2toZ2t2Y2p6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NzMwODcwNzYsImV4cCI6MTk4ODY2MzA3Nn0.sAMxjlcJSSozBGr-LNcsudyxzUEM9e-UspMHHQLqLr4';
|
347
|
-
class DeNotifyClient {
|
348
|
-
url;
|
349
|
-
token;
|
350
|
-
headers = {};
|
351
|
-
constructor(url, token) {
|
352
|
-
this.url = url;
|
353
|
-
this.token = token;
|
354
|
-
this.headers = {
|
355
|
-
'Authorization': `Bearer ${token}`,
|
356
|
-
'Content-Type': 'application/json'
|
357
|
-
};
|
358
|
-
}
|
359
|
-
static async create(options) {
|
360
|
-
if (options.key) {
|
361
|
-
// TODO
|
362
|
-
throw new Error('Auth by key not yet supported');
|
363
|
-
}
|
364
|
-
else if (options.email && options.password) {
|
365
|
-
const url = options.projectId ? toApiUrl(options.projectId) : toApiUrl(PROD_PROJECT_ID);
|
366
|
-
const functionsUrl = options.projectId ? toFunctionsUrl(options.projectId) : toFunctionsUrl(PROD_PROJECT_ID);
|
367
|
-
const anonKey = options.anonKey ? options.anonKey : ANON_KEY;
|
368
|
-
const supabase = createClient(url, anonKey);
|
369
|
-
const { data: login, error } = await supabase.auth.signInWithPassword({
|
370
|
-
email: options.email,
|
371
|
-
password: options.password,
|
372
|
-
});
|
373
|
-
if (error)
|
374
|
-
throw error;
|
375
|
-
if (login.session?.access_token === undefined)
|
376
|
-
throw new Error('Access token not found');
|
377
|
-
return new DeNotifyClient(functionsUrl, login.session?.access_token);
|
378
|
-
}
|
379
|
-
else {
|
380
|
-
throw new Error('Authentication Required. DeNotify supports either username/password or API key authentication');
|
381
|
-
}
|
382
|
-
}
|
383
|
-
async alertHistory(id, pagination) {
|
384
|
-
const url = `alert-history`;
|
385
|
-
const params = pagination ? pagination : {};
|
386
|
-
if (id)
|
387
|
-
params.id = id;
|
388
|
-
if (Object.keys(params).length > 0) {
|
389
|
-
return await this.request('get', url, { params: pagination });
|
390
|
-
}
|
391
|
-
else {
|
392
|
-
return await this.request('get', url);
|
393
|
-
}
|
394
|
-
}
|
395
|
-
// TODO - Beutify the reponse
|
396
|
-
async getAlert(id) {
|
397
|
-
const alerts = await this.request('get', `alerts/${id}`);
|
398
|
-
return alerts[0];
|
399
|
-
}
|
400
|
-
async getAlerts() {
|
401
|
-
const alerts = await this.request('get', 'alerts');
|
402
|
-
return alerts;
|
403
|
-
}
|
404
|
-
async createAlert(config) {
|
405
|
-
const trigger = Trigger.SimpleToRaw(config.name, config.triggerId, config.network, config.trigger);
|
406
|
-
const notification = Notification.SimpleToRaw(config.notificationId, config.notification);
|
407
|
-
const alert = await this.request('post', `alerts`, { body: { trigger, notification } });
|
408
|
-
return alert;
|
409
|
-
}
|
410
|
-
async deleteAlert(id) {
|
411
|
-
const alerts = await this.request('delete', `alerts/${id}`);
|
412
|
-
return alerts;
|
413
|
-
}
|
414
|
-
async request(method, path, options = {}) {
|
415
|
-
const url = new URL(`${this.url}${path}`);
|
416
|
-
// append params
|
417
|
-
if (options.params) {
|
418
|
-
for (const param of Object.keys(options.params)) {
|
419
|
-
url.searchParams.append(param, options.params[param]);
|
420
|
-
}
|
421
|
-
}
|
422
|
-
const payload = {
|
423
|
-
method,
|
424
|
-
headers: this.headers
|
425
|
-
};
|
426
|
-
if (options.body)
|
427
|
-
payload.body = JSON.stringify(options.body);
|
428
|
-
const res = await fetch(url.toString(), payload);
|
429
|
-
if (!res.ok)
|
430
|
-
throw new Error(`unexpected response ${res.statusText}`);
|
431
|
-
return await res.json();
|
432
|
-
}
|
433
|
-
async getAbi(network, address) {
|
434
|
-
const ret = await this.request('get', `abi/${network}/${address}`);
|
435
|
-
return ret;
|
436
|
-
}
|
437
|
-
async getAbiHash(abi) {
|
438
|
-
const ret = await this.request('post', 'abi', { body: abi });
|
439
|
-
return ret.hash;
|
440
|
-
}
|
441
|
-
async setAlertName(triggerId, name) {
|
442
|
-
throw new Error('Not yet supported - Sorry!');
|
443
|
-
}
|
444
|
-
async enableAlert(triggerId) {
|
445
|
-
throw new Error('Not yet supported - Sorry!');
|
446
|
-
}
|
447
|
-
async disableAlert(triggerId) {
|
448
|
-
throw new Error('Not yet supported - Sorry!');
|
449
|
-
}
|
450
|
-
async updateNotification(triggerId) {
|
451
|
-
throw new Error('Not yet supported - Sorry!');
|
452
|
-
}
|
453
|
-
async updateTrigger(triggerId, update) {
|
454
|
-
// TODO - Input validation
|
455
|
-
const ret = await this.request('patch', `alerts/trigger-handler/${triggerId}`, { body: update });
|
456
|
-
return ret;
|
457
|
-
}
|
458
|
-
}
|
459
|
-
|
460
|
-
class AlertBuilder {
|
461
|
-
name;
|
462
|
-
network;
|
463
|
-
triggerId;
|
464
|
-
trigger;
|
465
|
-
notificationId;
|
466
|
-
notification;
|
467
|
-
constructor(name) {
|
468
|
-
this.name = name;
|
469
|
-
}
|
470
|
-
static create(name) {
|
471
|
-
return new AlertBuilder(name);
|
472
|
-
}
|
473
|
-
onNetwork(network) {
|
474
|
-
this.network = network;
|
475
|
-
return this;
|
476
|
-
}
|
477
|
-
/**
|
478
|
-
* Call withTrigger with one of the TriggerConfig types:
|
479
|
-
* PollFunctionV2 | PollFunctionV1 | OnchainEventV1
|
480
|
-
* @param id Simple ID
|
481
|
-
* @param options Desired trigger configuration
|
482
|
-
* @returns self for piping
|
483
|
-
*/
|
484
|
-
withTrigger(id, options) {
|
485
|
-
this.triggerId = id;
|
486
|
-
this.trigger = options;
|
487
|
-
return this;
|
488
|
-
}
|
489
|
-
withNotification(id, options) {
|
490
|
-
this.notificationId = id;
|
491
|
-
this.notification = options;
|
492
|
-
return this;
|
493
|
-
}
|
494
|
-
async validate() {
|
495
|
-
// Validate trigger
|
496
|
-
switch (this.triggerId) {
|
497
|
-
case 'OnchainEventV1': return HandlerOnchainEvent.validateCreate(this.trigger);
|
498
|
-
case 'PollFunctionV1': return HandlerFunctionCall.validateCreate(this.trigger);
|
499
|
-
case 'PollFunctionV2': return HandlerFunctionCallV2.validateCreate(this.trigger);
|
500
|
-
}
|
501
|
-
switch (this.notificationId) {
|
502
|
-
case 'Discord': return NotifyDiscordWebhook.validateCreate(this.notification);
|
503
|
-
}
|
504
|
-
}
|
505
|
-
async config() {
|
506
|
-
if (this.trigger === undefined || this.triggerId === undefined)
|
507
|
-
throw new Error('Trigger not configured');
|
508
|
-
if (this.notification === undefined || this.notificationId === undefined)
|
509
|
-
throw new Error('Notification not configured');
|
510
|
-
if (this.network === undefined)
|
511
|
-
throw new Error('Network not configured');
|
512
|
-
await this.validate();
|
513
|
-
return {
|
514
|
-
name: this.name,
|
515
|
-
network: this.network,
|
516
|
-
triggerId: this.triggerId,
|
517
|
-
trigger: this.trigger,
|
518
|
-
notificationId: this.notificationId,
|
519
|
-
notification: this.notification,
|
520
|
-
};
|
521
|
-
}
|
522
|
-
}
|
523
|
-
|
524
|
-
var index$1 = /*#__PURE__*/Object.freeze({
|
525
|
-
__proto__: null,
|
526
|
-
Trigger: Trigger,
|
527
|
-
HandlerFunctionCallV2: HandlerFunctionCallV2,
|
528
|
-
HandlerFunctionCall: HandlerFunctionCall,
|
529
|
-
HandlerOnchainEvent: HandlerOnchainEvent
|
530
|
-
});
|
531
|
-
|
532
|
-
var index = /*#__PURE__*/Object.freeze({
|
533
|
-
__proto__: null,
|
534
|
-
NOTIFY_DISCORD_WEBHOOK_RAW_ID: NOTIFY_DISCORD_WEBHOOK_RAW_ID,
|
535
|
-
NotifyDiscordWebhook: NotifyDiscordWebhook,
|
536
|
-
Notification: Notification
|
537
|
-
});
|
538
|
-
|
539
|
-
export { AlertBuilder, DeNotifyClient, FilterBuilder, FunctionBuilder, index as Notification, index$1 as Trigger };
|
@@ -1,21 +0,0 @@
|
|
1
|
-
import * as yup from 'yup';
|
2
|
-
export const NOTIFY_DISCORD_WEBHOOK_RAW_ID = 'notify_discord_webhook';
|
3
|
-
export class NotifyDiscordWebhook {
|
4
|
-
static SimpleToRaw(config) {
|
5
|
-
return {
|
6
|
-
name: '',
|
7
|
-
notify_type: NOTIFY_DISCORD_WEBHOOK_RAW_ID,
|
8
|
-
notify: config
|
9
|
-
};
|
10
|
-
}
|
11
|
-
static validateCreate(options) {
|
12
|
-
const urlRegex = /^(https?|ftp):\/\/(-\.)?([^\s/?\.#]+\.?)+([^\s\.?#]+)?(\?\S*)?$/;
|
13
|
-
const schema = yup.object({
|
14
|
-
url: yup.string().matches(urlRegex, 'url is not a valid url').required(),
|
15
|
-
username: yup.string(),
|
16
|
-
avatar_url: yup.string().matches(urlRegex, 'url is not a valid url'),
|
17
|
-
message: yup.string().required()
|
18
|
-
});
|
19
|
-
return schema.validate(options);
|
20
|
-
}
|
21
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import * as yup from 'yup';
|
2
|
-
const HANDLER_FUNCTION_CALL_V1_RAW_ID = 'handler_function_call';
|
3
|
-
export class HandlerFunctionCall {
|
4
|
-
static SimpleToRaw(name, network, config) {
|
5
|
-
return {
|
6
|
-
alertType: 'event',
|
7
|
-
network,
|
8
|
-
nickname: name,
|
9
|
-
type: HANDLER_FUNCTION_CALL_V1_RAW_ID,
|
10
|
-
handler: config
|
11
|
-
};
|
12
|
-
}
|
13
|
-
static validateCreate(options) {
|
14
|
-
const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
|
15
|
-
const schema = yup.object({
|
16
|
-
address: yup.string().required(),
|
17
|
-
abi: yup.array().required(),
|
18
|
-
nBlocks: yup.number().min(10),
|
19
|
-
condition: yup.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
|
20
|
-
constant: yup.number().min(0).when('condition', requiredWhenConditional),
|
21
|
-
responseArgIndex: yup.number().min(0).when('condition', requiredWhenConditional),
|
22
|
-
responseArgDecimals: yup.number().min(0).when('condition', requiredWhenConditional),
|
23
|
-
});
|
24
|
-
return schema.validate(options);
|
25
|
-
}
|
26
|
-
}
|
@@ -1,41 +0,0 @@
|
|
1
|
-
import { FunctionBuilder } from "../functionbuilder.js";
|
2
|
-
import { FilterBuilder } from "../util/filter.js";
|
3
|
-
import * as yup from 'yup';
|
4
|
-
const HANDLER_FUNCTION_CALL_V2_RAW_ID = 'handler_function_call_v2';
|
5
|
-
export class HandlerFunctionCallV2 {
|
6
|
-
static SimpleToRaw(name, network, config) {
|
7
|
-
return {
|
8
|
-
alertType: 'event',
|
9
|
-
network,
|
10
|
-
nickname: name,
|
11
|
-
type: HANDLER_FUNCTION_CALL_V2_RAW_ID,
|
12
|
-
handler: config
|
13
|
-
};
|
14
|
-
}
|
15
|
-
static validateCreate(options) {
|
16
|
-
const timePeriodRegex = /^(\d+)([SMHD])$/i;
|
17
|
-
const onchainEventSchema = yup.object({
|
18
|
-
timeBase: yup.string().oneOf(['blocks', 'time']).required(),
|
19
|
-
// Blocks config
|
20
|
-
nBlocks: yup.number()
|
21
|
-
.min(10)
|
22
|
-
.when('timeBase', ([timeBase], schema) => timeBase === 'blocks' ? schema.required() : schema),
|
23
|
-
// Or Time config
|
24
|
-
timePeriod: yup.string()
|
25
|
-
.matches(timePeriodRegex, 'timePeriod must be of the format n[s|m|h|d], eg 10s for 10 seconds')
|
26
|
-
.when('timeBase', ([timeBase], schema) => timeBase === 'time' ? schema.required() : schema),
|
27
|
-
startTime: yup.number().default(0),
|
28
|
-
// Debouncing. Default is 0
|
29
|
-
debounceCount: yup.number(),
|
30
|
-
// Functions
|
31
|
-
functions: FunctionBuilder.schema(),
|
32
|
-
// Trigger
|
33
|
-
triggerOn: yup.string().oneOf(['always', 'filter']).default('always'),
|
34
|
-
latch: yup.boolean().default(false),
|
35
|
-
// Filter
|
36
|
-
filterVersion: yup.string().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
|
37
|
-
filter: FilterBuilder.schema().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
|
38
|
-
});
|
39
|
-
return onchainEventSchema.validate(options);
|
40
|
-
}
|
41
|
-
}
|
@@ -1,28 +0,0 @@
|
|
1
|
-
import * as yup from 'yup';
|
2
|
-
const HANDLER_ONCHAIN_EVENT_V1_RAW_ID = 'handler_onchain_event';
|
3
|
-
export class HandlerOnchainEvent {
|
4
|
-
static SimpleToRaw(name, network, config) {
|
5
|
-
return {
|
6
|
-
alertType: 'event',
|
7
|
-
network,
|
8
|
-
nickname: name,
|
9
|
-
type: HANDLER_ONCHAIN_EVENT_V1_RAW_ID,
|
10
|
-
handler: config
|
11
|
-
};
|
12
|
-
}
|
13
|
-
static validateCreate(options) {
|
14
|
-
const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
|
15
|
-
const onchainEventSchema = yup.object({
|
16
|
-
address: yup.string().required(),
|
17
|
-
event: yup.string().required(),
|
18
|
-
abi: yup.array().required(),
|
19
|
-
condition: yup.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
|
20
|
-
constant: yup.number().min(0).when('condition', requiredWhenConditional),
|
21
|
-
paramsIndex: yup.number().min(0).when('condition', requiredWhenConditional),
|
22
|
-
paramsDecimals: yup.number().min(0).when('condition', requiredWhenConditional),
|
23
|
-
});
|
24
|
-
return onchainEventSchema.validate(options);
|
25
|
-
}
|
26
|
-
static validateUpdate(options) {
|
27
|
-
}
|
28
|
-
}
|
package/dist/triggers/index.js
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
import { Trigger } from "./trigger.js";
|
2
|
-
import { HandlerFunctionCallV2 } from "./handler_function_call_v2.js";
|
3
|
-
import { HandlerFunctionCall } from "./handler_function_call.js";
|
4
|
-
import { HandlerOnchainEvent } from "./handler_onchain_event.js";
|
5
|
-
export { Trigger, HandlerFunctionCallV2, HandlerFunctionCall, HandlerOnchainEvent, };
|