Skip to content

Commit 74ebe3f

Browse files
committed
feat: check entity address balance
1 parent 503d8c6 commit 74ebe3f

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ todos:
3131

3232
- more checks:
3333
- general/fsp:
34-
- [ ] check if addresses (submit, signature, sign) have enough tokens for gas
34+
- [x] check if addresses (submit, signature, sign) have enough tokens for gas
3535
- [ ] collect fast updates addresses and check them too
3636
- [ ] check for unclaimed rewards
3737
- [ ] check for registration:

configuration/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@ def get_config() -> Configuration:
117117
if identity_address is None:
118118
raise ConfigError("IDENTITY_ADDRESS environment variable must be set.")
119119

120+
_fee_threshold = os.environ.get("FEE_THRESHOLD", "25")
121+
fee_threshold = int(_fee_threshold)
122+
120123
config = Configuration(
121124
rpc_url=rpc_url,
122125
identity_address=to_checksum_address(identity_address),
123126
chain_id=chain_id,
124127
contracts=Contracts.get_contracts(w),
125128
epoch=get_epoch(chain_id),
126129
notification=get_notification_config(),
130+
fee_threshold=fee_threshold,
127131
)
128132

129133
return config

configuration/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,4 @@ class Configuration:
224224
rpc_url: str
225225
epoch: Epoch
226226
notification: Notification
227+
fee_threshold: int

observer/observer.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import time
3+
from collections.abc import Sequence
34
from typing import Self
45

56
from eth_account._utils.signing import to_standard_v
@@ -19,6 +20,7 @@
1920
Configuration,
2021
)
2122
from observer.reward_epoch_manager import (
23+
Entity,
2224
SigningPolicy,
2325
)
2426
from observer.types import (
@@ -190,6 +192,32 @@ def log_message(config: Configuration, message: Message):
190192
notify_generic(n.generic, message)
191193

192194

195+
async def cron(config: Configuration, w: AsyncWeb3, e: Entity) -> Sequence[Message]:
196+
mb = Message.builder()
197+
messages = []
198+
199+
addrs = (
200+
("submit", e.submit_address),
201+
("submit signatures", e.submit_signatures_address),
202+
("signing policy", e.signing_policy_address),
203+
)
204+
205+
for name, addr in addrs:
206+
balance = await w.eth.get_balance(addr, "latest")
207+
if balance < config.fee_threshold * 1e18:
208+
level = MessageLevel.WARNING
209+
if balance <= 5e18:
210+
level = MessageLevel.ERROR
211+
212+
messages.append(
213+
mb.build(
214+
level, f"low balance for {name} address ({balance / 1e18:.4f} NAT)"
215+
)
216+
)
217+
218+
return messages
219+
220+
193221
async def observer_loop(config: Configuration) -> None:
194222
w = AsyncWeb3(
195223
AsyncWeb3.AsyncHTTPProvider(config.rpc_url),
@@ -270,6 +298,8 @@ async def observer_loop(config: Configuration) -> None:
270298
# # f"current reward epoch: {current_rid}",
271299
# )
272300

301+
cron_time = time.time()
302+
273303
# wait until next voting epoch
274304
block_number = block["number"]
275305
while True:
@@ -453,10 +483,17 @@ async def observer_loop(config: Configuration) -> None:
453483
except Exception:
454484
pass
455485

486+
messages: list[Message] = []
487+
entity = signing_policy.entity_mapper.by_identity_address[tia]
488+
489+
if int(time.time() - cron_time) < 60 * 60:
490+
messages.extend(await cron(config, w, entity))
491+
456492
rounds = vrm.finalize(block_data)
457493
for r in rounds:
458-
entity = signing_policy.entity_mapper.by_identity_address[tia]
459-
for message in validate_round(r, signing_policy, entity, config):
460-
log_message(config, message)
494+
messages.extend(validate_round(r, signing_policy, entity, config))
495+
496+
for m in messages:
497+
log_message(config, m)
461498

462499
block_number = latest_block

0 commit comments

Comments
 (0)