Skip to content

Commit

Permalink
Merge pull request #34 from algorandfoundation/multiple-filters
Browse files Browse the repository at this point in the history
feat: Multiple filters
  • Loading branch information
robdmoore authored Mar 20, 2024
2 parents c573099 + 83c3728 commit 70d34f5
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 80 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ subscriber.on('usdc', (transfer) => {
subscriber.start()
```

## Roadmap

- Subscribe to contract events ([ARC-28](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0028.md))
- Multiple filters
- Dynamic filters (e.g. subscribe to axfer's for assets that you subscribe to the creation of)
- GraphQL example ideally with subscriptions

## Getting started

To try examples in this repository:
Expand Down
27 changes: 26 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ There is a core type that is used to specify the filters [`TransactionFilter`](s
```typescript
const subscriber = new AlgorandSubscriber({filter: {/* Filter properties */}, ...}, ...)
// or:
getSubscribedTransactions(filter: {/* Filter properties */, ...}, ...)
getSubscribedTransactions({filter: {/* Filter properties */, ...}, ... }, ...)
```

Currently this allows you filter based on any combination (AND logic) of:
Expand Down Expand Up @@ -184,6 +184,16 @@ Currently this allows you filter based on any combination (AND logic) of:
- Algo transfers (pay transactions)
- Amount transferred (min and/or max) e.g. `{ type: TransactionType.pay, minAmount: 1, maxAmount: 100 }`

You can also supply multiple, named filters via the [`NamedTransactionFilter`](subscriptions.md#namedtransactionfilter) type:

```typescript
const subscriber = new AlgorandSubscriber({filter: [{name: 'filter1Name', filter: { /* Filter properties */ }}, {name: 'filter2Name', filter: { /* Filter properties */ }}], ...}, ...)
// or:
getSubscribedTransactions({filter: [{name: 'filter1Name', filter: { /* Filter properties */ }}, {name: 'filter2Name', filter: { /* Filter properties */ }}], ...}, ...)
```

When subscribed transactions are returned each transaction will have a `filtersMatched` property that will have an array of any filter(s) that caused that transaction to be returned.

### ARC-28 event subscription and reads

You can [subscribe to ARC-28 events](#extensive-subscription-filtering) for a smart contract, similar to how you can [subscribe to events in Ethereum](https://docs.web3js.org/guides/events_subscriptions/).
Expand Down Expand Up @@ -310,6 +320,21 @@ Any [filter](#extensive-subscription-filtering) you apply will be seamlessly tra
To see this in action, you can run the Data History Museum example in this repository against MainNet and see it sync millions of rounds in seconds.
The indexer catchup isn't magic - if the filter you are trying to catch up with generates an enormous number of transactions (e.g. hundreds of thousands or millions) then it will run very slowly and has the potential for running out of compute and memory time depending on what the constraints are in the deployment environment you are running in. To understand how the indexer behaviour works to know if you are likely to generate a lot of transactions it's worth understanding the architecture of the indexer catchup.
Indexer catchup runs in two stages:
1. **Pre-filtering**: Any filters that can be translated to the [indexer search transactions endpoint](https://developer.algorand.org/docs/rest-apis/indexer/#get-v2transactions). This query is then run between the rounds that need to be synced and paginated in the max number of results (1000) at a time until all of the transactions are retrieved. This ensures we get round-based transactional consistency. This is the filter that can easily explode out though and take a long time when using indexer catchup. For avoidance of doubt, the following filters are the ones that are converted to a pre-filter:
- `sender`
- `receiver`
- `type`
- `notePrefix`
- `appId`
- `assetId`
- `minAmount`
- `maxAmount`
2. **Post-filtering**: All remaining filters are then applied in-memory to the resulting list of transactions that are returned from the pre-filter before being returned as subscribed transactions.
## Entry points
There are two entry points into the subscriber functionality. The lower level [`getSubscribedTransactions`](./subscriptions.md) method that contains the raw subscription logic for a single "poll", and the [`AlgorandSubscriber`](./subscriber.md) class that provides a higher level interface that is easier to use and takes care of a lot more orchestration logic for you (particularly around the ability to continuously poll).
Expand Down
4 changes: 2 additions & 2 deletions docs/code/classes/index.AlgorandSubscriber.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ The subscriber so `on`/`onBatch` calls can be chained

#### Defined in

[subscriber.ts:152](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L152)
[subscriber.ts:156](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L156)

___

Expand Down Expand Up @@ -192,7 +192,7 @@ The subscriber so `on`/`onBatch` calls can be chained

#### Defined in

[subscriber.ts:169](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L169)
[subscriber.ts:173](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L173)

___

Expand Down
38 changes: 38 additions & 0 deletions docs/code/interfaces/types_subscription.NamedTransactionFilter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[@algorandfoundation/algokit-subscriber](../README.md) / [types/subscription](../modules/types_subscription.md) / NamedTransactionFilter

# Interface: NamedTransactionFilter

[types/subscription](../modules/types_subscription.md).NamedTransactionFilter

Specify a named filter to apply to find transactions of interest.

## Table of contents

### Properties

- [filter](types_subscription.NamedTransactionFilter.md#filter)
- [name](types_subscription.NamedTransactionFilter.md#name)

## Properties

### filter

**filter**: [`TransactionFilter`](types_subscription.TransactionFilter.md)

The filter itself.

#### Defined in

[types/subscription.ts:156](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L156)

___

### name

**name**: `string`

The name to give the filter.

#### Defined in

[types/subscription.ts:154](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L154)
14 changes: 7 additions & 7 deletions docs/code/interfaces/types_subscription.SubscriptionConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Any ARC-28 event definitions to process from app call logs

#### Defined in

[types/subscription.ts:189](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L189)
[types/subscription.ts:227](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L227)

___

Expand All @@ -40,7 +40,7 @@ The set of events to subscribe to / emit

#### Defined in

[types/subscription.ts:191](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L191)
[types/subscription.ts:229](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L229)

___

Expand All @@ -52,7 +52,7 @@ The frequency to poll for new blocks in seconds; defaults to 1s

#### Defined in

[types/subscription.ts:183](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L183)
[types/subscription.ts:221](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L221)

___

Expand All @@ -64,7 +64,7 @@ The maximum number of rounds to sync at a time; defaults to 500

#### Defined in

[types/subscription.ts:187](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L187)
[types/subscription.ts:225](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L225)

___

Expand All @@ -85,7 +85,7 @@ The behaviour when the number of rounds to sync is greater than `maxRoundsToSync

#### Defined in

[types/subscription.ts:203](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L203)
[types/subscription.ts:241](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L241)

___

Expand All @@ -97,7 +97,7 @@ Whether to wait via algod `/status/wait-for-block-after` endpoint when at the ti

#### Defined in

[types/subscription.ts:185](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L185)
[types/subscription.ts:223](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L223)

___

Expand All @@ -117,4 +117,4 @@ its position in the chain

#### Defined in

[types/subscription.ts:206](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L206)
[types/subscription.ts:244](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L244)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Name / identifier to uniquely describe the event

#### Defined in

[types/subscription.ts:217](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L217)
[types/subscription.ts:255](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L255)

___

Expand All @@ -42,7 +42,7 @@ The transaction filter that determines if the event has occurred

#### Defined in

[types/subscription.ts:219](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L219)
[types/subscription.ts:257](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L257)

___

Expand Down Expand Up @@ -70,4 +70,4 @@ If not specified, then the event will receive a `SubscribedTransaction`.

#### Defined in

[types/subscription.ts:224](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L224)
[types/subscription.ts:262](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L262)
30 changes: 15 additions & 15 deletions docs/code/interfaces/types_subscription.TransactionFilter.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Filter to app transactions that meet the given app arguments predicate.

#### Defined in

[types/subscription.ts:153](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L153)
[types/subscription.ts:191](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L191)

___

Expand All @@ -62,7 +62,7 @@ Filter to transactions that are creating an app.

#### Defined in

[types/subscription.ts:134](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L134)
[types/subscription.ts:172](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L172)

___

Expand All @@ -74,7 +74,7 @@ Filter to transactions against the app with the given ID.

#### Defined in

[types/subscription.ts:132](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L132)
[types/subscription.ts:170](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L170)

___

Expand All @@ -86,7 +86,7 @@ Filter to transactions that have given on complete(s).

#### Defined in

[types/subscription.ts:136](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L136)
[types/subscription.ts:174](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L174)

___

Expand All @@ -99,7 +99,7 @@ Note: the definitions for these events must be passed in to the subscription con

#### Defined in

[types/subscription.ts:157](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L157)
[types/subscription.ts:195](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L195)

___

Expand All @@ -111,7 +111,7 @@ Filter to transactions that are creating an asset.

#### Defined in

[types/subscription.ts:140](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L140)
[types/subscription.ts:178](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L178)

___

Expand All @@ -123,7 +123,7 @@ Filter to transactions against the asset with the given ID.

#### Defined in

[types/subscription.ts:138](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L138)
[types/subscription.ts:176](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L176)

___

Expand All @@ -136,7 +136,7 @@ or equal to the given maximum (microAlgos or decimal units of an ASA if type: ax

#### Defined in

[types/subscription.ts:146](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L146)
[types/subscription.ts:184](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L184)

___

Expand All @@ -149,7 +149,7 @@ the given method signature as the first app argument.

#### Defined in

[types/subscription.ts:149](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L149)
[types/subscription.ts:187](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L187)

___

Expand All @@ -161,7 +161,7 @@ Filter to app transactions that match one of the given ARC-0004 method selectors

#### Defined in

[types/subscription.ts:151](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L151)
[types/subscription.ts:189](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L189)

___

Expand All @@ -174,7 +174,7 @@ than or equal to the given minimum (microAlgos or decimal units of an ASA if typ

#### Defined in

[types/subscription.ts:143](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L143)
[types/subscription.ts:181](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L181)

___

Expand All @@ -186,7 +186,7 @@ Filter to transactions with a note having the given prefix.

#### Defined in

[types/subscription.ts:130](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L130)
[types/subscription.ts:168](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L168)

___

Expand All @@ -198,7 +198,7 @@ Filter to transactions being received by the specified address.

#### Defined in

[types/subscription.ts:128](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L128)
[types/subscription.ts:166](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L166)

___

Expand All @@ -210,7 +210,7 @@ Filter to transactions sent from the specified address.

#### Defined in

[types/subscription.ts:126](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L126)
[types/subscription.ts:164](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L164)

___

Expand All @@ -222,4 +222,4 @@ Filter based on the given transaction type.

#### Defined in

[types/subscription.ts:124](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L124)
[types/subscription.ts:162](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L162)
Loading

0 comments on commit 70d34f5

Please sign in to comment.