Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use safecast for any downcasting #2306

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions contracts/contracts/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pragma solidity ^0.8.0;
* @author Origin Protocol Inc
*/
import { Governable } from "../governance/Governable.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";

/**
* NOTE that this is an ERC20 token but the invariant that the sum of
Expand All @@ -16,6 +17,9 @@ import { Governable } from "../governance/Governable.sol";
*/

contract OUSD is Governable {
using SafeCast for int256;
using SafeCast for uint256;

event TotalSupplyUpdatedHighres(
uint256 totalSupply,
uint256 rebasingCredits,
Expand Down Expand Up @@ -257,11 +261,11 @@ contract OUSD is Governable {
(
int256 fromRebasingCreditsDiff,
int256 fromNonRebasingSupplyDiff
) = _adjustAccount(_from, -int256(_value));
) = _adjustAccount(_from, -_value.toInt256());
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_to, int256(_value));
) = _adjustAccount(_to, _value.toInt256());

_adjustGlobals(
fromRebasingCreditsDiff + toRebasingCreditsDiff,
Expand All @@ -274,43 +278,41 @@ contract OUSD is Governable {
returns (int256 rebasingCreditsDiff, int256 nonRebasingSupplyDiff)
{
RebaseOptions state = rebaseState[account];
int256 currentBalance = int256(balanceOf(account));
uint256 newBalance = uint256(
int256(currentBalance) + int256(balanceChange)
);
int256 currentBalance = balanceOf(account).toInt256();
int256 newBalance = currentBalance + balanceChange;
if (newBalance < 0) {
revert("Transfer amount exceeds balance");
}
if (state == RebaseOptions.YieldDelegationSource) {
address target = yieldTo[account];
uint256 targetOldBalance = balanceOf(target);
uint256 targetNewCredits = _balanceToRebasingCredits(
targetOldBalance + newBalance
targetOldBalance + newBalance.toUint256()
);
rebasingCreditsDiff =
int256(targetNewCredits) -
int256(_creditBalances[target]);
targetNewCredits.toInt256() -
_creditBalances[target].toInt256();

_creditBalances[account] = newBalance;
_creditBalances[account] = newBalance.toUint256();
_creditBalances[target] = targetNewCredits;
alternativeCreditsPerToken[account] = 1e18;
} else if (state == RebaseOptions.YieldDelegationTarget) {
uint256 newCredits = _balanceToRebasingCredits(
newBalance + _creditBalances[yieldFrom[account]]
newBalance.toUint256() + _creditBalances[yieldFrom[account]]
);
rebasingCreditsDiff =
int256(newCredits) -
int256(_creditBalances[account]);
newCredits.toInt256() -
_creditBalances[account].toInt256();
_creditBalances[account] = newCredits;
} else if (_isNonRebasingAccount(account)) {
nonRebasingSupplyDiff = balanceChange;
alternativeCreditsPerToken[account] = 1e18;
_creditBalances[account] = newBalance;
_creditBalances[account] = newBalance.toUint256();
} else {
uint256 newCredits = _balanceToRebasingCredits(newBalance);
uint256 newCredits = _balanceToRebasingCredits(newBalance.toUint256());
rebasingCreditsDiff =
int256(newCredits) -
int256(_creditBalances[account]);
newCredits.toInt256() -
_creditBalances[account].toInt256();
_creditBalances[account] = newCredits;
}
}
Expand All @@ -320,20 +322,16 @@ contract OUSD is Governable {
int256 nonRebasingSupplyDiff
) internal {
if (rebasingCreditsDiff != 0) {
if (uint256(int256(_rebasingCredits) + rebasingCreditsDiff) < 0) {
if (_rebasingCredits.toInt256() + rebasingCreditsDiff < 0) {
revert("rebasingCredits underflow");
}
_rebasingCredits = uint256(
int256(_rebasingCredits) + rebasingCreditsDiff
);
_rebasingCredits = (_rebasingCredits.toInt256() + rebasingCreditsDiff).toUint256();
}
if (nonRebasingSupplyDiff != 0) {
if (int256(nonRebasingSupply) + nonRebasingSupplyDiff < 0) {
if (nonRebasingSupply.toInt256() + nonRebasingSupplyDiff < 0) {
revert("nonRebasingSupply underflow");
}
nonRebasingSupply = uint256(
int256(nonRebasingSupply) + nonRebasingSupplyDiff
);
nonRebasingSupply = (nonRebasingSupply.toInt256() + nonRebasingSupplyDiff).toUint256();
}
}

Expand Down Expand Up @@ -430,7 +428,7 @@ contract OUSD is Governable {
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_account, int256(_amount));
) = _adjustAccount(_account, _amount.toInt256());
// Globals
_adjustGlobals(toRebasingCreditsDiff, toNonRebasingSupplyDiff);
_totalSupply = _totalSupply + _amount;
Expand Down Expand Up @@ -467,7 +465,7 @@ contract OUSD is Governable {
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_account, -int256(_amount));
) = _adjustAccount(_account, -_amount.toInt256());
// Globals
_adjustGlobals(toRebasingCreditsDiff, toNonRebasingSupplyDiff);
_totalSupply = _totalSupply - _amount;
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/flipper/flipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("Flipper", function () {
// eslint-disable-next-line
`buyOusdWith${titleName}`
](ousdUnits("1"));
await expect(call).to.be.revertedWith("Transfer greater than balance");
await expect(call).to.be.revertedWith("Transfer amount exceeds balance");
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/vault/oeth-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ describe("OETH Vault", function () {
.connect(josh)
.requestWithdrawal(dataBefore.userOeth.add(1));

await expect(tx).to.revertedWith("Remove exceeds balance");
await expect(tx).to.revertedWith("Transfer amount exceeds balance");
});
it("capital is paused", async () => {
const { oethVault, governor, josh } = fixture;
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/vault/redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe("Vault Redeem", function () {
// Try to withdraw more than balance
await expect(
vault.connect(anna).redeem(ousdUnits("100.0"), 0)
).to.be.revertedWith("Remove exceeds balance");
).to.be.revertedWith("Transfer amount exceeds balance");
});

it("Should only allow Governor to set a redeem fee", async () => {
Expand Down
Loading