-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nft staking compound interest tested
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.20; | ||
|
||
import {UD60x18, ud} from "@prb/math/src/UD60x18.sol"; | ||
|
||
contract NFTStakingAndBorrowing { | ||
uint256 internal constant YEAR_IN_SECONDS = 31536000; // 365 days | ||
uint256 internal constant DECIMALS_MULTIPLIER = 1e18; | ||
uint256 internal constant BPS = 1e4; | ||
uint256 internal lastUpdateTimestamp; | ||
|
||
uint256 public constant PROTOCOL_YIELD = 1200 * DECIMALS_MULTIPLIER / BPS; | ||
|
||
function calculateDebt(uint256 borrowedAmount, uint256 currentTime, uint256 expirationTime) | ||
public | ||
pure | ||
returns (uint256) | ||
{ | ||
UD60x18 timeDelta = ud(expirationTime) - ud(currentTime); | ||
|
||
UD60x18 debtLog2 = (timeDelta / ud(YEAR_IN_SECONDS)) * (ud(DECIMALS_MULTIPLIER) + ud(PROTOCOL_YIELD)).log2() | ||
+ ud(borrowedAmount).log2(); | ||
|
||
return debtLog2.exp2().intoUint256(); | ||
} | ||
|
||
function calculateMaxBorrow(uint256 totalAmount, uint256 currentTime, uint256 expirationTime) | ||
public | ||
pure | ||
returns (uint256) | ||
{ | ||
UD60x18 timeDelta = ud(expirationTime) - ud(currentTime); | ||
|
||
UD60x18 maxBorrow = ud(totalAmount).log2() | ||
- (timeDelta / ud(YEAR_IN_SECONDS)) * (ud(DECIMALS_MULTIPLIER) + ud(PROTOCOL_YIELD)).log2(); | ||
|
||
return maxBorrow.exp2().intoUint256(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {NFTStakingAndBorrowing} from "../src/NFTStakingAndBorrowing.sol"; | ||
|
||
contract NFTStakingAndBorrowingTest is Test { | ||
NFTStakingAndBorrowing public nftStaking; | ||
|
||
function setUp() public { | ||
nftStaking = new NFTStakingAndBorrowing(); | ||
} | ||
|
||
function test_calculateDebt() public view { | ||
uint256 newDebt = nftStaking.calculateDebt(89896e16, 1706745600, 1735689600); | ||
assertEq(newDebt, 997500388704920390522); | ||
} | ||
|
||
function test_calculateMaxBorrow() public view { | ||
uint256 maxBorrow = nftStaking.calculateMaxBorrow(9975e17, 1706745600, 1735689600); | ||
assertEq(maxBorrow, 898959649694196407801); | ||
} | ||
|
||
function testFuzz_MaxBorrow(uint64 x) public view { | ||
uint256 x256 = (uint256(x) + uint256(2)) * 1e18; | ||
uint256 maxBorrow = nftStaking.calculateMaxBorrow(x256, 1706745600, 1735689600); | ||
assertLe(maxBorrow, x256); | ||
} | ||
} |