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 checks #90

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
42 changes: 22 additions & 20 deletions contracts/TimeswapPair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {BurnMath} from './libraries/BurnMath.sol';
import {LendMath} from './libraries/LendMath.sol';
import {WithdrawMath} from './libraries/WithdrawMath.sol';
import {BorrowMath} from './libraries/BorrowMath.sol';
import {PayMath} from './libraries/PayMath.sol';
import {SafeTransfer} from './libraries/SafeTransfer.sol';
import {Array} from './libraries/Array.sol';
import {Callback} from './libraries/Callback.sol';
Expand Down Expand Up @@ -147,10 +146,10 @@ contract TimeswapPair is IPair {
)
{
require(block.timestamp < maturity, 'E202');
require(maturity - block.timestamp < 0x100000000, 'E208');
unchecked { require(maturity - block.timestamp < 0x100000000, 'E208'); }
require(liquidityTo != address(0) && dueTo != address(0), 'E201');
require(liquidityTo != address(this) && dueTo != address(this), 'E204');
require(xIncrease > 0 && yIncrease > 0 && zIncrease > 0, 'E205');
require(xIncrease != 0 && yIncrease != 0 && zIncrease != 0, 'E205');

Pool storage pool = pools[maturity];

Expand All @@ -167,7 +166,7 @@ contract TimeswapPair is IPair {
pool.state.totalLiquidity += liquidityTotal;
pool.liquidities[factory.owner()] += liquidityTotal - liquidityOut;
}
require(liquidityOut > 0, 'E212');
require(liquidityOut != 0, 'E212');
pool.liquidities[liquidityTo] += liquidityOut;

dueOut.debt = MintMath.getDebt(maturity, xIncrease, yIncrease);
Expand Down Expand Up @@ -200,7 +199,7 @@ contract TimeswapPair is IPair {
require(block.timestamp >= maturity, 'E203');
require(assetTo != address(0) && collateralTo != address(0), 'E201');
require(assetTo != address(this) && collateralTo != address(this), 'E204');
require(liquidityIn > 0, 'E205');
require(liquidityIn != 0, 'E205');

Pool storage pool = pools[maturity];

Expand All @@ -211,11 +210,14 @@ contract TimeswapPair is IPair {

pool.liquidities[msg.sender] -= liquidityIn;

pool.state.reserves.asset -= tokensOut.asset;
pool.state.reserves.collateral -= tokensOut.collateral;

if (tokensOut.asset > 0) asset.safeTransfer(assetTo, tokensOut.asset);
if (tokensOut.collateral > 0) collateral.safeTransfer(collateralTo, tokensOut.collateral);
if (tokensOut.asset != 0) {
pool.state.reserves.asset -= tokensOut.asset;
asset.safeTransfer(assetTo, tokensOut.asset);
}
if (tokensOut.collateral != 0) {
pool.state.reserves.collateral -= tokensOut.collateral;
collateral.safeTransfer(collateralTo, tokensOut.collateral);
}

emit Burn(maturity, msg.sender, assetTo, collateralTo, liquidityIn, tokensOut);
}
Expand All @@ -233,10 +235,10 @@ contract TimeswapPair is IPair {
require(block.timestamp < maturity, 'E202');
require(bondTo != address(0) && insuranceTo != address(0), 'E201');
require(bondTo != address(this) && insuranceTo != address(this), 'E204');
require(xIncrease > 0, 'E205');
require(xIncrease != 0, 'E205');

Pool storage pool = pools[maturity];
require(pool.state.totalLiquidity > 0, 'E206');
require(pool.state.totalLiquidity != 0, 'E206');

LendMath.check(pool.state, xIncrease, yDecrease, zDecrease, fee);

Expand Down Expand Up @@ -271,7 +273,7 @@ contract TimeswapPair is IPair {
require(block.timestamp >= maturity, 'E203');
require(assetTo != address(0) && collateralTo != address(0), 'E201');
require(assetTo != address(this) && collateralTo != address(this), 'E204');
require(claimsIn.bond > 0 || claimsIn.insurance > 0, 'E205');
require(claimsIn.bond != 0 || claimsIn.insurance != 0, 'E205');

Pool storage pool = pools[maturity];

Expand All @@ -289,8 +291,8 @@ contract TimeswapPair is IPair {
pool.state.reserves.asset -= tokensOut.asset;
pool.state.reserves.collateral -= tokensOut.collateral;

if (tokensOut.asset > 0) asset.safeTransfer(assetTo, tokensOut.asset);
if (tokensOut.collateral > 0) collateral.safeTransfer(collateralTo, tokensOut.collateral);
if (tokensOut.asset != 0) asset.safeTransfer(assetTo, tokensOut.asset);
if (tokensOut.collateral != 0) collateral.safeTransfer(collateralTo, tokensOut.collateral);

emit Withdraw(maturity, msg.sender, assetTo, collateralTo, claimsIn, tokensOut);
}
Expand All @@ -308,10 +310,10 @@ contract TimeswapPair is IPair {
require(block.timestamp < maturity, 'E202');
require(assetTo != address(0) && dueTo != address(0), 'E201');
require(assetTo != address(this) && dueTo != address(this), 'E204');
require(xDecrease > 0, 'E205');
require(xDecrease != 0, 'E205');

Pool storage pool = pools[maturity];
require(pool.state.totalLiquidity > 0, 'E206');
require(pool.state.totalLiquidity != 0, 'E206');

BorrowMath.check(pool.state, xDecrease, yIncrease, zIncrease, fee);

Expand Down Expand Up @@ -360,18 +362,18 @@ contract TimeswapPair is IPair {
Due storage due = dues[ids[i]];
require(due.startBlock != BlockNumber.get(), 'E207');
if (owner != msg.sender) require(collateralsOut[i] == 0, 'E213');
PayMath.checkProportional(assetsIn[i], collateralsOut[i], due);
require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303');
due.debt -= assetsIn[i];
due.collateral -= collateralsOut[i];
assetIn += assetsIn[i];
collateralOut += collateralsOut[i];
}
if (assetIn > 0) Callback.pay(asset, assetIn, data);
if (assetIn != 0) Callback.pay(asset, assetIn, data);

pool.state.reserves.asset += assetIn;
pool.state.reserves.collateral -= collateralOut;

if (collateralOut > 0) collateral.safeTransfer(to, collateralOut);
if (collateralOut != 0) collateral.safeTransfer(to, collateralOut);

emit Pay(maturity, msg.sender, to, owner, ids, assetsIn, collateralsOut, assetIn, collateralOut);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/libraries/FullMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library FullMath {

// Handle non-overflow cases, 256 by 256 division
if (prod1 == 0) {
require(denominator > 0);
require(denominator != 0);
assembly {
result := div(prod0, denominator)
}
Expand Down Expand Up @@ -121,6 +121,6 @@ library FullMath {
uint256 denominator
) internal pure returns (uint256 result) {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) result++;
if (mulmod(a, b, denominator) != 0) result++;
}
}
2 changes: 1 addition & 1 deletion contracts/libraries/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity =0.8.4;
library Math {
function divUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = x / y;
if (x % y > 0) z++;
if (x % y != 0) z++;
}

function shiftRightUp(uint256 x, uint8 y) internal pure returns (uint256 z) {
Expand Down
14 changes: 0 additions & 14 deletions contracts/libraries/PayMath.sol

This file was deleted.

6 changes: 4 additions & 2 deletions contracts/libraries/SafeCast.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ library SafeCast {
}

function toUint112(uint256 x) internal pure returns (uint112 y) {
require((y = uint112(x)) == x);
require(x <= type(uint112).max);
y = uint112(x);
}

function toUint128(uint256 x) internal pure returns (uint128 y) {
require((y = uint128(x)) == x);
require(x <= type(uint128).max);
y = uint128(x);
}

function truncateUint112(uint256 x) internal pure returns (uint112 y) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/WithdrawMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ library WithdrawMath {
{
if (state.reserves.asset >= state.totalClaims.bond) return collateralOut;
uint256 deficit = state.totalClaims.bond;
deficit -= state.reserves.asset;
unchecked { deficit -= state.reserves.asset; }
if (uint256(state.reserves.collateral) * state.totalClaims.bond >= deficit * state.totalClaims.insurance) {
uint256 _collateralOut = deficit;
_collateralOut *= insuranceIn;
Expand Down
16 changes: 0 additions & 16 deletions contracts/test/libraries/PayMathTest.sol

This file was deleted.

20 changes: 0 additions & 20 deletions test/libraries/PayMath.ts

This file was deleted.