# txGasPrice

Description: txGasPrice cheatcode documentation

Note: This document was authored using MDX

  Source: https://github.com/NomicFoundation/hardhat-website/tree/main/src/content/docs/docs/reference/cheatcodes/Environment/tx-gas-price.mdx

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

### Signature

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

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

### Description

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

### Examples

We can use this to get accurate gas usage for a transaction.

```solidity
function testCalculateGas() public {
  vm.txGasPrice(2);
  uint256 gasStart = gasleft();
  myContract.doStuff();
  uint256 gasEnd = gasleft();
  uint256 gasUsed = (gasStart - gasEnd) * tx.gasprice; // tx.gasprice is now 2
}
```
