Skip to content

Commit

Permalink
fixup! feat: add optional CBOR transaction representation to SUBSCRIB…
Browse files Browse the repository at this point in the history
…E_ADDRESS
  • Loading branch information
iccicci committed Oct 25, 2024
1 parent cca73a5 commit 3d86ac8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { getTransactionsWithDetails } from './utils/transaction.js';
import { TxNotification } from './types/response.js';
import { EMIT_MAX_MISSED_BLOCKS } from './constants/config.js';
import { logger } from './utils/logger.js';
import { SubscribeAddressOptions } from './types/message.js';

interface EmitBlockOptions {
fetchTimeoutMs?: number;
Expand All @@ -17,7 +16,7 @@ interface EmitBlockOptions {

export interface SubscribedAddress {
address: string;
options?: SubscribeAddressOptions;
cbor?: boolean;
}

// eslint-disable-next-line unicorn/prefer-event-target
Expand Down Expand Up @@ -131,9 +130,9 @@ export const onBlock = async (
const txsCbor: Record<string, boolean | undefined> = {};

for (const address of affectedAddresses) {
const { cbor } =
subscribedAddresses.find(subscription => subscription.address === address.address)!
.options || {};
const { cbor } = subscribedAddresses.find(
subscription => subscription.address === address.address,
)!;

for (const tx of address.transactions) {
txsCbor[tx.tx_hash] ||= cbor;
Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ wss.on('connection', async (ws: Server.Ws) => {
}

case MESSAGES.SUBSCRIBE_ADDRESS: {
const { addresses, options } = { options: {}, ...data.params };
const { addresses, cbor } = { ...data.params };

if (addresses && addresses.length > 0) {
for (const address of addresses) {
Expand All @@ -314,9 +314,9 @@ wss.on('connection', async (ws: Server.Ws) => {
);

// Subscribe to new address...
if (subscriptionIndex === -1) addressesSubscribed[clientId].push({ address, options });
// ... or update the options
else addressesSubscribed[clientId][subscriptionIndex].options = options;
if (subscriptionIndex === -1) addressesSubscribed[clientId].push({ address, cbor });
// ... or update the cbor option
else addressesSubscribed[clientId][subscriptionIndex].cbor ||= cbor;
}

const activeAddressSubIndex = activeSubscriptions[clientId].findIndex(
Expand Down
6 changes: 1 addition & 5 deletions src/types/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export type Details = 'basic' | 'tokens' | 'tokenBalances' | 'txids' | 'txs';

export interface SubscribeAddressOptions {
cbor?: boolean;
}

export type Messages =
| {
id: number;
Expand Down Expand Up @@ -68,7 +64,7 @@ export type Messages =
command: 'SUBSCRIBE_ADDRESS';
params: {
addresses: string[];
options?: SubscribeAddressOptions;
cbor?: boolean;
};
}
| {
Expand Down

0 comments on commit 3d86ac8

Please sign in to comment.