---
description: Returns the deployed (runtime) bytecode for a contract in the project
---

## `getDeployedCode`

### Signature

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

### Description

This cheatcode works similar to [`getCode`](/reference/cheatcodes/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.

### Parameters

| Parameter      | Type     | Description                                    |
|----------------|----------|------------------------------------------------|
| `artifactPath` | `string` | Path to the contract artifact (see supported formats below) |

### Returns

| Type    | Description                          |
|---------|--------------------------------------|
| `bytes` | The runtime bytecode of the contract |

### Examples

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

:::code-group

```solidity [test/Override.t.sol]
// 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);
```

```solidity [src/Override.sol]
// 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;
    }
}
```

:::

### 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");
```

### Gotchas

:::warning
`getDeployedCode` requires read permission for the output directory. To grant read access, set `fs_permissions = [{ access = "read", path = "./out"}]` in your `foundry.toml`.
:::

### Related Cheatcodes

- [`getCode`](/reference/cheatcodes/get-code) - Returns the creation bytecode
- [`etch`](/reference/cheatcodes/etch) - Sets the bytecode of an address

### See Also

- [`deployCode`](/reference/forge-std/deployCode)
- [`deployCodeTo`](/reference/forge-std/deployCodeTo)
