@@ -445,11 +445,14 @@ access(all) contract TidalProtocol {
445445 /// available without topping up the position.
446446 access (all ) fun availableBalance (pid : UInt64 , type : Type , pullFromTopUpSource : Bool ): UFix64 {
447447 let position = self ._borrowPosition (pid : pid )
448+ log (" position, \( pid ) " )
448449
449450 if pullFromTopUpSource && position .topUpSource ! = nil {
450451 let topUpSource = position .topUpSource !
451452 let sourceType = topUpSource .getSourceType ()
452453 let sourceAmount = topUpSource .minimumAvailable ()
454+ log (" sourceAmount" )
455+ log (sourceAmount )
453456
454457 return self .fundsAvailableAboveTargetHealthAfterDepositing (
455458 pid : pid ,
@@ -645,8 +648,7 @@ access(all) contract TidalProtocol {
645648 if potentialHealth > = targetHealth {
646649 // We can reach the target health by paying off some or all of the debt. We can easily
647650 // compute how many units of the token would be needed to reach the target health.
648- let healthChange = targetHealth - healthAfterWithdrawal
649- let requiredEffectiveDebt = healthChange * effectiveCollateralAfterWithdrawal / (targetHealth * targetHealth )
651+ let requiredEffectiveDebt = effectiveDebtAfterWithdrawal - effectiveCollateralAfterWithdrawal / targetHealth
650652
651653 // The amount of the token to pay back, in units of the token.
652654 let paybackAmount = requiredEffectiveDebt * self .borrowFactor [depositType ]! / self .priceOracle .price (ofToken : depositType )!
@@ -680,6 +682,8 @@ access(all) contract TidalProtocol {
680682
681683 // The amount of the token to deposit, in units of the token.
682684 let collateralTokenCount = requiredEffectiveCollateral / self .priceOracle .price (ofToken : depositType )! / self .collateralFactor [depositType ]!
685+ log (" collateralTokenCount" )
686+ log (collateralTokenCount )
683687
684688 // debtTokenCount is the number of tokens that went towards debt, zero if there was no debt.
685689 return collateralTokenCount + debtTokenCount
@@ -733,42 +737,30 @@ access(all) contract TidalProtocol {
733737 scaledBalance : debtBalance ,
734738 interestIndex : depositTokenState .debitInterestIndex
735739 )
736- log (" balanceSheetEffectiveDebt \( balanceSheet .effectiveDebt ) " )
737-
738- log (" trueDebt \( trueDebt ) " )
739- log (" depositAmount \( depositAmount ) " )
740740
741741 if trueDebt > = depositAmount {
742742 // This deposit will pay down some debt, but won't result in net collateral, we
743743 // just need to account for the debt decrease.
744744 effectiveDebtAfterDeposit = balanceSheet .effectiveDebt -
745745 (depositAmount * self .priceOracle .price (ofToken : depositType )! / self .borrowFactor [depositType ]! )
746- log (" effectiveDebtAfterDeposit1 \( effectiveDebtAfterDeposit ) " )
747746 } else {
748747 // The deposit will wipe out all of the debt, and create some collateral.
749748 effectiveDebtAfterDeposit = balanceSheet .effectiveDebt -
750749 (trueDebt * self .priceOracle .price (ofToken : depositType )! / self .borrowFactor [depositType ]! )
751750
752751 effectiveCollateralAfterDeposit = balanceSheet .effectiveCollateral +
753752 ((depositAmount - trueDebt ) * self .priceOracle .price (ofToken : depositType )! * self .collateralFactor [depositType ]! )
754- log (" effectiveDebtAfterDeposit2 \( effectiveDebtAfterDeposit ) " )
755753 }
756754 }
757755 }
758756
759757 // We now have new effective collateral and debt values that reflect the proposed deposit (if any!)
760758 // Now we can figure out how many of the withdrawal token are available while keeping the position
761759 // at or above the target health value.
762- log (" effectiveCollateralAfterDeposit" )
763- log (effectiveCollateralAfterDeposit )
764- log (" effectiveDebtAfterDeposit" )
765- log (effectiveDebtAfterDeposit )
766760 var healthAfterDeposit = TidalProtocol .healthComputation (
767761 effectiveCollateral : effectiveCollateralAfterDeposit ,
768762 effectiveDebt : effectiveDebtAfterDeposit
769763 )
770- log (" healthAfterDeposit" )
771- log (healthAfterDeposit )
772764
773765
774766 if healthAfterDeposit < = targetHealth {
@@ -799,14 +791,12 @@ access(all) contract TidalProtocol {
799791 effectiveDebt : effectiveDebtAfterDeposit
800792 )
801793
802- log (" potentialHealth \( potentialHealth ) " )
803794
804795 // Does drawing down all of the collateral go below the target health? Then the max withdrawal comes from collateral only.
805796 if potentialHealth < = targetHealth {
806797 // We will hit the health target before using up all of the withdraw token credit. We can easily
807798 // compute how many units of the token would bring the position down to the target health.
808799 let availableHealth = healthAfterDeposit - targetHealth
809- log (" effectiveDebtAfterDeposit \( effectiveDebtAfterDeposit ) " )
810800 let safeHealthAfterDeposit = effectiveDebtAfterDeposit < 0.000001
811801 ? nil
812802 : effectiveCollateralAfterDeposit / effectiveDebtAfterDeposit
@@ -818,10 +808,8 @@ access(all) contract TidalProtocol {
818808 availableEffectiveValue = availableHealth * effectiveCollateralAfterDeposit / safeHealthAfterDeposit !
819809 }
820810
821- log (" availableEffectiveValue, \( availableEffectiveValue ) " )
822811 // The amount of the token we can take using that amount of health
823812 let availableTokenCount = availableEffectiveValue / self .collateralFactor [withdrawType ]! / self .priceOracle .price (ofToken : withdrawType )!
824- log (" availableTokenCount, \( availableTokenCount ) " )
825813
826814 return availableTokenCount
827815 } else {
@@ -1086,8 +1074,6 @@ access(all) contract TidalProtocol {
10861074
10871075 var canWithdraw = false
10881076
1089- log (" requiredDeposit \( requiredDeposit ) " )
1090-
10911077 if requiredDeposit == 0.0 {
10921078 // We can service this withdrawal without any top up
10931079 canWithdraw = true
@@ -1214,10 +1200,6 @@ access(all) contract TidalProtocol {
12141200 return
12151201 }
12161202
1217- log (" balanceSheet" )
1218- log (" pid \( pid ) " )
1219- log (balanceSheet )
1220-
12211203 if balanceSheet .health < position .targetHealth {
12221204 // The position is undercollateralized, see if the source can get more collateral to bring it up to the target health.
12231205 if position .topUpSource ! = nil {
@@ -1231,8 +1213,6 @@ access(all) contract TidalProtocol {
12311213 targetHealth : position .targetHealth
12321214 )
12331215
1234- log (" idealDeposit \( idealDeposit ) " )
1235-
12361216 let pulledVault <- topUpSource .withdrawAvailable (maxAmount : idealDeposit )
12371217
12381218 emit Rebalanced (pid : pid , poolUUID : self .uuid , atHealth : balanceSheet .health , amount : pulledVault .balance , fromUnder : true )
@@ -1244,15 +1224,11 @@ access(all) contract TidalProtocol {
12441224 if position .drawDownSink ! = nil {
12451225 let drawDownSink = position .drawDownSink !
12461226 let sinkType = drawDownSink .getSinkType ()
1247- log (" drawDownSink" )
1248- log (drawDownSink )
1249- log (" target health \( position .targetHealth ) " )
12501227 let idealWithdrawal = self .fundsAvailableAboveTargetHealth (
12511228 pid : pid ,
12521229 type : sinkType ,
12531230 targetHealth : position .targetHealth
12541231 )
1255- log (" idealWithdrawal \( idealWithdrawal ) " )
12561232
12571233 // Compute how many tokens of the sink's type are available to hit our target health.
12581234 let sinkCapacity = drawDownSink .minimumCapacity ()
@@ -1381,6 +1357,8 @@ access(all) contract TidalProtocol {
13811357 interestIndex : tokenState .creditInterestIndex )
13821358
13831359 let value = priceOracle .price (ofToken : type )! * trueBalance
1360+ log (" credit \( self .collateralFactor [type ]! ) " )
1361+ log (value )
13841362
13851363 effectiveCollateral = effectiveCollateral + (value * self .collateralFactor [type ]! )
13861364 } else {
@@ -1393,6 +1371,8 @@ access(all) contract TidalProtocol {
13931371 }
13941372 }
13951373
1374+ log (" here effectiveCollateral, \( effectiveCollateral ) " )
1375+
13961376 return BalanceSheet (effectiveCollateral : effectiveCollateral , effectiveDebt : effectiveDebt )
13971377 }
13981378
0 commit comments