Solidity

如何在 Solidity 中獲取呼叫的還原原因,以便可以在同一個鏈上交易中使用它?

  • March 11, 2022

我想獲得失敗的恢復原因,call然後在同一個鏈上交易中使用該恢復原因。這可能嗎?

我修改了shanes的答案:

function _getRevertMsg(bytes memory _returnData) internal pure returns (string memory) {
   // If the _res length is less than 68, then the transaction failed silently (without a revert message)
   if (_returnData.length < 68) return 'Transaction reverted silently';

   assembly {
       // Slice the sighash.
       _returnData := add(_returnData, 0x04)
   }
   return abi.decode(_returnData, (string)); // All that remains is the revert string
}

它似乎有效,這樣我們就不需要額外的庫

引用自:https://ethereum.stackexchange.com/questions/83528