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

How to get transaction log, please give some help, thank you! #154

Open
July-Only opened this issue Mar 1, 2023 · 11 comments
Open

How to get transaction log, please give some help, thank you! #154

July-Only opened this issue Mar 1, 2023 · 11 comments

Comments

@July-Only
Copy link

How do I get all the transaction logs in my wallet? I don't know if that's the right way to write it.

image

@Florian-S-A-W
Copy link
Member

You can use the getLogs function like this:

let logs = try await web3.eth.getLogs(
                    addresses: nil,
                    topics: topics,
                    fromBlock: .block(block),
                    toBlock: .block(block)
                )

This would get all event logs for the topics given on the specified block.
You could define the topics like this to fetch logs for all Mint Events on any Uniswap V3 pool:

let topics: [[EthereumData]] = [
   [
        // UniswapV3 Mint
        try! EthereumData(
            ethereumValue: "0x7a53080ba414158be7ec69b987b5fb7d07dee101fe85488f0853ae16239d0bde"
        )
]
]

What exactly are you trying to do?

@July-Only
Copy link
Author

@Florian-S-A-W hello,
image
I don't know whether it is correct to define the "topics" parameter in this way, or can you give me some specific implementation? Thank you!

@Florian-S-A-W
Copy link
Member

Yes this is correct.
The way the topics are defined in the JSON RPC Standard is a bit confusing as its not well documented in the API documentation in my opinion.
https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs
The topics are an Array of Arrays because every transaction can have up to 4 topics, the first is always the signature of the Event, in your case Transfer. Each inner array defines one Collection of 4 topics you are filtering for, in order. The way you defined it, you are filtering for all Transactions that have the Transfer Event as a first topic and not discriminating what the other 3 topics are.
If you want to filter for multiple Events, you need to add them in the outer array.

So basically the inner array acts as an AND filter and the outer array acts as an OR filter.

@July-Only
Copy link
Author

@Florian-S-A-W Thank you!
Through eth_getLogs, I can only get the transaction log of the contract, but cannot get the simple log of the main chain. What should I do?

@koraykoska
Copy link
Member

@dengbangquan What do you mean? Logs are always emitted during transactions. So eth_getLogs searches for logs emitted during transactions in the range of blocks you define. Each log entry has a transaction hash property that shows which transaction the log belongs to.
There are no logs emitted outside of txs.

@July-Only
Copy link
Author

@Florian-S-A-W @koraykoska At present, I use getLogs according to the current writing method, and I can only get tokens, such as usdt transaction logs, but not original coins, such as BTC and ETH transaction logs. I don't know what the problem is, can you give me some instance codes?
image

@koraykoska
Copy link
Member

@dengbangquan You cannot track ETH transfers with logs (and BTC is another blockchain). For ETH transfers you will need to manually check the transactions. An indexer is good for those use cases. Or you use the Etherscan API.

@Florian-S-A-W
Copy link
Member

@dengbangquan as Koray mentioned, you cannot track ETH transfers with logs. Logs (or Events) are emitted by smart contracts when certain criteria are met, for example a transfer function being triggered.
This can help you understand how ERC20 tokens emit these transaction logs:
https://eips.ethereum.org/EIPS/eip-20
I would suggest reading the foundational topics in the Ethereum docs as well to help you understand the difference between different native currencies and different blockchains:
https://ethereum.org/en/developers/docs/intro-to-ethereum/

What are you actually trying to achieve?

@July-Only
Copy link
Author

@Florian-S-A-W @koraykoska
Do I have to use the socket if I want to use subscribeToLogs?

@July-Only
Copy link
Author

@Florian-S-A-W @koraykoska
image
error:providerDoesNotSupportSubscriptions

@koraykoska
Copy link
Member

koraykoska commented Mar 27, 2023 via email

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

No branches or pull requests

3 participants