Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Add comments to Solidity contracts #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion contracts/AaveRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,26 +15,30 @@ 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;
}
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;
}
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]);
Expand Down
3 changes: 2 additions & 1 deletion contracts/BalancerHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion contracts/BancorFinder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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;

IERC20 constant internal ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
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
Expand Down
6 changes: 5 additions & 1 deletion contracts/CompoundRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,26 +15,30 @@ 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;
}
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;
}
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]);
Expand Down
11 changes: 10 additions & 1 deletion contracts/UniversalERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -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));
}
Expand Down