-
-
Save hexmanshu/d1ad1460b40d71185a9d79a67d51a7a9 to your computer and use it in GitHub Desktop.
Python script to fetch Impermax.finance position total collateral and debt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Impermax related functions. | |
imxc = Impermax collateral token, given in exchange of the actual LP pair. | |
slp = staked LP token. | |
""" | |
from providers import polygon | |
from chain_data import get_lp_pair_holdings, get_token_decimals | |
import json | |
imxc_abi = json.load(open("./abis/imxc.json", "rb")) | |
imxb_abi = json.load(open("./abis/imxb.json", "rb")) | |
slp_abi = json.load(open("./abis/slp.json", "rb")) | |
def get_collateral(address: str, collateral: str) -> tuple[float, float]: | |
"""Get the total amount of collateral of an Impermax position.""" | |
imxc_instance = polygon.eth.contract(polygon.toChecksumAddress(collateral), abi=imxc_abi) | |
slp_pair = imxc_instance.functions.underlying().call() | |
slp_instance = polygon.eth.contract(polygon.toChecksumAddress(slp_pair), abi=slp_abi) | |
imxc_balance = float(imxc_instance.functions.balanceOf(address).call()) | |
imxc_total_supply = float(imxc_instance.functions.totalSupply().call()) | |
imxc_total_balance = float(imxc_instance.functions.totalBalance().call()) | |
slp_balance = float(slp_instance.functions.totalBalance().call()) | |
slp_supply = float(slp_instance.functions.totalSupply().call()) | |
lp_holdings = imxc_balance * (imxc_total_balance / imxc_total_supply) * (slp_balance / slp_supply) | |
underlying_lp = slp_instance.functions.underlying().call() | |
lp_value = get_lp_pair_holdings(underlying_lp, lp_balance=lp_holdings) | |
return (lp_value[0], lp_value[1]) | |
def get_debt(address: str, collateral: str) -> tuple[float, float]: | |
"""Get the total amount of debt for an Impermax position.""" | |
imxc_instance = polygon.eth.contract(polygon.toChecksumAddress(collateral), abi=imxc_abi) | |
borrowable0_address = imxc_instance.functions.borrowable0().call() | |
borrowable1_address = imxc_instance.functions.borrowable1().call() | |
borrowable0_instance = polygon.eth.contract(polygon.toChecksumAddress(borrowable0_address), abi=imxb_abi) | |
borrowable1_instance = polygon.eth.contract(polygon.toChecksumAddress(borrowable1_address), abi=imxb_abi) | |
borrowable0_underlying = borrowable0_instance.functions.underlying().call() | |
borrowable1_underlying = borrowable1_instance.functions.underlying().call() | |
borrowable0_decimals = get_token_decimals(borrowable0_underlying) | |
borrowable1_decimals = get_token_decimals(borrowable1_underlying) | |
borrow0_balance = float(borrowable0_instance.functions.borrowBalance(address).call()) / (10 ** borrowable0_decimals) | |
borrow1_balance = float(borrowable1_instance.functions.borrowBalance(address).call()) / (10 ** borrowable1_decimals) | |
return (borrow0_balance, borrow1_balance) | |
def main(): | |
"""Print the collateral and debt for the given address and collateral.""" | |
address = "0x<your-address>" | |
collateral_address = "0x<imxc-token>" | |
collateral = get_collateral(address, collateral_address) | |
debt = get_debt(address, collateral_address) | |
print(collateral, debt) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment