1

I'm using Nethereum and see this example of how to decode the Input data of a Transaction object. Nethereum Playground C#

This looks like it assumes that you have to know which function data you're decoding. I have the full contract ABI, but I don't know which function was called in this transaction.

Is there a way to decode the Input data without already knowing which function it is? In EthersJs, for example, this is done simply with a ContractInterface.parseTransaction method call.

I'm also open to suggestions for other .NET based decoding solutions/libraries.

2 Answers 2

0

This is possible but would involve some work. The first 4 bytes of that Input data is the method ID. So if you find a transaction on Etherscan then look at the input data you can find it. Method id in transaction input data

This is a hex value and is calculated by taking the method name and its argument types, removing the whitespace then generating a SHA-3 hash of the result.

So you'd first need to write code to generate hashes of the contracts method names using SHA-3, this may not be straightforward as there doesn't seem to be an implementation of it in .NET.

Once you find a way to generate the SHA-3 hashes figuring out the function would be a case of comparing the input data you are using with the generated hashes. When you have confirmed the function you can create an instance of it then call the decodeInput() method.

0

This is how I've done it in Netherium - this decodes a Transaction class to the correct Function class (Generated from Abi):

foreach (var type in Assembly.GetEntryAssembly().GetTypes())
{
    if (type.IsSubclassOf(typeof(FunctionMessage)))
    {
        var abiFunction = ABITypedRegistry.GetFunctionABI(type);

        if (txn.Input.StartsWith($"0x{abiFunction.Sha3Signature}"))
        {
            var m = typeof(FunctionMessageExtensions)
                .GetMethod("DecodeTransactionToFunctionMessage")
                .MakeGenericMethod(type);
            var function = m.Invoke(null, new object[] { txn });
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.