Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DeFiActions
372 changes: 182 additions & 190 deletions cadence/contracts/TidalProtocol.cdc

Large diffs are not rendered by default.

262 changes: 0 additions & 262 deletions cadence/contracts/TidalProtocolUtils.cdc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ access(all)
fun main(
pid: UInt64,
withdrawType: String,
targetHealth: UInt256,
targetHealth: UInt128,
depositType: String,
depositAmount: UFix64
): UFix64 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ access(all)
fun main(
pid: UInt64,
depositType: String,
targetHealth: UInt256,
targetHealth: UInt128,
withdrawType: String,
withdrawAmount: UFix64
): UFix64 {
Expand Down
2 changes: 1 addition & 1 deletion cadence/scripts/tidal-protocol/position_health.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "TidalProtocol"
/// @param pid: The Position ID
///
access(all)
fun main(pid: UInt64): UInt256 {
fun main(pid: UInt64): UInt128 {
let protocolAddress= Type<@TidalProtocol.Pool>().address!
return getAccount(protocolAddress).capabilities.borrow<&TidalProtocol.Pool>(TidalProtocol.PoolPublicPath)
?.positionHealth(pid: pid)
Expand Down
7 changes: 4 additions & 3 deletions cadence/tests/funds_available_above_target_health_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "test_helpers.cdc"

import "MOET"
import "TidalProtocol"
import "TidalProtocolUtils"

access(all) let protocolAccount = Test.getAccount(0x0000000000000007)
access(all) let userAccount = Test.createAccount()
Expand Down Expand Up @@ -107,7 +106,9 @@ fun testFundsAvailableAboveTargetHealthAfterDepositingWithPushFromHealthy() {
let moetBalance = positionDetails.balances[0]
let flowPositionBalance = positionDetails.balances[1]
Test.assertEqual(positionFundingAmount, flowPositionBalance.balance)
Test.assertEqual(expectedBorrowAmount, moetBalance.balance)

Test.assert(equalWithinVariance(expectedBorrowAmount, moetBalance.balance),
message: "Expected borrow amount to be \(expectedBorrowAmount), but got \(moetBalance.balance)")
Test.assertEqual(TidalProtocol.BalanceDirection.Credit, flowPositionBalance.direction)
Test.assertEqual(TidalProtocol.BalanceDirection.Debit, moetBalance.direction)

Expand Down Expand Up @@ -351,4 +352,4 @@ fun runFundsAvailableAboveTargetHealthAfterDepositing(
log("[TEST] Actual Available: \(actualAvailable)")
Test.assert(equalWithinVariance(expectedAvailable, actualAvailable),
message: "Values are not equal within variance - expected: \(expectedAvailable), actual: \(actualAvailable)")
}
}
36 changes: 18 additions & 18 deletions cadence/tests/funds_required_for_target_health_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "test_helpers.cdc"

import "MOET"
import "TidalProtocol"
import "TidalProtocolUtils"
import "DeFiActionsMathUtils"

access(all) let protocolAccount = Test.getAccount(0x0000000000000007)
access(all) let userAccount = Test.createAccount()
Expand Down Expand Up @@ -316,9 +316,9 @@ fun testFundsRequiredForTargetHealthAfterWithdrawingWithPushFromOvercollateraliz
)
let actualHealthAfterPriceIncrease = getPositionHealth(pid: positionID, beFailed: false)
// calculate new health based on updated collateral value - should increase proportionally to price increase
let expectedHealthAfterPriceIncrease = TidalProtocolUtils.mul(
let expectedHealthAfterPriceIncrease = DeFiActionsMathUtils.mul(
actualHealthBeforePriceIncrease,
TidalProtocolUtils.ufix64ToUInt256(1.0 + priceIncrease, decimals: TidalProtocolUtils.decimals)
DeFiActionsMathUtils.toUInt128(1.0 + priceIncrease)
)
Test.assertEqual(expectedHealthAfterPriceIncrease, actualHealthAfterPriceIncrease)

Expand Down Expand Up @@ -474,9 +474,9 @@ fun testFundsRequiredForTargetHealthAfterWithdrawingWithPushFromUndercollaterali
)
let actualHealthAfterPriceDecrease = getPositionHealth(pid: positionID, beFailed: false)
// calculate new health based on updated collateral value - should increase proportionally to price increase
let expectedHealthAfterPriceDecrease = TidalProtocolUtils.mul(
let expectedHealthAfterPriceDecrease = DeFiActionsMathUtils.mul(
actualHealthBeforePriceIncrease,
TidalProtocolUtils.ufix64ToUInt256(1.0 - priceDecrease, decimals: TidalProtocolUtils.decimals)
DeFiActionsMathUtils.toUInt128(1.0 - priceDecrease)
)
Test.assertEqual(expectedHealthAfterPriceDecrease, actualHealthAfterPriceDecrease)

Expand Down Expand Up @@ -526,26 +526,26 @@ fun runFundsRequiredForTargetHealthAfterWithdrawing(
) {
log("..............................")

let intFLOWCollateralFactor = TidalProtocolUtils.ufix64ToUInt256(flowCollateralFactor, decimals: TidalProtocolUtils.decimals)
let intFLOWBorrowFactor = TidalProtocolUtils.ufix64ToUInt256(flowBorrowFactor, decimals: TidalProtocolUtils.decimals)
let intFLOWPrice = TidalProtocolUtils.ufix64ToUInt256(currentFLOWPrice, decimals: TidalProtocolUtils.decimals)
let intFLOWCollateral = TidalProtocolUtils.ufix64ToUInt256(existingFLOWCollateral, decimals: TidalProtocolUtils.decimals)
let intFLOWBorrowed = TidalProtocolUtils.ufix64ToUInt256(existingBorrowed, decimals: TidalProtocolUtils.decimals)
let intWithdrawAmount = TidalProtocolUtils.ufix64ToUInt256(withdrawAmount, decimals: TidalProtocolUtils.decimals)
let intFLOWCollateralFactor = DeFiActionsMathUtils.toUInt128(flowCollateralFactor)
let intFLOWBorrowFactor = DeFiActionsMathUtils.toUInt128(flowBorrowFactor)
let intFLOWPrice = DeFiActionsMathUtils.toUInt128(currentFLOWPrice)
let intFLOWCollateral = DeFiActionsMathUtils.toUInt128(existingFLOWCollateral)
let intFLOWBorrowed = DeFiActionsMathUtils.toUInt128(existingBorrowed)
let intWithdrawAmount = DeFiActionsMathUtils.toUInt128(withdrawAmount)

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

var expectedRequired: UInt256 = 0
var expectedRequired: UInt128 = 0
if desiredFinalDebt > expectedBorrowCapacity {
let valueDiff = desiredFinalDebt - expectedBorrowCapacity
expectedRequired = TidalProtocolUtils.div(TidalProtocolUtils.mul(valueDiff, intTargetHealth), intFLOWPrice)
expectedRequired = TidalProtocolUtils.div(expectedRequired, intFLOWCollateralFactor)
expectedRequired = DeFiActionsMathUtils.div(DeFiActionsMathUtils.mul(valueDiff, intTargetHealth), intFLOWPrice)
expectedRequired = DeFiActionsMathUtils.div(expectedRequired, intFLOWCollateralFactor)
}
let ufixExpectedRequired = TidalProtocolUtils.uint256ToUFix64(expectedRequired, decimals: TidalProtocolUtils.decimals)
let ufixExpectedRequired = DeFiActionsMathUtils.toUFix64Round(expectedRequired)

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