@@ -54,18 +54,18 @@ func GetInterest(borrowAmount sdkmath.Int, startBorrowIndex sdkmath.LegacyDec, b
5454}
5555
5656// GetTotalInterest calculates the total loan interest based on the given params
57- func GetTotalInterest (borrowAmount sdkmath.Int , maturity int64 , borrowAPR uint32 , blocksPerYear uint64 ) sdkmath.Int {
57+ func GetTotalInterest (borrowAmount sdkmath.Int , maturity int64 , borrowAPR sdkmath. LegacyDec , blocksPerYear uint64 ) sdkmath.Int {
5858 totalBlocks := uint64 (maturity ) * blocksPerYear / uint64 (OneYear )
5959
60- borrowRatePerBlock := sdkmath . LegacyNewDec ( int64 ( borrowAPR )). Quo ( sdkmath . LegacyNewDec ( 1000 )) .Quo (sdkmath .LegacyNewDec (int64 (blocksPerYear )))
60+ borrowRatePerBlock := borrowAPR .Quo (sdkmath .LegacyNewDec (int64 (blocksPerYear )))
6161 borrowIndexRatio := sdkmath .LegacyOneDec ().Add (borrowRatePerBlock )
6262
6363 return borrowAmount .ToLegacyDec ().Mul (borrowIndexRatio .Power (totalBlocks )).TruncateInt ().Sub (borrowAmount )
6464}
6565
6666// GetProtocolFee calculates the protocol fee based on the given interest and reserve factor
67- func GetProtocolFee (interest sdkmath.Int , reserveFactor uint32 ) sdkmath.Int {
68- return interest .Mul ( sdkmath . NewInt ( int64 ( reserveFactor ))). Quo ( Permille )
67+ func GetProtocolFee (interest sdkmath.Int , reserveFactor sdkmath. LegacyDec ) sdkmath.Int {
68+ return interest .ToLegacyDec (). Mul ( reserveFactor ). TruncateInt ( )
6969}
7070
7171// GetLiquidationPrice calculates the liquidation price according to the liquidation LTV
@@ -74,14 +74,14 @@ func GetProtocolFee(interest sdkmath.Int, reserveFactor uint32) sdkmath.Int {
7474// liquidation price = (borrow amount + interest) / lltv / collateral amount
7575// 2. collateral is NOT the base price asset:
7676// liquidation price = collateral amount * lltv / (borrow amount + interest)
77- func GetLiquidationPrice (collateralAmount sdkmath.Int , collateralAssetDecimals int , borrowAmount sdkmath.Int , borrowAssetDecimals int , maturity int64 , borrowAPR uint32 , blocksPerYear uint64 , lltv uint32 , collateralIsBaseAsset bool ) sdkmath.LegacyDec {
77+ func GetLiquidationPrice (collateralAmount sdkmath.Int , collateralAssetDecimals int , borrowAmount sdkmath.Int , borrowAssetDecimals int , maturity int64 , borrowAPR sdkmath. LegacyDec , blocksPerYear uint64 , lltv sdkmath. LegacyDec , collateralIsBaseAsset bool ) sdkmath.LegacyDec {
7878 interest := GetTotalInterest (borrowAmount , maturity , borrowAPR , blocksPerYear )
7979
8080 var liquidationPrice sdkmath.LegacyDec
8181 if collateralIsBaseAsset {
82- liquidationPrice = borrowAmount .Add (interest ).Mul (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )).Mul ( Percent ). ToLegacyDec ().QuoInt ( sdkmath . NewInt ( int64 ( lltv )) ).QuoInt (collateralAmount ).QuoInt (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals ))
82+ liquidationPrice = borrowAmount .Add (interest ).Mul (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )).ToLegacyDec ().Quo ( lltv ).QuoInt (collateralAmount ).QuoInt (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals ))
8383 } else {
84- liquidationPrice = collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).Mul ( sdkmath . NewInt ( int64 ( lltv ))). ToLegacyDec ().QuoInt ( Percent ).QuoInt (borrowAmount .Add (interest )).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals ))
84+ liquidationPrice = collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).ToLegacyDec ().Mul ( lltv ).QuoInt (borrowAmount .Add (interest )).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals ))
8585 }
8686
8787 return NormalizePrice (liquidationPrice , collateralIsBaseAsset )
@@ -97,12 +97,12 @@ func ToBeLiquidated(price sdkmath.LegacyDec, liquidationPrice sdkmath.LegacyDec,
9797}
9898
9999// CheckLTV returns true if the collateral amount and borrow amount satisfy the max LTV limitation by the given price, false otherwise
100- func CheckLTV (collateralAmount sdkmath.Int , collateralAssetDecimals int , borrowAmount sdkmath.Int , borrowAssetDecimals int , maxLTV uint32 , price sdkmath.LegacyDec , collateralIsBaseAsset bool ) bool {
100+ func CheckLTV (collateralAmount sdkmath.Int , collateralAssetDecimals int , borrowAmount sdkmath.Int , borrowAssetDecimals int , maxLTV sdkmath. LegacyDec , price sdkmath.LegacyDec , collateralIsBaseAsset bool ) bool {
101101 if collateralIsBaseAsset {
102- return collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).Mul ( sdkmath . NewInt ( int64 ( maxLTV ))). ToLegacyDec (). Mul (price ).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )). QuoInt ( Percent ).TruncateInt ().GTE (borrowAmount )
102+ return collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).ToLegacyDec (). Mul ( maxLTV ). Mul (price ).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )).TruncateInt ().GTE (borrowAmount )
103103 }
104104
105- return collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).Mul ( sdkmath . NewInt ( int64 ( maxLTV ))). ToLegacyDec (). Quo (price ).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )). QuoInt ( Percent ).TruncateInt ().GTE (borrowAmount )
105+ return collateralAmount .Mul (sdkmath .NewIntWithDecimal (1 , borrowAssetDecimals )).ToLegacyDec (). Mul ( maxLTV ). Quo (price ).QuoInt (sdkmath .NewIntWithDecimal (1 , collateralAssetDecimals )).TruncateInt ().GTE (borrowAmount )
106106}
107107
108108// GetPricePair gets the price pair from the given pool config
@@ -292,23 +292,23 @@ func ValidatePoolConfig(config PoolConfig) error {
292292 }
293293
294294 if config .SupplyCap .IsNil () || config .SupplyCap .IsNegative () {
295- return errorsmod .Wrap (ErrInvalidPoolConfig , "supply cap can not be nil or negative" )
295+ return errorsmod .Wrap (ErrInvalidPoolConfig , "supply cap cannot be nil or negative" )
296296 }
297297
298298 if config .BorrowCap .IsNil () || config .BorrowCap .IsNegative () {
299- return errorsmod .Wrap (ErrInvalidPoolConfig , "borrow cap can not be nil or negative" )
299+ return errorsmod .Wrap (ErrInvalidPoolConfig , "borrow cap cannot be nil or negative" )
300300 }
301301
302302 if config .MinBorrowAmount .IsNil () || config .MinBorrowAmount .IsNegative () {
303- return errorsmod .Wrap (ErrInvalidPoolConfig , "min borrow amount can not be nil or negative" )
303+ return errorsmod .Wrap (ErrInvalidPoolConfig , "min borrow amount cannot be nil or negative" )
304304 }
305305
306306 if config .MaxBorrowAmount .IsNil () || config .MaxBorrowAmount .IsNegative () {
307- return errorsmod .Wrap (ErrInvalidPoolConfig , "max borrow amount can not be nil or negative" )
307+ return errorsmod .Wrap (ErrInvalidPoolConfig , "max borrow amount cannot be nil or negative" )
308308 }
309309
310310 if config .MinBorrowAmount .IsPositive () && config .MaxBorrowAmount .IsPositive () && config .MaxBorrowAmount .LT (config .MinBorrowAmount ) {
311- return errorsmod .Wrap (ErrInvalidPoolConfig , "max borrow amount can not be less than min borrow amount" )
311+ return errorsmod .Wrap (ErrInvalidPoolConfig , "max borrow amount cannot be less than min borrow amount" )
312312 }
313313
314314 if err := validatePoolTranches (config .Tranches ); err != nil {
@@ -319,19 +319,19 @@ func ValidatePoolConfig(config PoolConfig) error {
319319 return errorsmod .Wrap (ErrInvalidPoolConfig , "invalid request fee" )
320320 }
321321
322- if config .OriginationFeeFactor >= 1000 {
322+ if config .OriginationFeeFactor . IsNegative () || config . OriginationFeeFactor . GTE ( sdkmath . LegacyOneDec ()) {
323323 return errorsmod .Wrap (ErrInvalidPoolConfig , "invalid origination fee factor" )
324324 }
325325
326- if config .ReserveFactor >= 1000 {
326+ if config .ReserveFactor . IsNegative () || config . ReserveFactor . GTE ( sdkmath . LegacyOneDec ()) {
327327 return errorsmod .Wrap (ErrInvalidPoolConfig , "invalid reserve factor" )
328328 }
329329
330- if config .LiquidationThreshold == 0 || config .LiquidationThreshold >= 100 {
330+ if ! config .LiquidationThreshold . IsPositive () || config .LiquidationThreshold . GTE ( sdkmath . LegacyOneDec ()) {
331331 return errorsmod .Wrap (ErrInvalidPoolConfig , "invalid liquidation threshold" )
332332 }
333333
334- if config .MaxLtv == 0 || config .MaxLtv >= 100 || config .MaxLtv >= config .LiquidationThreshold {
334+ if ! config .MaxLtv . IsPositive () || config .MaxLtv . GTE ( sdkmath . LegacyOneDec ()) || config .MaxLtv . GTE ( config .LiquidationThreshold ) {
335335 return errorsmod .Wrap (ErrInvalidPoolConfig , "invalid max ltv" )
336336 }
337337
@@ -379,16 +379,16 @@ func validateAssetMetadata(metadata AssetMetadata) error {
379379// validatePoolTrancheConfig validates the given tranche config
380380func validatePoolTranches (tranches []PoolTrancheConfig ) error {
381381 if len (tranches ) == 0 {
382- return errorsmod .Wrap (ErrInvalidPoolConfig , "tranches can not be empty" )
382+ return errorsmod .Wrap (ErrInvalidPoolConfig , "tranches cannot be empty" )
383383 }
384384
385385 for _ , tranche := range tranches {
386386 if tranche .Maturity <= 0 {
387387 return errorsmod .Wrap (ErrInvalidPoolConfig , "maturity must be greater than 0" )
388388 }
389389
390- if tranche .BorrowAPR == 0 || tranche .BorrowAPR >= 1000 {
391- return errorsmod .Wrap (ErrInvalidPoolConfig , "borrow apr must be between (0, 1000 )" )
390+ if ! tranche .BorrowAPR . IsPositive () || tranche .BorrowAPR . GTE ( sdkmath . LegacyOneDec ()) {
391+ return errorsmod .Wrap (ErrInvalidPoolConfig , "borrow apr must be between (0, 1 )" )
392392 }
393393 }
394394
0 commit comments