0

I'm using the below functions to get the revert reason :

func ErrorReason(ctx context.Context, from common.Address, tx *types.Transaction, blockNum *big.Int, ethClient ethclient.Client) (string, error) {
    msg := ethereum.CallMsg{
        From:     from,
        To:       tx.To(),
        Gas:      tx.Gas(),
        GasPrice: tx.GasPrice(),
        Value:    tx.Value(),
        Data:     tx.Data(),
    }

    res, err := ethClient.CallContract(ctx, msg, blockNum)
    if err != nil {
        return "", errors.Wrap(err, "CallContract")
    }
    return unpackError(res)
}

var (
    errorSig            = []byte{0x08, 0xc3, 0x79, 0xa0} // Keccak256("Error(string)")[:4]
    abiString, _        = abi.NewType("string", "", nil)
)

func unpackError(result []byte) (string, error) {
    if !bytes.Equal(result[:4], errorSig) {
        return "<tx result not Error(string)>", errors.New("TX result not of type Error(string)")
    }
    vs, err := abi.Arguments{{Type: abiString}}.UnpackValues(result[4:])
    if err != nil {
        return "<invalid tx result>", errors.Wrap(err, "unpacking revert reason")
    }
    return vs[0].(string), nil
}

But I'm getting an error message in this line :

    res, err := ethClient.CallContract(ctx, msg, blockNum)
    if err != nil {
        return "", errors.Wrap(err, "CallContract") //This is returning CallContract : Execution reverted
    }

So I'm not able to actually get the reason. Is there a simpler way? Or how can I modify this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.