Replies: 17 comments
-
Hey.. Is it possible to search for a element inside an array logged through indexed event? |
Beta Was this translation helpful? Give feedback.
-
Oh! Sorry, I don't know how I missed this issue. You cannot search within an indexed set of an array, but you can search for an exact match. For example, if you were to log an array When a value that is not exactly 32 bytes long is indexed, the entry that is logged to the receipt is the hash of that value. This might still require some fiddling with to find the exact hash you want, and based on the error you posted above, I may not have added support for that yet. Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo. Yes that does make sense. What should I do then. I checked in web3.js and they also have same issue. Is there a way to find out the txn hash based on the docHash? |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo. Does this mean it is only possible to search for hashes if we have all the hashes that was passed to the function. Is it not possible to search by using a single hash? |
Beta Was this translation helpful? Give feedback.
-
That is correct, for the API you have. You could log multiple indexed hashes, however you are still limited to up to 3 indexed values. And the complexity and has cost would go up. The other option might be to build something like a bloom filter, which would result in false positives you would need to remove afterward. Or build an extra service which indexed all events and derives this, and caches it in a database to be queried via some backend. There are options, but nothing built into Ethereum directly... :s |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo So, as you are saying I have to have a database consisting of transaction ids and query them to blockchain? |
Beta Was this translation helpful? Give feedback.
-
You mean you are able to get the full list of docHash from the onSubmission event? I don’t think you should be able to. Or do you mean you can query the contract? Can you give an example transaction hash and the code that can return the full set? The main thing about this is the indexing. The way indexing works is by creating a bloom filter for each of the indexed fields, which allows efficient searching by filters, but is also why there is an additional cost of 375 gas per log topic. This bloom filter is then stored in the receipt and a receipt root it added in the block header so this can be verified (although, I have never used that feature). But for items which are not fixed-width, they cannot be included in the bloom filter (since the bloom filter requires fixed-width entries), so the hash is used instead. Does that make sense? Let me know the code snippet that lets you access all the docHash based on transaction hash too and I’ll look more into it. :) |
Beta Was this translation helpful? Give feedback.
-
Another option is to emit multiple events per call. Like |
Beta Was this translation helpful? Give feedback.
-
This code gives me the bytes32[] and string[] in decoded form. By the way this is ropsten test net |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo. Thanks for your hints. I am using your way of emitting onSumbission(bytes32 docHash) by looping through all the docHashes |
Beta Was this translation helpful? Give feedback.
-
Ah yes. You are not decoding the event data. You are decoding the transaction data. If you look at the receipts for that TX, the events for onSubmission will only be the hash of those docHashes. However, keep in mind this would fail if another contract submitted a docHash. I could have a contract method that took no parameters, but called your contract with the values A and B. The event would be emitted but the tx.data would contain zero hashes (it would only be my 4-byte selector), but the event would contain the hash(A + B). Your costs will go up, but if you want the ability to search for a specific docHash, you are asking it to build a more complex index for you, so you would expect it to cost more. That said, I don’t think the cost would be /that/ much more. You will pay an additional 750 gas (ish; I think) per docHash. :) |
Beta Was this translation helpful? Give feedback.
-
I did not get this totally. But I have onlyOwner modifier so that only I can call the function |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo . Also how do I search indexed string. |
Beta Was this translation helpful? Give feedback.
-
Hey @ricmoo I have indexed the string. When I use web3.utils.sha3 for the indexed string I am able to filter it. But for |
Beta Was this translation helpful? Give feedback.
-
You should be using See here for more info on the available hashing functions. :) |
Beta Was this translation helpful? Give feedback.
-
(Also keep in mind if you are using the |
Beta Was this translation helpful? Give feedback.
-
@ricmoo Hey Thanks for the info. Ya it requires no hashing. Hah! very friendly api |
Beta Was this translation helpful? Give feedback.
-
Hey,
I have array of Hashes that I want to log in an event so that I can search for the hashes later on. But ethers.js doesn't seem to filter the array of Hashes present.
Solidity code:
event onSubmission(bytes32[] indexed docHash);
NodeJs code:
const filter = await contract.filters.onSubmission(["0x69a013f23c52f5415ae41e64561a6948e08df59904925db53f0df74fd64a3833"]);
I get:
Error: filtering with tuples or arrays not supported (argument="contract.docHash", value=[], code=INVALID_ARGUMENT, version=abi/5.0.1)
Is it possible to search for an array inside a event?
Beta Was this translation helpful? Give feedback.
All reactions