|
| 1 | +/* |
| 2 | + Copyright 2022 Set Labs Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +import { Provider } from '@ethersproject/providers'; |
| 20 | +import { Address } from '@setprotocol/set-protocol-v2/utils/types'; |
| 21 | +import { BigNumber } from 'ethers/lib/ethers'; |
| 22 | +import { VAssetDisplayInfo } from '../types'; |
| 23 | + |
| 24 | +import PerpV2LeverageModuleViewerWrapper from '../wrappers/set-protocol-v2/PerpV2LeverageModuleViewerWrapper'; |
| 25 | +import Assertions from '../assertions'; |
| 26 | + |
| 27 | +/** |
| 28 | + * @title PerpV2LeverageViewerAPI |
| 29 | + * @author Set Protocol |
| 30 | + * |
| 31 | + * The PerpV2LeverageViewerAPI exposes issue and redeem functionality for Sets that contain poitions that accrue |
| 32 | + * interest per block. The getter function syncs the position balance to the current block, so subsequent blocks |
| 33 | + * will cause the position value to be slightly out of sync (a buffer is needed). This API is primarily used for Sets |
| 34 | + * that rely on the ALM contracts to manage debt. The manager can define arbitrary issuance logic |
| 35 | + * in the manager hook, as well as specify issue and redeem fees. |
| 36 | + * |
| 37 | + */ |
| 38 | +export default class PerpV2LeverageViewerAPI { |
| 39 | + private perpV2LeverageModuleViewerWrapper: PerpV2LeverageModuleViewerWrapper; |
| 40 | + private assert: Assertions; |
| 41 | + |
| 42 | + public constructor(provider: Provider, perpV2LeverageModuleViewerAddress: Address, assertions?: Assertions) { |
| 43 | + this.perpV2LeverageModuleViewerWrapper = new PerpV2LeverageModuleViewerWrapper( |
| 44 | + provider, |
| 45 | + perpV2LeverageModuleViewerAddress |
| 46 | + ); |
| 47 | + this.assert = assertions || new Assertions(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the address of the collateral token |
| 52 | + * |
| 53 | + * @param callerAddress The address of user transferring from (optional) |
| 54 | + * @return The address of the ERC20 collateral token |
| 55 | + */ |
| 56 | + public async getCollateralTokenAsync( |
| 57 | + callerAddress: Address = undefined, |
| 58 | + ): Promise<Address> { |
| 59 | + return await this.perpV2LeverageModuleViewerWrapper.collateralToken(callerAddress); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the maximum amount of Sets that can be issued. Because upon issuance we lever up the Set |
| 64 | + * before depositing collateral there is a ceiling on the amount of Sets that can be issued before the max |
| 65 | + * leverage ratio is met. In order to accurately predict this amount the user must pass in an expected |
| 66 | + * slippage amount, this amount should be calculated relative to Index price(s) of vAssets held by the Set, |
| 67 | + * not the mid-market prices. The formulas used here are based on the "conservative" definition of free |
| 68 | + * collateral as defined in PerpV2's docs. |
| 69 | + * |
| 70 | + * @param setTokenAddress Instance of SetToken |
| 71 | + * @param slippage Expected slippage from entering position in precise units (1% = 10^16) |
| 72 | + * @param callerAddress Address of the method caller |
| 73 | + * |
| 74 | + * @return Maximum amount of Sets that can be issued |
| 75 | + */ |
| 76 | + public async getMaximumSetTokenIssueAmountAsync( |
| 77 | + setTokenAddress: Address, |
| 78 | + slippage: BigNumber, |
| 79 | + callerAddress: Address = undefined, |
| 80 | + ): Promise<BigNumber> { |
| 81 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 82 | + |
| 83 | + return await this.perpV2LeverageModuleViewerWrapper.getMaximumSetTokenIssueAmount( |
| 84 | + setTokenAddress, |
| 85 | + slippage, |
| 86 | + callerAddress, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the position unit for total collateral value as defined by Perpetual Protocol. |
| 92 | + * |
| 93 | + * @param setTokenAddress Instance of SetToken |
| 94 | + * @param callerAddress Address of the method caller |
| 95 | + * |
| 96 | + * @return Collateral token address |
| 97 | + * @return Total collateral value position unit |
| 98 | + */ |
| 99 | + public async getTotalCollateralUnitAsync( |
| 100 | + setTokenAddress: Address, |
| 101 | + callerAddress: Address = undefined, |
| 102 | + ): Promise<[Address, BigNumber]> { |
| 103 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 104 | + |
| 105 | + return await this.perpV2LeverageModuleViewerWrapper.getTotalCollateralUnit( |
| 106 | + setTokenAddress, |
| 107 | + callerAddress, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns relevant data for displaying current positions. Identifying info for each position plus current |
| 113 | + * size, index price, and leverage of each vAsset with an open position is returned. The sum quantity of vUSDC |
| 114 | + * is returned along with identifying info in last index of array. |
| 115 | + * |
| 116 | + * @param setTokenAddress Instance of the SetToken |
| 117 | + * @param callerAddress Address of the method caller |
| 118 | + * |
| 119 | + * @return Array of info concerning size and leverage of current vAsset positions |
| 120 | + */ |
| 121 | + public async getVirtualAssetsDisplayInfoAsync( |
| 122 | + setTokenAddress: Address, |
| 123 | + callerAddress: Address = undefined, |
| 124 | + ): Promise<VAssetDisplayInfo[]> { |
| 125 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 126 | + |
| 127 | + return await this.perpV2LeverageModuleViewerWrapper.getVirtualAssetsDisplayInfo( |
| 128 | + setTokenAddress, |
| 129 | + callerAddress, |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments