如何按字符串参数web3 2.0.0-alpha.1 Solidity事件过滤? [英] How to filter by string parameter , web3 2.0.0-alpha.1 Solidity events?
问题描述
我正在尝试过滤一些事件,并且我注意到自从将web3更新为2.0.0-alpha 1版本以来,事件捕获有所不同。
I'm trying to filter some events , and I noticed since I updated the web3 to version 2.0.0-alpha 1 the event catch is a little bit different.
我有一个与此事件相关的智能合约:
I have a Smart Contract with this event :
event catchMeIfYouCan (address indexed a, string indexed b, uint indexed c);
我想按其参数进行过滤,到目前为止效果很好。
但是,当我尝试按b(索引的字符串)过滤时,此方法不起作用。
我正在使用ExpressJS和上面提到的Web3版本在NodeJS中这样做。
And I want to filter by its parameters , so far so good. But when I try to filter by b ( the string indexed ) , this is not working. I'm doing that in NodeJS with ExpressJS and the Web3 version mentioned above.
如果我这样做了:
const event = smartContract.events.catchMeIfYouCan({ filter : {
a : accountAddress ,
b : web3.utils.toHex(stringValue) ,
c : web3.utils.toWei("" + numberValue) }
}, (error, event) => {
// do some things
});
我知道了:
Node error: {"code":-32602,"message":"invalid argument 1: hex has invalid length 96 after decoding"}
否则,如果我让NodeJS事件中的b参数捕获为:
Otherwise , if I let the b parameter , in NodeJS event catch as :
b : stringValue,
它不再捕获事件,与c相同(例如:再也没有 web3.utils.toWei()
)。
It doesn't catch the event anymore , same with c ( e.g : no more web3.utils.toWei()
).
所以,我的问题是:你有吗想法如何在Web3 2.0.0-Alpha 1版本中通过字符串参数过滤事件?
So , my question is : Do you have any idea how to filter the event by a string parameter in Web3 2.0.0-Alpha 1 version ?
感谢您的帮助&祝你有美好的一天!
Thank you for help & Have a nice day!
推荐答案
感谢您的提问,我对其进行了测试,并相信它是过滤器
Thanks for your question, I tested it and I believe its a bug with filter
但是我尝试使用 topics
,它有效
But I tried use topics
and it works
contract.events.CatchMeIfYouCan({
topics: [, web3.utils.sha3(stringValue)], // first element is empty, because its place for `address` index
fromBlock: 2000000
}, (error, event) => {
console.log(event)
})
因此,我创建了问题在 web3.js
回购
这篇关于如何按字符串参数web3 2.0.0-alpha.1 Solidity事件过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!