How to fetch a contract's current WASM code by address?

Hey everyone!

I’m building a smart contract on Klever and want to let users verify that the deployed bytecode matches the open-source repository. My plan is straightforward: rebuild the contract reproducibly, hash the result, and compare it against what’s actually running on-chain. The problem is the last step. I can’t find a way to read a contract’s current code from its address.

The only approach I’ve found is to look up the transaction history, find the most recent deployContract or upgradeContract transaction, and pull the bytecode out of its data field (which is hex-encoded twice, with the WASM sitting between @ separators). This works, but it feels like the wrong tool for the job.

Is there a node or API endpoint that returns a contract’s code directly by address?

Hello janill!

There is a way of verifying the smart contract source code vs the deployed wasm in kleverscan at the smart contract page:

https://kleverscan.org/smart-contract/{your_contract_address}

connect the wallet that has deployed it, and you’ll see a “verify contract” tab.


if you really need to get the current wasm bytes, you can get the latest deploy/upgrade tx hash here:

https://api.mainnet.klever.org/v1.0/sc/{your_contract_address}

then fetch the transaction at https://node.mainnet.klever.org/transaction/{deploy/upgrade_tx_hash} to get its metadata.

however validating the source code against the chain hash has a lot of underlying steps that might cause a mismatch, so i’d recommend trying the kleverscan way first, and if you have any feedback on it’s usage, let us know!

best regards.

2 Likes

Oh, I’ve managed to get it to work on testnet. I’ve never seen anyone using this feature. Thank you

3 Likes