|
| 1 | +from ..util import get_bsc_contract_instance |
| 2 | +from ._config import DELTA_NEUTRAL_ORACLE_ADDRESS, AUTOMATED_VAULT_CONTROLLER_ADDRESS |
| 3 | +# from .work_bytes import WithdrawWorkByte # <- Deprecated |
| 4 | + |
| 5 | +import requests |
| 6 | +import web3.contract |
| 7 | +from web3 import Web3 |
| 8 | +from bep20.util import checksum |
| 9 | +from eth_abi import encode_abi |
| 10 | + |
| 11 | + |
| 12 | +class DeltaNeutralOracle: |
| 13 | + def __init__(self, w3_provider: Web3 = None): |
| 14 | + self.contract = get_bsc_contract_instance(contract_address=DELTA_NEUTRAL_ORACLE_ADDRESS, |
| 15 | + abi_filename="DeltaNeutralOracle.json", |
| 16 | + w3_provider=w3_provider) |
| 17 | + |
| 18 | + def lpToDollar(self, lp_amount: int, pancakeswap_lp_token_address: str) -> int: |
| 19 | + """Return the value in USD for the given lpAmount""" |
| 20 | + return self.contract.functions.lpToDollar(lp_amount, checksum(pancakeswap_lp_token_address)).call() |
| 21 | + |
| 22 | + def dollarToLp(self, dollar_amount: int, lp_token_address: str) -> int: |
| 23 | + """Return the amount of LP for the given USD""" |
| 24 | + return self.contract.functions.dollarToLp(dollar_amount, checksum(lp_token_address)).call() |
| 25 | + |
| 26 | + def getTokenPrice(self, token_address: str) -> int: |
| 27 | + """Return the price of the given token (address) in USD""" |
| 28 | + return self.contract.functions.getTokenPrice(checksum(token_address)).call()[0] / 10 ** 18 |
| 29 | + |
| 30 | + |
| 31 | +class DeltaNeutralVault: |
| 32 | + def __init__(self, vault_address: str, w3_provider: Web3 = None, protocol: str = "pancakeswap"): |
| 33 | + """ |
| 34 | + :param vault_address: The address for the Delta Neutral Vault |
| 35 | + :param protocol: The protocol ID that the vault resides on (pancakeswap or biswap) |
| 36 | + :param w3_provider: Web3 provider (optional), if provided speeds up initialization time |
| 37 | + """ |
| 38 | + AVAILABLE_PROTOCOLS = ["pancakeswap", "biswap"] |
| 39 | + |
| 40 | + self.contract = get_bsc_contract_instance(contract_address=vault_address, |
| 41 | + abi_filename="DeltaNeutralVault.json", w3_provider=w3_provider) |
| 42 | + self.protocol = protocol.lower() |
| 43 | + assert protocol in AVAILABLE_PROTOCOLS, NotImplementedError(f"Available protocols are {AVAILABLE_PROTOCOLS}") |
| 44 | + |
| 45 | + self.ACTION_WORK = 1 |
| 46 | + |
| 47 | + r = requests.get("https://raw.githubusercontent.com/alpaca-finance/bsc-alpaca-contract/main/.mainnet.json").json() |
| 48 | + |
| 49 | + # Get vault addresses: |
| 50 | + try: |
| 51 | + self.addresses = list(filter(lambda v: v['address'].lower() == vault_address.lower(), r['DeltaNeutralVaults']))[0] |
| 52 | + """ |
| 53 | + E.x. |
| 54 | + "name": "Long 3x BUSD-BTCB PCS2", |
| 55 | + "symbol": "L3x-BUSDBTCB-PCS2", |
| 56 | + "address": "0xA1679223b7585725aFb425a6F59737a05e085C40", |
| 57 | + "deployedBlock": 18042257, |
| 58 | + "config": "0x39936A4eC165e2372C8d91c011eb36576dBE5d32", |
| 59 | + "assetToken": "0xe9e7cea3dedca5984780bafc599bd69add087d56", |
| 60 | + "stableToken": "0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c", |
| 61 | + "assetVault": "0x7C9e73d4C71dae564d41F78d56439bB4ba87592f", |
| 62 | + "stableVault": "0x08FC9Ba2cAc74742177e0afC3dC8Aed6961c24e7", |
| 63 | + "assetDeltaWorker": "0x071ac07A287C643b193bc107D29DF4D53BFFAFf7", |
| 64 | + "stableDeltaWorker": "0x3F89B3198cB710248136bbB640e88C1618436d20", |
| 65 | + "oracle": "0x08EA5fB66EA41f236E3001d2655e43A1E735787F", |
| 66 | + "gateway": "0x0256E784f73391797f80a9902c0fD05a718a812a", |
| 67 | + "assetVaultPosId": "54752", |
| 68 | + "stableVaultPosId": "5630" |
| 69 | + """ |
| 70 | + except IndexError: |
| 71 | + raise IndexError(f"Could not locate Delta Neutral Vault with address {vault_address}") |
| 72 | + |
| 73 | + # Get strategy addresses: |
| 74 | + # self.partialCloseMinimizeStrat = {"pancakeswap": "0x8dcEC5e136B6321a50F8567588c2f25738D286C2", |
| 75 | + # "biswap": "0x3739d1E01104b019Ff105B3A8F57BC6ed62F18a4"} |
| 76 | + self.partialCloseMinimizeStrat = {"pancakeswap": r['SharedStrategies']["Pancakeswap"]["StrategyPartialCloseMinimizeTrading"], |
| 77 | + "biswap": r['SharedStrategies']["Biswap"]["StrategyPartialCloseMinimizeTrading"]} |
| 78 | + |
| 79 | + def invest(self) -> web3.contract.ContractFunction: |
| 80 | + pass |
| 81 | + |
| 82 | + def withdraw(self, shares: int) -> web3.contract.ContractFunction: |
| 83 | + """ |
| 84 | + Withdraw the given amount of shares from the Delta Neutral Vault using the 'Minimize Trading' strategy. |
| 85 | +
|
| 86 | + :param shares: The amount of shares to withdraw (in shares/vault token) |
| 87 | + :return: Uncalled prepared ContractFunction |
| 88 | + """ |
| 89 | + # All Slippage Controls |
| 90 | + minStableTokenAmount = 0 |
| 91 | + minAssetTokenAmount = 0 |
| 92 | + |
| 93 | + # Constant calldata (should not change) |
| 94 | + _calldata = encode_abi( |
| 95 | + ["uint256", "uint256"], |
| 96 | + [25, 1] |
| 97 | + ) |
| 98 | + |
| 99 | + return self.contract.functions.withdraw(shares, minStableTokenAmount, minAssetTokenAmount, _calldata) |
| 100 | + |
| 101 | + def shares(self, user_address: str) -> int: |
| 102 | + """Return the number of shares owned by the given user""" |
| 103 | + return self.contract.functions.balanceOf(checksum(user_address)).call() |
| 104 | + |
| 105 | + def positionInfo(self) -> list: |
| 106 | + """ |
| 107 | + Return total equity and debt value in USD of stable and asset positions |
| 108 | +
|
| 109 | + :return: |
| 110 | + - stablePositionEquity |
| 111 | + - stablePositionDebtValue |
| 112 | + - StableLpAmount |
| 113 | + - assetPositionEquity |
| 114 | + - assetPositionDebtValue |
| 115 | + - assetLpAmount |
| 116 | + """ |
| 117 | + return list(self.contract.functions.positionInfo().call()) |
| 118 | + |
| 119 | + def sharesToUSD(self, share_amount: int) -> int: |
| 120 | + """Returns the value in USD for the given amount of vault shares""" |
| 121 | + return self.contract.functions.shareToValue(share_amount).call() |
| 122 | + |
| 123 | + def stableTokenAddress(self) -> str: |
| 124 | + """Returns the address for the delta vault stable token""" |
| 125 | + return self.contract.functions.stableToken().call() |
| 126 | + |
| 127 | + def assetTokenAddress(self) -> str: |
| 128 | + """Returns the address for the delta vault stable token""" |
| 129 | + return self.contract.functions.assetToken().call() |
| 130 | + |
| 131 | + |
| 132 | +class DeltaNeutralVaultGateway: |
| 133 | + def __init__(self, gateway_address: str, w3_provider: Web3 = None): |
| 134 | + self.contract = get_bsc_contract_instance(contract_address=gateway_address, |
| 135 | + abi_filename="DeltaNeutralVaultGateway.json", w3_provider=w3_provider) |
| 136 | + |
| 137 | + def withdraw(self, shares: int, stableReturnBps: int) -> web3.contract.ContractFunction: |
| 138 | + """ |
| 139 | + Withdraw shares from the vault using the gateway |
| 140 | +
|
| 141 | + :param shares: The amount of share to withdraw from the vault (in vault tokens) (e.g. 10 shares = 10 ** vault token decimals) |
| 142 | + :param stableReturnBps: The percentage of tokens returned that should be stable tokens (in Basis Points) |
| 143 | + :return: uncalled prepared ContractFunction |
| 144 | + """ |
| 145 | + |
| 146 | + # All Slippage Controls |
| 147 | + minWithdrawStableTokenAmount = 0 # Minimum stable token shareOwner expect to receive after withdraw. |
| 148 | + minWithdrawAssetTokenAmount = 0 # Minimum asset token shareOwner expect to receive after withdraw. |
| 149 | + minSwapStableTokenAmount = 0 # Minimum stable token shareOwner expect to receive after swap. |
| 150 | + minSwapAssetTokenAmount = 0 # Minimum asset token shareOwner expect to receive after swap. |
| 151 | + |
| 152 | + # Constant calldata (should not change) |
| 153 | + _calldata = encode_abi( |
| 154 | + ["uint256", "uint256"], |
| 155 | + [25, 1] |
| 156 | + ) |
| 157 | + |
| 158 | + # stableReturnBps = 10000 # Percentage stable token shareOwner expect to receive in bps (10000 == 100%) |
| 159 | + return self.contract.functions.withdraw(shares, minWithdrawStableTokenAmount, minWithdrawAssetTokenAmount, |
| 160 | + minSwapStableTokenAmount, minSwapAssetTokenAmount, _calldata, stableReturnBps) |
| 161 | + |
| 162 | + |
| 163 | +class AutomatedVaultController: |
| 164 | + def __init__(self, w3_provider: Web3 = None): |
| 165 | + self.contract = get_bsc_contract_instance(contract_address=AUTOMATED_VAULT_CONTROLLER_ADDRESS, |
| 166 | + abi_filename="AutomatedVaultController.json", |
| 167 | + w3_provider=w3_provider) |
| 168 | + |
| 169 | + def getUserVaultShares(self, owner_address: str, vault_address: str) -> int: |
| 170 | + return self.contract.functions.getUserVaultShares(checksum(owner_address), checksum(vault_address)).call() |
| 171 | + |
| 172 | + def totalCredit(self, user_address: str) -> int: |
| 173 | + """Get the user's total credit in USD""" |
| 174 | + return self.contract.functions.totalCredit(checksum(user_address)).call() |
0 commit comments