create account

Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script by gamer00

View this thread on: hive.blogpeakd.comecency.com
· @gamer00 · (edited)
$8.41
Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script
<center>![Not a Python](https://images.ecency.com/DQmQgHBWpBzayETP1EvtwLQgiQZYiGaHAQbrvpX8uPPj6NG/kiemis_mg_5396.jpg)</center>

So, you fancy dabbling with liquidity pools but don’t want to leave your cozy Python cocoon? Well, you're in luck! I've whipped up a small Python script — because why not add more joy to your life, right? — that fetches token pairs for any account you specify. It’s like Pokémon, but instead of Pikachu, you're catching SWAP.ETH. Here’s how you can use it:
`python fetch_liquidity_pools.py`, or use attributes like this:
`python fetch_liquidity_pools.py accountname TOKEN(:OTHERTOKEN)`


<div class="pull-right">

<center>![Saw this on Reddit](https://images.ecency.com/DQmRDDRQQ4AMMZb5Btj6iPKwA7ra6qXiQZMTDFRDmpeBLtr/image.png)</center>

</div>

By default, this script keeps it cool with hive-engine and BTC. You can try ALL if you're feeling adventurous, but be warned — Hive Engine has more pairs than a shoe store, and the script might just throw a tantrum (hence why BTC is the default).

## Example of the Magic in Action:
Run it like this:

`python fetch_liquidity_pools.py hive-engine ETH`

And voila, you'll see something like this:

```
Liquidity Pool Positions with ETH token:
Token Pair: SWAP.HIVE:SWAP.ETH | Base Balance: 31206.634533 SWAP.HIVE | Quote Balance: 2.242819 SWAP.ETH
Token Pair: BEE:SWAP.ETH | Base Balance: 42215.817136 BEE | Quote Balance: 1.422489 SWAP.ETH
Token Pair: SWAP.BTC:SWAP.ETH | Base Balance: 0.007309 SWAP.BTC | Quote Balance: 0.169925 SWAP.ETH
Token Pair: SWAP.ETH:SWAP.USDT | Base Balance: 0.151195 SWAP.ETH | Quote Balance: 351.954867 SWAP.USDT
Token Pair: SWAP.ETH:SPS | Base Balance: 0.033753 SWAP.ETH | Quote Balance: 11164.970135 SPS
```

## Using the Script Like a Pro:

If you’re the type who loves to DIY, you can also use the script as a function in your own scripts. Just do a:

```
from fetch_liquidity_pools import get_filtered_pools
```

And Bob’s your uncle!

## The Secret Sauce — AKA the Script Itself:

Now, I know you’re itching to see the magic behind the curtain, so here it is — all 99 lines of it. (Yes, I counted. Twice.) Don’t worry if it looks like hieroglyphics; I've sprinkled some comments to help you along the way.

```
# fetch_liquidity_pools.py
import argparse
import requests
from time import sleep

# Hive-Engine API Node
HIVE_ENGINE_NODE = 'https://api2.hive-engine.com/rpc/contracts'
retry_delay = 5  # seconds to wait between retries
max_retries = 3  # Maximum number of retries

# Default values
DEFAULT_ACCOUNT_NAME = 'hive-engine'  # Replace with your actual Hive account name
DEFAULT_FILTER_TOKEN = 'BTC'      # Replace with the desired token to filter, or use 'ALL' to list all tokens

def fetch_liquidity_positions(account_name):
    # Fetch liquidity positions for the given account
    url = HIVE_ENGINE_NODE
    payload = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "find",
        "params": {
            "contract": "marketpools",
            "table": "liquidityPositions",
            "query": {"account": account_name},
            "limit": 1000
        }
    }

    response = requests.post(url, json=payload)

    if response.status_code != 200:
        print(f"Error: Failed to fetch data. Status Code: {response.status_code}")
        return []

    try:
        data = response.json()
    except ValueError:
        print("Error: Failed to parse JSON response.")
        return []

    return data.get('result', [])

def fetch_pool_details(token_pair):
    # Fetch details of the specified liquidity pool
    for attempt in range(max_retries):
        url = HIVE_ENGINE_NODE
        payload = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "find",
            "params": {
                "contract": "marketpools",
                "table": "pools",
                "query": {"tokenPair": token_pair},
                "limit": 1
            }
        }

        response = requests.post(url, json=payload)

        if response.status_code == 200:
            try:
                data = response.json()
            except ValueError:
                print("Error: Failed to parse JSON response.")
                return {}

            if 'result' in data and data['result']:
                return data['result'][0]

        print(f"Error: Failed to fetch pool details for {token_pair}. Status Code: {response.status_code}")
        if attempt < max_retries - 1:
            sleep(retry_delay)
        else:
            print(f"Max retries exceeded for {token_pair}. Skipping.")

    return {}

def get_filtered_pools(account_name, filter_token):
    # Get and filter pools by the specified token
    positions = fetch_liquidity_positions(account_name)
    filtered_pools = []

    for position in positions:
        token_pair = position.get('tokenPair', 'Unknown')

        # If filter_token is 'ALL', skip filtering; otherwise, check for the token in the pair
        if filter_token.upper() != 'ALL' and filter_token.upper() not in token_pair.upper():
            continue

        shares = float(position.get('shares', '0'))
        pool_details = fetch_pool_details(token_pair)
        if not pool_details:
            continue

        total_shares = float(pool_details.get('totalShares', '0'))
        base_quantity = float(pool_details.get('baseQuantity', '0'))
        quote_quantity = float(pool_details.get('quoteQuantity', '0'))

        if total_shares == 0:
            continue

        user_base_balance = (shares / total_shares) * base_quantity
        user_quote_balance = (shares / total_shares) * quote_quantity

        if ':' in token_pair:
            base_symbol, quote_symbol = token_pair.split(':')
        else:
            base_symbol, quote_symbol = "Unknown", "Unknown"

        filtered_pools.append({
            "token_pair": token_pair,
            "base_symbol": base_symbol,
            "quote_symbol": quote_symbol,
            "base_balance": user_base_balance,
            "quote_balance": user_quote_balance
        })

    return filtered_pools

def main(account_name, filter_token):
    # Main function to fetch and print filtered pools
    pools = get_filtered_pools(account_name, filter_token)
    print(f"\nLiquidity Pool Positions with {filter_token.upper()} token:")
    for pool in pools:
        print(f"Token Pair: {pool['token_pair']} | Base Balance: {pool['base_balance']:.6f} {pool['base_symbol']} | "
              f"Quote Balance: {pool['quote_balance']:.6f} {pool['quote_symbol']}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Fetch Hive-Engine liquidity pools.')
    parser.add_argument('account_name', nargs='?', default=DEFAULT_ACCOUNT_NAME, help='Hive account name to fetch liquidity pools for.')
    parser.add_argument('filter_token', nargs='?', default=DEFAULT_FILTER_TOKEN, help="Token to filter by, or 'ALL' to list all tokens.")

    args = parser.parse_args()

    main(args.account_name, args.filter_token)
```

Give it a spin and let me know if it brings you joy or just mild frustration. Either way, I’m here for the feedback!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 413 others
properties (23)
authorgamer00
permlinkpython-wizardry-fetching-liquidity-pools
categoryhive-164872
json_metadata"{"app":"ecency/3.2.0-vision","description":"So, you fancy dabbling with liquidity pools but don’t want to leave your cozy Python cocoon? Well, you're in luck! I've whipped up a small Python script — because why not add more joy to your life, right?","format":"markdown+html","image":["https://images.ecency.com/DQmQgHBWpBzayETP1EvtwLQgiQZYiGaHAQbrvpX8uPPj6NG/kiemis_mg_5396.jpg","https://images.ecency.com/DQmRDDRQQ4AMMZb5Btj6iPKwA7ra6qXiQZMTDFRDmpeBLtr/image.png"],"image_ratios":["1.5023","1.3333"],"tags":["hive-164872","hive","coding","python","script","hive-engine","liquidity","liquidity-pairs","justforfun","programming","ecency"],"thumbnails":["https://images.ecency.com/DQmQgHBWpBzayETP1EvtwLQgiQZYiGaHAQbrvpX8uPPj6NG/kiemis_mg_5396.jpg","https://images.ecency.com/DQmRDDRQQ4AMMZb5Btj6iPKwA7ra6qXiQZMTDFRDmpeBLtr/image.png"]}"
created2024-09-10 19:24:45
last_update2024-09-10 21:18:57
depth0
children5
last_payout2024-09-17 19:24:45
cashout_time1969-12-31 23:59:59
total_payout_value4.226 HBD
curator_payout_value4.188 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,104
author_reputation168,798,415,632,608
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,968,425
net_rshares31,728,000,817,704
author_curate_reward""
vote details (477)
@arc7icwolf · (edited)
$0.02
I tried the code and it worked perfectly!


![test.JPG](https://files.peakd.com/file/peakd-hive/arc7icwolf/23tRvZozcFbZsw9h6ATtCAtDxRH5xUoPiyvrNvu7ora2SBEfBKPe9kGotGHWuwcknoTAG.JPG)


Love it! I also read it a few times and now I get how it works: now I have to check how the library "argparse" works because I may also use it in my code, so I'd like to better understand it :) it looks very straightforward, but I'd like to check what "nargs='?" exactly does.

EDIT: found it!
👍  
properties (23)
authorarc7icwolf
permlinkre-gamer00-sk0psx
categoryhive-164872
json_metadata{"tags":["hive-164872"],"app":"ecency/3.2.0-vision","format":"markdown+html"}
created2024-09-18 16:58:12
last_update2024-09-18 17:25:06
depth1
children1
last_payout2024-09-25 16:58:12
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length477
author_reputation508,055,258,566,757
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id137,184,702
net_rshares85,705,540,180
author_curate_reward""
vote details (1)
@gamer00 ·
Thanks for the encouragement! Glad you liked it! It's one of my first Hive-related scripts, so I was kind of anxious letting it out of the gates. (I'm kind of slow releasing these, lol, but I might get faster in time.)

I'm currently rewriting parts of the script, because after testing it for a while I noticed it needs better error handling, especially in situations where the servers act up. But from the command line it usually works best, because in case of error, one can always run it again.

About that `nargs='?'`, it just lets the script continue with no arguments, basically it falls back to using the default variable set for `account_name`.
properties (22)
authorgamer00
permlinkre-arc7icwolf-2024919t02812261z
categoryhive-164872
json_metadata{"tags":["hive-164872"],"app":"ecency/3.2.0-vision","format":"markdown+html"}
created2024-09-18 21:28:18
last_update2024-09-18 21:28:18
depth2
children0
last_payout2024-09-25 21:28:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length653
author_reputation168,798,415,632,608
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id137,189,434
net_rshares0
@ertytux ·
Anotated 👍👏
properties (22)
authorertytux
permlinkre-gamer00-2024916t174855207z
categoryhive-164872
json_metadata{"content_type":"general","type":"comment","tags":["hive-164872","hive","coding","python","script","hive-engine","liquidity","liquidity-pairs","justforfun","programming","ecency"],"app":"ecency/3.1.6-mobile","format":"markdown+html"}
created2024-09-16 21:48:57
last_update2024-09-16 21:48:57
depth1
children0
last_payout2024-09-23 21:48:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation32,350,492,372,378
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id137,132,975
net_rshares0
@kaminchan ·
$0.03
You have been very busy!  
Good to see you and your pet.
👍  ,
properties (23)
authorkaminchan
permlinkre-gamer00-2024915t2025407z
categoryhive-164872
json_metadata{"content_type":"general","type":"comment","tags":["hive-164872","hive","coding","python","script","hive-engine","liquidity","liquidity-pairs","justforfun","programming","ecency"],"app":"ecency/3.1.5-mobile","format":"markdown+html"}
created2024-09-15 13:25:39
last_update2024-09-15 13:25:39
depth1
children0
last_payout2024-09-22 13:25:39
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation551,433,449,481,874
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id137,092,854
net_rshares93,794,615,608
author_curate_reward""
vote details (2)
@stemsocial ·
$0.02
re-gamer00-python-wizardry-fetching-liquidity-pools-20240911t032211209z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
👍  ,
properties (23)
authorstemsocial
permlinkre-gamer00-python-wizardry-fetching-liquidity-pools-20240911t032211209z
categoryhive-164872
json_metadata{"app":"STEMsocial"}
created2024-09-11 03:22:12
last_update2024-09-11 03:22:12
depth1
children0
last_payout2024-09-18 03:22:12
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,920,436,264,631
root_title"Python Wizardry: Fetching Liquidity Pools from Hive Engine with a Simple Script"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,974,671
net_rshares97,848,969,640
author_curate_reward""
vote details (2)