@juicedollar/jusd 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/contracts/Leadrate.sol +1 -1
  2. package/contracts/{MintingHubV2 → MintingHubV3}/MintingHub.sol +93 -100
  3. package/contracts/{MintingHubV2 → MintingHubV3}/Position.sol +20 -13
  4. package/contracts/{MintingHubV2 → MintingHubV3}/PositionRoller.sol +17 -37
  5. package/contracts/{MintingHubV2 → MintingHubV3}/interface/IMintingHub.sol +2 -2
  6. package/contracts/{MintingHubV2 → MintingHubV3}/interface/IPosition.sol +3 -3
  7. package/contracts/Savings.sol +36 -6
  8. package/contracts/interface/ISavingsJUSD.sol +10 -1
  9. package/contracts/test/PositionExpirationTest.sol +17 -38
  10. package/contracts/test/PositionRollingTest.sol +3 -3
  11. package/contracts/test/ReentrantAttacker.sol +1 -1
  12. package/dist/index.d.mts +5609 -3568
  13. package/dist/index.d.ts +5609 -3568
  14. package/dist/index.js +9360 -6681
  15. package/dist/index.mjs +9350 -6675
  16. package/exports/abis/shared/Equity.ts +1286 -0
  17. package/exports/abis/shared/JuiceDollar.ts +1366 -0
  18. package/exports/abis/shared/StablecoinBridge.ts +279 -0
  19. package/exports/abis/utils/StartUSD.ts +213 -213
  20. package/exports/abis/{core → v2}/FrontendGateway.ts +1 -1
  21. package/exports/abis/{core → v2}/MintingHubGateway.ts +1 -1
  22. package/exports/abis/{MintingHubV2 → v2}/PositionRoller.ts +1 -1
  23. package/exports/abis/{core → v2}/SavingsGateway.ts +1 -1
  24. package/exports/abis/{core → v2}/SavingsVaultJUSD.ts +1 -1
  25. package/exports/abis/v3/MintingHub.ts +1024 -0
  26. package/exports/abis/v3/Position.ts +1142 -0
  27. package/exports/abis/v3/PositionFactory.ts +90 -0
  28. package/exports/abis/v3/PositionRoller.ts +255 -0
  29. package/exports/abis/v3/Savings.ts +553 -0
  30. package/exports/abis/v3/SavingsVaultJUSD.ts +925 -0
  31. package/exports/address.config.ts +43 -15
  32. package/exports/index.ts +22 -14
  33. package/package.json +9 -9
  34. package/contracts/gateway/FrontendGateway.sol +0 -224
  35. package/contracts/gateway/MintingHubGateway.sol +0 -82
  36. package/contracts/gateway/SavingsGateway.sol +0 -51
  37. package/contracts/gateway/interface/IFrontendGateway.sol +0 -49
  38. package/contracts/gateway/interface/IMintingHubGateway.sol +0 -12
  39. package/exports/abis/core/Equity.ts +0 -1286
  40. package/exports/abis/core/JuiceDollar.ts +0 -1366
  41. package/exports/abis/utils/MintingHubV2.ts +0 -888
  42. package/exports/abis/utils/Savings.ts +0 -453
  43. package/exports/abis/utils/StablecoinBridge.ts +0 -279
  44. /package/contracts/{MintingHubV2 → MintingHubV3}/PositionFactory.sol +0 -0
  45. /package/contracts/{MintingHubV2 → MintingHubV3}/interface/IPositionFactory.sol +0 -0
  46. /package/exports/abis/{MintingHubV2 → v2}/PositionFactoryV2.ts +0 -0
  47. /package/exports/abis/{MintingHubV2 → v2}/PositionV2.ts +0 -0
@@ -16,7 +16,7 @@ interface IPosition {
16
16
 
17
17
  function challengedAmount() external view returns (uint256);
18
18
 
19
- function original() external view returns (address);
19
+ function original() external view returns (address payable);
20
20
 
21
21
  function expiration() external view returns (uint40);
22
22
 
@@ -56,13 +56,13 @@ interface IPosition {
56
56
 
57
57
  function getMintAmount(uint256 usableMint) external view returns (uint256);
58
58
 
59
- function adjust(uint256 newMinted, uint256 newCollateral, uint256 newPrice, bool withdrawAsNative) external payable;
59
+ function adjust(uint256 newPrincipal, uint256 newCollateral, uint256 newPrice, bool withdrawAsNative) external payable;
60
60
 
61
61
  function adjustPrice(uint256 newPrice) external;
62
62
 
63
63
  function adjustPriceWithReference(uint256 newPrice, address referencePosition) external;
64
64
 
65
- function adjustWithReference(uint256 newMinted, uint256 newCollateral, uint256 newPrice, address referencePosition, bool withdrawAsNative) external payable;
65
+ function adjustWithReference(uint256 newPrincipal, uint256 newCollateral, uint256 newPrice, address referencePosition, bool withdrawAsNative) external payable;
66
66
 
67
67
  function isValidPriceReference(address referencePosition, uint256 newPrice) external view returns (bool);
68
68
 
@@ -19,6 +19,8 @@ contract Savings is Leadrate {
19
19
  IERC20 public immutable jusd;
20
20
 
21
21
  mapping(address => Account) public savings;
22
+ mapping(address => bool) public nonCompounding;
23
+ mapping(address => uint192) public claimableInterest;
22
24
 
23
25
  struct Account {
24
26
  uint192 saved;
@@ -26,8 +28,9 @@ contract Savings is Leadrate {
26
28
  }
27
29
 
28
30
  event Saved(address indexed account, uint192 amount);
29
- event InterestCollected(address indexed account, uint256 interest);
31
+ event InterestCollected(address indexed account, uint256 interest, bool compounded);
30
32
  event Withdrawn(address indexed account, uint192 amount);
33
+ event InterestClaimed(address indexed account, uint192 amount);
31
34
 
32
35
  // The module is considered disabled if the interest is zero or about to become zero within three days.
33
36
  error ModuleDisabled();
@@ -53,16 +56,20 @@ contract Savings is Leadrate {
53
56
  return refresh(owner).saved;
54
57
  }
55
58
 
56
- function refresh(address accountOwner) virtual internal returns (Account storage) {
59
+ function refresh(address accountOwner) internal returns (Account storage) {
57
60
  Account storage account = savings[accountOwner];
58
61
  uint64 ticks = currentTicks();
59
62
  if (ticks > account.ticks) {
60
63
  uint192 earnedInterest = calculateInterest(account, ticks);
61
64
  if (earnedInterest > 0) {
62
- // collect interest as you go and trigger accounting event
63
65
  (IJuiceDollar(address(jusd))).distributeProfits(address(this), earnedInterest);
64
- account.saved += earnedInterest;
65
- emit InterestCollected(accountOwner, earnedInterest);
66
+ bool compounded = !nonCompounding[accountOwner];
67
+ if (compounded) {
68
+ account.saved += earnedInterest;
69
+ } else {
70
+ claimableInterest[accountOwner] += earnedInterest;
71
+ }
72
+ emit InterestCollected(accountOwner, earnedInterest, compounded);
66
73
  }
67
74
  account.ticks = ticks;
68
75
  }
@@ -93,12 +100,21 @@ contract Savings is Leadrate {
93
100
  }
94
101
 
95
102
  /**
96
- * Save 'amount'.
103
+ * Save 'amount' with default compounding behavior.
97
104
  */
98
105
  function save(uint192 amount) public {
99
106
  save(msg.sender, amount);
100
107
  }
101
108
 
109
+ /**
110
+ * Save 'amount' and set the compounding preference.
111
+ * Pending interest is settled under the OLD mode before applying the new preference.
112
+ */
113
+ function save(uint192 amount, bool compound) public {
114
+ save(msg.sender, amount);
115
+ nonCompounding[msg.sender] = !compound;
116
+ }
117
+
102
118
  function adjust(uint192 targetAmount) public {
103
119
  Account storage balance = refresh(msg.sender);
104
120
  if (balance.saved < targetAmount) {
@@ -138,4 +154,18 @@ contract Savings is Leadrate {
138
154
  emit Withdrawn(msg.sender, amount);
139
155
  return amount;
140
156
  }
157
+
158
+ /**
159
+ * Claim accumulated non-compounding interest and transfer to target.
160
+ */
161
+ function claimInterest(address target) public returns (uint192) {
162
+ refresh(msg.sender);
163
+ uint192 interest = claimableInterest[msg.sender];
164
+ if (interest > 0) {
165
+ claimableInterest[msg.sender] = 0;
166
+ jusd.transfer(target, interest);
167
+ emit InterestClaimed(msg.sender, interest);
168
+ }
169
+ return interest;
170
+ }
141
171
  }
@@ -10,8 +10,9 @@ interface ISavingsJUSD {
10
10
  // ---------------------------------------------------------------------------------------
11
11
 
12
12
  event Saved(address indexed account, uint192 amount);
13
- event InterestCollected(address indexed account, uint256 interest);
13
+ event InterestCollected(address indexed account, uint256 interest, bool compounded);
14
14
  event Withdrawn(address indexed account, uint192 amount);
15
+ event InterestClaimed(address indexed account, uint192 amount);
15
16
 
16
17
  // ---------------------------------------------------------------------------------------
17
18
 
@@ -21,6 +22,10 @@ interface ISavingsJUSD {
21
22
 
22
23
  function savings(address account) external view returns (Account memory);
23
24
 
25
+ function nonCompounding(address account) external view returns (bool);
26
+
27
+ function claimableInterest(address account) external view returns (uint192);
28
+
24
29
  // ---------------------------------------------------------------------------------------
25
30
 
26
31
  function currentTicks() external view returns (uint64);
@@ -37,6 +42,8 @@ interface ISavingsJUSD {
37
42
 
38
43
  function save(uint192 amount) external;
39
44
 
45
+ function save(uint192 amount, bool compound) external;
46
+
40
47
  function save(address owner, uint192 amount) external;
41
48
 
42
49
  function withdraw(address target, uint192 amount) external returns (uint256);
@@ -46,4 +53,6 @@ interface ISavingsJUSD {
46
53
  function refreshMyBalance() external returns (uint192);
47
54
 
48
55
  function refreshBalance(address owner) external returns (uint192);
56
+
57
+ function claimInterest(address target) external returns (uint192);
49
58
  }
@@ -1,59 +1,38 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  pragma solidity ^0.8.0;
3
3
 
4
- import {Position} from "../MintingHubV2/Position.sol";
5
- import {MintingHubGateway} from "../gateway/MintingHubGateway.sol";
6
- import {IMintingHubGateway} from "../gateway/interface/IMintingHubGateway.sol";
4
+ import {Position} from "../MintingHubV3/Position.sol";
5
+ import {MintingHub} from "../MintingHubV3/MintingHub.sol";
7
6
  import {IJuiceDollar} from "../interface/IJuiceDollar.sol";
8
7
  import {TestToken} from "./TestToken.sol";
9
- import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
10
8
 
11
9
  contract PositionExpirationTest {
12
- MintingHubGateway public hub;
10
+ MintingHub public hub;
13
11
  TestToken public col;
14
12
  IJuiceDollar public jusd;
15
- bytes32 public frontendCode;
16
13
 
17
14
  constructor(address payable hub_) {
18
- hub = MintingHubGateway(hub_);
15
+ hub = MintingHub(hub_);
19
16
  col = new TestToken("Some Collateral", "COL", uint8(0));
20
17
  jusd = hub.JUSD();
21
18
  }
22
19
 
23
- function openPositionFor(address owner, bytes32 frontendCode_) public returns (address) {
24
- frontendCode = frontendCode_;
20
+ function openPositionFor(address owner) public returns (address) {
25
21
  col.mint(address(this), 100);
26
22
  col.approve(address(hub), 100);
27
23
  jusd.approve(address(hub), hub.OPENING_FEE());
28
- address pos;
29
- if (IERC165(hub).supportsInterface(type(IMintingHubGateway).interfaceId)) {
30
- pos = hub.openPosition(
31
- address(col),
32
- 10,
33
- 100 /* collateral */,
34
- 1000000 * 10 ** 18,
35
- 14 days,
36
- 30 days,
37
- 1 days,
38
- 50000,
39
- 1000 * 10 ** 36 /* price */,
40
- 200000,
41
- frontendCode
42
- );
43
- } else {
44
- pos = hub.openPosition(
45
- address(col),
46
- 10,
47
- 100 /* collateral */,
48
- 1000000 * 10 ** 18,
49
- 14 days,
50
- 30 days,
51
- 1 days,
52
- 50000,
53
- 1000 * 10 ** 36 /* price */,
54
- 200000
55
- );
56
- }
24
+ address pos = hub.openPosition(
25
+ address(col),
26
+ 10,
27
+ 100 /* collateral */,
28
+ 1000000 * 10 ** 18,
29
+ 14 days,
30
+ 30 days,
31
+ 1 days,
32
+ 50000,
33
+ 1000 * 10 ** 36 /* price */,
34
+ 200000
35
+ );
57
36
  Position(payable(pos)).transferOwnership(owner);
58
37
  return pos;
59
38
  }
@@ -4,10 +4,10 @@ pragma solidity ^0.8.0;
4
4
  // import "./Strings.sol";
5
5
  import "./TestToken.sol";
6
6
  import "../Equity.sol";
7
- import "../MintingHubV2/Position.sol";
8
- import "../MintingHubV2/MintingHub.sol";
7
+ import "../MintingHubV3/Position.sol";
8
+ import "../MintingHubV3/MintingHub.sol";
9
9
  import "../StablecoinBridge.sol";
10
- import "../MintingHubV2/interface/IPosition.sol";
10
+ import "../MintingHubV3/interface/IPosition.sol";
11
11
  import "../interface/IReserve.sol";
12
12
  import "../interface/IJuiceDollar.sol";
13
13
 
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  pragma solidity ^0.8.0;
3
3
 
4
- import {IPosition} from "../MintingHubV2/interface/IPosition.sol";
4
+ import {IPosition} from "../MintingHubV3/interface/IPosition.sol";
5
5
  import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
6
6
 
7
7
  /**