ApeWorX / ape-etherscan

Etherscan explorer plugin using EVM-based networks for the Ape Framework
https://www.apeworx.io/
Apache License 2.0
24 stars 26 forks source link

Unverified contracts with "similiar code" not handled [APE-1451] #104

Open fubuloubu opened 12 months ago

fubuloubu commented 12 months ago

example: https://etherscan.io/address/0x5777d92f208679db4b9778590fa3cab3ac9e2168#code

raises:

ERROR (ape): Error with contract ABI: Expecting value: line 1 column 1 (char 0)
ERROR (ape): (ContractNotFoundError) Failed to get contract type for address '0x5777d92f208679DB4b9778590Fa3CAB3aC9e2168'. Contract may need verification.
salparadi commented 9 months ago

I just came across this and dug a little. There isn't a way to see the "similar code" contract via the API. So while I don't have a true fix, I have a band-aid for the issue I came across: Uniswap V3 pools. For some reason not all of them are verified despite Uniswap saying they should be on deploy (some certainly do get verified automatically). Not sure this is worth implementing, but figured I would add this here for consideration and maybe to get someone smarter than I thinking creatively.

So when ape-etherscan comes across a pool that isn't verified, get info on the contract creation:

https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses=0x9e31EfFFE1e016f514F774F43d91A85eBBC11564

That returns something like this:

{
    "status": "1",
    "message": "OK",
    "result": [
        {
            "contractAddress": "0x9e31efffe1e016f514f774f43d91a85ebbc11564",
            "contractCreator": "0x80bb828aa8935b716af2a481db357d3a453db9ea",
            "txHash": "0x97672a1f8b4a8b4ae6d1522396fdb9567a132b030b04b2727dca6a61f35a04c6"
        }
    ]
}

Pull out the txHash and you can call the eth_getTransactionByHash endpoint to get info about the transaction

https://api.etherscan.io/api?module=proxy&action=eth_getTransactionByHash&txhash=0x97672a1f8b4a8b4ae6d1522396fdb9567a132b030b04b2727dca6a61f35a04c6

That returns a bunch of JSON. Pull out the to value and check it against some sort of list of curated important addresses. This is the part that is messy and not ideal. For example, to support unverified V3 Pools, you could use the address below which is the Uniswap V3: Positions NFT address. It seems all pool creation transactions are sent to this address. Save the generic V3 ABI somewhere as they are all the same.

if to == 0xC36442b4a4522E871399CD717aBDD847Ab11FE88

   grab the abi constant and save the details to the contract cache for the original unverified contract.

I recognize this isn't perfect.

fubuloubu commented 9 months ago

makes me wonder if we should index and publish all the pools that were never published lol

antazoey commented 8 months ago

It should be working! https://github.com/ApeWorX/ape-etherscan/blob/main/ape_etherscan/verify.py#L402-L403

I have seen it work plenty of times. Something else may be going on here, circumstantial

Edit: sorry I thought this ticket was for verification but it is for retrieval, so idk actually!

fubuloubu commented 8 months ago

Edit: sorry I thought this ticket was for verification but it is for retrieval, so idk actually!

Yeah, this is when a contract is not verified on etherscan but it still has some ABI information because it is "similar to" another contract (matching bytecode, but basically unsure about the initcode matching)

This is common for contracts created via "factory pattern" that produces a lot of child contracts with the same code

Ninjagod1251 commented 2 months ago

we stumbled on a solution for this error. @salparadi A solution to the "Etherscan doesn't have the ABI for this unverified contract" issue would be to BYPASS this check.

Check out this snippet and see if it can work for you: https://github.com/ApeWorX/uniswap-sdk/pull/4#discussion_r1697479131

It is an alternative solution which solves the issue specifically for Uniswap pools contracts. Since it bypasses ever having to go talk to etherscan in the first place. We are loading the type from the package resource bundled with the Uniswap sdk.