Skip to content

Commit

Permalink
feat(relayer): adds possibility to filter messages based on msg.sender
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed May 7, 2024
1 parent 5078066 commit 633a44e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/relayer/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ RPC=
PK=
WATCH_INTERVAL_TIME_MS=
MONGO_DB_URI=
MIN_BATCH_SIZE=
MIN_BATCH_SIZE=
WHITELISTED_SENDER_ADDRESSES=
2 changes: 1 addition & 1 deletion packages/relayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The Relayer is a service used to relay batches of dispatched messages through Ya
 

## Installation

To install the Relayer, follow these steps:

```bash
Expand Down Expand Up @@ -45,4 +46,3 @@ cd packages/relayer
```bash
yarn start dotenv_config_path="your env file"
```

18 changes: 10 additions & 8 deletions packages/relayer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dotenv/config'
import "dotenv/config"
import { createWalletClient, http, Chain, publicActions, Log } from "viem"
import { privateKeyToAccount } from "viem/accounts"
import * as chains from "viem/chains"
Expand All @@ -18,6 +18,10 @@ const mongoClient = new MongoClient(process.env.MONGO_DB_URI as string)
await mongoClient.connect()
const db = mongoClient.db("hashi")

const whitelistedSenderAddresses = process.env.WHITELISTED_SENDER_ADDRESSES?.split(",").map((_address: string) =>
_address.toLowerCase(),
) as string[]

const watcher = new Watcher({
service: "RelayerWatcher",
logger,
Expand All @@ -27,13 +31,11 @@ const watcher = new Watcher({
eventName: "MessageDispatched",
watchIntervalTimeMs: Number(process.env.WATCH_INTERVAL_TIME_MS as string),
onLogs: async (_logs: Log[]) => {
// TODO: filter messages that you don't want to relay. For example it could be possible
// to relayMessagesToAdapters only those coming from specific message.sender

/*const blocks = (await Promise.all(
_logs.map((_log: Log) => client.getBlock({ blockHash: _log.blockHash as `0x${string}` })),
)) as Block[]*/
const messages = _logs.map((_log: Log) => Message.fromLog(_log))
let messages = _logs.map((_log: Log) => Message.fromLog(_log))
if (whitelistedSenderAddresses.length) {
logger.child({ service: "Relayer" }).info(`Filtering messages ...`)
messages = messages.filter((_message) => whitelistedSenderAddresses.includes(_message.sender.toLowerCase()))
}

await Promise.all(
messages.map((_message: Message, _index: number) =>
Expand Down

0 comments on commit 633a44e

Please sign in to comment.