This script processes staking data from a JSON file containing pool details, calculates balances and rewards, and outputs a summary of staking data in JSON format. It uses MintLayerApiStrategy and MintLayerV2Api for interacting with MintLayer APIs and leverages on coinmarketcap for upto date Mintlayer price.
- Python Version: Ensure you have Python 3.7 or higher installed.
- Dependencies:
- Custom modules:
apicontaining:MintLayerApiStrategyMintLayerV2Api
rw_stakemodelcontaining:StakingDataPool
- Custom modules:
Install the required libraries using:
pip install -r requirements.txt.
├── pools.json # Input file containing pool details
├── api.py # API interactions module
├── rw_stakemodel.py # Data models for staking
└── app.py # The main script file
The script expects an input file named pools.json in the same directory. The file should contain an array of staking pools with the following structure:
[
{
"pool_id": "string",
"delegation_id": "string",
"pool_balance": float,
"delegation_datetime": "string"
}
]The script calculates the following metrics:
- MintLayer Coin Price (
ml_price): Retrieved from the MintLayer API. - Total Staked Balance: The sum of all delegation balances.
- Total Staked Rewards: The sum of rewards for all pools.
- Total Pool Balance: The sum of all pool balances.
- Total Staked Rewards Amount: Rewards in monetary value.
- Staking Pools: Detailed breakdown of each pool.
The results are printed as a JSON object with the following structure:
{
"ml_price": float,
"total_staked_balance_amount": float,
"total_staked_rewards": float,
"total_staked_balance": float,
"total_pool_balance": float,
"total_staked_rewards_amount": float,
"staking_pools": [
{
"pool_id": "string",
"pool_balance": float,
"delegation_id": "string",
"delegation_datetime": "string",
"delegation_balance": float,
"pool_rewards": float,
"ml_amount": float
}
]
}-
Ensure the input file
pools.jsonis correctly formatted and present. -
Run the script:
python app.py
-
The script outputs the calculated data in a pretty-printed JSON format to the console.
-
MintLayer API Integration:
- Retrieves the current coin price using
ml_api.get_coin_price. - Fetches
pool_balanceanddelegation_balancefor each pool.
- Retrieves the current coin price using
-
Reward Calculation:
- Pool rewards:
delegation_balance - pool_balance(if delegation balance is positive). - Monetary value of rewards:
pool_rewards * ml_price.
- Pool rewards:
-
Aggregation:
- Total staked balance, total pool balance, and reward amounts are summed across all pools.