This project uses Flask as the backend framework and integrates the 1inch Dev API to provide a variety of blockchain-related query functions, including Token Balances, Token Prices, NFT Information, OrderBook, Gas Price, and more. It also leverages a simple in-memory Cache to optimize request speeds.
- Feature Overview
- Environment Requirements
- Installation and Execution
- API Documentation
- Cache Mechanism
- Additional Notes
- License
- Multi-Chain Support: Supports Ethereum, BSC, Polygon, Avalanche, Arbitrum, Optimism, etc. (You can customize within
CHAIN_IDS
) - Token / NFT Queries: Retrieve wallet balances, detailed Token information (name, logo), and NFT information (name, image URL)
- Historical Prices/Charts: Utilize 1inch-provided Token charts, historical price ranges, Gas prices, and other APIs
- In-Memory Cache: Reduce duplicate calls to external services, lowering wait times and API traffic
- Python 3.7+
- pip installed
- Installed packages: Flask, Requests, python-dotenv, Flask-Cors
- An API Key from 1inch Dev (to be placed in the
.env
file)
-
Download/Clone the Project
git clone https://github.com/your-repo/1inch-backend.git cd 1inch-backend
-
Install Packages
If you do not have arequirements.txt
, install manually:pip install flask requests python-dotenv flask-cors
-
Configure
.env
Create a.env
file in the project root with the following content:1INCH_API_KEY=YOUR_1INCH_KEY WALLET_ADDRESS=YOUR_DEFAULT_WALLET_ADDRESS
WALLET_ADDRESS
is optional and is only used as an example in the code. -
Run the Backend
python app.py
The service will run on
http://127.0.0.1:5000
by default.
(If the file is not namedapp.py
, please replace it with your filename.)
- Endpoint:
/api/Token/CombinedBalance/<network>/<wallet_address>
- Method:
GET
- Functionality:
- First, query the Token balances for the specified
wallet_address
on the givennetwork
usingbalance/v1.2
- Then, for each Token, call
token/v1.2/custom/{tokenAddress}
to retrieve Token Name, Decimals, Logo URI - Utilizes an in-memory Cache to avoid repeated queries in a short time period
- First, query the Token balances for the specified
- Example:
GET /api/Token/CombinedBalance/ethereum/0xYourWallet
/api/Token/TokenBalance/<network>/<wallet_address>
- Retrieves the Token balances for the wallet on the specified network (excluding additional Token details)
/api/Token/TokenInfo/<network>/<token_address>
- Retrieves detailed information for the specified Token contract, including name, symbol, logoURI, etc.
/api/Chart/Token/<network>/<token_address>
- Calls the
token-details/v1.0/charts
endpoint with parameters such as interval and from_time
- Calls the
/api/Chart/NaiveChain/<network>
- Similar to the above, but only passes the chainId without a token_address
/api/Chart/HistoryTokenPrice/<network>/<TimeFrom>/<TimeTo>/<token_address>
- Retrieves historical price data within a custom interval (
from
/to
)
- Retrieves historical price data within a custom interval (
/api/OrderBook/Hash/<network>/<hash_address>
- Retrieves OrderBook information using a transaction hash
/api/OrderBook/Wallet/<network>/<wallet_address>
- Retrieves OrderBook information based on a wallet address
/api/NFT/<wallet_address>
- Calls the
nft/v2/byaddress
endpoint, supporting multiple chains - Returns a JSON structure in the format:
{ "NFT Name": "NFT Image URL", ... }
- Calls the
/api/GasPrice/<network>
- Retrieves the current Gas Price information for the specified network
/api/data
- A simple test endpoint that returns example JSON:
{"id":1,"name":"Alice","message":"Hello from Python backend"}
- A simple test endpoint that returns example JSON:
- An in-memory global dictionary named
combined_balance_cache
is used to cache results for certain API calls (such asget_CombinedBalance
). - TTL (Time-To-Live) is set to
CACHE_TTL_SECONDS = 12000
seconds (approximately 3.3 hours) by default, but can be adjusted as needed. - If a valid cache entry is found within the TTL, the cached data is returned; otherwise, a call to the 1inch API is made.
Note: This in-memory cache works under a single backend instance. For multiple backend instances or a more robust caching solution, consider using an external service like Redis.
- CORS
- Using
Flask-Cors
and applyingCORS(app)
or@cross_origin()
in the code allows cross-domain requests from the frontend.
- Using
- Throttling / Rate Limit
- Some parts of the code use
time.sleep(0.5)
to reduce the rate of simultaneous requests to the 1inch API. - For more comprehensive rate limiting, consider integrating Flask-Limiter.
- Some parts of the code use
- API Key Protection
- Ensure the
1INCH_API_KEY
is stored in the.env
file and do not upload this key to public repositories.
- Ensure the
- Network Mapping
- The
CHAIN_IDS
variable defines the chainIds (as numbers) for common networks; expand or modify as needed.
- The
This project is released under the MIT License. Please refer to the LICENSE file for details (or add one if it is not present).
If you have any questions or suggestions, feel free to open an Issue or submit a Pull Request. Thank you for using this project!