# getDeployedCode

Description: getDeployedCode cheatcode documentation

Note: This document was authored using MDX

  Source: https://github.com/NomicFoundation/hardhat-website/tree/main/src/content/docs/docs/reference/cheatcodes/External/get-deployed-code.mdx

  Components used in this page:
    - :::note: An informational callout block. Supports custom title `:::note[Title]` and icon `:::note{icon="name"}` syntax.

{/* This document contains content copied/adapted from the Foundry Book (MIT licensed). See LICENSE in the parent directory. */}

### Signature

```solidity
function getDeployedCode(string calldata) external returns (bytes memory);
```

### Description

This cheatcode works similar to [`getCode`](/docs/reference/cheatcodes/external/get-code) but only returns the **deployed** bytecode (aka runtime
bytecode) for a contract in the project given the path to the contract.

The main use case for this cheatcode is as a shortcut to deploy stateless contracts to arbitrary addresses.

The calldata parameter can either be in the form `ContractFile.sol` (if the filename and contract name are the same)
, `ContractFile.sol:ContractName`, or the path to an artifact, relative to the root of your project.

:::note
`getDeployedCode` requires read permission for the output directory, see [file cheatcodes](/docs/reference/cheatcodes/file/fs).

To grant read access set `fsPermissions: { readDirectory: ["./out"] }` in your [Solidity tests configuration](https://hardhat.org/docs/reference/configuration#solidity-tests-configuration).
:::

### Examples

Deploy a stateless contract at an arbitrary address using `getDeployedCode` and [`etch`](/docs/reference/cheatcodes/environment/etch).

```solidity
// A stateless contract that we want deployed at a specific address
contract Override {
    event Payload(address sender, address target, bytes data);

    function emitPayload(
        address target, bytes calldata message
    ) external payable returns (uint256) {
        emit Payload(msg.sender, target, message);
        return 0;
    }
}

// get the **deployedBytecode**
bytes memory code = vm.getDeployedCode("Override.sol:Override");

// set the code of an arbitrary address
address overrideAddress = address(64);
vm.etch(overrideAddress, code);
assertEq(overrideAddress.code, code);
```

### Supported formats

You can fetch artifacts by either contract path or contract name. Fetching artifacts for a specific version is also supported. If not provided, cheatcode will default to the version of a test being executed or the only version artifact was compiled with.

```solidity
vm.getDeployedCode("MyContract.sol:MyContract");
vm.getDeployedCode("MyContract");
vm.getDeployedCode("MyContract.sol:0.8.18");
vm.getDeployedCode("MyContract:0.8.18");
```

### SEE ALSO

Forge Standard Library

[`getCode`](/docs/reference/cheatcodes/external/get-code)
[`etch`](/docs/reference/cheatcodes/environment/etch)

[forge-std]: https://getfoundry.sh/reference/forge-std
