Debugging Solidity

Here you can ask questions related to this specific course section.

4 Likes

This is a great walk through to conquer those bugs. Sometimes they are so silly yet hard to find. Happy coding!!

2 Likes

Hi @filip I followed your instructions and also tried myself to solve this problem, but it seems like I need your help.

I updated in truffle-config.js also the solc but still facing the error message.

Do you maybe know what I do wrong?

Hey @crypto.thoms

From your screenshot it seems like you did not save truffle-config

image

Save the file and reboot truffle.

Cheers,
Dani

4 Likes

Ummm, I still can’t work out the error. May is because I didn’t have the Government contract or Ownable contract?

pragma solidity 0.7.5;
pragma abicoder v2;
import "./Ownable.sol";

interface GovermentInterface{
    function addTransaction(address _from, address _to, uint _amount) external payable;
}

contract Bank is Ownable{
    
    GovermentInterface govermentInstance = GovermentInterface(0x3328358128832A260C76A4141e19E2A943CD4B6D);
    
    mapping(address => uint) balance;
    
    event depositDone(uint amount, address indexed depositedTo);
    
    
    function deposit() public payable returns (uint)  {
        balance[msg.sender] = msg.value;
        emit depositDone(msg.value, msg.sender);
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public onlyOwner returns (uint){
        require(balance[msg.sender] >= amount);
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }
    
    function getBalance() public view returns (uint){
        return balance[msg.sender];
    }
    
    function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount, "Balance not sufficient");
        require(msg.sender != recipient, "Don't transfer money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        //govermentInstance.addTransaction(msg.sender, recipient, amount);
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] - amount;
        balance[to] + amount;
    }
      function getTransfers() external view returns(uint[] memory) {

    }
    
}
1 Like

Hi @Hector_Martinez

What is the issue you are facing?

I was trying to debug the contract, and need assistance. I’ll try to deploy the contract in Remix, but it comes up with errors. I’m trying to do debugging exercise.

Hey @Hector_Martinez

What’s the error message you get? What’s the code you are compiling?

Cheers,
Dani

I’m glad you had the error with the wrong hard-coded Government address. That is the same error I was fighting with in the 101 class! I also see how the address should have been set in the Government constructor, which makes a lot of sense! Good walk through on debugging. I had several bugs in my Bank contract that took a lot of noodling to figure out what/where/how the errors were occuring. I tried using the debugger but could not make heads or tails. Now I’m anxious for the next lesson to learn how to use the debugger!

Thanks!

1 Like

Hello, I just have a really quick question. Below is some of the code from the section of the course on data storage. I can’t seem to figure out why the revert line of code does what it does and was hoping someone could explain it to me. I just haven’t ever come across that syntax before.
Thanks!

pragma solidity 0.8.0;

contract mappedWithUnorderedIndexAndDelete {

  struct EntityStruct {
    uint entityData;
    //more data
    uint listPointer; //0
  }

  mapping(address => EntityStruct) public entityStructs;
  address[] public entityList;

  function isEntity(address entityAddress) public view returns(bool isIndeed) {
    if(entityList.length == 0) return false;
    return (entityList[entityStructs[entityAddress].listPointer] == entityAddress);
  }

  function getEntityCount() public view returns(uint entityCount) {
    return entityList.length;
  }

  function newEntity(address entityAddress, uint entityData) public returns(bool success) {
    if(isEntity(entityAddress)) revert();
    entityStructs[entityAddress].entityData = entityData;
    entityList.push(entityAddress);
    entityStructs[entityAddress].listPointer = entityList.length - 1;
    return true;
  }

  function updateEntity(address entityAddress, uint entityData) public returns(bool success) {
    if(!isEntity(entityAddress)) revert();
    entityStructs[entityAddress].entityData = entityData;
    return true;
  }
  
  //[ADRESS1, ADDRESS4, ADDRESS3]

  function deleteEntity(address entityAddress) public returns(bool success) {
    if(!isEntity(entityAddress)) revert();
    uint rowToDelete = entityStructs[entityAddress].listPointer; // = 1
    address keyToMove   = entityList[entityList.length-1]; //save address4
    entityList[rowToDelete] = keyToMove;
    entityStructs[keyToMove].listPointer = rowToDelete; //= 2
    entityList.pop();
    delete entityStructs[entityAddress];
    return true;
  }

}
1 Like

Hey @Ben17, hope you are well.

You are missing a error message inside your revert().

Take a look into this lecture which explain the difference of each, you could use the revert the way you are doing it, or could use a require which could do the same trick.

https://medium.com/blockchannel/the-use-of-revert-assert-and-require-in-solidity-and-the-new-revert-opcode-in-the-evm-1a3a7990e06e

Carlos Z

What about logic error? - I learnt about it when I was learning C++.
Is it the same as usage error?

Has anyone faced the difficulty of trying to run “……> truffle develop” within the terminal and have had errors pop up due to unsupported digital envelope routines? Ive tried looking it up and I’ve found that the error is frustrating lots developers, has anyone found a workaround or solution worth trying?

Hey @jacobo, hope you are well.

Could you please share a screenshot of the error that is showed to you?? :nerd_face:

Carlos Z

Hey Carlos! I’m doing well and hope you are as well, here the picture of the error… am I correct in thinking this has to do with new version of nodejs?image

1 Like

It could be the node version, please try this guide to downgrade it to the version proposed on it:

Sorry for the late replay sir :nerd_face:

Carlos Z