使用geth的GraphQL进行简单查询

Geth支持EIP-1767提案中设计的GraphQL接口,可用于查询基本的数据。测试了一下,可替代部分JSON-RPC的功能。目前支持的schema有限,具体参考schema.go,对于复杂的查询也会相对耗时。以下两个例子供参考。

查询Mempool的信息:

{
  syncing {
    currentBlock
    highestBlock
  }
  pending {
    transactionCount
    transactions {
      hash
      from {
        address
      }
      to {
        address
      }
      value,
      inputData
    }
  }
}

上述语句可查询当前节点中Pending Tx的信息,结果如下:

Mempool Info

通过Topics filter,查询最新的Block中的DEX交易信息:

{
  block {
    number
    timestamp
    miner {
      address
    }
    logs(
      filter: {
        topics: [
          [
            "0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"
            "0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
            "0xa07a543ab8a018198e99ca0184c93fe9050a79400a0a723441f84de1d972cc17"
            "0xb9ed0243fdf00f0545c63a0af8850c090d86bb46682baec4bf3c496814fe4f02"
            "0xb2e76ae99761dc136e598d4a629bb347eccb9532a5f8bbd72e18467c3c34cc98"
            "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8"
          ]
        ]
      }
    ) {
      transaction {
        index
        hash
        from {
          address
        }
        to {
          address
        }
        value
        inputData
        logs {
          contractAddress: account {
            address
          }
          topics
          data
        }
        accessList {
          address
          storageKeys
        }
      }
    }
  }
}

上述查询的结果如下:

{
  "data": {
    "block": {
      "number": 16795884,
      "timestamp": "0x640aca0b",
      "miner": {
        "address": "0x690b9a9e9aa1c9db991c7721a92d351db4fac990"
      },
      "logs": [
        {
          "transaction": {
            "index": 0,
            "hash": "0xf24b3dad7570a30087df502d09c498822fecfd6810093a1b653ac36383861576",
            "from": {
              "address": "0xae2fc483527b8ef99eb5d9b44875f005ba1fae13"
            },
            "to": {
              "address": "0x6b75d8af000000e20b7a7ddf000ba900b4009a80"
            },
            "value": "0x155e08c",
            "inputData": "0x251ba36aef2ee8d692dde8f045226635d4b01b30d1a7116c7668",
            "logs": [
              {
                "contractAddress": {
                  "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
                },
                "topics": [
                  "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                  "0x0000000000000000000000006b75d8af000000e20b7a7ddf000ba900b4009a80",
                  "0x000000000000000000000000a36aef2ee8d692dde8f045226635d4b01b30d1a7"
                ],
                "data": "0x0000000000000000000000000000000000000000000000000155e08c00000000"
              },
              {
                "contractAddress": {
                  "address": "0x4384b85fe228ae727b129230211194e4a50877c4"
                },
                "topics": [
                  "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                  "0x000000000000000000000000a36aef2ee8d692dde8f045226635d4b01b30d1a7",
                  "0x0000000000000000000000006b75d8af000000e20b7a7ddf000ba900b4009a80"
                ],
                "data": "0x0000000000000000000000000000000000000000000000116c76680000000000"
              },
              {
                "contractAddress": {
                  "address": "0xa36aef2ee8d692dde8f045226635d4b01b30d1a7"
                },
                "topics": [
                  "0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"
                ],
                "data": "0x000000000000000000000000000000000000000000000d2f79a9a32be9a890dc0000000000000000000000000000000000000000000000010346c00814a029ea"
              },
              {
                "contractAddress": {
                  "address": "0xa36aef2ee8d692dde8f045226635d4b01b30d1a7"
                },
                "topics": [
                  "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822",
                  "0x0000000000000000000000006b75d8af000000e20b7a7ddf000ba900b4009a80",
                  "0x0000000000000000000000006b75d8af000000e20b7a7ddf000ba900b4009a80"
                ],
                "data": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000155e08c000000000000000000000000000000000000000000000000000000116c766800000000000000000000000000000000000000000000000000000000000000000000000000"
              }
            ],
            "accessList": [
              {
                "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "storageKeys": [
                  "0x12231cd4c753cb5530a43a74c45106c24765e6f81dc8927d4f4be7e53315d5a8",
                  "0x6cf9ecbf1a5addaedce62692dae3ebc86da40ffcb4064b89fb3be455181ecddc"
                ]
              },
              {
                "address": "0xa36aef2ee8d692dde8f045226635d4b01b30d1a7",
                "storageKeys": [
                  "0x000000000000000000000000000000000000000000000000000000000000000f",
                  "0x0000000000000000000000000000000000000000000000000000000000000008",
                  "0x0000000000000000000000000000000000000000000000000000000000000006",
                  "0x0000000000000000000000000000000000000000000000000000000000000007",
                  "0x000000000000000000000000000000000000000000000000000000000000000c",
                  "0x0000000000000000000000000000000000000000000000000000000000000009",
                  "0x000000000000000000000000000000000000000000000000000000000000000a"
                ]
              },
              {
                "address": "0x4384b85fe228ae727b129230211194e4a50877c4",
                "storageKeys": [
                  "0x230c19c5ed96875216b3acf817ebca9a29187dd363ec0134fe98443edfa98e06",
                  "0x000000000000000000000000000000000000000000000000000000000000000a",
                  "0xdae3a688260499966280beec675816d63f6333a3dfcad7a319d99d20774f2dfa",
                  "0x80afb76e257131935ae685d8fdd6e987b200e41c9bd2529c7b1dc13fa309ab44",
                  "0x59c5a0582853b22cec98b71efaf11d4e6e716112079a141dde18505c4b33c6eb",
                  "0xb39e9ba92c3c47c76d4f70e3bc9c3270ab78d2592718d377c8f5433a34d3470a",
                  "0x0000000000000000000000000000000000000000000000000000000000000008",
                  "0x0000000000000000000000000000000000000000000000000000000000000005",
                  "0x596076f9baf3f5b0c97be8e6ee1c333ab167d5ccfc395cda0e2a80e0232369d2",
                  "0x88664213c2a8b3c77bf36b3fca37fa201d9f5a3d51d467393c7d134b245a009a"
                ]
              }
            ]
          }
        },

从上述结果可以看到,查询交易信息时,要比手动解析方便。

发表评论