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

Deploy and proposal for USDT market on Goerli #801

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a669690
add usdt deployment
Aug 3, 2023
22a4dc8
add run scenario entry
Aug 3, 2023
f0d1503
when deploy all suply cap should be zero
Aug 3, 2023
132d591
use the same usdt contract that compoundV2 is using, so easier to get…
Aug 3, 2023
83dcc49
clean up
Aug 7, 2023
6c6e5bf
change targetreserver to 5 usdt
Aug 7, 2023
e791775
USDT fork from mainnet for better consistent scenario cases
Aug 8, 2023
fe550f4
move sol files to deploy branch
Aug 31, 2023
1844dd8
add some usdt token fees scenario tests
Sep 1, 2023
c25386d
add liquidation scenario tests
Sep 8, 2023
b4a026e
port migration deploy script changes to here
Sep 8, 2023
eed10de
Update scenario/SupplyScenario.ts
cwang25 Sep 8, 2023
01ed6e0
Update contracts/Comet.sol
cwang25 Sep 8, 2023
c68fc7f
Update scenario/SupplyScenario.ts
cwang25 Sep 8, 2023
3b9eeee
Update scenario/SupplyScenario.ts
cwang25 Sep 8, 2023
59d101c
address comments
Sep 8, 2023
eff6e14
add docling
Sep 8, 2023
736aee5
add unit test for supply/ supply collateral / buy colalteral into uni…
Sep 11, 2023
e94e693
addressed comments
Sep 11, 2023
8ad9799
Update contracts/test/NonStandardFaucetFeeToken.sol
cwang25 Sep 12, 2023
ba090f0
address comments
Sep 12, 2023
cd3776f
fixed some tsc error complains
Sep 12, 2023
12e5e8f
fix re-entry tests, that since evilToken never transfer anything, wit…
Sep 13, 2023
4b0fc48
another test case that need to set it to 0 balance
Sep 13, 2023
63f4061
EviltToken with more realistic attack, and adjusted test cases, and a…
Sep 15, 2023
277b286
add extra line
Sep 15, 2023
c0f710e
Merge branch 'main' into hans/goerli-usdt-deploy
cwang25 Sep 15, 2023
49588d6
add and leveraged oz's re-entrancy guard
Sep 15, 2023
d0f016a
reentrancy guard in Comet, isntead of importing external contracts wi…
Sep 16, 2023
976c81f
Revert "reentrancy guard in Comet, isntead of importing external cont…
Sep 18, 2023
a13050a
Revert "add and leveraged oz's re-entrancy guard"
Sep 18, 2023
513f451
Address OZ's feedback on USDT comet support (#818)
cwang25 Feb 7, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/run-scenarios.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
bases: [ development, mainnet, mainnet-weth, goerli, goerli-weth, fuji, mumbai, polygon, arbitrum-usdc.e, arbitrum-usdc, arbitrum-goerli-usdc, arbitrum-goerli-usdc.e, base-usdbc, base-weth, base-goerli, base-goerli-weth, linea-goerli]
bases: [ development, mainnet, mainnet-weth, goerli, goerli-weth, goerli-usdt, fuji, mumbai, polygon, arbitrum-usdc.e, arbitrum-usdc, arbitrum-goerli-usdc, arbitrum-goerli-usdc.e, base-usdbc, base-weth, base-goerli, base-goerli-weth, linea-goerli]
name: Run scenarios
env:
ETHERSCAN_KEY: ${{ secrets.ETHERSCAN_KEY }}
Expand Down
73 changes: 53 additions & 20 deletions contracts/Comet.sol
Copy link
Contributor Author

@cwang25 cwang25 Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 ···································|··············|·················
 |  CometFactory                    ·      24.044  ·                │
 ···································|··············|·················
 |  CometModifiedFactory            ·      24.182  ·                │
 ···································|··············|·················

The contract size is approaching to limit, compiler will complain with Warning: 2 contracts exceed the size limit for mainnet deployment.
But the actual size limit is 24.576 kb, so we should be fine :P

cwang25 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
pragma solidity 0.8.15;

import "./CometMainInterface.sol";
import "./ERC20.sol";
import "./IERC20NonStandard.sol";
import "./IPriceFeed.sol";
import "./ReentrancyGuard.sol";

/**
* @title Compound's Comet Contract
* @notice An efficient monolithic money market protocol
* @author Compound
*/
contract Comet is CometMainInterface {
contract Comet is CometMainInterface, ReentrancyGuard {
/** General configuration constants **/

/// @notice The admin of the protocol
Expand Down Expand Up @@ -139,7 +140,7 @@
**/
constructor(Configuration memory config) {
// Sanity checks
uint8 decimals_ = ERC20(config.baseToken).decimals();
uint8 decimals_ = IERC20NonStandard(config.baseToken).decimals();
if (decimals_ > MAX_BASE_DECIMALS) revert BadDecimals();
if (config.storeFrontPriceFactor > FACTOR_SCALE) revert BadDiscount();
if (config.assetConfigs.length > MAX_ASSETS) revert TooManyAssets();
Expand Down Expand Up @@ -224,7 +225,7 @@
function getPackedAssetInternal(AssetConfig[] memory assetConfigs, uint i) internal view returns (uint256, uint256) {
AssetConfig memory assetConfig;
if (i < assetConfigs.length) {
assembly {

Check warning on line 228 in contracts/Comet.sol

View workflow job for this annotation

GitHub Actions / Contract linter

Avoid to use inline assembly. It is acceptable only in rare cases
assetConfig := mload(add(add(assetConfigs, 0x20), mul(i, 0x20)))
}
} else {
Expand All @@ -241,7 +242,7 @@

// Sanity check price feed and asset decimals
if (IPriceFeed(priceFeed).decimals() != PRICE_FEED_DECIMALS) revert BadDecimals();
if (ERC20(asset).decimals() != decimals_) revert BadDecimals();
if (IERC20NonStandard(asset).decimals() != decimals_) revert BadDecimals();

// Ensure collateral factors are within range
if (assetConfig.borrowCollateralFactor >= assetConfig.liquidateCollateralFactor) revert BorrowCFTooLarge();
Expand Down Expand Up @@ -482,15 +483,15 @@
* @param asset The collateral asset
*/
function getCollateralReserves(address asset) override public view returns (uint) {
return ERC20(asset).balanceOf(address(this)) - totalsCollateral[asset].totalSupplyAsset;
return IERC20NonStandard(asset).balanceOf(address(this)) - totalsCollateral[asset].totalSupplyAsset;
}

/**
* @notice Gets the total amount of protocol reserves of the base asset
*/
function getReserves() override public view returns (int) {
(uint64 baseSupplyIndex_, uint64 baseBorrowIndex_) = accruedInterestIndices(getNowInternal() - lastAccrualTime);
uint balance = ERC20(baseToken).balanceOf(address(this));
uint balance = IERC20NonStandard(baseToken).balanceOf(address(this));
uint totalSupply_ = presentValueSupply(baseSupplyIndex_, totalSupplyBase);
uint totalBorrow_ = presentValueBorrow(baseBorrowIndex_, totalBorrowBase);
return signed256(balance) - signed256(totalSupply_) + signed256(totalBorrow_);
Expand Down Expand Up @@ -760,18 +761,50 @@
}

/**
* @dev Safe ERC20 transfer in, assumes no fee is charged and amount is transferred
* @dev Safe ERC20 transfer in and returns the final amount transferred (taking into account any fees)
* @dev Note: Safely handles non-standard ERC-20 tokens that do not return a value. See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
*/
function doTransferIn(address asset, address from, uint amount) internal {
bool success = ERC20(asset).transferFrom(from, address(this), amount);
function doTransferIn(address asset, address from, uint amount) internal returns (uint) {
uint256 preTransferBalance = IERC20NonStandard(asset).balanceOf(address(this));
IERC20NonStandard(asset).transferFrom(from, address(this), amount);
bool success;
assembly {

Check warning on line 771 in contracts/Comet.sol

View workflow job for this annotation

GitHub Actions / Contract linter

Avoid to use inline assembly. It is acceptable only in rare cases
switch returndatasize()
case 0 { // This is a non-standard ERC-20
success := not(0) // set success to true
}
case 32 { // This is a compliant ERC-20
returndatacopy(0, 0, 32)
success := mload(0) // Set `success = returndata` of override external call
}
default { // This is an excessively non-compliant ERC-20, revert.
revert(0, 0)
}
}
if (!success) revert TransferInFailed();
return IERC20NonStandard(asset).balanceOf(address(this)) - preTransferBalance;
}

/**
* @dev Safe ERC20 transfer out
* @dev Note: Safely handles non-standard ERC-20 tokens that do not return a value. See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
*/
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
function doTransferOut(address asset, address to, uint amount) internal {
bool success = ERC20(asset).transfer(to, amount);
IERC20NonStandard(asset).transfer(to, amount);
bool success;
assembly {

Check warning on line 795 in contracts/Comet.sol

View workflow job for this annotation

GitHub Actions / Contract linter

Avoid to use inline assembly. It is acceptable only in rare cases
switch returndatasize()
case 0 { // This is a non-standard ERC-20
success := not(0) // set success to true
}
case 32 { // This is a compliant ERC-20
returndatacopy(0, 0, 32)
success := mload(0) // Set `success = returndata` of override external call
}
default { // This is an excessively non-compliant ERC-20, revert.
revert(0, 0)
}
}
if (!success) revert TransferOutFailed();
}

Expand Down Expand Up @@ -809,7 +842,7 @@
* @dev Supply either collateral or base asset, depending on the asset, if operator is allowed
* @dev Note: Specifying an `amount` of uint256.max will repay all of `dst`'s accrued base borrow balance
*/
function supplyInternal(address operator, address from, address dst, address asset, uint amount) internal {
function supplyInternal(address operator, address from, address dst, address asset, uint amount) internal nonReentrant {
if (isSupplyPaused()) revert Paused();
if (!hasPermission(from, operator)) revert Unauthorized();

Expand All @@ -827,7 +860,7 @@
* @dev Supply an amount of base asset from `from` to dst
*/
function supplyBase(address from, address dst, uint256 amount) internal {
doTransferIn(baseToken, from, amount);
amount = doTransferIn(baseToken, from, amount);

accrueInternal();

Expand All @@ -854,7 +887,7 @@
* @dev Supply an amount of collateral asset from `from` to dst
*/
function supplyCollateral(address from, address dst, address asset, uint128 amount) internal {
doTransferIn(asset, from, amount);
amount = safe128(doTransferIn(asset, from, amount));

AssetInfo memory assetInfo = getAssetInfoByAddress(asset);
TotalsCollateral memory totals = totalsCollateral[asset];
Expand Down Expand Up @@ -920,7 +953,7 @@
* @dev Transfer either collateral or base asset, depending on the asset, if operator is allowed
* @dev Note: Specifying an `amount` of uint256.max will transfer all of `src`'s accrued base balance
*/
function transferInternal(address operator, address src, address dst, address asset, uint amount) internal {
function transferInternal(address operator, address src, address dst, address asset, uint amount) internal nonReentrant {
if (isTransferPaused()) revert Paused();
if (!hasPermission(src, operator)) revert Unauthorized();
if (src == dst) revert NoSelfTransfer();
Expand Down Expand Up @@ -1031,7 +1064,7 @@
* @dev Withdraw either collateral or base asset, depending on the asset, if operator is allowed
* @dev Note: Specifying an `amount` of uint256.max will withdraw all of `src`'s accrued base balance
*/
function withdrawInternal(address operator, address src, address to, address asset, uint amount) internal {
function withdrawInternal(address operator, address src, address to, address asset, uint amount) internal nonReentrant {
if (isWithdrawPaused()) revert Paused();
if (!hasPermission(src, operator)) revert Unauthorized();

Expand Down Expand Up @@ -1192,14 +1225,14 @@
* @param baseAmount The amount of base tokens used to buy the collateral
* @param recipient The recipient address
*/
function buyCollateral(address asset, uint minAmount, uint baseAmount, address recipient) override external {
function buyCollateral(address asset, uint minAmount, uint baseAmount, address recipient) override external nonReentrant {
if (isBuyPaused()) revert Paused();

int reserves = getReserves();
if (reserves >= 0 && uint(reserves) >= targetReserves) revert NotForSale();

// Note: Re-entrancy can skip the reserves check above on a second buyCollateral call.
doTransferIn(baseToken, msg.sender, baseAmount);
baseAmount = doTransferIn(baseToken, msg.sender, baseAmount);

uint collateralAmount = quoteCollateral(asset, baseAmount);
if (collateralAmount < minAmount) revert TooMuchSlippage();
Expand Down Expand Up @@ -1238,7 +1271,7 @@
* @param to An address of the receiver of withdrawn reserves
* @param amount The amount of reserves to be withdrawn from the protocol
*/
function withdrawReserves(address to, uint amount) override external {
function withdrawReserves(address to, uint amount) override external nonReentrant {
if (msg.sender != governor) revert Unauthorized();

int reserves = getReserves();
Expand All @@ -1261,7 +1294,7 @@
function approveThis(address manager, address asset, uint amount) override external {
if (msg.sender != governor) revert Unauthorized();

ERC20(asset).approve(manager, amount);
IERC20NonStandard(asset).approve(manager, amount);
}

/**
Expand Down Expand Up @@ -1311,9 +1344,9 @@
/**
* @notice Fallback to calling the extension delegate for everything else
*/
fallback() external payable {

Check warning on line 1347 in contracts/Comet.sol

View workflow job for this annotation

GitHub Actions / Contract linter

Fallback function must be simple
address delegate = extensionDelegate;
assembly {

Check warning on line 1349 in contracts/Comet.sol

View workflow job for this annotation

GitHub Actions / Contract linter

Avoid to use inline assembly. It is acceptable only in rare cases
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), delegate, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
Expand All @@ -1322,4 +1355,4 @@
default { return(0, returndatasize()) }
}
}
}
}
1 change: 1 addition & 0 deletions contracts/IERC20NonStandard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pragma solidity 0.8.15;
* See https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca
*/
interface IERC20NonStandard {
function decimals() external view returns (uint8);
function approve(address spender, uint256 amount) external;
function transfer(address to, uint256 value) external;
function transferFrom(address from, address to, uint256 value) external;
Expand Down
77 changes: 77 additions & 0 deletions contracts/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.

// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;

uint256 private _status;
cwang25 marked this conversation as resolved.
Show resolved Hide resolved

constructor() {
_status = _NOT_ENTERED;
}

/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}

function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
Fixed Show fixed Hide fixed

// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}

function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}

/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
25 changes: 16 additions & 9 deletions contracts/test/EvilToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,27 @@ contract EvilToken is FaucetToken {
attack = attack_;
}

function transfer(address, uint256) external override returns (bool) {
return performAttack();
function transfer(address dst, uint256 amount) public override returns (bool) {
numberOfCalls++;
if (numberOfCalls > attack.maxCalls){
return super.transfer(dst, amount);
} else {
return performAttack(address(this), dst, amount);
}
}

function transferFrom(address, address, uint256) external override returns (bool) {
return performAttack();
function transferFrom(address src, address dst, uint256 amount) public override returns (bool) {
numberOfCalls++;
if (numberOfCalls > attack.maxCalls) {
return super.transferFrom(src, dst, amount);
} else {
return performAttack(src, dst, amount);
}
}

function performAttack() internal returns (bool) {
function performAttack(address src, address dst, uint256 amount) internal returns (bool) {
ReentryAttack memory reentryAttack = attack;
numberOfCalls++;
if (numberOfCalls > reentryAttack.maxCalls) {
// do nothing
} else if (reentryAttack.attackType == AttackType.TRANSFER_FROM) {
if (reentryAttack.attackType == AttackType.TRANSFER_FROM) {
Comet(payable(msg.sender)).transferFrom(
reentryAttack.source,
reentryAttack.destination,
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/FaucetToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ contract StandardToken {
decimals = _decimalUnits;
}

function transfer(address dst, uint256 amount) external virtual returns (bool) {
function transfer(address dst, uint256 amount) public virtual returns (bool) {
require(amount <= balanceOf[msg.sender], "ERC20: transfer amount exceeds balance");
balanceOf[msg.sender] = balanceOf[msg.sender] - amount;
balanceOf[dst] = balanceOf[dst] + amount;
emit Transfer(msg.sender, dst, amount);
return true;
}

function transferFrom(address src, address dst, uint256 amount) external virtual returns (bool) {
function transferFrom(address src, address dst, uint256 amount) public virtual returns (bool) {
require(amount <= allowance[src][msg.sender], "ERC20: transfer amount exceeds allowance");
require(amount <= balanceOf[src], "ERC20: transfer amount exceeds balance");
allowance[src][msg.sender] = allowance[src][msg.sender] - amount;
Expand Down
Loading
Loading