Conversation
|
The event is only emitted inside Could Another option would be to calculate position health off-chain entirely.
|
|
@holyfuchs Thanks for starting this discussion!
Yes, we could write a batch script, but it only mitigates the problem rather than solving it:
I think it adds significant complexity - we'd need to replicate the health calculation logic off-chain, keep it in sync with any contract changes, and independently fetch and combine interest indices + oracle prices + per-position balances. I think events are a much simpler and more maintainable approach, and I'd prefer to go with it and only fall back to scripts if events turn out not to be doable for some reason. |
Yes, but this only queues positions when the
Scripts can't persist state changes, but they do reflect them during execution.
Position health changes constantly — every second due to interest accrual, and on every price tick. Events are only useful if you emit one regularly for every position. You could add a function that iterates all positions and emits health events on a schedule, but unlike a script this isn't free — each event costs gas, so at any meaningful scale this gets expensive fast. Scripts are cheaper here precisely because they don't write to chain.
I agree it definitely adds a significant amount of complexity but I don't think there is a nice solution here. |
Motivation
The FCM observer currently fetches positions’ health by running scripts (
get_position_by_id.cdc). This approach doesn’t scale well.For example, with ~10,000 positions, we need to execute 10,000 script calls. Access nodes are rate-limited (typically ~1–5 requests/sec), so processing all positions takes a significant amount of time.
Change
Result