Skip to content

available effective value calc#33

Merged
nialexsan merged 21 commits into
mainfrom
nialexsan/tidal-calculations
Aug 13, 2025
Merged

available effective value calc#33
nialexsan merged 21 commits into
mainfrom
nialexsan/tidal-calculations

Conversation

@nialexsan

@nialexsan nialexsan commented Jul 23, 2025

Copy link
Copy Markdown
Contributor

align changes with DeFi Actions Math Utilis to UInt128 and 10^24 decimal places
tweaked variance to address rounding errors in tests

@codecov

codecov Bot commented Jul 23, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 67.56757% with 36 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cadence/contracts/TidalProtocol.cdc 67.56% 36 Missing ⚠️

📢 Thoughts on this report? Let us know!

@nialexsan nialexsan marked this pull request as ready for review July 29, 2025 20:48

@sisyphusSmiling sisyphusSmiling left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work @nialexsan! The utils are much less verbose and rounding operations look good.

Comment thread cadence/contracts/TidalProtocol.cdc Outdated
Comment thread cadence/contracts/TidalProtocol.cdc Outdated
Comment thread cadence/contracts/TidalProtocol.cdc Outdated
Comment on lines +927 to +930
// let availableHealth = healthAfterDeposit == UInt128.max ? UInt128.max : healthAfterDeposit - targetHealth
// let availableEffectiveValue = (effectiveDebtAfterDeposit == 0 || availableHealth == UInt128.max)
// ? effectiveCollateralAfterDeposit
// : DeFiActionsMathUtils.mul(availableHealth, effectiveDebtAfterDeposit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Curious why we no longer have to concern ourselves with these conditions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

for this comment

$$\begin{aligned} C = effectiveCollateralAfterDeposit \\\ D = effectiveDebtAfterDeposit \\\ H = healthAfterDeposit \\\ T = targetHealth \\\ \\\ H = C / D \\\ \\\ availableHealth = H - T \\\ \end{aligned}$$

availableHealth could be UInt128.max only when effectiveDebtAfterDeposit($$D$$) is 0, which should set
$$availableEffectiveValue = C - T * D = C - T * 0 = C$$

and the second branch is the same as the one-liner:

$$\begin{aligned} availableHealth * D = \\ = (H - T) * D = \\\ = H * D - T * D = \\\ = {C * \cancel{D} \over \cancel{D}} - T * D = \\\ = C - T*D \end{aligned}$$

Comment thread cadence/contracts/TidalProtocol.cdc Outdated
Comment thread cadence/contracts/TidalProtocol.cdc Outdated
}

/// Returns the compounded interest index reflecting the passage of time
/// The result is: newIndex = oldIndex * perSecondRate ^ seconds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason we can't implement this directly? Something like

let base = DeFiActionsMathUtils.mul(oldIndex, perSecondRate)
// would require pow to take UInt128 exponent value
return DeFiActionsMathUtils.pow(base, to: DeFiActionsMathUtils.toUInt128(elapsedSeconds))

Maybe because the bitwise operations are more performant, but we should explicitly state that in a comment since the implementation is not as intuitive as the direct implementation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll need to look into it again

nialexsan and others added 2 commits August 13, 2025 11:28
Co-authored-by: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com>
Co-authored-by: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com>
@nialexsan nialexsan merged commit 6024bab into main Aug 13, 2025
1 of 2 checks passed
@nialexsan nialexsan deleted the nialexsan/tidal-calculations branch August 13, 2025 15:40
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