1

I wrote a very simple smart contract which emits an event everytime I call one of its functions:

pragma solidity ^0.8.0;

contract MyContract {

    event Score(
        uint date,
        string player,
        string team
    );

    function goalScored(string memory _player, string memory _team) external {
        emit Score(block.timestamp, _player, _team);
    }
}

Then I deploy the contract using truffle migrate. Finally I interact with the contract within the truffle console. To do so I therefore load the contract by using the following lines of code:

MyContract.deployed().then(function(i) { contract=i;})
contract.goalScored('messi','inter')

Basically everytime Messi scores against a team a I launch the function to emit an Score event.

Here is my question: how can I visualize the list of all events within the truffle console?

What I tried: I ran the goalScored() function 4 times with different inputs and then I tried to used the following command:

contract.allEvents()

But all I got was this:

EventEmitter {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}
truffle(development)> Uncaught [Error: The current provider doesn't support subscriptions: HttpProvider] {
  domainEmitter: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  domainThrown: false
}

Would you be able to suggest a smart and elegant way to achieve my goal?

1 Answer 1

2

from truffle develop you could try something like

let c = await MyContract.deployed()
await c.goalScored('a','b')
let events = await c.getPastEvents('Score',{ fromBlock:0, toBlock:'latest'})
events

furthermore, if you need ALL events, you could use

c.getPastEvents('allEvents',{ fromBlock:0, toBlock:'latest'})

full truffle develop input:

migrate 
let c = await Messi.deployed()
await c.goalScored('first','inter')
await c.getPastEvents('allEvents',{fromBlock:0})

example output:

[
{
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x3b09d2d01dd03c5bea61e84cc43529b9676a0be8d895b780aee00a42a7c0971d',
blockHash: '0x05cfc5a9204adb909dd4870ceb39c2376fc894fe06a8d6491888041bc91e1810',
blockNumber: 5,
address: '0x8D595a50a2F4cA295784fC2FFde8De6EE3346ca6',
type: 'mined',
id: 'log_fda64066',
returnValues: Result {
  '0': '1633018291',
  '1': 'first',
  '2': 'inter',
  date: '1633018291',
  player: 'first',
  team: 'inter'
},
event: 'Score',
signature: '0xba74004ad4978cd3d2ecfe8c15e7c5f91bd7da14e199e9ae8638f20f378a7293',
raw: {
  data: '0x000000000000000000000000000000000000000000000000000000006155e1b3000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000566697273740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005696e746572000000000000000000000000000000000000000000000000000000',
  topics: [Array]
},
args: Result {
  '0': [BN],
  '1': 'first',
  '2': 'inter',
  __length__: 3,
  date: [BN],
  player: 'first',
  team: 'inter'
}
}]
4
  • Thanks for the answer! I just run your code but I get an empty list for some reason.... do you know what the issue could be? Sep 30, 2021 at 15:30
  • Hi, I tried inside truffle console; allow me some time and I'll update the answer with the whole list of commands (it's already the one above though..)
    – Stormsson
    Sep 30, 2021 at 15:54
  • @FedericoGentile a note; I replicated your situation if you deploy with c.deployed() without the migration. I don't know why it behaves this way
    – Stormsson
    Sep 30, 2021 at 16:16
  • Perfect Thank you very much!!! You got it! Sep 30, 2021 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.