2

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

1
  • 1
    if you have the ABI of this contract, this must be possible just the same way as decoding input of any other method. Commented Aug 25, 2018 at 15:15

1 Answer 1

0

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.

2
  • Hello StuartLC, your revert here did give me hope but i am getting below error using this method : System.OverflowException: Value was either too large or too small for an Int32.............. By anychance do you have any code repo which i can use for reference. Commented May 28, 2019 at 10:38
  • @logeekal those kinds of errors usually result from not matching the provided ABI, and bytecode to the same version that was used to actually write at the contract address. It might also mean that your contract address or transaction hash don't add up. In the above we're using the constructor but this should work for any transaction on the contract Commented May 28, 2019 at 11:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.