|
| 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 { ContractTransaction } from 'ethers'; |
| 20 | +import { Provider } from '@ethersproject/providers'; |
| 21 | +import { Address } from '@setprotocol/set-protocol-v2/utils/types'; |
| 22 | +import { TransactionOverrides } from '@setprotocol/set-protocol-v2/dist/typechain'; |
| 23 | +import { BigNumber } from 'ethers/lib/ethers'; |
| 24 | +import { VAssetDisplayInfo } from '../types'; |
| 25 | + |
| 26 | +import PerpV2LeverageModuleViewerWrapper from '../wrappers/set-protocol-v2/PerpV2LeverageModuleViewerWrapper'; |
| 27 | +import Assertions from '../assertions'; |
| 28 | + |
| 29 | +/** |
| 30 | + * @title PerpV2LeverageViewerAPI |
| 31 | + * @author Set Protocol |
| 32 | + * |
| 33 | + * The PerpV2LeverageViewerAPI exposes issue and redeem functionality for Sets that contain poitions that accrue |
| 34 | + * interest per block. The getter function syncs the position balance to the current block, so subsequent blocks |
| 35 | + * will cause the position value to be slightly out of sync (a buffer is needed). This API is primarily used for Sets |
| 36 | + * that rely on the ALM contracts to manage debt. The manager can define arbitrary issuance logic |
| 37 | + * in the manager hook, as well as specify issue and redeem fees. |
| 38 | + * |
| 39 | + */ |
| 40 | +export default class PerpV2LeverageViewerAPI { |
| 41 | + private perpV2LeverageModuleViewerWrapper: PerpV2LeverageModuleViewerWrapper; |
| 42 | + private assert: Assertions; |
| 43 | + |
| 44 | + public constructor(provider: Provider, perpV2LeverageModuleViewerAddress: Address, assertions?: Assertions) { |
| 45 | + this.perpV2LeverageModuleViewerWrapper = new PerpV2LeverageModuleViewerWrapper( |
| 46 | + provider, |
| 47 | + perpV2LeverageModuleViewerAddress |
| 48 | + ); |
| 49 | + this.assert = assertions || new Assertions(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Initializes the PerpV2LeverageModuleViewer to the SetToken. Only callable by the SetToken's manager. |
| 54 | + * |
| 55 | + * @param setTokenAddress Address of the SetToken to initialize |
| 56 | + * @param callerAddress The address of user transferring from (optional) |
| 57 | + * @param txOpts Overrides for transaction (optional) |
| 58 | + * |
| 59 | + * @return Transaction hash of the initialize transaction |
| 60 | + */ |
| 61 | + public async initializeAsync( |
| 62 | + setTokenAddress: Address, |
| 63 | + callerAddress: Address = undefined, |
| 64 | + txOpts: TransactionOverrides = {} |
| 65 | + ): Promise<ContractTransaction> { |
| 66 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 67 | + |
| 68 | + return await this.perpV2LeverageModuleViewerWrapper.initialize( |
| 69 | + setTokenAddress, |
| 70 | + callerAddress, |
| 71 | + txOpts, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets the address of the collateral token |
| 77 | + * |
| 78 | + * @param callerAddress The address of user transferring from (optional) |
| 79 | + * @return The address of the ERC20 collateral token |
| 80 | + */ |
| 81 | + public async getCollateralTokenAsync( |
| 82 | + callerAddress: Address = undefined, |
| 83 | + ): Promise<Address> { |
| 84 | + return await this.perpV2LeverageModuleViewerWrapper.collateralToken(callerAddress); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the maximum amount of Sets that can be issued. Because upon issuance we lever up the Set |
| 89 | + * before depositing collateral there is a ceiling on the amount of Sets that can be issued before the max |
| 90 | + * leverage ratio is met. In order to accurately predict this amount the user must pass in an expected |
| 91 | + * slippage amount, this amount should be calculated relative to Index price(s) of vAssets held by the Set, |
| 92 | + * not the mid-market prices. The formulas used here are based on the "conservative" definition of free |
| 93 | + * collateral as defined in PerpV2's docs. |
| 94 | + * |
| 95 | + * @param setTokenAddress Instance of SetToken |
| 96 | + * @param slippage Expected slippage from entering position in precise units (1% = 10^16) |
| 97 | + * @param callerAddress Address of the method caller |
| 98 | + * |
| 99 | + * @return Maximum amount of Sets that can be issued |
| 100 | + */ |
| 101 | + public async getMaximumSetTokenIssueAmountAsync( |
| 102 | + setTokenAddress: Address, |
| 103 | + slippage: BigNumber, |
| 104 | + callerAddress: Address = undefined, |
| 105 | + ): Promise<BigNumber> { |
| 106 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 107 | + |
| 108 | + return await this.perpV2LeverageModuleViewerWrapper.getMaximumSetTokenIssueAmount( |
| 109 | + setTokenAddress, |
| 110 | + slippage, |
| 111 | + callerAddress, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns the position unit for total collateral value as defined by Perpetual Protocol. |
| 117 | + * |
| 118 | + * @param setTokenAddress Instance of SetToken |
| 119 | + * @param callerAddress Address of the method caller |
| 120 | + * |
| 121 | + * @return Collateral token address |
| 122 | + * @return Total collateral value position unit |
| 123 | + */ |
| 124 | + public async getTotalCollateralUnitAsync( |
| 125 | + setTokenAddress: Address, |
| 126 | + callerAddress: Address = undefined, |
| 127 | + ): Promise<[Address, BigNumber]> { |
| 128 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 129 | + |
| 130 | + return await this.perpV2LeverageModuleViewerWrapper.getTotalCollateralUnit( |
| 131 | + setTokenAddress, |
| 132 | + callerAddress, |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns relevant data for displaying current positions. Identifying info for each position plus current |
| 138 | + * size, index price, and leverage of each vAsset with an open position is returned. The sum quantity of vUSDC |
| 139 | + * is returned along with identifying info in last index of array. |
| 140 | + * |
| 141 | + * @param setTokenAddress Instance of the SetToken |
| 142 | + * @param callerAddress Address of the method caller |
| 143 | + * |
| 144 | + * @return Array of info concerning size and leverage of current vAsset positions |
| 145 | + */ |
| 146 | + public async getVirtualAssetsDisplayInfoAsync( |
| 147 | + setTokenAddress: Address, |
| 148 | + callerAddress: Address = undefined, |
| 149 | + ): Promise<VAssetDisplayInfo[]> { |
| 150 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 151 | + |
| 152 | + return await this.perpV2LeverageModuleViewerWrapper.getVirtualAssetsDisplayInfo( |
| 153 | + setTokenAddress, |
| 154 | + callerAddress, |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments