From a48acc7753eae4ecff7502079dd77b80b077d2d6 Mon Sep 17 00:00:00 2001 From: Seth Landry Date: Thu, 23 Jan 2025 04:44:07 -0600 Subject: [PATCH] Add comments to Solidity contracts Add comments to describe the purpose of contracts and functions in various files. * **AaveRegistry.sol** - Add a comment at the top of the file to describe the purpose of the contract. - Add comments above each function to describe its purpose. * **BalancerHelper.sol** - Add a comment at the top of the file to describe the purpose of the contract. - Add a comment above the function to describe its purpose. * **BancorFinder.sol** - Add a comment at the top of the file to describe the purpose of the contract. - Add a comment above the function to describe its purpose. * **CompoundRegistry.sol** - Add a comment at the top of the file to describe the purpose of the contract. - Add comments above each function to describe its purpose. * **UniversalERC20.sol** - Add a comment at the top of the file to describe the purpose of the library. - Add comments above each function to describe its purpose. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/1inch/1inchProtocol?shareId=XXXX-XXXX-XXXX-XXXX). --- contracts/AaveRegistry.sol | 6 +++++- contracts/BalancerHelper.sol | 3 ++- contracts/BancorFinder.sol | 3 ++- contracts/CompoundRegistry.sol | 6 +++++- contracts/UniversalERC20.sol | 11 ++++++++++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/contracts/AaveRegistry.sol b/contracts/AaveRegistry.sol index a26cada..04a13ab 100644 --- a/contracts/AaveRegistry.sol +++ b/contracts/AaveRegistry.sol @@ -5,7 +5,7 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "./interface/IAaveRegistry.sol"; import "./UniversalERC20.sol"; - +// The AaveRegistry contract is used to manage mappings between Aave tokens (aTokens) and their underlying assets. contract AaveRegistry is Ownable, IAaveRegistry { using UniversalERC20 for IERC20; @@ -15,6 +15,7 @@ contract AaveRegistry is Ownable, IAaveRegistry { mapping(address => address) private _tokenByAToken; mapping(address => address) private _aTokenByToken; + // Returns the underlying asset for a given aToken. function tokenByAToken(IAaveToken aToken) external view returns(IERC20) { if (aToken == aETH) { return ETH; @@ -22,6 +23,7 @@ contract AaveRegistry is Ownable, IAaveRegistry { return IERC20(_tokenByAToken[address(aToken)]); } + // Returns the aToken for a given underlying asset. function aTokenByToken(IERC20 token) external view returns(IAaveToken) { if (token.isETH()) { return aETH; @@ -29,12 +31,14 @@ contract AaveRegistry is Ownable, IAaveRegistry { return IAaveToken(_aTokenByToken[address(token)]); } + // Adds a new aToken to the registry. function addAToken(IAaveToken aToken) public onlyOwner { IERC20 token = IERC20(aToken.underlyingAssetAddress()); _tokenByAToken[address(aToken)] = address(token); _aTokenByToken[address(token)] = address(aToken); } + // Adds multiple aTokens to the registry. function addATokens(IAaveToken[] calldata cTokens) external onlyOwner { for (uint i = 0; i < cTokens.length; i++) { addAToken(cTokens[i]); diff --git a/contracts/BalancerHelper.sol b/contracts/BalancerHelper.sol index de17495..0f073e8 100644 --- a/contracts/BalancerHelper.sol +++ b/contracts/BalancerHelper.sol @@ -5,10 +5,11 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "./interface/IBalancerPool.sol"; import "./BalancerLib.sol"; - +// The BalancerHelper contract provides utility functions for interacting with Balancer pools. contract BalancerHelper { using SafeMath for uint256; + // Returns the expected output amounts for a given input amount when swapping tokens in a Balancer pool. function getReturns( IBalancerPool pool, IERC20 fromToken, diff --git a/contracts/BancorFinder.sol b/contracts/BancorFinder.sol index 34a67c0..83e8b9b 100644 --- a/contracts/BancorFinder.sol +++ b/contracts/BancorFinder.sol @@ -5,7 +5,7 @@ import "./interface/IBancorContractRegistry.sol"; import "./interface/IBancorConverterRegistry.sol"; import "./UniversalERC20.sol"; - +// The BancorFinder contract is used to build a path for token conversion using the Bancor protocol. contract BancorFinder { using UniversalERC20 for IERC20; @@ -13,6 +13,7 @@ contract BancorFinder { IERC20 constant internal bnt = IERC20(0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C); IBancorContractRegistry constant internal bancorContractRegistry = IBancorContractRegistry(0x52Ae12ABe5D8BD778BD5397F99cA900624CfADD4); + // Builds a path for token conversion using the Bancor protocol. function buildBancorPath( IERC20 fromToken, IERC20 destToken diff --git a/contracts/CompoundRegistry.sol b/contracts/CompoundRegistry.sol index d8d1a7f..80914bc 100644 --- a/contracts/CompoundRegistry.sol +++ b/contracts/CompoundRegistry.sol @@ -5,7 +5,7 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "./interface/ICompoundRegistry.sol"; import "./UniversalERC20.sol"; - +// The CompoundRegistry contract is used to manage mappings between Compound tokens (cTokens) and their underlying assets. contract CompoundRegistry is Ownable, ICompoundRegistry { using UniversalERC20 for IERC20; @@ -15,6 +15,7 @@ contract CompoundRegistry is Ownable, ICompoundRegistry { mapping(address => address) private _tokenByCToken; mapping(address => address) private _cTokenByToken; + // Returns the underlying asset for a given cToken. function tokenByCToken(ICompoundToken cToken) external view returns(IERC20) { if (cToken == cETH) { return ETH; @@ -22,6 +23,7 @@ contract CompoundRegistry is Ownable, ICompoundRegistry { return IERC20(_tokenByCToken[address(cToken)]); } + // Returns the cToken for a given underlying asset. function cTokenByToken(IERC20 token) external view returns(ICompoundToken) { if (token.isETH()) { return cETH; @@ -29,12 +31,14 @@ contract CompoundRegistry is Ownable, ICompoundRegistry { return ICompoundToken(_cTokenByToken[address(token)]); } + // Adds a new cToken to the registry. function addCToken(ICompoundToken cToken) public onlyOwner { IERC20 token = IERC20(cToken.underlying()); _tokenByCToken[address(cToken)] = address(token); _cTokenByToken[address(token)] = address(cToken); } + // Adds multiple cTokens to the registry. function addCTokens(ICompoundToken[] calldata cTokens) external onlyOwner { for (uint i = 0; i < cTokens.length; i++) { addCToken(cTokens[i]); diff --git a/contracts/UniversalERC20.sol b/contracts/UniversalERC20.sol index 171131e..1bd82c4 100644 --- a/contracts/UniversalERC20.sol +++ b/contracts/UniversalERC20.sol @@ -4,7 +4,7 @@ import "@openzeppelin/contracts/math/SafeMath.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; - +// The UniversalERC20 library provides utility functions for handling ERC20 tokens and ETH in a unified manner. library UniversalERC20 { using SafeMath for uint256; @@ -13,6 +13,7 @@ library UniversalERC20 { IERC20 private constant ZERO_ADDRESS = IERC20(0x0000000000000000000000000000000000000000); IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE); + // Transfers a specified amount of tokens or ETH to a given address. function universalTransfer(IERC20 token, address to, uint256 amount) internal returns(bool) { if (amount == 0) { return true; @@ -26,6 +27,7 @@ library UniversalERC20 { } } + // Transfers a specified amount of tokens or ETH from one address to another. function universalTransferFrom(IERC20 token, address from, address to, uint256 amount) internal { if (amount == 0) { return; @@ -44,6 +46,7 @@ library UniversalERC20 { } } + // Transfers a specified amount of tokens or ETH from the sender to the current contract. function universalTransferFromSenderToThis(IERC20 token, uint256 amount) internal { if (amount == 0) { return; @@ -59,6 +62,7 @@ library UniversalERC20 { } } + // Approves a specified amount of tokens for a given address. function universalApprove(IERC20 token, address to, uint256 amount) internal { if (!isETH(token)) { if (amount == 0) { @@ -76,6 +80,7 @@ library UniversalERC20 { } } + // Returns the balance of tokens or ETH for a given address. function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) { if (isETH(token)) { return who.balance; @@ -84,6 +89,7 @@ library UniversalERC20 { } } + // Returns the number of decimals for a given token or ETH. function universalDecimals(IERC20 token) internal view returns (uint256) { if (isETH(token)) { @@ -102,14 +108,17 @@ library UniversalERC20 { return (success && data.length > 0) ? abi.decode(data, (uint256)) : 18; } + // Checks if a given token is ETH. function isETH(IERC20 token) internal pure returns(bool) { return (address(token) == address(ZERO_ADDRESS) || address(token) == address(ETH_ADDRESS)); } + // Checks if two tokens are equal or both are ETH. function eq(IERC20 a, IERC20 b) internal pure returns(bool) { return a == b || (isETH(a) && isETH(b)); } + // Checks if a token does not exist. function notExist(IERC20 token) internal pure returns(bool) { return (address(token) == address(-1)); }