starting with the addFees function and how it updates the cumulativeFeePerToken. We’ll…
- User
starting with the addFees function and how it updates the cumulativeFeePerToken. We’ll then proceed to the onBalanceChange and fee claiming functions (claimFees and batchClaiming). the process of adding fees to a token’s fee pool and updating the cumulative fee per token. Next, we’ll expand on the balance change and fee claiming processes. Does this initial flowchart align with your understanding of the addFees function? // SPDX-License-Identifier: MIT pragma solidity ^0.8.7;
import “./Curves.sol”; import “./Security.sol”;
import “@openzeppelin/contracts/token/ERC20/IERC20.sol”;
contract FeeSplitter is Security { Curves public curves; uint256 constant PRECISION = 1e18;
// Custom errors error NoFeesToClaim(); error NoTokenHolders();
struct TokenData { uint256 cumulativeFeePerToken; mapping(address => uint256) userFeeOffset; mapping(address => uint256) unclaimedFees; }
struct UserClaimData { uint256 claimableFees; address token; }
mapping(address => TokenData) internal tokensData; mapping(address => address[]) internal userTokens;
event FeesClaimed(address indexed token, address indexed user, uint256 amount);
constructor() Security() {}
function setCurves(Curves curves_) public { curves = curves_; }
function balanceOf(address token, address account) public view returns (uint256) { return curves.curvesTokenBalance(token, account) * PRECISION; }
function totalSupply(address token) public view returns (uint256) { //@dev: this is the amount of tokens that are not locked in the contract. The locked tokens are in the ERC20 contract return (curves.curvesTokenSupply(token) - curves.curvesTokenBalance(token, address(curves))) * PRECISION; }
function getUserTokens(address user) public view returns (address[] memory) { return userTokens[user]; }
function getUserTokensAndClaimable(address user) public view returns (UserClaimData[] memory) { address[] memory tokens = getUserTokens(user); UserClaimData[] memory result = new UserClaimData; for (uint256 i = 0; i < tokens.length; i++) { address token = tokens[i]; uint256 claimable = getClaimableFees(token, user); result[i] = UserClaimData(claimable, token); } return result; }
function updateFeeCredit(address token, address account) internal { TokenData storage data = tokensData[token]; uint256 balance = balanceOf(token, account); if (balance > 0) { uint256 owed = (data.cumulativeFeePerToken - data.userFeeOffset[account]) * balance; data.unclaimedFees[account] += owed / PRECISION; data.userFeeOffset[account] = data.cumulativeFeePerToken; } }
function getClaimableFees(address token, address account) public view returns (uint256) { TokenData storage data = tokensData[token]; uint256 balance = balanceOf(token, account); uint256 owed = (data.cumulativeFeePerToken - data.userFeeOffset[account]) * balance; return (owed / PRECISION) + data.unclaimedFees[account]; }
- Assistant
- User
- Assistant
Let's address the identified issues one at a time:
-
Reentrancy Attack The vulnerability described is accurate. Both the
claimFeesand functions are susceptible to reentrancy attacks because they transfer Ether (with ) before updating the user's unclaimed fees balance.
-
- User
in this contract // SPDX-License-Identifier: MIT pragma solidity 0.8.7;
import "./CurvesERC20.sol";
contract CurvesERC20Factory { function deploy(string memory name, string memory symbol, address owner) public returns (address) { CurvesERC20 tokenContract = new CurvesERC20(name, symbol, owner); return address(tokenContract); } } a factory that deploys ERC20 tokens. Let's start diagramming this. We'll begin with the CurvesERC20Factory contract and its deploy function.
Could you tell me more about the CurvesERC20 contract and its constructor parameters?v
- Assistant
The
CurvesERC20Factorycontract is a simple Solidity factory contract responsible for deploying instances ofCurvesERC20contracts. Factory patterns are a common practice in Ethereum smart contract development as they allow for the creation of multiple contract instances with similar functionality but separate states.The
deployfunction in theCurvesERC20Factorycontract does the following: