Skip to content

Commit 6024bab

Browse files
authored
Merge pull request #33 from onflow/nialexsan/tidal-calculations
available effective value calc
2 parents 88a89bc + e866e78 commit 6024bab

14 files changed

Lines changed: 235 additions & 1198 deletions

DeFiActions

cadence/contracts/TidalProtocol.cdc

Lines changed: 178 additions & 190 deletions
Large diffs are not rendered by default.

cadence/contracts/TidalProtocolUtils.cdc

Lines changed: 0 additions & 262 deletions
This file was deleted.

cadence/scripts/tidal-protocol/funds_avail_above_target_health_after_deposit.cdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ access(all)
44
fun main(
55
pid: UInt64,
66
withdrawType: String,
7-
targetHealth: UInt256,
7+
targetHealth: UInt128,
88
depositType: String,
99
depositAmount: UFix64
1010
): UFix64 {

cadence/scripts/tidal-protocol/funds_req_for_target_health_after_withdraw.cdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ access(all)
44
fun main(
55
pid: UInt64,
66
depositType: String,
7-
targetHealth: UInt256,
7+
targetHealth: UInt128,
88
withdrawType: String,
99
withdrawAmount: UFix64
1010
): UFix64 {

cadence/scripts/tidal-protocol/position_health.cdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "TidalProtocol"
55
/// @param pid: The Position ID
66
///
77
access(all)
8-
fun main(pid: UInt64): UInt256 {
8+
fun main(pid: UInt64): UInt128 {
99
let protocolAddress= Type<@TidalProtocol.Pool>().address!
1010
return getAccount(protocolAddress).capabilities.borrow<&TidalProtocol.Pool>(TidalProtocol.PoolPublicPath)
1111
?.positionHealth(pid: pid)

cadence/tests/funds_available_above_target_health_test.cdc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "test_helpers.cdc"
55

66
import "MOET"
77
import "TidalProtocol"
8-
import "TidalProtocolUtils"
98

109
access(all) let protocolAccount = Test.getAccount(0x0000000000000007)
1110
access(all) let userAccount = Test.createAccount()
@@ -107,7 +106,9 @@ fun testFundsAvailableAboveTargetHealthAfterDepositingWithPushFromHealthy() {
107106
let moetBalance = positionDetails.balances[0]
108107
let flowPositionBalance = positionDetails.balances[1]
109108
Test.assertEqual(positionFundingAmount, flowPositionBalance.balance)
110-
Test.assertEqual(expectedBorrowAmount, moetBalance.balance)
109+
110+
Test.assert(equalWithinVariance(expectedBorrowAmount, moetBalance.balance),
111+
message: "Expected borrow amount to be \(expectedBorrowAmount), but got \(moetBalance.balance)")
111112
Test.assertEqual(TidalProtocol.BalanceDirection.Credit, flowPositionBalance.direction)
112113
Test.assertEqual(TidalProtocol.BalanceDirection.Debit, moetBalance.direction)
113114

@@ -351,4 +352,4 @@ fun runFundsAvailableAboveTargetHealthAfterDepositing(
351352
log("[TEST] Actual Available: \(actualAvailable)")
352353
Test.assert(equalWithinVariance(expectedAvailable, actualAvailable),
353354
message: "Values are not equal within variance - expected: \(expectedAvailable), actual: \(actualAvailable)")
354-
}
355+
}

cadence/tests/funds_required_for_target_health_test.cdc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "test_helpers.cdc"
55

66
import "MOET"
77
import "TidalProtocol"
8-
import "TidalProtocolUtils"
8+
import "DeFiActionsMathUtils"
99

1010
access(all) let protocolAccount = Test.getAccount(0x0000000000000007)
1111
access(all) let userAccount = Test.createAccount()
@@ -316,9 +316,9 @@ fun testFundsRequiredForTargetHealthAfterWithdrawingWithPushFromOvercollateraliz
316316
)
317317
let actualHealthAfterPriceIncrease = getPositionHealth(pid: positionID, beFailed: false)
318318
// calculate new health based on updated collateral value - should increase proportionally to price increase
319-
let expectedHealthAfterPriceIncrease = TidalProtocolUtils.mul(
319+
let expectedHealthAfterPriceIncrease = DeFiActionsMathUtils.mul(
320320
actualHealthBeforePriceIncrease,
321-
TidalProtocolUtils.ufix64ToUInt256(1.0 + priceIncrease, decimals: TidalProtocolUtils.decimals)
321+
DeFiActionsMathUtils.toUInt128(1.0 + priceIncrease)
322322
)
323323
Test.assertEqual(expectedHealthAfterPriceIncrease, actualHealthAfterPriceIncrease)
324324

@@ -474,9 +474,9 @@ fun testFundsRequiredForTargetHealthAfterWithdrawingWithPushFromUndercollaterali
474474
)
475475
let actualHealthAfterPriceDecrease = getPositionHealth(pid: positionID, beFailed: false)
476476
// calculate new health based on updated collateral value - should increase proportionally to price increase
477-
let expectedHealthAfterPriceDecrease = TidalProtocolUtils.mul(
477+
let expectedHealthAfterPriceDecrease = DeFiActionsMathUtils.mul(
478478
actualHealthBeforePriceIncrease,
479-
TidalProtocolUtils.ufix64ToUInt256(1.0 - priceDecrease, decimals: TidalProtocolUtils.decimals)
479+
DeFiActionsMathUtils.toUInt128(1.0 - priceDecrease)
480480
)
481481
Test.assertEqual(expectedHealthAfterPriceDecrease, actualHealthAfterPriceDecrease)
482482

@@ -526,26 +526,26 @@ fun runFundsRequiredForTargetHealthAfterWithdrawing(
526526
) {
527527
log("..............................")
528528

529-
let intFLOWCollateralFactor = TidalProtocolUtils.ufix64ToUInt256(flowCollateralFactor, decimals: TidalProtocolUtils.decimals)
530-
let intFLOWBorrowFactor = TidalProtocolUtils.ufix64ToUInt256(flowBorrowFactor, decimals: TidalProtocolUtils.decimals)
531-
let intFLOWPrice = TidalProtocolUtils.ufix64ToUInt256(currentFLOWPrice, decimals: TidalProtocolUtils.decimals)
532-
let intFLOWCollateral = TidalProtocolUtils.ufix64ToUInt256(existingFLOWCollateral, decimals: TidalProtocolUtils.decimals)
533-
let intFLOWBorrowed = TidalProtocolUtils.ufix64ToUInt256(existingBorrowed, decimals: TidalProtocolUtils.decimals)
534-
let intWithdrawAmount = TidalProtocolUtils.ufix64ToUInt256(withdrawAmount, decimals: TidalProtocolUtils.decimals)
529+
let intFLOWCollateralFactor = DeFiActionsMathUtils.toUInt128(flowCollateralFactor)
530+
let intFLOWBorrowFactor = DeFiActionsMathUtils.toUInt128(flowBorrowFactor)
531+
let intFLOWPrice = DeFiActionsMathUtils.toUInt128(currentFLOWPrice)
532+
let intFLOWCollateral = DeFiActionsMathUtils.toUInt128(existingFLOWCollateral)
533+
let intFLOWBorrowed = DeFiActionsMathUtils.toUInt128(existingBorrowed)
534+
let intWithdrawAmount = DeFiActionsMathUtils.toUInt128(withdrawAmount)
535535

536536
// effectiveCollateralValue = collateralBalance * collateralPrice * collateralFactor
537-
let effectiveFLOWCollateralValue = TidalProtocolUtils.mul(TidalProtocolUtils.mul(intFLOWCollateral, intFLOWPrice), intFLOWCollateralFactor)
537+
let effectiveFLOWCollateralValue = DeFiActionsMathUtils.mul(DeFiActionsMathUtils.mul(intFLOWCollateral, intFLOWPrice), intFLOWCollateralFactor)
538538
// borrowLimit = (effectiveCollateralValue / targetHealth) * borrowFactor
539-
let expectedBorrowCapacity = TidalProtocolUtils.mul(TidalProtocolUtils.div(effectiveFLOWCollateralValue, intTargetHealth), intFLOWBorrowFactor)
539+
let expectedBorrowCapacity = DeFiActionsMathUtils.mul(DeFiActionsMathUtils.div(effectiveFLOWCollateralValue, intTargetHealth), intFLOWBorrowFactor)
540540
let desiredFinalDebt = intFLOWBorrowed + intWithdrawAmount
541541

542-
var expectedRequired: UInt256 = 0
542+
var expectedRequired: UInt128 = 0
543543
if desiredFinalDebt > expectedBorrowCapacity {
544544
let valueDiff = desiredFinalDebt - expectedBorrowCapacity
545-
expectedRequired = TidalProtocolUtils.div(TidalProtocolUtils.mul(valueDiff, intTargetHealth), intFLOWPrice)
546-
expectedRequired = TidalProtocolUtils.div(expectedRequired, intFLOWCollateralFactor)
545+
expectedRequired = DeFiActionsMathUtils.div(DeFiActionsMathUtils.mul(valueDiff, intTargetHealth), intFLOWPrice)
546+
expectedRequired = DeFiActionsMathUtils.div(expectedRequired, intFLOWCollateralFactor)
547547
}
548-
let ufixExpectedRequired = TidalProtocolUtils.uint256ToUFix64(expectedRequired, decimals: TidalProtocolUtils.decimals)
548+
let ufixExpectedRequired = DeFiActionsMathUtils.toUFix64Round(expectedRequired)
549549

550550
log("[TEST] existingFLOWCollateral: \(existingFLOWCollateral)")
551551
log("[TEST] existingBorrowed: \(existingBorrowed)")
@@ -567,4 +567,4 @@ fun runFundsRequiredForTargetHealthAfterWithdrawing(
567567
log("[TEST] Actual Required: \(actualRequired)")
568568
Test.assert(equalWithinVariance(ufixExpectedRequired, actualRequired),
569569
message: "Expected required funds to be \(ufixExpectedRequired), but got \(actualRequired)")
570-
}
570+
}

0 commit comments

Comments
 (0)