Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to deserialize event from ABI for usage in getLogs call #7

Open
pointtoken opened this issue Aug 30, 2017 · 5 comments
Open
Assignees

Comments

@pointtoken
Copy link

Hi Guys - Love the library. We are using it in several places as we continue to develop our decentralized reward and loyalty system. One thing we do is use you to call getLogs in order to query for events on the blockchain, which is how we store achievements on chain. However, try as we might we could not successfully serialize our event from the ABI such that it could be passed to your getLogs call and we had to resort to Web3 as you can see below:

let getAchievements = (address) => {
	//using web3 to format the options correctly
	let event = pointTokenContractInstanceWeb3.Award({ _to: address }, { fromBlock: config.blockFrom, toBlock: 'latest' });
	eth.getLogs(event.options).then(result => {
		result.map(r => {
			//console.log(r);
			console.log(new BN(util.stripHexPrefix(r.topics[3])).toNumber());
		});
	});
};

It would be really cool to use your stuff only and not have to pull in Web3. The ABI created from your code works great for everything else. Any ideas?

@pointtoken pointtoken changed the title Unable Unable to serialize event from ABI for usage in getLogs call Aug 30, 2017
@pointtoken pointtoken changed the title Unable to serialize event from ABI for usage in getLogs call Unable to deserialize event from ABI for usage in getLogs call Aug 30, 2017
@SilentCicero
Copy link
Member

Heyo, absolutely, can you expand further on your problem? @pointtoken

getLogs does not deserialize or decode Ethereum Log/Solidity event data. You need to do that with ethjs-abi.

See this example:
https://github.com/ethjs/ethjs-abi/blob/master/docs/user-guide.md#usage

Look for the decodeLogItem method. You would enter the event method ABI object in question, then the log data.

You can also use the decoder I believe:

const decoder = abi.logDecoder(SimpeStoreABI)

const events = decoder(receipt.logs)

But I haven't tried this approach with getLogs yet.

If you are listening for events, have you tried ethjs-filter? That is for listening to events on chain.

https://github.com/ethjs/ethjs-filter

Note this module will be updated before DevCon3 to be stateless.

Hope this helps. Please provide more information though.

@pointtoken
Copy link
Author

pointtoken commented Sep 1, 2017

Hi - Thanks for the quick response. I first when down the path of using ethjs-filter, but it turns out that the infura nodes don't support that API call, but they do support getLogs, which turns out is really the right call. The problem is the behavior of the abi in ethjs as compared to web3. Say I have this solidity event:

    event Award(address indexed _from, address indexed _to,  uint indexed _type, uint _amount, uint _date);

If I use ethJS to serialize it and look at the options property it looks like this:

{ delay: 300,
  decoder: [Function: decoder],
  defaultFilterObject:
   { _to: '0xA5268Dd58FedFA5CB81F9B76EF0BA0a6f1DD4f60',
     to: '0x1547d3c12b088361c521ae96a51a96b563c26cf9',
     topics: [ '0x8d29b85495bacff376854d3c8a32962a8f1abf21a9f568aa8ce782d6777e2f06' ] },
  _to: '0xA5268Dd58FedFA5CB81F9B76EF0BA0a6f1DD4f60' }

But if I use web3 to serialize it, it looks like this:

{ topics:
   [ '0x8d29b85495bacff376854d3c8a32962a8f1abf21a9f568aa8ce782d6777e2f06',
     null,
     '0x000000000000000000000000a5268dd58fedfa5cb81f9b76ef0ba0a6f1dd4f60',
     null ],
  from: undefined,
  to: undefined,
  address: '0x1547d3c12b088361c521ae96a51a96b563c26cf9',
  fromBlock: '0xa7b51',
  toBlock: 'latest' }

So there is something funky about the way ethjs is serializing events from the abi.

I checked in a test.js file to this branch https://github.com/pointtoken/CLI/tree/event_abi_issue so you can repo. If you want to pull that branch. Just run

npm install
node test.js

It should be clear what is going on.

@pointtoken
Copy link
Author

I guess I was expecting Ethjs to behave like Web3 in this manner, but maybe there is a different path? I looked briefly at the ethjs-abi code but at first glance it wasn't creating the expected object required by the getLogs call.

@SilentCicero
Copy link
Member

I will look into this further. I'll need to check how logs are handled and then see which is actually correct. Obviously we should patch this asap.

@pointtoken
Copy link
Author

I created a pull request with a broken test in the ethjs repo, since that's where the issue is: ethjs/ethjs#5

maybe close this bug since it is now in two places?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants