bitcoin-sv / sol2scrypt

Solidity to sCrypt Transplier
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

create examples of transpiling return Fix #90 #91

Closed zhfnjust closed 2 years ago

xhliu commented 2 years ago

To see why it fails, consider the following counter-example

pragma solidity ^0.8.10;

contract Return {
    int counter;

    function test(uint x) public returns (int) {
        if (x > 0) {
            return -1;
        }

        counter++; // <--- this would NOT run if -1 is returned
        return 1;
    }
}

It transpiles to

contract Return {
    @state
    int counter;

    public function unlock(){

        require(true);
    }

    function test(int x) : int {
        int ret = 0;
        bool returned = false;

        if (x > 0) {
            if (!returned) {
                returned = true;
                ret = -1;
            }
        }

        this.counter++; // <--- this would run even if -1 is returned
        return returned ? ret : 1;
    }
}