Created
January 20, 2022 11:02
-
-
Save t0mm4rx/df55c4676517c495794b3a3d19ec51c5 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 |
@vbalashi these are custom functions.
polygon
is a Web3 instance configured with Polygon RPC.
get_lp_pair_holdings
returns the balances of tokens from a LP token.
For example, get_lp_pair_holdings(LP_USDC_USDT_ADDRESS, BALANCE) will return [AMOUNT_USDC, AMOUNT_USDT].
get_token_decimals
returns the number of decimals of the given token.
Ok, meaning these functions are not published here. Neither they are a part of any web3/ether public libraries. So, i can't use this impermax.py as is.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please help to understand which packages are you using:
from providers import polygon
from chain_data import get_lp_pair_holdings, get_token_decimals
?