Skip to content

Comments

Added lifecycle hooks to solver table#85

Open
tamir-cow wants to merge 4 commits intomainfrom
feature/solver-table-hooks
Open

Added lifecycle hooks to solver table#85
tamir-cow wants to merge 4 commits intomainfrom
feature/solver-table-hooks

Conversation

@tamir-cow
Copy link
Contributor

@tamir-cow tamir-cow commented Feb 18, 2026

As part of our recent efforts to improve data integrity in the CMS we are adding event hooks to certain actions in the cms that update other fields, this way we ensure that data is always up to date in realtime unlike now when fields are not updated because of manual mistakes or things changing in one table but not reflected in another.

In this pr we add some simple lifecycles to the solver table:
before create or update operations on the solver table:

  1. Update active networks field (list of chains solver is live on) of solver. Checked by referncing solver network table.
  2. Update service fee enabled status field based on bonding pool solver is on and date he joined it (CoW, colocated, full).

To test this change:

  1. Deploy cms locally
  2. Create solver bonding pool entry
  3. Create solver network entry
  4. Create / update solver table
  5. Check isServiceFeeEnabled field
  6. Check activeNetworks

@anxolin
Copy link
Contributor

anxolin commented Feb 18, 2026

Hello Tamir. Could you try to add a bit more context in the description of the PR. The description is a bit hard to read, and is good to add a section explaining how to test this (steps), this makes the review easier and reduce the chance of misunderstanding the scope and goal of the PR

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of the PR is way better now. Thanks for applying the changes

}

// Helper function to calculate months difference between two dates
function getMonthsDifference(startDate: Date, endDate: Date): number {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belong to a utils file, in case you need it in another part of the project you don't want to add a dependency to this file

// For create operation, we handle it in afterCreate since we need the ID
}

async function calculateServiceFeeEnabled(solver: Solver, data: SolverData): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this called calculate, but returns nothing?

// For create operation, we handle it in afterCreate since we need the ID
}

async function calculateServiceFeeEnabled(solver: Solver, data: SolverData): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be positive to add some minmial JsDoc to explain what is the responsability of the function

}

async function calculateServiceFeeEnabled(solver: Solver, data: SolverData): Promise<void> {
data.isServiceFeeEnabled = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is mutating the parameter, is this desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if its not set to true it needs to be false by default

const joinedDate = new Date(bondingPool.joinedOn);
const monthsDifference = getMonthsDifference(joinedDate, currentDate);

if (bondingPool.name === "CoW" && solver.isColocated === "No") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why name CoW, its hard to me to reason about this logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bonding pool name in the referenced table (an existing entry)

const monthsDifference = getMonthsDifference(joinedDate, currentDate);

if (bondingPool.name === "CoW" && solver.isColocated === "No") {
if (monthsDifference >= 6) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number. Why 6?

}
// Colocated bonding pool
else if (solver.isColocated === "Yes") {
if (monthsDifference >= 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

);

if (solver) {
await calculateServiceFeeEnabled(solver as Solver, solverData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like you want to update something and first you want to do some calculations. If that's the case, is better to make a pure function to decide the new value and then do the update

This function relies on side effects and does too many things. Follow SOLID single responsability principle "https://www.digitalocean.com/community/conceptual-articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design#single-responsibility-principle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants