如何在solidity中使用Oraclize向数据源传递参数?

How to use Oraclize in solidity to pass arguments to the data source?

我的目标是将一些字符串传递给数据源,然后在那里进行处理并取回结果。 下面给定的代码可以正常工作

oraclize_query("URL", "json(https://clever-ape-38.localtunnel.me).a","sfdg");

但是当我尝试从区块链中获取一些值并使用它时,如果失败

string memory st = arr[msg.sender];
oraclize_query("URL", "json(https://clever-ape-38.localtunnel.me).a",st); 

一切都正确编译。甚至 truffle migrate --reset 也能正常工作。 我觉得从区块链中获取数据需要一些时间,并且 oraclize_query() 在获取之前被调用。

下面提到了错误。

    [2019-05-28T08:54:50.206Z] INFO new HTTP query created, id: 72e3a263387ed365d97bc97a64c601380a5b4e8bbd1b3740bcc2af1e2bc97b19
[2019-05-28T08:54:50.215Z] INFO checking HTTP query 72e3a263387ed365d97bc97a64c601380a5b4e8bbd1b3740bcc2af1e2bc97b19 status in 0 seconds
[2019-05-28T08:54:50.215Z] INFO checking HTTP query 72e3a263387ed365d97bc97a64c601380a5b4e8bbd1b3740bcc2af1e2bc97b19 status every 5 seconds...
[2019-05-28T08:54:56.634Z] INFO 72e3a263387ed365d97bc97a64c601380a5b4e8bbd1b3740bcc2af1e2bc97b19 HTTP query result: 
    {
    "result": {
        "_timestamp": 1559033691,
        "id": "72e3a263387ed365d97bc97a64c601380a5b4e8bbd1b3740bcc2af1e2bc97b19",
        "daterange": [
            1559033689,
            1559035489
        ],
        "_lock": false,
        "id2": "72fdf59adf3ecc92244e3b173ce1657252ab5eb877c3569a393599d34dd2c848",
        "actions": [],
        "interval": 3600,
        "checks": [
            {
                "errors": [
                    "TypeError",
                    "parsing_helper.wrong_path"
                ],
                "success": true,
                "timestamp": 1559033691,
                "results": [
                    ""
                ],
                "proofs": [
                    null
                ],
                "match": true
            }
        ],
        "version": 3,
        "_timestamp_creation": 1559033689,
        "context": {
            "protocol": "eth",
            "relative_timestamp": 1559033687,
            "type": "blockchain",
            "name": "eth_AB65E563DB"
        },
        "active": false,
        "hidden": false,
        "payload": {
            "conditions": [
                {
                    "query": [
                        "json(https://purple-squid-54.localtunnel.me).a",
                        "28189689"
                    ],
                    "proof_type": 0,
                    "check_op": "tautology",
                    "datasource": "URL",
                    "value": null
                }
            ]
        }
    },
    "success": true
}
[2019-05-28T08:54:56.637Z] ERROR HTTP query error
    [
    "TypeError",
    "parsing_helper.wrong_path"
]
[2019-05-28T08:54:56.639Z] INFO sending __callback tx...
    {
    "contract_myid": "0x72fdf59adf3ecc92244e3b173ce1657252ab5eb877c3569a393599d34dd2c848",
    "contract_address": "0x481a276d14a6a74e1ec1f74b64c2af226ba7033c"
}
[2019-05-28T08:55:01.853Z] INFO contract 0x481a276d14a6a74e1ec1f74b64c2af226ba7033c __callback tx sent, transaction hash: 0xfada229b6f9860e0717b3a098dd93aaef280852dbf75109c830b555c488e6c81
    {
    "myid": "0x72fdf59adf3ecc92244e3b173ce1657252ab5eb877c3569a393599d34dd2c848",
    "result": "",
    "proof": null,
    "proof_type": "0x00",
    "contract_address": "0x481a276d14a6a74e1ec1f74b64c2af226ba7033c",
    "gas_limit": 200000,
    "gas_price": null
}

请帮忙解决这个问题。

感谢您添加错误日志。

实际答案:

如果您将 POST 数据作为字符串添加,但它是有效的 JSON 格式,它将被这样解析。为了将其保存为字符串,您需要添加换行符或空格所以字符串的开头:

"\n<post-data-here"

关于 parsing-error 的原始答案最终证明不是主要问题...

您在 ethereum-bridge 中遇到了解析错误,因为它尝试但未能解析从您的查询返回的 json

但是在撰写本文时,您的本地隧道正在返回一个 404 错误,而不是它最初返回的数据。

然后,作为 ethereum-bridgeattempts to parse thejsonto pluck the value from thea` 键,根据您的查询:

json(https://purple-squid-54.localtunnel.me).a

...它不能,因为没有 json,所以没有 a 字段,所以你得到一个解析错误。

要修复它,请确保您的本地隧道正常工作并且返回的数据采用正确的 json 格式,并且有一个 a 密钥可供解析器使用。