Boilertalk / Web3.swift

A pure swift Ethereum Web3 library
MIT License
636 stars 187 forks source link

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

Open July-Only opened 1 year ago

July-Only commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

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

July-Only commented 1 year ago

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

koraykoska commented 1 year ago

You have to use Websocket provider. It’s part of Web3.swift so it should be a drag & drop replacement. Just use the initializer of Web3 with wsURL instead of rpcURL and pass a Websocket url. If your provider doesn’t support Websocket you can use Chainnodes:

https://www.chainnodes.org

On Mon, 27 Mar 2023 at 04:30, dengbangquan @.***> wrote:

@Florian-S-A-W https://github.com/Florian-S-A-W @koraykoska https://github.com/koraykoska [image: image] https://user-images.githubusercontent.com/16422981/227833912-2d7ee2f4-c689-496e-84f5-9c62ad702cbb.png error:providerDoesNotSupportSubscriptions

— Reply to this email directly, view it on GitHub https://github.com/Boilertalk/Web3.swift/issues/154#issuecomment-1484429532, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACWUTTODQJ3W7XU2Q6VKXHLW6ECT3ANCNFSM6AAAAAAVLQ4OY4 . You are receiving this because you were mentioned.Message ID: @.***>