🟪 Polygon
eth_getCode

Polygon API Documentation


eth_getCode

The eth_getCode is a JSON-RPC method in Polygon that returns the code at a given address

🔌 To use eth_getCode you must be connected to a blockchain node.

Returns

code - The bytecode from a given address returned as a string ( Shows the compiled smart contract code ).

Parameters

1. address [required] - String - Address of the smart contract zou want to get code from (20 Bytes)

2. blockNumber/Hash or Tag [required] - The hexadecimal block number OR block hash OR the string Tag ("latest", "earliest", "pending", "safe" or "finalized").

"Safe" and "finalized" are only availible on Ethereum and Arbitrum One chains. Read block parameter description (opens in a new tab).

Request

POST https://<network>.chainnodes.org/YOUR-API-KEY

Example

💡 Confusing?

Ask our experienced blockchain developers in Telegram

  • HTTPS POST Request with a JSON RPC call in the body
  • Replace YOUR-API-KEY with the API key from your CHAINNODES.ORG Dashboard
  • You can use a different supported network by replacing polygon-mainnet
curl https://polygon-mainnet.chainnodes.org/YOUR-API-KEY \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_getCode","params":["0xD54f502e184B6B739d7D27a6410a67dc462D69c8", "latest"],"id":1}'

Body

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "0x6080604052600436106100c85760003560e01c80635e3a97e71161007a5780635e3a97e7146103265780636684b1d6146103b35780637147855d146103c857806372a44f07146104485780638757653f1461045d578063b449ea5d14610490578063e907fa3c146104d7578063f9bcdde4146104ec576100c8565b806312f16e6d1461014c57806314778a831461018157806320cea94d146101a857806322175a32146101bd578063439fab91146101f057806356f36dbf1461026b5780635c60da1b146102f5575b6000610...................."
}

Some use cases of eth_getCode

  1. Smart Contract Verification: You can use eth_getCode to verify if the code of a deployed smart contract matches the expected code. This is often used for verifying smart contract source code on block explorers.
  2. Checking Contract Existence: If the method returns '0x', it means there is no contract deployed at the given address.
  3. Interacting with Contracts: Before interacting with a contract, you might use eth_getCode to fetch the contract's code to ensure it's the correct contract.
  4. Security Audits: In a security audit, eth_getCode can be used to retrieve the contract code for analysis. This can help identify potential vulnerabilities in the contract.
  5. Blockchain Analytics: eth_getCode can be used to gather data about smart contracts on the network, such as how many contracts are deployed or what kind of contracts they are.

JSON-RPC Polygon API Documentation by CHAINNODES is based on Erigon node client. Contact us if you are interested in specific methods that are only available on geth, besu, Nethermind or reth