JS SDK getLogs方法无法正常解析标记为indexed的参数

合约代码如下:

pragma solidity ^0.4.24;

contract HelloWorld {
string name;
uint times;

event LogChange(uint indexed _times, string _name);

constructor(string _name) public {
    name = _name;
    times = 0;
}

function sayHi() public view returns (string) {
    return (name);
}

function changeName(string _name) public {
    name = _name;
    times ++;
    emit LogChange(times, name);
}

}

getLogs方法调用如下:

const abi = [
{
“indexed”: true,
“name”: “_times”,
“type”: “uint256”
},
{
“indexed”: false,
“name”: “_name”,
“type”: “string”
}
]

const filter = {
    address: contracts.HelloWorld._address,
    fromBlock: "0x0",
}

sdk.base.getLogs(filter, abi).then(
    (res => {
        console.log(res)
    })
).catch(err => {
    console.log(err)
})

返回的数据中decodedLogs字段下的_name可以正确解析,_times无法正确解析

1赞

@cryptape/cita-sdk@^0.24.0

uint indexed _times 去掉 indexed 改为 uint _times 后字段解析正常

@luqz812, 你看一下用 JSON-RPC 返回的数据正常吗?

JSON-RPC 返回的数据没有问题,decode 之前的数据也是没有问题的。

那可能是 JS SDK 对数据解析的问题。@ChenYu 帮忙看一下吧?

麻烦贴一下返回的 log, 谢谢 @luqz812

[{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000001"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000950617261626f7836320000000000000000000000000000000000000000000000","blockHash":"0xca84d4ceaac3455d0bd6dc8d95ebb8eacae4015f61d596d24f02adeb4abc6c27","blockNumber":"0x16af1c","transactionHash":"0xd6158c010cdc5b8f79ea254e1f9cc3fc5e9cc0eb754c8df355f38ce593cc5761","transactionIndex":"0x0","logIndex":"0x0","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox62","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox62"}},{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000002"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000950617261626f7838310000000000000000000000000000000000000000000000","blockHash":"0xaa3ac5370d407b4c931a3079e22ef6ac1d09d18355e7ead0a19a589ca614473e","blockNumber":"0x16af1d","transactionHash":"0x0f62b1af96340afe23d5681fb3b2869cbcecf6d3ea3656cd3d07d9cd4747f9bb","transactionIndex":"0x0","logIndex":"0x0","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox81","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox81"}},{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000003"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000850617261626f7832000000000000000000000000000000000000000000000000","blockHash":"0xff5fb4a967c39154b320e8c0ea52cab89e18ed66b625ea9a3f9451ce892faa39","blockNumber":"0x16af1f","transactionHash":"0xac72281ae5c6efd5f0329d96604c9284b598b2b964fa9c4a77932d194ae0b951","transactionIndex":"0x0","logIndex":"0x0","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox2","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox2"}},{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000004"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000950617261626f7839370000000000000000000000000000000000000000000000","blockHash":"0xb89019001f150ba890e2c4c7efddbde1106bd0e0ea7abc9ffe850bbcc7f25267","blockNumber":"0x16af23","transactionHash":"0x01a2ed1ce5f5b4d91aa68879f8c981c7dacf4068b15959190254860423ce7a69","transactionIndex":"0x0","logIndex":"0x0","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox97","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox97"}},{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000005"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000950617261626f7833330000000000000000000000000000000000000000000000","blockHash":"0x58a1afd23f2c2d8417dafe6f080f136d0ad23d6d1dc5f013a5d302de9c05d559","blockNumber":"0x16af27","transactionHash":"0x809aa2d9a0cdaf8b2f223221c2b562f61181df8bd5c7c5e211e1b248f6e8ae0f","transactionIndex":"0x0","logIndex":"0x0","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox33","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox33"}},{"address":"0x8d90c685ea9abc1c1d2d06179499fc16bb2fac3c","topics":["0x7905819d441f10241169dcedecceb7fc89976ab53715125ffa832627eb9b2105","0x0000000000000000000000000000000000000000000000000000000000000006"],"data":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000950617261626f7837310000000000000000000000000000000000000000000000","blockHash":"0xde05fac6a496924bc9c2df304de7b21c7ce456f2c46ac65128bdeca522cc4058","blockNumber":"0x16af35","transactionHash":"0xb35f3a74a032bd000415ba35ab64f5515cdebeaa9e74e65b201eed4a7b0e75d0","transactionIndex":"0x1","logIndex":"0x1","transactionLogIndex":"0x0","decodedLogs":{"0":"54739583479063502071391643825396492492243453859751133445752824553938538406149","1":"Parabox71","__length__":2,"_times":"54739583479063502071391643825396492492243453859751133445752824553938538406149","_name":"Parabox71"}}]

大概知道你遇到的问题了, topics 数组里第一个值不是 _times, 而是事件签名(这是一般默认的), 第二个值才是 _times, 可以看到他是有序递增的.

然后第一个值是这样来的:

cita.eth.abi.encodeEventSignature({
    name: 'LogChange',
    type: 'event',
    inputs: [{
        type: 'uint256',
        name: '_times',
    }, {
        type: 'string',
        name: '_name',
    }]
})

相关文档: https://solidity.readthedocs.io/en/develop/abi-spec.html?highlight=topics
搜索 topics[0]

是我理解错了, 你的意思应该是指这个方法返回结果有误.
已经发布 @cryptape/cita-sdk@0.24.1 修复了这个问题, 非常抱歉.

1赞