Web3 by Example: Reading Smart Contract Bytecode

EVM bytecode is compiled from Solidity contracts. The Ethereum runtime environment only understands and can execute the bytecode. Sometimes, we need bytecode from a deployed contract. For example, use it in another contract.

We use the contract address that has deployed in the deploy example. And a provider to get the code of a deployed contract.

Get the contract code of address. If there is no contract currently deployed, the result is 0x. You can see that the bytecode is the same as the one in the deploy example.

bytecode.js

const { ethers } = require("ethers");
(async () => {
const address = "0x4deA5308A17Bc20589802f3E2C23e79Ba044d497";
const provider = new ethers.providers.AlchemyProvider("goerli");
const code = await provider.getCode(address);
console.log(code);
})();

output

➜ node bytecode.js
0x608060405234801561001057600080fd5b50600436106100365760003560e01c8063209652551461003b57806393a0935214610059575b600080fd5b610043610075565b60405161005091906102ae565b60405180910390f35b610073600480360381019061006e9190610419565b610107565b005b60606000805461008490610491565b80601f01602080910402602001604051908101604052809291908181526020018280546100b090610491565b80156100fd5780601f106100d2576101008083540402835291602001916100fd565b820191906000526020600020905b8154815290600101906020018083116100e057829003601f168201915b5050505050905090565b806000908051906020019061011d929190610172565b503373ffffffffffffffffffffffffffffffffffffffff167fe826f71647b8486f2bae59832124c70792fba044036720a54ec8dacdd5df4fcb600083604051610167929190610558565b60405180910390a250565b82805461017e90610491565b90600052602060002090601f0160209004810192826101a057600085556101e7565b82601f106101b957805160ff19168380011785556101e7565b828001600101855582156101e7579182015b828111156101e65782518255916020019190600101906101cb565b5b5090506101f491906101f8565b5090565b5b808211156102115760008160009055506001016101f9565b5090565b600081519050919050565b600082825260208201905092915050565b60005b8381101561024f578082015181840152602081019050610234565b8381111561025e576000848401525b50505050565b6000601f19601f8301169050919050565b600061028082610215565b61028a8185610220565b935061029a818560208601610231565b6102a381610264565b840191505092915050565b600060208201905081810360008301526102c88184610275565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61032682610264565b810181811067ffffffffffffffff82111715610345576103446102ee565b5b80604052505050565b60006103586102d0565b9050610364828261031d565b919050565b600067ffffffffffffffff821115610384576103836102ee565b5b61038d82610264565b9050602081019050919050565b82818337600083830152505050565b60006103bc6103b784610369565b61034e565b9050828152602081018484840111156103d8576103d76102e9565b5b6103e384828561039a565b509392505050565b600082601f830112610400576103ff6102e4565b5b81356104108482602086016103a9565b91505092915050565b60006020828403121561042f5761042e6102da565b5b600082013567ffffffffffffffff81111561044d5761044c6102df565b5b610459848285016103eb565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806104a957607f821691505b602082108114156104bd576104bc610462565b5b50919050565b60008190508160005260206000209050919050565b600081546104e581610491565b6104ef8186610220565b9450600182166000811461050a576001811461051c5761054f565b60ff198316865260208601935061054f565b610525856104c3565b60005b8381101561054757815481890152600182019150602081019050610528565b808801955050505b50505092915050565b6000604082019050818103600083015261057281856104d8565b905081810360208301526105868184610275565b9050939250505056fea26469706673582212209cdcbd87338d5258c875cf92fccc3ce306b02eaeee49c3b6a194a0c049a3f88364736f6c634300080c0033

Next example: Querying an ERC20 Token Smart Contract