@glowlabs-org/events-sdk 0.1.1 → 1.0.1

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.
Files changed (39) hide show
  1. package/README.md +311 -151
  2. package/dist/admin.d.ts +7 -6
  3. package/dist/admin.js +6 -6
  4. package/dist/base-event.d.ts +65 -0
  5. package/dist/base-event.js +22 -0
  6. package/dist/emitter.d.ts +17 -0
  7. package/dist/emitter.js +67 -0
  8. package/dist/event-registry.d.ts +2 -0
  9. package/dist/event-registry.js +27 -0
  10. package/dist/index.d.ts +2 -5
  11. package/dist/index.js +2 -5
  12. package/dist/listener.d.ts +14 -0
  13. package/dist/listener.js +88 -0
  14. package/dist/schemas/application-created.v1.d.ts +25 -0
  15. package/dist/schemas/application-created.v1.js +12 -0
  16. package/dist/schemas/audit-pfees-paid.v1.d.ts +15 -0
  17. package/dist/schemas/audit-pfees-paid.v1.js +14 -0
  18. package/dist/schemas/audit-pfees-paid.v2.d.ts +15 -0
  19. package/dist/schemas/audit-pfees-paid.v2.js +14 -0
  20. package/dist/schemas/audit-pushed.v1.d.ts +15 -0
  21. package/dist/schemas/audit-pushed.v1.js +13 -0
  22. package/dist/schemas/audit-slashed.v1.d.ts +12 -0
  23. package/dist/schemas/audit-slashed.v1.js +10 -0
  24. package/dist/types.d.ts +32 -21
  25. package/dist/utils.d.ts +20 -0
  26. package/dist/utils.js +45 -0
  27. package/dist/zones.d.ts +7 -0
  28. package/dist/zones.js +8 -0
  29. package/package.json +1 -1
  30. package/dist/consumer.d.ts +0 -14
  31. package/dist/consumer.js +0 -84
  32. package/dist/glow-listener.d.ts +0 -31
  33. package/dist/glow-listener.js +0 -74
  34. package/dist/producer.d.ts +0 -8
  35. package/dist/producer.js +0 -46
  36. package/dist/schemas/auditPushed.d.ts +0 -69
  37. package/dist/schemas/auditPushed.js +0 -42
  38. package/dist/typed-emitter.d.ts +0 -7
  39. package/dist/typed-emitter.js +0 -17
@@ -1,14 +0,0 @@
1
- import { AuditPushedDataZ } from "./schemas/auditPushed";
2
- import { z } from "zod";
3
- export declare function createAuditPushedConsumer({ username, password, onEvent, skipAssertExchange, queueName, skipAssertQueue, exchange, }: {
4
- username: string;
5
- password: string;
6
- onEvent: (evt: z.infer<typeof AuditPushedDataZ>) => void;
7
- skipAssertExchange?: boolean;
8
- queueName?: string;
9
- skipAssertQueue?: boolean;
10
- exchange?: string;
11
- }): {
12
- start: () => Promise<void>;
13
- stop: () => Promise<void>;
14
- };
package/dist/consumer.js DELETED
@@ -1,84 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createAuditPushedConsumer = createAuditPushedConsumer;
7
- const amqplib_1 = __importDefault(require("amqplib"));
8
- const auditPushed_1 = require("./schemas/auditPushed");
9
- const EXCHANGE = "glow.audit.v1.exchange";
10
- function createAuditPushedConsumer({ username, password, onEvent, skipAssertExchange = false, queueName, skipAssertQueue = false, exchange = "glow.audit.v1.exchange", }) {
11
- // amqplib types are not always compatible with runtime objects, so we use 'as any' as a workaround
12
- let connection = null;
13
- let channel = null;
14
- let internalQueueName;
15
- let consumerTag = null;
16
- function buildAmqpUrl() {
17
- const url = new URL(`amqp://${username}:${password}@turntable.proxy.rlwy.net:50784`);
18
- return url.toString();
19
- }
20
- async function connect() {
21
- if (!connection) {
22
- connection = (await amqplib_1.default.connect(buildAmqpUrl()));
23
- channel = (await connection.createChannel());
24
- }
25
- if (channel) {
26
- // Strict read-only: skip all asserts/binds, just use the provided queueName
27
- if (skipAssertQueue) {
28
- if (!queueName) {
29
- throw new Error("[Glow SDK] In strict read-only mode (skipAssertQueue: true), you must provide a queueName.");
30
- }
31
- internalQueueName = queueName;
32
- // Do not assert or bind anything
33
- return;
34
- }
35
- // Default pattern: assert exchange, create/bind queue
36
- if (!skipAssertExchange) {
37
- await channel.assertExchange(exchange, "fanout", { durable: true });
38
- }
39
- const q = await channel.assertQueue(queueName !== null && queueName !== void 0 ? queueName : "", {
40
- exclusive: !queueName,
41
- });
42
- internalQueueName = q.queue;
43
- await channel.bindQueue(internalQueueName, exchange, "");
44
- }
45
- }
46
- async function start() {
47
- try {
48
- await connect();
49
- if (!internalQueueName)
50
- throw new Error("Queue not initialized");
51
- const { consumerTag: tag } = await channel.consume(internalQueueName, (msg) => {
52
- if (msg) {
53
- try {
54
- const decoded = JSON.parse(msg.content.toString());
55
- onEvent(auditPushed_1.AuditPushedDataZ.parse(decoded));
56
- channel.ack(msg);
57
- }
58
- catch (error) {
59
- console.error("Failed to process audit pushed event:", error);
60
- channel.nack(msg, false, false);
61
- }
62
- }
63
- }, { noAck: false });
64
- consumerTag = tag;
65
- }
66
- catch (error) {
67
- console.error("Failed to start audit pushed consumer:", error);
68
- throw error;
69
- }
70
- }
71
- async function stop() {
72
- if (channel && consumerTag && internalQueueName) {
73
- await channel.cancel(consumerTag);
74
- consumerTag = null;
75
- }
76
- if (connection) {
77
- await connection.close();
78
- connection = null;
79
- channel = null;
80
- internalQueueName = undefined;
81
- }
82
- }
83
- return { start, stop };
84
- }
@@ -1,31 +0,0 @@
1
- import type { GlowListener } from "./types";
2
- /**
3
- * Options for creating a GlowListener instance.
4
- *
5
- * - Use admin credentials to emit events and create/bind queues/exchanges.
6
- * - Use listener credentials for read-only event consumption (with pre-created queues).
7
- * - Specify a custom exchange for multi-exchange support.
8
- */
9
- interface GlowListenerOptions {
10
- /** Username for RabbitMQ authentication (admin or listener) */
11
- username: string;
12
- /** Password for RabbitMQ authentication */
13
- password: string;
14
- /**
15
- * The pre-created queue to use for the listener. Must be set up by an admin.
16
- */
17
- queueName: string;
18
- /**
19
- * The pre-created exchange to use. Must be set up by an admin. Defaults to 'glow.audit.v1.exchange'.
20
- */
21
- exchange?: string;
22
- }
23
- /**
24
- * Create a GlowListener instance for consuming and emitting typed events on the Glow platform.
25
- *
26
- * - Use admin credentials to emit events and manage queues/exchanges.
27
- * - Use listener credentials for read-only event consumption.
28
- * - Supports specifying a custom exchange for multi-exchange scenarios.
29
- */
30
- export declare function createGlowListener({ username, password, queueName, exchange, }: GlowListenerOptions): Promise<GlowListener>;
31
- export type { GlowListener } from "./types";
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createGlowListener = createGlowListener;
4
- const typed_emitter_1 = require("./typed-emitter");
5
- const consumer_1 = require("./consumer");
6
- const producer_1 = require("./producer");
7
- function validateName(name, type) {
8
- // Require at least two segments separated by dots, only alphanumerics, dashes, underscores, and dots
9
- const pattern = /^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
10
- if (!pattern.test(name)) {
11
- throw new Error(`${type} name '${name}' is invalid. Must be dot-separated (e.g., 'glow.audit.v1.exchange').`);
12
- }
13
- }
14
- /**
15
- * Create a GlowListener instance for consuming and emitting typed events on the Glow platform.
16
- *
17
- * - Use admin credentials to emit events and manage queues/exchanges.
18
- * - Use listener credentials for read-only event consumption.
19
- * - Supports specifying a custom exchange for multi-exchange scenarios.
20
- */
21
- async function createGlowListener({ username, password, queueName, exchange = "glow.audit.v1.exchange", }) {
22
- validateName(queueName, "queue");
23
- validateName(exchange, "exchange");
24
- const emitter = (0, typed_emitter_1.createTypedEmitter)();
25
- // RabbitMQ consumer instance
26
- const consumer = (0, consumer_1.createAuditPushedConsumer)({
27
- username,
28
- password,
29
- onEvent: (event) => {
30
- emitter.emit("AuditPushed", event);
31
- },
32
- skipAssertExchange: true,
33
- queueName,
34
- skipAssertQueue: true,
35
- exchange,
36
- });
37
- // RabbitMQ producer instance (only if emit permission)
38
- const producer = (0, producer_1.createAuditPushedProducer)({ username, password, exchange });
39
- function emitAuditSlashed(id) {
40
- emitter.emit("AuditSlashed", id); // Local event only
41
- }
42
- async function emitAuditPushed(data) {
43
- try {
44
- await producer.emit(data);
45
- }
46
- catch (error) {
47
- throw error;
48
- }
49
- }
50
- async function startListener() {
51
- try {
52
- await consumer.start();
53
- }
54
- catch (error) {
55
- throw error;
56
- }
57
- }
58
- async function stopListener() {
59
- try {
60
- await consumer.stop();
61
- }
62
- catch (error) {
63
- throw error;
64
- }
65
- }
66
- return {
67
- on: emitter.on,
68
- off: emitter.off,
69
- emitAuditPushed,
70
- emitAuditSlashed,
71
- startListener,
72
- stopListener,
73
- };
74
- }
@@ -1,8 +0,0 @@
1
- export declare function createAuditPushedProducer({ username, password, exchange, }: {
2
- username: string;
3
- password: string;
4
- exchange?: string;
5
- }): {
6
- emit: (data: unknown) => Promise<void>;
7
- disconnect: () => Promise<void>;
8
- };
package/dist/producer.js DELETED
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createAuditPushedProducer = createAuditPushedProducer;
7
- const amqplib_1 = __importDefault(require("amqplib"));
8
- const auditPushed_1 = require("./schemas/auditPushed");
9
- function createAuditPushedProducer({ username, password, exchange = "glow.audit.v1.exchange", }) {
10
- // amqplib types are not always compatible with runtime objects, so we use 'as any' as a workaround
11
- let connection = null;
12
- let channel = null;
13
- function buildAmqpUrl() {
14
- const url = new URL(`amqp://${username}:${password}@turntable.proxy.rlwy.net:50784`);
15
- return url.toString();
16
- }
17
- async function connect() {
18
- if (!connection) {
19
- connection = (await amqplib_1.default.connect(buildAmqpUrl()));
20
- channel = (await connection.createChannel());
21
- }
22
- if (channel) {
23
- await channel.assertExchange(exchange, "fanout", { durable: true });
24
- }
25
- }
26
- async function emit(data) {
27
- try {
28
- const payload = auditPushed_1.AuditPushedDataZ.parse(data);
29
- await connect();
30
- channel.publish(exchange, "", // routingKey is ignored for fanout
31
- Buffer.from(JSON.stringify(payload)), { persistent: true });
32
- }
33
- catch (error) {
34
- console.error("Failed to emit audit pushed event:", error);
35
- throw error;
36
- }
37
- }
38
- async function disconnect() {
39
- if (connection) {
40
- await connection.close();
41
- connection = null;
42
- channel = null;
43
- }
44
- }
45
- return { emit, disconnect };
46
- }
@@ -1,69 +0,0 @@
1
- import { z } from "zod";
2
- export declare const AuditPushedDataZ: z.ZodObject<{
3
- farmId: z.ZodString;
4
- protocolFeeUSDPrice_12Decimals: z.ZodString;
5
- tokenPrices: z.ZodArray<z.ZodObject<{
6
- token: z.ZodString;
7
- priceWAD_usd12Decimals: z.ZodString;
8
- }, "strip", z.ZodTypeAny, {
9
- token: string;
10
- priceWAD_usd12Decimals: string;
11
- }, {
12
- token: string;
13
- priceWAD_usd12Decimals: string;
14
- }>, "many">;
15
- expectedProduction_12Decimals: z.ZodString;
16
- glowRewardSplits: z.ZodArray<z.ZodObject<{
17
- receiver: z.ZodString;
18
- percent: z.ZodNumber;
19
- }, "strip", z.ZodTypeAny, {
20
- receiver: string;
21
- percent: number;
22
- }, {
23
- receiver: string;
24
- percent: number;
25
- }>, "many">;
26
- cashRewardSplits: z.ZodArray<z.ZodObject<{
27
- receiver: z.ZodString;
28
- percent: z.ZodNumber;
29
- }, "strip", z.ZodTypeAny, {
30
- receiver: string;
31
- percent: number;
32
- }, {
33
- receiver: string;
34
- percent: number;
35
- }>, "many">;
36
- }, "strict", z.ZodTypeAny, {
37
- farmId: string;
38
- protocolFeeUSDPrice_12Decimals: string;
39
- tokenPrices: {
40
- token: string;
41
- priceWAD_usd12Decimals: string;
42
- }[];
43
- expectedProduction_12Decimals: string;
44
- glowRewardSplits: {
45
- receiver: string;
46
- percent: number;
47
- }[];
48
- cashRewardSplits: {
49
- receiver: string;
50
- percent: number;
51
- }[];
52
- }, {
53
- farmId: string;
54
- protocolFeeUSDPrice_12Decimals: string;
55
- tokenPrices: {
56
- token: string;
57
- priceWAD_usd12Decimals: string;
58
- }[];
59
- expectedProduction_12Decimals: string;
60
- glowRewardSplits: {
61
- receiver: string;
62
- percent: number;
63
- }[];
64
- cashRewardSplits: {
65
- receiver: string;
66
- percent: number;
67
- }[];
68
- }>;
69
- export type AuditPushedData = z.infer<typeof AuditPushedDataZ>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuditPushedDataZ = void 0;
4
- const zod_1 = require("zod");
5
- /* ────────────────────────────────────────────────────────── *
6
- * Helper regexes for on‑chain primitives *
7
- * ────────────────────────────────────────────────────────── */
8
- const hexBytes32 = /^0x[0-9a-fA-F]{64}$/;
9
- const ethAddress = /^0x[0-9a-fA-F]{40}$/;
10
- const uint256 = /^[0-9]+$/; // unsigned big‑int as decimal string
11
- /* ────────────────────────────────────────────────────────── *
12
- * AuditPushed data payload *
13
- * ────────────────────────────────────────────────────────── */
14
- exports.AuditPushedDataZ = zod_1.z
15
- .object({
16
- farmId: zod_1.z.string().regex(hexBytes32, "bytes32 hex string"),
17
- protocolFeeUSDPrice_12Decimals: zod_1.z
18
- .string()
19
- .regex(uint256, "uint256 (decimal) − 12 implied decimals"),
20
- tokenPrices: zod_1.z.array(zod_1.z.object({
21
- token: zod_1.z.string().regex(ethAddress, "ERC‑20 address"),
22
- priceWAD_usd12Decimals: zod_1.z.string().regex(uint256),
23
- })),
24
- // might be removed
25
- // payers: z.array(
26
- // z.object({
27
- // payer: z.string().regex(ethAddress, "payer address"),
28
- // amountToPay_USD12Decimals: z.string().regex(uint256),
29
- // })
30
- // ),
31
- expectedProduction_12Decimals: zod_1.z.string().regex(uint256),
32
- glowRewardSplits: zod_1.z.array(zod_1.z.object({
33
- receiver: zod_1.z.string().regex(ethAddress),
34
- percent: zod_1.z.number().min(0).max(100),
35
- })),
36
- cashRewardSplits: zod_1.z.array(zod_1.z.object({
37
- receiver: zod_1.z.string().regex(ethAddress),
38
- percent: zod_1.z.number().min(0).max(100),
39
- })),
40
- })
41
- .strict()
42
- .describe("Glow AuditPushed event payload");
@@ -1,7 +0,0 @@
1
- type Listener<T extends any[]> = (...args: T) => void;
2
- export declare function createTypedEmitter<TEvents extends Record<string, any[]>>(): {
3
- on: <K extends keyof TEvents>(event: K, listener: Listener<TEvents[K]>) => () => void;
4
- off: <K extends keyof TEvents>(event: K, listener: Listener<TEvents[K]>) => void;
5
- emit: <K extends keyof TEvents>(event: K, ...args: TEvents[K]) => void;
6
- };
7
- export {};
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTypedEmitter = createTypedEmitter;
4
- function createTypedEmitter() {
5
- const listeners = {};
6
- function on(event, listener) {
7
- (listeners[event] || (listeners[event] = [])).push(listener);
8
- return () => off(event, listener);
9
- }
10
- function off(event, listener) {
11
- listeners[event] = (listeners[event] || []).filter((l) => l !== listener);
12
- }
13
- function emit(event, ...args) {
14
- (listeners[event] || []).forEach((listener) => listener(...args));
15
- }
16
- return { on, off, emit };
17
- }