Debugging

As someone who is involved in many development groups, I see questions asked constantly about errors/bugs. The vast majority of these bugs could be located and solved with a simple method.

Let's take the following example.

[[eosio::on_notify("*::transfer")]] void receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo);

void mycontract::receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo){
    function_a();
    function_b();
    function_c();
    function_d();
}

Let's say my contract now receives an asset transfer, and throws some error. If I don't know where the error is coming from, the easiest way I can track it down is by doing this...

[[eosio::on_notify("*::transfer")]] void receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo);

void mycontract::receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo){
    check( false, "no error yet" );
    function_a();
    function_b();
    function_c();
    function_d();
}

If I now try to make a transfer to my contract, and the error message is no error yet, it means the notification handler works.

Next, I'll move the check down one line.

[[eosio::on_notify("*::transfer")]] void receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo);

void mycontract::receive_token_transfer(name from, name to, eosio::asset quantity, std::string memo){
    function_a();
    check( false, "passed function_a" );
    function_b();
    function_c();
    function_d();
}

If the new error is passed function_a, then function_a is not causing the error. I will continue this process until I figure out which function causes the initial error.

Then, I can go into that helper function and do the exact same thing. As a developer it's important that you learn how to debug properly. And I always encourage people to ask questions when they are stuck - just like I encourage experienced developers to help new people learn when they need help.

This doesn't mean you shouldn't attempt to debug first though, so keep this tool in your arsenal and use it when needed.

Last updated