darwinia-network / darwinia-messages-sol

Darwinia cross-chain messages gateway and protocol for EVM developers 💌
MIT License
29 stars 8 forks source link

How to try/catch error when every message execute #45

Closed hujw77 closed 3 years ago

hujw77 commented 3 years ago

https://github.com/darwinia-network/darwinia-bridge-sol/pull/41#discussion_r664371982

hujw77 commented 3 years ago
pragma solidity >0.6.0;

contract TryCatch {

    event LogRes(bool);

    uint64 public nonce;

    function execute(uint256 amount) external {

        // the low level call will return `false` if its execution reverts
        (bool success, bytes memory returnData) = address(this).call(
            abi.encodeWithSignature(
                "onlyEven(uint256)",
                amount
            )
        );
        emit LogRes(success);
        nonce++;
    }

    function onlyEven(uint256 a) public {
        require(a % 2 == 0, "Ups! Reverting");
    }
}