Write a python script to monitor and rebalnce the target weighting for assets within the OLP pool.
-
Consistent Indentation: Please use tabs throughout your codebase. This improves readability and makes your code more maintainable.
-
Descriptive Variable and Function Names: Avoid single-letter variable names unless they have clear meanings within the context.
-
Comments and Documentation: Include comments in your code to explain complex sections, provide context, and clarify your thought process.
-
Error Handling: Implement appropriate error handling mechanisms. Use try-except blocks to catch and handle exceptions gracefully, providing informative error messages to help with debugging and troubleshooting.
-
Modular Code: Break your code into smaller, reusable functions and classes. Modular code promotes reusability, readability, and easier maintenance. Each function or class should have a single responsibility and be relatively small.
Step 1. Clone the rebalancer template here: OmniDex OLP Rebalancer Template
Template Information
The config.json file contains the relevant contract addresses for this project, along with the RPCs and other relevant configurations.
abis.py file contains the relevant ABIs used to interact with the relevant contracts.
- Create a web3 instance connected to TelosEVM using a primary web3 provider.
- Check if the connection is successful.
- Fallback to a secondary web3 provider in case of connection failure using appropriate error capturing.
RPC1: https://mainnet.telos.net/evm RPC2: https://rpc1.us.telos.net/evm RPC3: https://mainnet.telos.caleos.io/evm
- Read Vault data from the "vault_reader" contract by calling the "getVaultTokenInfoV4" function
Use the data from Task 2 to calculate the following:
-
TargetShortSize For volatileToken: TargetShortSize = usdgAmounts(token) * shortSizePoolRatio
-
TargetLongSize For each volatileToken: TargetLongSize = usdgAmounts(token) * longSizePoolRatio
-
Shorts Deviation for each volatileToken: shortsDeviation = abs((target_short_size - maxGlobalShortSizes) / maxGlobalShortSizes) * 100
-
Longs Deviation for each volatileToken: longsDeviation = abs((TargetLongSize - maxGlobalLongSizes) / maxGlobalLongSizes) * 100
Check if any of the longs/shorts deviations are greater than the GlobalMaxDeviation (this is set in config.json as 10%). If they are, call the "setMaxGlobalSizes' function on positionRouter passing in tokens[], longSizes[], and shortSizes[]. Ensure you are passing values with correct decimal precision. (You will not be able to call this function without special admin privileges, for this task just make a dummy call).
Consider edge cases and add handling for such conditions: Example: What happens if rebalance conditions are met, however, the current long/shorts for a token are greater than the new maxGlobal long/short sizes. Kudos for finding/handing other edge cases!
-
Run the program on a loop every X minutes. Choose an appropriate value for X.
-
Generate a requiremenets.txt file
-
Run the script inside a Dockerfile. Hint: Use the template for guidance.