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

feat: mix bytes audit #69

Merged
merged 18 commits into from
Feb 21, 2025
Merged
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
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"code-complexity": ["error", 15],
"function-max-lines": ["error", 80],
"max-line-length": ["warn", 120],
"max-states-count": ["error", 15],
"max-states-count": ["error", 16],
"no-empty-blocks": "warn",
"no-unused-vars": "error",
"payable-fallback": "off",
Expand Down
1 change: 1 addition & 0 deletions script/Deploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ contract Deploy is Config {
0,
0,
0,
0,
100,
selfPeggingAssetBeacon,
lpTokenBeacon,
Expand Down
6 changes: 4 additions & 2 deletions script/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ contract Pool is Config {
tokenB: usdt,
tokenAType: SelfPeggingAssetFactory.TokenType.Standard,
tokenAOracle: address(0),
tokenAFunctionSig: "",
tokenARateFunctionSig: "",
tokenADecimalsFunctionSig: "",
tokenBType: SelfPeggingAssetFactory.TokenType.Standard,
tokenBOracle: address(0),
tokenBFunctionSig: ""
tokenBRateFunctionSig: "",
tokenBDecimalsFunctionSig: ""
});

vm.recordLogs();
Expand Down
63 changes: 54 additions & 9 deletions src/LPToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
uint256 public constant BUFFER_DENOMINATOR = 10 ** 10;

/**
* @dev Constant value representing the number of dead shares.
*/
uint256 public constant NUMBER_OF_DEAD_SHARES = 1000;

/**
* @dev The total amount of shares.
*/
Expand All @@ -56,7 +61,7 @@
/**
* @dev The mapping of account allowances.
*/
mapping(address => mapping(address => uint256)) public allowances;
mapping(address => mapping(address => uint256)) private allowances;

/**
* @dev The mapping of pools.
Expand All @@ -83,6 +88,11 @@
*/
string internal tokenSymbol;

/**
* @dev The bad debt of the buffer.
*/
uint256 public bufferBadDebt;

/**
* @notice Emitted when shares are transferred.
*/
Expand Down Expand Up @@ -128,6 +138,11 @@
*/
event BufferDecreased(uint256, uint256);

/**
* @notice Emitted when there is negative rebase.
*/
event NegativelyRebased(uint256, uint256);

/**
* @notice Emitted when the symbol is modified.
*/
Expand Down Expand Up @@ -172,6 +187,9 @@
/// @notice Error thrown when the pool is not found.
error PoolNotFound();

/// @notice Error thrown when the supply is insufficient.
error InsufficientSupply();

function initialize(string memory _name, string memory _symbol) public initializer {
tokenName = _name;
tokenSymbol = _symbol;
Expand Down Expand Up @@ -222,7 +240,7 @@
* @dev Mints shares for the `_account` and transfers them to the `_account`.
*/
function mintShares(address _account, uint256 _tokenAmount) external {
require(pools[msg.sender], NoPool());

Check warning on line 243 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
_mintShares(_account, _tokenAmount);
}

Expand Down Expand Up @@ -308,7 +326,7 @@
*/
function decreaseAllowance(address _spender, uint256 _subtractedValue) external returns (bool) {
uint256 currentAllowance = allowances[msg.sender][_spender];
require(currentAllowance >= _subtractedValue, AllowanceBelowZero());

Check warning on line 329 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
_approve(msg.sender, _spender, currentAllowance - _subtractedValue);
return true;
}
Expand All @@ -318,7 +336,7 @@
* @notice This function is called by the owner to set the buffer rate.
*/
function setBuffer(uint256 _buffer) external onlyOwner {
require(_buffer < BUFFER_DENOMINATOR, OutOfRange());

Check warning on line 339 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
bufferPercent = _buffer;
emit SetBufferPercent(_buffer);
}
Expand All @@ -336,31 +354,56 @@
* the total supply of LPToken by the staking rewards and the swap fee.
*/
function addTotalSupply(uint256 _amount) external {
require(pools[msg.sender], NoPool());

Check warning on line 357 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
require(_amount != 0, InvalidAmount());

Check warning on line 358 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements

if (bufferBadDebt >= _amount) {
bufferBadDebt -= _amount;
bufferAmount += _amount;
emit BufferIncreased(_amount, bufferAmount);
return;
}

uint256 prevAmount = _amount;
uint256 prevBufferBadDebt = bufferBadDebt;
_amount = _amount - bufferBadDebt;
bufferAmount += bufferBadDebt;
bufferBadDebt = 0;

uint256 _deltaBuffer = (bufferPercent * _amount) / BUFFER_DENOMINATOR;
uint256 actualAmount = _amount - _deltaBuffer;

totalSupply += actualAmount;
totalRewards += actualAmount;
bufferAmount += _deltaBuffer;

emit BufferIncreased(_deltaBuffer, bufferAmount);
emit RewardsMinted(_amount, actualAmount);
emit BufferIncreased(_deltaBuffer + prevBufferBadDebt, bufferAmount);
emit RewardsMinted(prevAmount, actualAmount);
}

/**
* @notice This function is called only by a stableSwap pool to decrease
* the total supply of LPToken by lost amount.
* @param _amount The amount of lost tokens.
* @param isBuffer The flag to indicate whether to use the buffer or not.
* @param withDebt The flag to indicate whether to add the lost amount to the buffer bad debt or not.
*/
function removeTotalSupply(uint256 _amount) external {
function removeTotalSupply(uint256 _amount, bool isBuffer, bool withDebt) external {
Copy link

Choose a reason for hiding this comment

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

Please update the NatSpec for proper descriptions of params

require(pools[msg.sender], NoPool());

Check warning on line 392 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
require(_amount != 0, InvalidAmount());

Check warning on line 393 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
require(_amount <= bufferAmount, InsufficientBuffer());

bufferAmount -= _amount;

emit BufferDecreased(_amount, bufferAmount);
if (isBuffer) {
require(_amount <= bufferAmount, InsufficientBuffer());

Check warning on line 396 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
bufferAmount -= _amount;
if (withDebt) {
bufferBadDebt += _amount;
}
emit BufferDecreased(_amount, bufferAmount);
} else {
require(_amount <= totalSupply, InsufficientSupply());

Check warning on line 403 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
totalSupply -= _amount;
emit NegativelyRebased(_amount, totalSupply);
}
}

/**
Expand All @@ -368,7 +411,7 @@
* the total supply of LPToken
*/
function addBuffer(uint256 _amount) external {
require(pools[msg.sender], NoPool());

Check warning on line 414 in src/LPToken.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
require(_amount != 0, InvalidAmount());

bufferAmount += _amount;
Expand Down Expand Up @@ -537,7 +580,9 @@
if (totalSupply != 0 && totalShares != 0) {
_sharesAmount = getSharesByPeggedToken(_tokenAmount);
} else {
_sharesAmount = _tokenAmount;
_sharesAmount = totalSupply + _tokenAmount - NUMBER_OF_DEAD_SHARES;
shares[address(0)] = NUMBER_OF_DEAD_SHARES;
totalShares += NUMBER_OF_DEAD_SHARES;
}
shares[_recipient] += _sharesAmount;
totalShares += _sharesAmount;
Expand Down
Loading
Loading