A Go library for managing Ethereum wallet transactions with advanced gas management and retry mechanisms.
- Transaction Builder Pattern: Fluent API for building and executing transactions
- Gas Buffer Management: Percentage-based gas buffer for safer transaction execution
- Automatic Gas Estimation: Auto-estimate gas with configurable safety buffers
- Retry Mechanism: Automatic retry with gas price adjustment for failed transactions
- Transaction Monitoring: Monitor transaction status and handle edge cases
- Hook System: Custom hooks for transaction lifecycle events
go get github.com/tranvictor/walletarmyimport (
"github.com/tranvictor/walletarmy"
"github.com/ethereum/go-ethereum/common"
)
// Create wallet manager
wm := walletarmy.NewWalletManager(...)
// Execute a transaction
tx, receipt, err := wm.R().
SetFrom(fromAddress).
SetTo(toAddress).
SetValue(value).
SetNetwork(network).
Execute()The gas buffer feature allows you to add a percentage-based safety margin to your gas limit:
tx, receipt, err := wm.R().
SetFrom(fromAddress).
SetTo(contractAddress).
SetGasLimit(0). // 0 = Auto-estimate (e.g., 50,000)
SetGasBufferPercent(0.40). // Add 40% buffer (20,000)
SetData(callData).
SetNetwork(network).
Execute()
// Result: Gas limit = 70,000 (50,000 + 40% buffer)You can combine percentage-based buffer with fixed extra gas:
tx, receipt, err := wm.R().
SetFrom(fromAddress).
SetTo(contractAddress).
SetGasLimit(0). // Auto-estimate: 50,000
SetGasBufferPercent(0.40). // 40% buffer: +20,000 = 70,000
SetExtraGasLimit(5000). // Fixed extra: +5,000 = 75,000
SetNetwork(network).
Execute()
// Final gas limit = 75,000SetGasLimit(gasLimit uint64)- Set fixed gas limit (0 = auto-estimate)SetGasBufferPercent(percent float64)- Set percentage-based gas buffer- Applied as:
gasLimit + (gasLimit × gasBufferPercent) + extraGasLimit - Only applied when
percent > 0 - Example:
0.40= 40% buffer
- Applied as:
SetExtraGasLimit(extraGas uint64)- Add fixed amount to gas limitSetGasPrice(gasPrice float64)- Set gas price in GweiSetExtraGasPrice(extraGasPrice float64)- Add extra to gas price
SetFrom(from common.Address)- Set sender addressSetTo(to common.Address)- Set recipient addressSetValue(value *big.Int)- Set transaction value in WeiSetData(data []byte)- Set transaction data/calldataSetNetwork(network networks.Network)- Set target network
SetTxType(txType uint8)- Set transaction type (0=legacy, 2=EIP-1559)SetTipCapGwei(tipCap float64)- Set priority fee (tip) in GweiSetExtraTipCapGwei(extra float64)- Add extra to tip capSetMaxGasPrice(maxPrice float64)- Set maximum gas price limitSetMaxTipCap(maxTip float64)- Set maximum tip cap limit
SetNumRetries(retries int)- Set number of retry attemptsSetSleepDuration(duration time.Duration)- Set sleep between retriesSetTxCheckInterval(interval time.Duration)- Set transaction check interval
SetBeforeSignAndBroadcastHook(hook Hook)- Hook before signingSetAfterSignAndBroadcastHook(hook Hook)- Hook after broadcastingSetGasEstimationFailedHook(hook GasEstimationFailedHook)- Hook for gas estimation failures
Execute()- Execute the transaction and return tx, receipt, error
The gas buffer is calculated and applied in the following order:
- Base Gas: Either auto-estimated or provided via
SetGasLimit() - Apply Buffer:
bufferedGas = baseGas + (baseGas × gasBufferPercent) - Apply Extra:
finalGas = bufferedGas + extraGasLimit
SetGasLimit(0) // Estimates: 50,000
SetGasBufferPercent(0.40) // Buffer: 50,000 × 0.40 = 20,000
// Final: 50,000 + 20,000 = 70,000SetGasLimit(100000) // Fixed: 100,000
SetGasBufferPercent(0.20) // Buffer: 100,000 × 0.20 = 20,000
// Final: 100,000 + 20,000 = 120,000SetGasLimit(0) // Estimates: 50,000
SetGasBufferPercent(0.40) // Buffer: 20,000 → Total: 70,000
SetExtraGasLimit(5000) // Extra: 5,000
// Final: 70,000 + 5,000 = 75,000- Simple Transfers: 10-20% buffer (0.10 - 0.20)
- Contract Calls: 20-40% buffer (0.20 - 0.40)
- Complex DeFi: 40-50% buffer (0.40 - 0.50)
- Unpredictable Contracts: 50%+ buffer (0.50+)
Gas estimation isn't always perfect and can underestimate in these scenarios:
- Complex smart contract interactions
- Transactions with dynamic state changes
- Edge cases in contract execution paths
- Network congestion scenarios
A gas buffer provides a safety margin while still being cost-effective (unused gas is refunded).
// Swap tokens on Uniswap with safety buffer
tx, receipt, err := wm.R().
SetFrom(myAddress).
SetTo(uniswapRouterAddress).
SetData(swapCallData).
SetGasLimit(0). // Auto-estimate
SetGasBufferPercent(0.40). // 40% safety buffer
SetTxType(2). // EIP-1559
SetTipCapGwei(2.0). // Priority fee
SetNetwork(networks.EthereumMainnet).
Execute()// Mint NFT with retry on failure
tx, receipt, err := wm.R().
SetFrom(myAddress).
SetTo(nftContractAddress).
SetData(mintCallData).
SetGasLimit(0).
SetGasBufferPercent(0.30). // 30% buffer
SetExtraGasLimit(10000). // Extra 10k gas
SetNumRetries(3). // Retry 3 times
SetSleepDuration(5 * time.Second).
SetNetwork(network).
Execute()tx, receipt, err := wm.R().
SetFrom(fromAddress).
SetTo(toAddress).
SetGasBufferPercent(0.40).
Execute()
if err != nil {
if errors.Is(err, walletarmy.ErrEstimateGasFailed) {
// Handle gas estimation failure
} else if errors.Is(err, walletarmy.ErrTxFailed) {
// Handle transaction failure
}
// Handle other errors
}This project includes a Makefile for common development tasks:
# View all available commands
make help
# Run tests
make test # Run all tests
make test-short # Run tests in short mode
make test-coverage # Generate coverage report
make test-race # Run tests with race detector
make gas-buffer-test # Run only gas buffer tests
# Code quality
make fmt # Format code
make fmt-check # Check code formatting
make vet # Run go vet
make lint # Run linters (requires golangci-lint)
# Build and dependencies
make build # Build the project
make deps # Download dependencies
make tidy # Tidy go modules
make verify # Verify dependencies
# Pre-commit and CI
make pre-commit # Run pre-commit checks (fmt, vet, test-short)
make check # Run all checks (fmt-check, vet, test)
make ci # Run full CI pipeline
# Utilities
make clean # Clean build artifacts and test cache
make install-tools # Install development tools
make benchmark # Run benchmarks# Before committing
make pre-commit
# Generate coverage report
make test-coverage
# Run full CI pipeline locally
make ciContributions are welcome! Please ensure:
- All tests pass (
make testorgo test ./...) - Code is formatted (
make fmt) - Code passes linting (
make vet) - New features include tests and documentation
- Run
make pre-commitbefore committing
[License information]