Skip to main content

Web3Gateway - A Restful Web3 Gateway Service

Project description

🌐 Web3 Restful Gateway

A multi-chain Web3 gateway service that provides unified RESTful APIs for blockchain interaction.

Python 3.10+ FastAPI Web3.py License: MIT

🚀 Features

  • 🔗 Multi-Chain Support: Seamlessly interact with multiple EVM-compatible blockchains
  • 🔄 Real-time Data: Live blockchain data through Infura and Etherscan V2 API
  • 🛡️ Battle-tested Security: Basic auth + rate limiting
  • 🎯 High Performance: Redis caching for frequently accessed data
  • 📊 Gas Optimization: Smart gas estimation and transaction cost prediction

🛠️ Tech Stack

  • FastAPI: High-performance async web framework
  • Web3.py: Ethereum interaction library
  • Etherscan V2: Multi-chain data queries
  • Redis: Query result caching
  • Pydantic: Data validation and settings management

🔧 Quick Start

Step1: Install the package

pip install web3gateway

Step2: Create a config file and edit it with your credentials

touch config.json
{
    "auth_username": "test_user",
    "auth_password": "test_password",
    "infura_project_id": "your infura project id",
    "etherscan_api_key": "your etherscan api key",
    "redis_url": "redis://localhost:6379",
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
    "redis_password": "",
    "rate_limit_calls": 5,
    "rate_limit_period": 1,
    "cache_expiration": 10
}

Step3: Make sure you have redis-server installed and running correctly

redis-server

Step4: Start the server

web3gateway -c config.json

Step5: Test the server

curl -X GET "http://localhost:8000/ping"

📦 Development Installation

# Clone the repo
git clone https://github.com/daboooooo/web3gateway.git

# Create virtual environment and install dependencies
cd web3gateway
./setup.sh -i
source .env/bin/activate

# Set up configuration
cp config.json.example config.json
# Edit config.json with your credentials

# Start the server
web3gateway

# Test the server
curl -X GET "http://localhost:8000/ping"

🔥 Core APIs

Transaction Operations

POST /transaction/assemble
POST /transaction/send
POST /transaction/get_receipt

Account Operations

POST /account/balance
POST /account/token_balance
POST /account/txlist

System Operations

GET /ping

🎮 API Examples

Get Account Balance

curl -X POST "http://localhost:8000/account/balance" \
     -H "Content-Type: application/json" \
     -u "test_user:test_password" \
     -d '{
       "chain_id": 1,
       "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
     }'

Response:

{
    "timestamp": 1677654321000,
    "data": {
        "balance": "1234567890000000000"
    }
}

Get Token Balance

curl -X POST "http://localhost:8000/account/token_balance" \
     -H "Content-Type: application/json" \
     -u "test_user:test_password" \
     -d '{
       "chain_id": 1,
       "contractaddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
       "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
     }'

Response:

{
    "timestamp": 1677654321000,
    "data": {
        "contract address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
        "token balance": "150000000"
    }
}

Assemble Transaction

curl -X POST "http://localhost:8000/transaction/assemble" \
     -H "Content-Type: application/json" \
     -u "test_user:test_password" \
     -d '{
       "chain_id": 1,
       "tx_params": {
         "from": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
         "to": "0x1234567890123456789012345678901234567890",
         "value": "1000000000000000000"
       },
       "gas_level": "normal"
     }'

Response:

{
    "timestamp": 1677654321000,
    "data": {
        "chainId": 1,
        "nonce": 5,
        "gasPrice": "20000000000",
        "gas": 21000,
        "to": "0x1234567890123456789012345678901234567890",
        "value": 1000000000000000000,
        "data": "0x"
    }
}

Get Transaction List

curl -X POST "http://localhost:8000/account/txlist" \
     -H "Content-Type: application/json" \
     -u "test_user:test_password" \
     -d '{
       "chain_id": 1,
       "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
     }'

Response:

{
    "timestamp": 1677654321000,
    "data": {
        "last transactions": [
            {
                "blockNumber": "17584321",
                "timeStamp": "1677654000",
                "hash": "0xabcd...",
                "from": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
                "to": "0x1234...",
                "value": "1000000000000000000",
                "gas": "21000",
                "gasPrice": "20000000000"
            }
        ]
    }
}

🔌 Supported Networks

  • Ethereum Mainnet (ChainID: 1)
  • Binance Smart Chain (ChainID: 56)
  • Polygon (ChainID: 137)
  • Arbitrum (ChainID: 42161)
  • Optimism (ChainID: 10)
  • And more...

⚡️ Performance

  • Response Time: < 100ms (cached)
  • Throughput: 1000+ TPS
  • Cache Hit Ratio: ~80%

🔒 Security Features

  • Basic Authentication
  • Rate Limiting
  • Input Validation
  • Error Handling
  • Response Sanitization

🤝 Contributing

PRs are welcome! Check out our contribution guidelines.

📜 License

MIT License - fork, modify and use as you wish.

⚠️ Disclaimer

This is a production-ready gateway but use at your own risk. Always verify transactions before signing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web3gateway-0.1.2.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web3gateway-0.1.2-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file web3gateway-0.1.2.tar.gz.

File metadata

  • Download URL: web3gateway-0.1.2.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for web3gateway-0.1.2.tar.gz
Algorithm Hash digest
SHA256 206aab1fbe153cca8b638c6d7928844017ec33a48dd5249cdb9cf3ea52e7d596
MD5 b236f876cd0f6a91a9626f3da8396504
BLAKE2b-256 df1095724cf6570ebf9f6879c62231d5ab93721414defcce7f32d116d5763d4f

See more details on using hashes here.

File details

Details for the file web3gateway-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: web3gateway-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for web3gateway-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df3fc1347caa5f4a0cb0d9917aa6fb44a69918a61a0559fe7a30dbc04002f42c
MD5 a2a7f57dc7b2c2985c4f0d60cb084b0e
BLAKE2b-256 b1322afb919a56d81e4fd6556cfe69002ec97fc43e7dd40a1a99163b69330d08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page