xf00f / web3x

Ethereum TypeScript Client Library - for perfect types and tiny builds.
211 stars 27 forks source link

[Feature Request] Easy parsing of transaction.input against a Contract method. #33

Closed SirensOfTitan closed 5 years ago

SirensOfTitan commented 5 years ago

Unless I"m missing something, one cannot currently parse out the data inside transaction.input using a lot of the nice codegen features inside web3x. It would be really nice to have that ability.

Concrete use case: I have several scripts running that watch on new transactions and parse out input for particular actions. It's really fast right now as we can get all of the transactions for a particular block in one go: if we wanted to switch to parse out events we'd have to do N requests for each transaction in the request to get its receipt.

This library is amazing by the way: practically overnight I've significantly cleaned up our codebase, builds are much faster too. Thank you for all of the hard work you've put into it. 👍

xf00f commented 5 years ago

@SirensOfTitan Thank you for the kind words.

Am I to understand the event logs are insufficient because they don't contain the data you need, or simply because it's slow to request each transactions receipt and read the logs?

If it's the later, could that not be resolved by either subscribing to, or requesting past logs for all events, and then having them pushed to you as they come in, or back in one large response respectively?

If the issue is you specifically need to pass input data, it's not trivial to do it with type safety. There would need to be an equivalent tx decode function for every method the contract supported.

If happy to live without type safety, you could possibly use this: https://github.com/xf00f/web3x/blob/master/src/contract/abi/contract-function-entry.ts#L70

You'd have to import the contracts abi e.g. import { DaiContractAbi } from 'DaiContract' and lookup the function entry by slicing the signature (first 4 bytes e.g. 0xdeadbeef) off your input data. The response object will have both numbered and named fields.

If that's helpful, then I'll look into providing a simpler decodeTx method on a contract that will do that slicing etc. I'll ponder a way of making the response type safe but it maybe more effort than it's worth...

xf00f commented 5 years ago

Not sure if you'll find it useful, but I added a helper method to ContractAbi in v3.0.5 as discussed above. You can use it as in this test case: https://github.com/xf00f/web3x/blob/master/src/contract/abi/contract-abi.test.ts

Will close this issue for now. If you have a better, and generalised idea of something that would suite your kind of use case let me know.