Skip to content

Commit

Permalink
Merge pull request #189 from DistributedCollective/development_bzx_co…
Browse files Browse the repository at this point in the history
…mpare

Development bzx compare
  • Loading branch information
tjcloa committed Apr 28, 2021
2 parents fd8de64 + a26cd99 commit 20e718e
Show file tree
Hide file tree
Showing 58 changed files with 657 additions and 432 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
.DS_Store
.history
.hypothesis/
.gradle/
build/
reports/
node_modules/
Expand All @@ -26,4 +27,7 @@ solc-select
.env
node_modules
artifacts/
cache/
cache/
contracts_bzx/
tmp/
tmp
12 changes: 5 additions & 7 deletions contracts/connectors/loantoken/AdvancedToken.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down Expand Up @@ -41,13 +41,12 @@ contract AdvancedToken is AdvancedTokenStorage {
uint256 _assetAmount,
uint256 _price
) internal returns (uint256) {
require(_tokenAmount <= balances[_who], "16");
// no need to require value <= totalSupply, since that would imply the
// sender's balance is greater than the totalSupply, which *should* be an assertion failure
//bzx compare
//TODO: Unit test
uint256 _balance = balances[_who].sub(_tokenAmount, "16");

uint256 _balance = balances[_who].sub(_tokenAmount);
// a rounding error may leave dust behind, so we clear this out
if (_balance <= 10) {
// we can't leave such small balance quantities
_tokenAmount = _tokenAmount.add(_balance);
_balance = 0;
}
Expand All @@ -57,7 +56,6 @@ contract AdvancedToken is AdvancedTokenStorage {

emit Burn(_who, _tokenAmount, _assetAmount, _price);
emit Transfer(_who, address(0), _tokenAmount);

return _balance;
}
}
8 changes: 3 additions & 5 deletions contracts/connectors/loantoken/AdvancedTokenStorage.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand All @@ -10,18 +10,16 @@ import "./LoanTokenBase.sol";
contract AdvancedTokenStorage is LoanTokenBase {
using SafeMath for uint256;

// topic: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
event Transfer(address indexed from, address indexed to, uint256 value);

// topic: 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925
event Approval(address indexed owner, address indexed spender, uint256 value);

// topic: 0xb4c03061fb5b7fed76389d5af8f2e0ddb09f8c70d1333abbb62582835e10accb
event Mint(address indexed minter, uint256 tokenAmount, uint256 assetAmount, uint256 price);

// topic: 0x743033787f4738ff4d6a7225ce2bd0977ee5f86b91a902a58f5e4d0b297b4644
event Burn(address indexed burner, uint256 tokenAmount, uint256 assetAmount, uint256 price);

event FlashBorrow(address borrower, address target, address loanToken, uint256 loanAmount);

mapping(address => uint256) internal balances;
mapping(address => mapping(address => uint256)) internal allowed;
uint256 internal totalSupply_;
Expand Down
1 change: 0 additions & 1 deletion contracts/connectors/loantoken/LoanToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pragma solidity 0.5.17;

import "./AdvancedTokenStorage.sol";

//@todo can I change this proxy to EIP-1822 proxy standard, please. https://eips.ethereum.org/EIPS/eip-1822. It's really hard to work with this.
contract LoanToken is AdvancedTokenStorage {
// It is important to maintain the variables order so the delegate calls can access sovrynContractAddress and wrbtcTokenAddress
address public sovrynContractAddress;
Expand Down
4 changes: 2 additions & 2 deletions contracts/connectors/loantoken/LoanTokenBase.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand All @@ -13,7 +13,7 @@ import "../../openzeppelin/Address.sol";
import "../../interfaces/IWrbtcERC20.sol";
import "./Pausable.sol";

contract LoanTokenBase is ReentrancyGuard, Ownable {
contract LoanTokenBase is ReentrancyGuard, Ownable, Pausable {
uint256 internal constant WEI_PRECISION = 10**18;
uint256 internal constant WEI_PERCENT_PRECISION = 10**20;

Expand Down
3 changes: 1 addition & 2 deletions contracts/connectors/loantoken/LoanTokenLogicDai.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ contract LoanTokenLogicDai is LoanTokenLogicStandard {
return returnData;
}

// ***** NOTE: Reentrancy is allowed here to allow flashloan use cases *****
function borrow(
bytes32 loanId, // 0 if new loan
uint256 withdrawAmount,
Expand All @@ -125,7 +124,7 @@ contract LoanTokenLogicDai is LoanTokenLogicStandard {
address collateralToken, // if address(0), this means ETH and ETH must be sent with the call or loanId must be provided
address borrower,
address receiver,
bytes memory /*loanDataBytes*/ // arbitrary order data (for future use)
bytes memory // arbitrary order data (for future use) /*loanDataBytes*/
)
public
payable
Expand Down
80 changes: 39 additions & 41 deletions contracts/connectors/loantoken/LoanTokenLogicStandard.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand All @@ -11,12 +11,14 @@ import "./interfaces/ProtocolLike.sol";
import "./interfaces/FeedsLike.sol";

contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
using SafeMath for uint256;
using SignedSafeMath for int256;

// DON'T ADD VARIABLES HERE, PLEASE

uint256 public constant VERSION = 5;
uint256 public constant VERSION = 6;
address internal constant arbitraryCaller = 0x000F400e6818158D541C3EBE45FE3AA0d47372FF;
bytes32 internal constant iToken_ProfitSoFar = 0x37aa2b7d583612f016e4a4de4292cb015139b3d7762663d06a53964912ea2fb6; // keccak256("iToken_ProfitSoFar")

function() external {
revert("loan token logic - fallback not allowed");
Expand Down Expand Up @@ -53,7 +55,6 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
payable
nonReentrant
pausable(msg.sig)
settlesInterest
returns (bytes memory)
{
require(borrowAmount != 0, "38");
Expand All @@ -73,6 +74,8 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
// transfer assets to calling contract
_safeTransfer(loanTokenAddress, borrower, borrowAmount, "39");
emit FlashBorrow(borrower, target, loanTokenAddress, borrowAmount);
bytes memory callData;
if (bytes(signature).length == 0) {
callData = data;
Expand Down Expand Up @@ -123,11 +126,11 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
address collateralTokenAddress, // if address(0), this means ETH and ETH must be sent with the call or loanId must be provided
address borrower,
address receiver,
bytes memory /*loanDataBytes*/ // arbitrary order data (for future use)
bytes memory // arbitrary order data (for future use) /*loanDataBytes*/
)
public
payable
nonReentrant //note: needs to be removed to allow flashloan use cases
nonReentrant
hasEarlyAccessToken
returns (
uint256,
Expand All @@ -146,7 +149,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
require(collateralTokenAddress != address(0) || msg.value != 0 || loanId != 0, "9");

// ensures authorized use of existing loan
require(loanId == 0 || msg.sender == borrower, "unauthorized use of existing loan");
require(loanId == 0 || msg.sender == borrower, "unauthorized use of existing loan");

if (collateralTokenAddress == address(0)) {
collateralTokenAddress = wrbtcTokenAddress;
Expand Down Expand Up @@ -215,7 +218,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
require(collateralTokenAddress != loanTokenAddress, "11");

// ensures authorized use of existing loan
require(loanId == 0 || msg.sender == trader, "unauthorized use of existing loan");
require(loanId == 0 || msg.sender == trader, "unauthorized use of existing loan");

//temporary: limit transaction size
if (transactionLimit[collateralTokenAddress] > 0) require(collateralTokenSent <= transactionLimit[collateralTokenAddress]);
Expand Down Expand Up @@ -274,6 +277,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
_from,
_to,
_value,
//allowed[_from][msg.sender]
ProtocolLike(sovrynContractAddress).isLoanPool(msg.sender) ? uint256(-1) : allowed[_from][msg.sender]
);
}
Expand All @@ -285,14 +289,13 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 _allowanceAmount
) internal returns (bool) {
if (_allowanceAmount != uint256(-1)) {
require(_value <= _allowanceAmount, "14");
allowed[_from][msg.sender] = _allowanceAmount.sub(_value);
allowed[_from][msg.sender] = _allowanceAmount.sub(_value, "14");
}

uint256 _balancesFrom = balances[_from];
require(_value <= _balancesFrom && _to != address(0), "14");
require(_to != address(0), "15");

uint256 _balancesFromNew = _balancesFrom.sub(_value);
uint256 _balancesFrom = balances[_from];
uint256 _balancesFromNew = _balancesFrom.sub(_value, "16");
balances[_from] = _balancesFromNew;

uint256 _balancesTo = balances[_to];
Expand Down Expand Up @@ -322,14 +325,13 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 _newBalance,
uint256 _currentPrice
) internal {
// keccak256("iToken_ProfitSoFar")
bytes32 slot = keccak256(abi.encodePacked(_user, uint256(0x37aa2b7d583612f016e4a4de4292cb015139b3d7762663d06a53964912ea2fb6)));
bytes32 slot = keccak256(abi.encodePacked(_user, iToken_ProfitSoFar));

uint256 _currentProfit;
if (_oldBalance != 0 && _newBalance != 0) {
_currentProfit = _profitOf(slot, _oldBalance, _currentPrice, checkpointPrices_[_user]);
} else if (_newBalance == 0) {
int256 _currentProfit;
if (_newBalance == 0) {
_currentPrice = 0;
} else if (_oldBalance != 0) {
_currentProfit = _profitOf(slot, _oldBalance, _currentPrice, checkpointPrices_[_user]);
}

assembly {
Expand All @@ -341,9 +343,8 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {

/* Public View functions */

function profitOf(address user) public view returns (uint256) {
// keccak256("iToken_ProfitSoFar")
bytes32 slot = keccak256(abi.encodePacked(user, uint256(0x37aa2b7d583612f016e4a4de4292cb015139b3d7762663d06a53964912ea2fb6)));
function profitOf(address user) public view returns (int256) {
bytes32 slot = keccak256(abi.encodePacked(user, iToken_ProfitSoFar));

return _profitOf(slot, balances[user], tokenPrice(), checkpointPrices_[user]);
}
Expand All @@ -353,31 +354,16 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 _balance,
uint256 _currentPrice,
uint256 _checkpointPrice
) internal view returns (uint256) {
) internal view returns (int256 profitSoFar) {
if (_checkpointPrice == 0) {
return 0;
}

uint256 profitSoFar;
uint256 profitDiff;

assembly {
profitSoFar := sload(slot)
}

if (_currentPrice > _checkpointPrice) {
profitDiff = _balance.mul(_currentPrice - _checkpointPrice).div(10**18);
profitSoFar = profitSoFar.add(profitDiff);
} else {
profitDiff = _balance.mul(_checkpointPrice - _currentPrice).div(10**18);
if (profitSoFar > profitDiff) {
profitSoFar = profitSoFar - profitDiff;
} else {
profitSoFar = 0;
}
}

return profitSoFar;
profitSoFar = int256(_currentPrice).sub(int256(_checkpointPrice)).mul(int256(_balance)).div(sWEI_PRECISION).add(profitSoFar);
}

function tokenPrice() public view returns (uint256 price) {
Expand All @@ -397,7 +383,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 totalSupply = _totalAssetSupply(0);
uint256 totalBorrow = totalAssetBorrow();
if (totalSupply > totalBorrow) {
return totalSupply.sub(totalBorrow);
return totalSupply - totalBorrow;
}
}

Expand Down Expand Up @@ -572,6 +558,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
require(burnAmount != 0, "19");

if (burnAmount > balanceOf(msg.sender)) {
require(burnAmount == uint256(-1), "32");
burnAmount = balanceOf(msg.sender);
}

Expand Down Expand Up @@ -711,7 +698,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
bytes32 loanParamsId = loanParamsIds[uint256(keccak256(abi.encodePacked(collateralTokenAddress, withdrawAmountExist)))];
// converting to initialMargin
leverageAmount = SafeMath.div(10**38, leverageAmount);
(sentAmounts[1], sentAmounts[4]) = ProtocolLike(sovrynContractAddress).borrowOrTradeFromPool.value(msgValue)( // newPrincipal, newCollateral
(sentAmounts[1], sentAmounts[4]) = ProtocolLike(sovrynContractAddress).borrowOrTradeFromPool.value(msgValue)( //newPrincipal,newCollateral
loanParamsId,
loanId,
withdrawAmountExist,
Expand Down Expand Up @@ -824,7 +811,7 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
function _avgBorrowInterestRate(uint256 assetBorrow) internal view returns (uint256) {
if (assetBorrow != 0) {
(uint256 interestOwedPerDay, ) = _getAllInterest();
return interestOwedPerDay.mul(10**20).div(assetBorrow).mul(365);
return interestOwedPerDay.mul(10**20).mul(365).div(assetBorrow);
}
}

Expand Down Expand Up @@ -932,6 +919,17 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
}
}

function _adjustValue(
uint256 interestRate,
uint256 maxDuration,
uint256 marginAmount
) internal pure returns (uint256) {
return
maxDuration != 0
? interestRate.mul(WEI_PERCENT_PRECISION).mul(maxDuration).div(365 days).div(marginAmount).add(WEI_PERCENT_PRECISION)
: WEI_PERCENT_PRECISION;
}

/**
* used to read externally from the smart contract to see if a function is paused
* returns a bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
2 changes: 1 addition & 1 deletion contracts/connectors/loantoken/Pausable.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
2 changes: 1 addition & 1 deletion contracts/connectors/loantoken/interfaces/FeedsLike.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
2 changes: 1 addition & 1 deletion contracts/connectors/loantoken/interfaces/ProtocolLike.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/Objects.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand Down
3 changes: 1 addition & 2 deletions contracts/core/Protocol.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/

Expand All @@ -8,7 +8,6 @@ pragma experimental ABIEncoderV2;

import "./State.sol";

//@todo can I change this proxy to EIP-1822 proxy standard, please. https://eips.ethereum.org/EIPS/eip-1822.
contract sovrynProtocol is State {
function() external payable {
if (gasleft() <= 2300) {
Expand Down
Loading

0 comments on commit 20e718e

Please sign in to comment.