trueblocks-core icon indicating copy to clipboard operation
trueblocks-core copied to clipboard

chifra transactions --reconciliation to capture ERC-20/ERC-721 balance changes for a transaction with multiple internal transactions?

Open trev77 opened this issue 3 years ago • 5 comments

Would like to calculate token balance differences when a given transaction happens e.g. flashloans. Given a transaction, I want to find out what the difference in token balances were before and after the flashloan. This is more invovled than just checking the initiator contract because it's possible that it may just store funds in an intermediary staking/borrow contract instead of returning all funds to the receiver. Thus, it becomes necessary to keep track of all contracts which are invoked during the transaction, and query each of them individually for ERC-20/ERC-721 balance changes. Could we discuss a way to solve this using chifra?

trev77 avatar Jul 18 '22 20:07 trev77

This is a hard problem. A few questions:

  1. Would this be for, at any given transaction, the balances of a particular address? In other words, if I do a flashloan from my address, are you interested in the balances of these tokens for my address or for all the addresses that own the various tokens during the tx?

  2. Have you tried chifra traces <tx_hash> --fmt json? I'm not exactly sure what that will show.

  3. You could use chifra transactions --uniq <tx_hash> to find every address that appears anywhere in that transaction and then use that list to query for balances as this will be a list of not only all tokens in that transaction but all addresses of any type.

  4. In the end, probably the easiest way (if you're a programmer) would be to write a Dynamic Traverser: https://trueblocks.io/blog/dynamic-traversers-for-trueblocks/. This allows you to do anything you want for each transaction in an address's history.

Not sure if this helps. Please help me understand what works and that may help me help you better.

tjayrush avatar Jul 20 '22 03:07 tjayrush

It seems that --accounting does exactly what I want it to do but its usage is limited to chifra export. Is there any way to use --accounting with transactions or traces?

trev77 avatar Jul 27 '22 11:07 trev77

To answer your questions:

  1. Yes - given a unique address one could produce the balance change of that given address following a given transaction. One can then query all token addresses for a flashloan transaction and do the cumulative math after

  2. chifra traces <tx_hash> --fmt json only gives the trace of the transaction, which can also be obtained from running the trace_transaction method from erigon directly

  3. I think this is the most straightforward way of doing it, but was wondering if I could leverage the work already done using --accounting in chifra export for chifra transactions. The main issue for me is exhaustively checking each address to see if it's an ERC-20 contract. If that's already been done somewhere with Trueblocks I'd like to use it here as well.

  4. I've begun looking into this, but the main problem in 3 - building a central repository of ERC-20 token contracts - remains

trev77 avatar Jul 27 '22 11:07 trev77

It seems that --accounting does exactly what I want it to do but its usage is limited to chifra export. Is there any way to use --accounting with transactions or traces?

For traces, the answer is no.

For transactions, the answer is "not quite, but you can help us perhaps."

There is an option to chifra transactions called --reconcile which takes an address. Every reconciliation must be against a particular address, so chifra transactions --reconcile <address> should present a reconciliation.

To be perfectly honest, though, I'm not sure how "robust" that is. It's not been tested all that well.

We would be very interested to learn what you find.

tjayrush avatar Jul 27 '22 11:07 tjayrush

To answer your questions:

Thanks for this. Very helpful.

  1. Yes - given a unique address one could produce the balance change of that given address following a given transaction. One can then query all token addresses for a flashloan transaction and do the cumulative math after

That's good.

  1. chifra traces <tx_hash> --fmt json only gives the trace of the transaction, which can also be obtained from running the trace_transaction method from erigon directly

Yes. That's right. This is exactly what it does. In general, a good portion of what all the tools do is read from the node and write to the command line (with some improvements -- for example chifra transactions 1201200.* shows all transactions in block 1201200 with all the options of the transactions tool.

chifra export does similar things (given it has options such as --logs, --traces, etc.) but it does so filtering by address(es).

  1. I think this is the most straightforward way of doing it, but was wondering if I could leverage the work already done using --accounting in chifra export for chifra transactions.

I think so, yes. With chifra transactions --reconcile <address>.

The main issue for me is exhaustively checking each address to see if it's an ERC-20 contract. If that's already been done somewhere with Trueblocks I'd like to use it here as well.

It's not done well -- there's a function called isToken (name may be slightly different, but it's used in the file ./src/apps/acctExport/handle_accounting.cpp.

You could help us by improving that and pushing it back into the code base with a PR.

  1. I've begun looking into this, but the main problem in 3 - building a central repository of ERC-20 token contracts - remains

Our code shys away from building a 'central repository' of anything because it first and foremost wants to run on a small, local machine.

tjayrush avatar Jul 27 '22 11:07 tjayrush

We're going to start working on this over the next few days. Will you be available to test?

tjayrush avatar Nov 07 '22 05:11 tjayrush

Yes should definitely be available to test :) Thanks for the follow-up

unixftw avatar Nov 07 '22 18:11 unixftw

There's a branch called issue_2203/counterparties/second that we're working on. It's not quite ready yet, but I hope it's nearing finalization by Friday of this week. I'll let you know when it's ready. If you use it, though, be aware that you'll have to make a copy of the reconciliation cache if you want to go back to develop.

I'll make a note here when it's ready.

tjayrush avatar Nov 08 '22 20:11 tjayrush

@trev77 @unixftw We pushed some work on this to develop and will be releasing it to master later today or tomorrow. I am going to close this issue. I would be very pleased if you were to check out this new work to see if it does what you want it to do. If not, please open a new issue. Cheers.

tjayrush avatar Nov 21 '22 03:11 tjayrush

Awesome, thanks for the update @tjayrush! I'll check it out :)

unixftw avatar Nov 21 '22 03:11 unixftw