TransWikia.com

How can I decode the inputs to a transaction written by a smart contract constructor with Nethereum?

Ethereum Asked by StuartLC on January 2, 2021

I’ve been able to decode the inputs to a contract function transaction given the ABI as follows:

var myFunction = contract.GetFunction("MyFunction");
var myFunctionTxn = await contract.Eth.Transactions.GetTransactionByHash
        .SendRequestAsync(myTxnHash);
var inputs = myFunction.DecodeInput(myFunctionTxn.Input);

However, now I would like to do the same (i.e. retrieve a transaction hash and then decode the constructor inputs) for the constructor itself. I’ve looked at the ContractABI.Functions – I can see both the Constructor and the Functions, but the types are not compatable.

Also, I can see the types of the inputs from this, but unfortunately not the values:

var ctor = contract.ContractBuilder.ContractABI.Constructor;

Is there an equivalent for DecodeInput for constructors?

From here, it looks like this must be possible

One Answer

Based on a brief conversation on Gitter with Nethereum's Juan Blanco, I'm using the code from the ConstructorCallDecoder to decode the Constructor.

using Nethereum.Web3;
...
var contract = _ethClient.Eth.GetContract(_contractAbi, contractAddress);
var ctorTransaction = await contract.Eth.Transactions.GetTransactionByHash
    .SendRequestAsync(constructorTransactionHash);
var decodedCtor = new ConstructorCallDecoder()
    .DecodeConstructorParameters(
        pocoToPopulate, _contractByteCode, ctorTransaction.Input);

Where

  • _contractAbi is the ABI for the smart contract
  • contractAddress is the address of the contract instance
  • constructorTransactionHash is the transaction hash of the constructor
  • _contractByteCode is the byte code of the Smart Contract
  • pocoToPopulate is an instance of a class decorated with the necessary Parameter attributes to receive the decoded parameters.

There are likely simpler ways to do this in future (e.g. without the redundancy of requiring both ABI and ByteCode to do the decoding), but this is good enough for me right now.

Answered by StuartLC on January 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP