Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

62

Q What does the "indexed" keyword do in the below line of code? I'm guessing it just tells the event object that the following input should be logged? The indexed parameters for logged events will allow you to search for these events using the indexed parameters as filters. Q Can we use it other places ie outside of events? The indexed keyword is only ...


37

One could consider topics as different index names. EVM uses low level primitives called logs to map them to high level Solidity construct called Event. Logs may contain different topics that are indexed arguments. Consider Event: emit PersonCreated(uint indexed age, uint indexed height); And you fire it in MyContract: function foobar() { ...


34

Do this: You'll need to pull code from web3, and it works best if your frontend is bundled using something like webpack or browserify: var SolidityCoder = require("web3/lib/solidity/coder.js"); var log = receipt.logs[0]; var data = SolidityCoder.decodeParams(["string", "uint"], log.data.replace("0x", "")); In this case, we're decoding log data that ...


33

Logs are part of the transaction receipts. They are generated by the clients when executing transactions and stored alongside the blockchain to allow retrieving them. Logs are not part of the blockchain itself per se, since they are not required for consensus (they are just historical data), however they are verified by the blockchain as the transaction ...


29

With an exception, as with throw, all effects (including events) of a transaction are reverted, except for the payment to the miner. So there would be no benefit in firing an event before a throw. Possible Alternative If full reversion via an exception isn't required, an error code might be usable. Example: contract C { function foo() returns(int) { ...


28

Take a look at web3.eth.filter and watch. Something like this: const filter = web3.eth.filter({ fromBlock: 0, toBlock: 'latest', address: contractAddress, topics: [web3.sha3('newtest(string,uint256,string,string,uint256)')] }) filter.watch((error, result) => { // }) Note the part that "In Solidity: The first topic is the hash of the ...


26

The JSON is called an ABI. You do need the source code, as you have, and one way to get the ABI is to paste it in Solidity Browser, then copy the Interface value.


24

The blockchain is a list of blocks which are fundamentally lists of transactions. Each transaction has an attached receipt which contains zero or more log entries. Log entries represent the result of events having fired from a smart contract. In the Solidity source code, to define an event, you mark it thus by preceding it with the event keyword (similar in ...


23

I will try to anwser your question. I worked on bloom filters in cpp-ethereum and Parity. will retrieving event logs become prohibitively slow as the blockchain becomes larger? Not necessarily. Everything depends on the implementation, logs density (average number of logs / block) and number of cache levels. More specifically, what is the time ...


22

Firstly, events are not accessible to contracts. The simple answer is Yes, events are permanently stored. The nuanced answer is Yes, events are as permanent as the blockchain. It helps to realize that events are the result of LOG opcodes being executed in the EVM. For an analogy, "internal transactions" are derived by executing transaction data through ...


18

I finally figured out how to properly use .get() mentioned in the answer by Roland: myContract.myEvent({}, { fromBlock: 0, toBlock: 'latest' }).get((error, eventResult) => { if (error) console.log('Error in myEvent event handler: ' + error); else console.log('myEvent: ' + JSON.stringify(eventResult.args)); }); Worth mentioning that the ...


17

Here's code that waits specified number of blocks and verifies the transaction receipt is still valid. If a fork occurs and the replay fails, the receipt check should fail and the callback will call with Error set. I've only tested this for success and timeout failures, I've not tested it on an actual fork of the blockchain, because I haven't figured out ...


17

I've discovered that not all events are displayed in the truffle output window, although they might have fired correctly with the execution of a contract. I believe this to still be an issue After spending hours on this today I have come up with a solution to test that specific events are fired. Here's my example truffle javascript test: it("should do ...


16

A contract cannot listen to events of another contract. From Solidity docs: Log and event data is not accessible from within contracts (not even from the contract that created a log). web3.js is a wrapper around JSON-RPC, so another way of accessing event data is via "filters" in JSON-RPC such as eth_newFilter. Note the dichotomy that a contract can'...


15

To help with @Peter's answer, it helps to realize what are events and logs. Events, logs, and event logs are usually Ethereum terms that are interchangeable (in some contexts a particular term is favored, for example events in Solidity and web3.js, and logs as in the EVM and Yellow Paper). Events/logs are the result of LOG opcodes being executed in the EVM....


14

Most of the answers for what's possible are in the Yellow Paper. From Equation 20, page 5: A log entry, O, is a tuple of a logger’s address, Oa , a series of 32-bytes log topics, Ot, and some number of bytes of data, Od There is no size limit to the data: you will be limited by how much Ether you have. (And of course the block gas limit.) If using ...


14

You are most likely looking for Events. Not only do they help with debugging, but they are useful in normal production code. Events are declared like functions, like so: event voteCast(address voter, uint votes, bool inFavor); Then somewhere (for example, in a vote counting function): function vote(bool inFavor) { var votes = shares[msg.sender]; /...


13

Events are dispatched signals the smart contracts can fire. DApps, or anything connected to Ethereum JSON-RPC API, can listen to these events and act accordingly. Event can be indexed, so that the event history is searchable later. An example event from a wallet contract is: event Deposit(address from, uint value); The application (dapp, web application, ...


13

Instead of logging, you can assert the events emitted by a smart contract: npm install --save truffle-test-utils At the top of your test: require('truffle-test-utils').init(); When testing that an event occurred: // Regular call thanks to Truffle let result = await testedSmartContract.testedFunction(); // Check event assert.web3Event(result, { event: '...


12

Events are convenience interfaces with the EVM logging facilities. Define an Event event HighestBidIncreased(address bidder, uint amount); // Event Triggering an Event HighestBidIncreased(msg.sender, msg.value); // Triggering event You can avoid the params if you don't want them. source


12

topics[0] is the hash of the signature of the event. Example from Solidity docs: keccak256("Deposit(address,bytes32,uint256)") is the signature of the event: event Deposit( address indexed _from, bytes32 indexed _id, uint _value );


12

I wrote the truffle-assertions package just for this. It has an assertion to check that an event has been emitted. Essentially, it is similar to the answer above, but it does not need to check a specific index of the logs, and it has the option add complex conditions in a straightforward way by passing a filter function. npm install truffle-assertions You ...


11

Use .get instead of .watch with web3.eth.filter: contractAddress = "0x00.." web3.eth.filter({ address: contractAddress from: 1, to: 'latest' }).get(function (err, result) { // callback code here })


11

Tim: Thanks so much for the pointer. You forced me to finally understand some of the internals of web3.js. I found a cleaner way to do this that covers all the corner cases of the actually fairly complicated log message format (e.g. indexing). I just used SolidityEvent from web3 to do the already-tested work for me. Below is the code. I have this code ...


11

The combination of string and indexed does not work. To understand why, see how event arguments are stored in the blockchain. All transactions that are executed generate a transaction receipt, which contains a property called logs. You can look up the receipt using eth.getTransactionReceipt("0x..."). In the transaction receipt, all event arguments that are ...


9

This is a reproducable bug in web3.js. ref. https://github.com/ethereum/web3.js/issues/434


9

Thank you @BokkyPooBah for the general idea, but the final code is a bit different from what you wrote. The prototype to subscribe an event in solidity is (I'm using web3): SolidityEvent.prototype.execute = function (indexed, options, callback) So, the first parameter is indexed which I pass an empty object. Options parameter should have the block ...


9

You can't make an Ethereum contract start with plaintext and meaningfully encrypt a message, because all contract execution has to be verifiable by all the nodes in the network, so as you say everybody would also have to be able to see your plaintext. If you want to send a message from one user of a contract to another user of the contract, the obvious ...


9

The indexed keyword helps you to filter the logs to find the wanted data. thus you can search for specific items instead getting all the logs. in general : event <Identifier>(<parameters>) Where at most 3 parameters can receive the property indexed. If called as a function, issues a log opcode containing the non-indexed arguments as data, ...


9

According to my observation. We need to wait for a while to see the watched event result for first time watch . Solidity contract CrowdFunding { event DeadlineSet( address indexed _from, uint deadline, uint timeNow ); uint deadline; function withdrawal() returns(bool) { DeadlineSet(msg.sender,deadline,...


Only top voted, non community-wiki answers of a minimum length are eligible