Skip to content

Commit d9d02d6

Browse files
committed
feat_: gas estimator
1 parent 2b12438 commit d9d02d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+148930
-42
lines changed

examples/gas-comparison/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Gas Comparison Tool
2+
3+
A multi-network gas fee comparison tool that compares our gas estimation implementation against legacy estimators and Infura's Gas API.
4+
5+
## What It Does
6+
7+
- **Compares implementations**: Our new `GetTxSuggestions` vs old estimator vs Infura API
8+
- **Multi-network support**: Ethereum, Arbitrum, Optimism, Base, Polygon, Linea, BSC, Status Network
9+
- **Comprehensive analysis**: Priority fees, max fees, base fees, wait times, network congestion
10+
- **Real vs local data**: Test with live networks or use local mock data
11+
12+
## Quick Start
13+
14+
```bash
15+
# Test with local mock data
16+
./gas-comparison -fake
17+
18+
# Test with real networks (requires Infura API key)
19+
./gas-comparison -infura-api-key YOUR_API_KEY
20+
```
21+
22+
## What You'll See
23+
24+
```
25+
🔸 LOW PRIORITY FEES
26+
Current Implementation: 200000 wei
27+
Old Implementation: 330120 wei
28+
Infura: 1000000 wei
29+
Current vs Old: -130120 wei (-39.4%)
30+
Current vs Infura: -800000 wei (-80.0%)
31+
32+
🔸 LOW WAIT TIME
33+
Wait Time (Current): 72.0--12.0 seconds
34+
Wait Time (Old): 125 seconds
35+
Wait Time (Infura): 12.0-48.0 seconds
36+
```
37+
38+
## Test Transaction
39+
40+
Uses a simple 0-valued ETH transfer from Vitalik's address:
41+
- **From**: `0xd8da6bf26964af9d7eed9e03e53415d37aa96045`
42+
- **To**: Zero address
43+
- **Value**: 0 ETH
44+
- **Data**: Empty
45+
46+
## Networks Tested
47+
48+
- **Ethereum Mainnet** (L1)
49+
- **Arbitrum One** (ArbStack)
50+
- **Optimism** (OPStack)
51+
- **Base** (OPStack)
52+
- **Polygon** (L1)
53+
- **Linea** (LineaStack)
54+
- **BNB Smart Chain** (L1)
55+
- **Status Network Sepolia** (LineaStack)
56+
57+
## Use Cases
58+
59+
- **Development**: Test gas estimation accuracy across networks
60+
- **Comparison**: Evaluate different fee strategies
61+
- **Monitoring**: Track gas fee trends and network conditions
62+
- **Validation**: Ensure our implementation matches industry standards
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Gas Data Generator
2+
3+
This program converts the functionality of `feesData.sh` into a Go program that fetches gas data from any blockchain network and generates a `data.go` file with the results using the existing SDK types.
4+
5+
## Usage
6+
7+
Run the generator with the required command line arguments:
8+
9+
```bash
10+
cd examples/gas-comparison/internal/data
11+
12+
# BSC Mainnet
13+
go run main.go -infura-api-key YOUR_API_KEY -rpc https://bsc-mainnet.infura.io/v3/YOUR_API_KEY
14+
15+
# Ethereum Mainnet
16+
go run main.go -infura-api-key YOUR_API_KEY -rpc https://mainnet.infura.io/v3/YOUR_API_KEY
17+
18+
# Polygon Mainnet
19+
go run main.go -infura-api-key YOUR_API_KEY -rpc https://polygon-mainnet.infura.io/v3/YOUR_API_KEY
20+
21+
# Show help
22+
go run main.go -help
23+
```
24+
25+
3. The program will:
26+
- Create an RPC client connection to the specified network
27+
- Automatically detect the chain ID from the RPC endpoint
28+
- Use SDK's ethclient to fetch the latest block with full transactions
29+
- Use SDK's ethclient to fetch fee history with percentiles [0, 5, 10, ..., 95, 100]
30+
- Use SDK's infura client to fetch suggested gas fees for the detected chain ID
31+
- Generate a chain-specific package and `data.go` file based on the detected chain ID
32+
33+
### Command Line Arguments
34+
35+
- **`-infura-api-key`** (required): Infura API key for gas suggestions
36+
- **`-rpc`** (required): RPC URL for the blockchain network
37+
- **`-help`**: Show help message with examples
38+
39+
### Automatic Chain ID Detection
40+
41+
The generator automatically detects the chain ID by calling `eth_chainId` on the RPC endpoint, eliminating the need to manually specify it.
42+
43+
### Supported Networks
44+
45+
The generator creates human-readable package names for popular networks:
46+
47+
| Chain ID | Package Name | Network Name |
48+
|----------|--------------|--------------|
49+
| 1 | `ethereum` | Ethereum Mainnet |
50+
| 56 | `bsc` | BSC Mainnet |
51+
| 137 | `polygon` | Polygon Mainnet |
52+
| 42161 | `arbitrum` | Arbitrum One |
53+
| 10 | `optimism` | Optimism Mainnet |
54+
| 8453 | `base` | Base Mainnet |
55+
| 43114 | `avalanche` | Avalanche C-Chain |
56+
| 250 | `fantom` | Fantom Opera |
57+
| 100 | `gnosis` | Gnosis Chain |
58+
| 25 | `cronos` | Cronos Mainnet |
59+
| Other | `chainXXX` | Chain XXX |
60+
61+
For unknown networks, the package name follows the format `chainXXX` where XXX is the chain ID.
62+
63+
## What it replaces
64+
65+
This Go program replaces the shell script `feesData.sh` which made these 3 requests:
66+
67+
1. **Latest Block**: `eth_getBlockByNumber` with `"latest"` and `true` parameters
68+
2. **Fee History**: `eth_feeHistory` with block count `0x400` (1024 blocks) and percentiles
69+
3. **Infura Suggested Fees**: GET request to `https://gas-api.metaswap.codefi.network/networks/{chainID}/suggestedGasFees`
70+
71+
Now supports any blockchain network through command line arguments with automatic chain ID detection.
72+
73+
## Generated Output
74+
75+
The generator creates chain-specific packages to allow multiple networks in a single program:
76+
77+
### **Package Structure:**
78+
```
79+
examples/gas-comparison/internal/data/
80+
├── ethereum/ # Chain ID 1 (Ethereum Mainnet)
81+
│ └── data.go
82+
├── bsc/ # Chain ID 56 (BSC Mainnet)
83+
│ └── data.go
84+
├── polygon/ # Chain ID 137 (Polygon Mainnet)
85+
│ └── data.go
86+
└── chain123/ # Chain ID 123 (Unknown networks use chainXXX format)
87+
└── data.go
88+
```
89+
90+
### **Generated File Contents:**
91+
Each `data.go` file contains:
92+
- Chain-specific package name (e.g., `package ethereum`, `package bsc`)
93+
- Import of the shared `data.GasData` type from `github.com/status-im/go-wallet-sdk/examples/gas-comparison/internal/data`
94+
- A `GetGasData()` function that returns `*data.GasData` with parsed data from embedded JSON constants
95+
- Embedded JSON data from all three API calls
96+
- Comments indicating the specific network and chain ID
97+
98+
The `GasData` struct is defined once in `data/types.go` and reused across all generated packages, ensuring consistency and reducing code duplication.
99+
100+
### **Usage in Go Programs:**
101+
```go
102+
import (
103+
"github.com/status-im/go-wallet-sdk/examples/gas-comparison/internal/data"
104+
ethereumData "github.com/status-im/go-wallet-sdk/examples/gas-comparison/internal/data/ethereum"
105+
bscData "github.com/status-im/go-wallet-sdk/examples/gas-comparison/internal/data/bsc"
106+
polygonData "github.com/status-im/go-wallet-sdk/examples/gas-comparison/internal/data/polygon"
107+
)
108+
109+
// Use data from different networks - all return the same *data.GasData type
110+
var ethData, bscGasData, polygonGasData *data.GasData
111+
var err error
112+
113+
ethData, err = ethereumData.GetGasData()
114+
bscGasData, err = bscData.GetGasData()
115+
polygonGasData, err = polygonData.GetGasData()
116+
117+
// All data uses the same GasData type for consistency
118+
fmt.Printf("Ethereum base fee: %s\n", ethData.LatestBlock.BaseFeePerGas)
119+
fmt.Printf("BSC base fee: %s\n", bscGasData.LatestBlock.BaseFeePerGas)
120+
```
121+
122+
## SDK Integration
123+
124+
The generator now uses the SDK's client methods instead of raw HTTP requests:
125+
126+
### **Client Methods Used:**
127+
- **Ethereum Client**: `ethclient.NewClient()` with RPC client
128+
- `client.ChainID(ctx)` - automatically detects chain ID
129+
- `client.GetLatestBlock(ctx)` - fetches latest block with full transactions
130+
- `client.FeeHistory(ctx, blockCount, lastBlock, percentiles)` - fetches fee history
131+
- **Infura Client**: `infura.NewClient(apiKey)`
132+
- `client.GetGasSuggestions(ctx, chainID)` - fetches gas suggestions
133+
134+
### **SDK Types Used:**
135+
- **Block Data**: `github.com/status-im/go-wallet-sdk/pkg/ethclient.BlockWithFullTxs`
136+
- **Fee History**: `github.com/status-im/go-wallet-sdk/pkg/ethclient.FeeHistory`
137+
- **Infura Fees**: `github.com/status-im/go-wallet-sdk/pkg/gas/infura.GasResponse`
138+
139+
### **Benefits:**
140+
**Proper Error Handling**: Uses SDK's built-in error handling and retries
141+
**Type Safety**: Automatic JSON marshaling/unmarshaling with proper types
142+
**Connection Management**: Proper RPC connection lifecycle management
143+
**Consistency**: Uses the same client methods as the rest of the SDK
144+
**Timeout Handling**: Built-in timeout and context management
145+
**Auto Chain Detection**: Automatically detects chain ID from RPC endpoint
146+
**Multi-Network Support**: Generate data for multiple networks without conflicts
147+
**Clean Package Structure**: Each network gets its own package namespace
148+
**Shared Type Definitions**: Single `GasData` type definition prevents duplication and ensures consistency
149+
**Type Safety**: All generated packages return the same `*data.GasData` type for easy interoperability
150+
151+
This ensures full integration with the SDK and allows you to use the gas data in your Go programs without making additional API calls.
152+
153+
## Important Note
154+
155+
If you have existing generated files from previous versions of the generator, you should regenerate them to use the new shared `data.GasData` type instead of having duplicate type definitions in each file. Simply run the generator again for each network you want to update:
156+
157+
```bash
158+
cd examples/gas-comparison/internal/data/generator
159+
160+
# Regenerate existing data files to use shared types
161+
go run main.go -infura-api-key YOUR_API_KEY -rpc https://mainnet.infura.io/v3/YOUR_API_KEY
162+
go run main.go -infura-api-key YOUR_API_KEY -rpc https://bsc-mainnet.infura.io/v3/YOUR_API_KEY
163+
```

0 commit comments

Comments
 (0)