## `txGasPrice`

### Signature

```solidity
function txGasPrice(uint256 newGasPrice) external;
```

### Description

Sets `tx.gasprice` **for the rest of the transaction**.

### Parameters

| Parameter     | Type      | Description                       |
|---------------|-----------|-----------------------------------|
| `newGasPrice` | `uint256` | The new gas price to set (in wei) |

### Examples

```solidity [test/TxGasPrice.t.sol]
function testCalculateGasCost() public {
    vm.txGasPrice(2 gwei);
    
    uint256 gasStart = gasleft();
    myContract.doStuff();
    uint256 gasEnd = gasleft();
    
    uint256 gasCost = (gasStart - gasEnd) * tx.gasprice;
    // gasCost is now in wei at 2 gwei per gas
}
```

### Related Cheatcodes

* [`fee`](/reference/cheatcodes/fee) - Sets `block.basefee`
