Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Wang committed Nov 9, 2023
1 parent b3dbaa3 commit ad99f2e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
15 changes: 4 additions & 11 deletions contracts/Comet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ contract Comet is CometMainInterface {
(asset14_a, asset14_b) = getPackedAssetInternal(config.assetConfigs, 14);
}

function _nonReentrantBefore() private {
modifier nonReentrant() {
bytes32 slot = COMET_REENTRANCY_GUARD_FLAG_SLOT;
uint256 status;
assembly {
Expand All @@ -212,10 +212,7 @@ contract Comet is CometMainInterface {
assembly {
sstore(slot, COMET_REENTRANCY_GUARD_ENTERED)
}
}

function _nonReentrantAfter() private {
bytes32 slot = COMET_REENTRANCY_GUARD_FLAG_SLOT;
_;
assembly {
sstore(slot, COMET_REENTRANCY_GUARD_NOT_ENTERED)
}
Expand Down Expand Up @@ -783,28 +780,24 @@ contract Comet is CometMainInterface {
* @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 returns (uint) {
_nonReentrantBefore();
function doTransferIn(address asset, address from, uint amount) nonReentrant internal returns (uint) {
uint256 preTransferBalance = ERC20(asset).balanceOf(address(this));
(bool success, bytes memory returndata) = asset.call(abi.encodeWithSelector(ERC20.transferFrom.selector, from, address(this), amount));
if (!success || !(returndata.length == 0 || abi.decode(returndata, (bool)))) {
revert TransferInFailed();
}
_nonReentrantAfter();
return ERC20(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
*/
function doTransferOut(address asset, address to, uint amount) internal {
_nonReentrantBefore();
function doTransferOut(address asset, address to, uint amount) nonReentrant internal {
(bool success, bytes memory returndata) = asset.call(abi.encodeWithSelector(ERC20.transfer.selector, to, amount));
if (!success || !(returndata.length == 0 || abi.decode(returndata, (bool)))) {
revert TransferOutFailed();
}
_nonReentrantAfter();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/CometCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract contract CometCore is CometConfiguration, CometStorage, CometMath {

/// @dev The reentrancy guard status
uint256 internal constant COMET_REENTRANCY_GUARD_NOT_ENTERED = 0;
uint256 internal constant COMET_REENTRANCY_GUARD_ENTERED = 2;
uint256 internal constant COMET_REENTRANCY_GUARD_ENTERED = 1;

/**
* @notice Determine if the manager has permission to act on behalf of the owner
Expand Down
5 changes: 2 additions & 3 deletions test/supply-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ describe('supplyTo', function () {
decimals: 6,
initialPrice: 2,
factory: await ethers.getContractFactory('EvilToken') as EvilToken__factory,
supplyCap: 250e6
supplyCap: 100e6
}
}
});
Expand Down Expand Up @@ -692,7 +692,6 @@ describe('supplyFrom', function () {
await comet.connect(alice).allow(EVIL.address, true);
await wait(EVIL.connect(alice).approve(comet.address, 75e6));
await EVIL.allocateTo(alice.address, 75e6);
await comet.connect(alice).supplyTo(bob.address, EVIL.address, 75e6);
expect(await comet.collateralBalanceOf(bob.address, EVIL.address)).to.be.revertedWith("custom error 'TransferInFailed()'");
await expect(comet.connect(alice).supplyTo(bob.address, EVIL.address, 75e6)).to.be.revertedWith("custom error 'TransferInFailed()'");
});
});

0 comments on commit ad99f2e

Please sign in to comment.