[Ethereum] Decode input data on transaction

web3js

I have an ABI and the input value of the transaction and want to know what function has been called on a contract and with what arguments (one or multiple of different types), preferably using geth, JSON-RPC and some pseudo code that I can then translate into Ruby.

There are a couple of similar questions on SO but none of them really answered the question in a way that's understandable to someone new to Ethereum.

As an example, I use the MetaCoin contract generated by Truffle. I call the method getBalance('0xfd46f749f9d916122fe958d7f8d5ad033b187472') and get the input 0xf8b2cb4f000000000000000000000000fd46f749f9d916122fe958d7f8d5ad033b187472.

truffle(development)> web3.eth.getTransaction('0x5d7a91c9f068d723ac52480c0ef61b9bf3f7b52dc726046e0b47f1beaa2f44ff')
{ hash: '0x5d7a91c9f068d723ac52480c0ef61b9bf3f7b52dc726046e0b47f1beaa2f44ff',
  nonce: 5,
  blockHash: '0xd150b520eb69a621a7218722c0de5b270c9abc499a90a81ca96340e274599c28',
  blockNumber: 6,
  transactionIndex: 0,
  from: '0xfd46f749f9d916122fe958d7f8d5ad033b187472',
  to: '0x8dec2f9cccbe718c7c51e9154f223eb226bb7942',
  value: { [String: '0'] s: 1, e: 0, c: [ 0 ] },
  gas: 4712388,
  gasPrice: { [String: '100000000000'] s: 1, e: 11, c: [ 100000000000 ] },
  input: '0xf8b2cb4f000000000000000000000000fd46f749f9d916122fe958d7f8d5ad033b187472' }

The ABI is as follows:

[
    {
      "constant": false,
      "inputs": [
        {
          "name": "addr",
          "type": "address"
        }
      ],
      "name": "getBalanceInEth",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "receiver",
          "type": "address"
        },
        {
          "name": "amount",
          "type": "uint256"
        }
      ],
      "name": "sendCoin",
      "outputs": [
        {
          "name": "sufficient",
          "type": "bool"
        }
      ],
      "payable": false,
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "addr",
          "type": "address"
        }
      ],
      "name": "getBalance",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "type": "function"
    },
    {
      "inputs": [],
      "payable": false,
      "type": "constructor"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "name": "_from",
          "type": "address"
        },
        {
          "indexed": true,
          "name": "_to",
          "type": "address"
        },
        {
          "indexed": false,
          "name": "_value",
          "type": "uint256"
        }
      ],
      "name": "Transfer",
      "type": "event"
    }
  ]

How can I decode any input to know what method was called and what arguments were passed in?

Update

I've added all data of the block with the input field at the very bottom in the logs.

{"number"=>"0x09",
 "hash"=>"0xf543eba0c80d49beea1f934c0f0ad9f0babe272a8318312545d43e6b612e4006",
 "parentHash"=>
  "0xde7a61e8dd84b5e080da8e16430951f7f138347c77ed263d0093e367c9d30775",
 "nonce"=>"0x0",
 "sha3Uncles"=>
  "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
 "logsBloom"=>
  "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
 "transactionsRoot"=>
  "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
 "stateRoot"=>
  "0x269cfa04ca1fe73202f788397ece62c520c64d5da7225ba2af2d673a2e9892f2",
 "receiptRoot"=>
  "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
 "miner"=>"0x0000000000000000000000000000000000000000",
 "difficulty"=>"0x0",
 "totalDifficulty"=>"0x0",
 "extraData"=>"0x0",
 "size"=>"0x03e8",
 "gasLimit"=>"0x47e7c4",
 "gasUsed"=>"0x5a3e",
 "timestamp"=>"0x58c83fda",
 "transactions"=>
  [{"hash"=>
     "0xce1eaf03dd1dfa5898243711ebcc1dc5c6357701f5a6c19f0b1f84130cb651fa",
    "nonce"=>"0x08",
    "blockHash"=>
     "0xf543eba0c80d49beea1f934c0f0ad9f0babe272a8318312545d43e6b612e4006",
    "blockNumber"=>"0x09",
    "transactionIndex"=>"0x0",
    "from"=>"0xfd46f749f9d916122fe958d7f8d5ad033b187472",
    "to"=>"0x8dec2f9cccbe718c7c51e9154f223eb226bb7942",
    "value"=>"0x0",
    "gas"=>"0x47e7c4",
    "gasPrice"=>"0x174876e800",
    "input"=>
     "0xf8b2cb4f000000000000000000000000fd46f749f9d916122fe958d7f8d5ad033b187472"}],
 "uncles"=>[]}

Best Answer

You can refer to a packaged developed by Consensys https://github.com/ConsenSys/abi-decoder

Related Topic