Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions typescript/gas-inefficiency-detector-example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BSC RPC Endpoint
BSC_RPC_URL=https://bsc-dataseed1.binance.org/

# Transaction hash to analyze (optional - can also be passed as argument)
# Note: This requires a transaction hash (0x...), not a wallet address
# If you have an address, find a transaction hash from BSCScan
# If not provided, the server will start by default
TX_HASH=

# Server port (default: 3000)
PORT=3000
10 changes: 10 additions & 0 deletions typescript/gas-inefficiency-detector-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules/
dist/
.env
*.log
.DS_Store
coverage/
.nyc_output/



261 changes: 261 additions & 0 deletions typescript/gas-inefficiency-detector-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
# Gas Inefficiency Detector

BNBChain Cookbook: Detector to identify gas inefficiencies in smart contract transactions on BSC

## Overview

The Gas Inefficiency Detector is a powerful tool that analyzes smart contract transactions on BNB Smart Chain (BSC) to identify potential gas optimization opportunities. It examines transaction patterns, gas usage, and common inefficiency indicators to provide actionable recommendations for improving contract efficiency.

![Gas Inefficiency Detector UI](https://i.imgur.com/PLACEHOLDER.png)

## Features

- **Transaction Analysis**: Analyze any BSC transaction by hash to identify gas inefficiencies
- **Efficiency Scoring**: Get a 0-100 efficiency score for each transaction
- **Inefficiency Detection**: Identifies multiple types of inefficiencies:
- Storage inefficiencies (unnecessary state changes, excessive events)
- Computation issues (redundant calculations)
- Loop problems (unbounded iterations)
- External call inefficiencies (multiple calls that could be batched)
- Memory usage issues
- Failed transactions wasting gas
- **Severity Classification**: Categorizes issues as LOW, MEDIUM, HIGH, or CRITICAL
- **Actionable Recommendations**: Provides specific suggestions for optimization
- **Gas Savings Estimation**: Estimates potential gas savings for each inefficiency

## Tech Stack

- **TypeScript**: Type-safe development
- **Ethers.js v6**: Blockchain interaction
- **Express**: Web server for UI
- **Jest**: Unit testing framework

## Installation

### Quick Setup (Recommended)

Run the setup script for a hassle-free installation:

```bash
chmod +x setup.sh
./setup.sh
```

This will:
- Install all dependencies
- Create a `.env` file with working BSC RPC configuration
- Build the TypeScript project
- Run tests to verify everything works

### Manual Setup

1. Install dependencies:
```bash
npm install
```

2. Create a `.env` file:
```bash
BSC_RPC_URL=https://bsc-dataseed1.binance.org/
```

3. Build the project:
```bash
npm run build
```

4. Run tests:
```bash
npm test
```

## Usage

### Web UI

Start the web server:

```bash
npm run dev server
```

Or after building:

```bash
npm start server
```

Then open your browser to `http://localhost:3000` and enter a BSC transaction hash to analyze.

### CLI

Analyze a single transaction:

```bash
npm start <transaction-hash>
```

Example:
```bash
npm start 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
```

## API Endpoints

### POST /api/analyze

Analyze a single transaction.

Request body:
```json
{
"txHash": "0x1234..."
}
```

Response:
```json
{
"txHash": "0x1234...",
"blockNumber": 12345,
"from": "0x1111...",
"to": "0x2222...",
"gasUsed": "100000",
"gasPrice": "5000000000",
"totalCost": "500000000000000",
"inefficiencies": [...],
"efficiencyScore": 85.5,
"recommendations": [...]
}
```

### POST /api/analyze-multiple

Analyze multiple transactions and get aggregate statistics.

Request body:
```json
{
"txHashes": ["0x1234...", "0x5678..."]
}
```

## Inefficiency Types

### Storage
- Unnecessary state changes
- Excessive event emissions
- Inefficient storage slot usage

### Computation
- Redundant calculations
- Inefficient algorithms
- Unnecessary operations

### Loop
- Unbounded iterations
- Inefficient loop patterns
- Missing pagination

### External Call
- Multiple calls that could be batched
- Unnecessary external calls
- Expensive delegate calls

### Memory
- Inefficient data structures
- Excessive memory allocation
- Unoptimized memory operations

### Other
- Failed transactions
- High gas prices
- General inefficiency patterns

## Severity Levels

- **LOW**: Minor optimizations, small gas savings
- **MEDIUM**: Moderate issues, noticeable gas savings
- **HIGH**: Significant inefficiencies, substantial savings
- **CRITICAL**: Critical issues, major gas waste (e.g., failed transactions)

## Testing

Run the test suite:

```bash
npm test
```

Run tests in watch mode:

```bash
npm run test:watch
```

## Project Structure

```
gas-inefficiency-detector-example/
├── app.ts # Main application logic
├── app.test.ts # Unit tests
├── index.html # Web UI
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── jest.config.js # Jest configuration
├── setup.sh # Setup script
└── README.md # This file
```

## Examples

### Analyzing a High Gas Transaction

```bash
npm start 0xabc123...
```

Output will show:
- Gas usage breakdown
- Detected inefficiencies
- Efficiency score
- Optimization recommendations

### Using the Web UI

1. Start the server: `npm run dev server`
2. Open `http://localhost:3000`
3. Enter a transaction hash
4. View detailed analysis with visual indicators

## Best Practices

1. **Regular Analysis**: Analyze transactions regularly to catch inefficiencies early
2. **Focus on High Severity**: Prioritize CRITICAL and HIGH severity issues
3. **Batch Operations**: Look for opportunities to batch multiple operations
4. **Optimize Storage**: Minimize state changes and use storage efficiently
5. **Test Optimizations**: Always test optimizations before deploying

## Limitations

- Analysis is based on transaction data and patterns, not contract source code
- Some inefficiencies may require source code access for full optimization
- Gas savings estimates are approximate
- Network conditions may affect actual gas usage

## Contributing

This is a BNBChain Cookbook example project. Feel free to extend it with additional detection patterns and optimizations.

## License

MIT

## Resources

- [BNB Smart Chain Documentation](https://docs.bnbchain.org/)
- [Ethers.js Documentation](https://docs.ethers.org/)
- [Gas Optimization Best Practices](https://docs.soliditylang.org/en/latest/gas-optimization.html)



Loading