scaffold-eth / se-2-challenges

SpeedRunEthereum challenges (Powered by Scaffold-ETH 2)
https://speedrunethereum.com
MIT License
82 stars 162 forks source link

Remove variable redeclarations #151

Closed miki-saarna closed 7 months ago

miki-saarna commented 7 months ago

Within the documentation in README.md, there were 2 instances of variable redeclarations. These redeclarations simply cause a warning and will still allow the code to compile.

function ethToToken() public payable returns (uint256 tokenOutput) {
    require(msg.value > 0, "cannot swap 0 ETH");
    uint256 ethReserve = address(this).balance - msg.value;
    uint256 token_reserve = token.balanceOf(address(this));
    uint256 tokenOutput = price(msg.value, ethReserve, token_reserve); // tokenOutput redeclared

    require(token.transfer(msg.sender, tokenOutput), "ethToToken(): reverted swap.");
    emit EthToTokenSwap(msg.sender, tokenOutput, msg.value);
    return tokenOutput;
 }
 ...
function tokenToEth(uint256 tokenInput) public returns (uint256 ethOutput) {
    require(tokenInput > 0, "cannot swap 0 tokens");
    uint256 token_reserve = token.balanceOf(address(this));
    uint256 ethOutput = price(tokenInput, token_reserve, address(this).balance); // ethOutput redeclared
    require(token.transferFrom(msg.sender, address(this), tokenInput), "tokenToEth(): reverted swap.");
    (bool sent, ) = msg.sender.call{ value: ethOutput }("");
    require(sent, "tokenToEth: revert in transferring eth to you!");
    emit TokenToEthSwap(msg.sender, tokenInput, ethOutput);
    return ethOutput;
}

The following 2 lines should have their type annotation removed to avoid warnings:

VSCode provider the following error: This declaration shadows an existing declaration Screenshot 2024-03-05 at 11 01 50 PM

When deploying, the code successfully compiles but with the following warning:

Warning: This declaration shadows an existing declaration.
   --> contracts/DEX.sol:123:5:
    |
123 |     uint256 tokenOutput = price(msg.value, ethReserve, tokenReserve);
    |     ^^^^^^^^^^^^^^^^^^^
Note: The shadowed declaration is here:
   --> contracts/DEX.sol:119:48:
    |
119 |   function ethToToken() public payable returns (uint256 tokenOutput) {
    |                                                 ^^^^^^^^^^^^^^^^^^^
Removing the above suggested type annotations will remove this warning.