如何经过getTransaction来确认交易成功

当经过sendTransaction发出交易以后,transaction并无当即上链,因此要先经过txpool.status.pending来看看若是大于0,说明你的结点中有pending的transaction。(或者经过eth.getBlock("pending")来查看你的交易是否在pending块中)web

固然若是没有上链,这个交易是在你的结点中发出的,能够直接经过getTransaction来获取交易,返回的结果中blockHash为零,blockNumber为null:json

{ blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  blockNumber: null,
  from: '0xdfb1b9b8693366eb9044ffd8c00058abc904558b',
  gas: 90000,
  gasPrice: ...,
  hash: '0x0b4f742149fc3018a168950b56786846da99675913dbc043971cbe25ac7792ac',
  input: '0x48656c6c6f20576f726c64',
  nonce: 2,
  to: null,
  transactionIndex: null,
  value: ...

可是他奶奶的,若是有一种状况,交易成功了,可是out of gas,以前的逻辑都是成功了,也上链了,可是交易却没有成功,我在转erc20币的时候遇到很多这种状况,这怎搞?api

经过debug_traceTransaction来看看这笔交易有没有error。app

首先你的结点rpc必须是开放debug的:curl

geth --rpc --rpcapi "eth,net,web3,debug" console

而后调用debug_traceTransaction:url

curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"debug_traceTransaction", "params":["0x3684f071b34da1116282ee88a106a8f2a266d273ef7d8964957f65128fb58d77", {}], "id":1}'

结果相似这样:debug

{
  gas: 85301,
  returnValue: "",
  structLogs: [{
      depth: 1,
      error: "",
      gas: 162106,
      gasCost: 3,
      memory: null,
      op: "PUSH1",
      pc: 0,
      stack: [],
      storage: {}
  },
    /* snip */
  {
      depth: 1,
      error: "",
      gas: 100000,
      gasCost: 0,
      memory: ["0000000000000000000000000000000000000000000000000000000000000006", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000060"],
      op: "STOP",
      pc: 120,
      stack: ["00000000000000000000000000000000000000000000000000000000d67cbec9"],
      storage: {
        0000000000000000000000000000000000000000000000000000000000000004: "8241fa522772837f0d05511f20caa6da1d5a3209000000000000000400000001",
        0000000000000000000000000000000000000000000000000000000000000006: "0000000000000000000000000000000000000000000000000000000000000001",
        f652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f: "00000000000000000000000002e816afc1b5c0f39852131959d946eb3b07b5ad"
      }
  }]

从structLogs中一个个看是否有error非null的存在,若是有说明实际是失败了3d

相关文章
相关标签/搜索