viem
Version:
35 lines • 1.22 kB
JavaScript
import { numberToHex, } from '../../utils/encoding/toHex.js';
/**
* Retrieves the bytecode at an address.
*
* - Docs: https://viem.sh/docs/contract/getCode
* - JSON-RPC Methods: [`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
*
* @param client - Client to use
* @param parameters - {@link GetCodeParameters}
* @returns The contract's bytecode. {@link GetCodeReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getCode } from 'viem/contract'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const code = await getCode(client, {
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
* })
*/
export async function getCode(client, { address, blockNumber, blockTag = 'latest' }) {
const blockNumberHex = blockNumber !== undefined ? numberToHex(blockNumber) : undefined;
const hex = await client.request({
method: 'eth_getCode',
params: [address, blockNumberHex || blockTag],
}, { dedupe: Boolean(blockNumberHex) });
if (hex === '0x')
return undefined;
return hex;
}
//# sourceMappingURL=getCode.js.map