@glowlabs-org/events-sdk 0.1.0 → 1.0.0

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,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
- }