Skip to content

Commit 637cd3c

Browse files
committed
round in the favour of the protocol
1 parent 3ef2219 commit 637cd3c

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

contracts/contracts/token/WOETH.sol

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,22 @@ contract WOETH is ERC4626, Governable, Initializable {
111111
* be able to rebase.
112112
*
113113
* @param oethAmount Amount of OETH to be converted to OETH credits
114+
* @param roundUp when true round the amount of credits returned up
114115
* @return amount of OETH credits the OETH amount corresponds to
115116
*/
116-
function _oethToCredits(uint256 oethAmount) internal returns (uint256) {
117+
function _oethToCredits(uint256 oethAmount, bool roundUp) internal returns (uint256) {
117118
uint256 creditsPerTokenHighres = OETH(asset())
118119
.rebasingCreditsPerTokenHighres();
119120

120121
/**
121122
* Multiplying OETH amount with the creditsPerTokenHighres is exactly the math that
122-
* is internally being done in OETH. (Except that in case of OUSD it is being rounded up)
123+
* is internally being done in OETH. OETH is always rounding up, WOETH is rounding
124+
* up / down depending on what favours this contract
123125
*/
124126
// solhint-disable-next-line max-line-length
125127
/** https://github.com/OriginProtocol/origin-dollar/blob/c02572bd1c06eb3c2652c8692e52144be2efa741/contracts/contracts/token/OUSD.sol#L498
126-
*
127-
* This should make sure that the rounding will always be correct / mimic the rounding
128-
* of OETH.
129128
*/
130-
return oethAmount.mulTruncate(creditsPerTokenHighres);
129+
return (oethAmount + (roundUp ? 1e18 - 1 : 0)).mulTruncate(creditsPerTokenHighres);
131130
}
132131

133132
/** @dev See {IERC4262-totalAssets} */
@@ -145,7 +144,7 @@ contract WOETH is ERC4626, Governable, Initializable {
145144
returns (uint256 woethAmount)
146145
{
147146
woethAmount = super.deposit(oethAmount, receiver);
148-
oethCreditsHighres += _oethToCredits(oethAmount);
147+
oethCreditsHighres += _oethToCredits(oethAmount, false);
149148
}
150149

151150
/** @dev See {IERC4262-mint} */
@@ -155,7 +154,7 @@ contract WOETH is ERC4626, Governable, Initializable {
155154
returns (uint256 oethAmount)
156155
{
157156
oethAmount = super.mint(woethAmount, receiver);
158-
oethCreditsHighres += _oethToCredits(oethAmount);
157+
oethCreditsHighres += _oethToCredits(oethAmount, false);
159158
}
160159

161160
/** @dev See {IERC4262-withdraw} */
@@ -165,7 +164,7 @@ contract WOETH is ERC4626, Governable, Initializable {
165164
address owner
166165
) public override returns (uint256 woethAmount) {
167166
woethAmount = super.withdraw(oethAmount, receiver, owner);
168-
oethCreditsHighres -= _oethToCredits(oethAmount);
167+
oethCreditsHighres -= _oethToCredits(oethAmount, true);
169168
}
170169

171170
/** @dev See {IERC4262-redeem} */
@@ -175,6 +174,6 @@ contract WOETH is ERC4626, Governable, Initializable {
175174
address owner
176175
) public override returns (uint256 oethAmount) {
177176
oethAmount = super.redeem(woethAmount, receiver, owner);
178-
oethCreditsHighres -= _oethToCredits(oethAmount);
177+
oethCreditsHighres -= _oethToCredits(oethAmount, true);
179178
}
180179
}

0 commit comments

Comments
 (0)