|
| 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