|
| 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 | + |
| 25 | +import PerpV2LeverageModuleWrapper from '../wrappers/set-protocol-v2/PerpV2LeverageModuleWrapper'; |
| 26 | +import Assertions from '../assertions'; |
| 27 | + |
| 28 | +/** |
| 29 | + * @title PerpV2LeverageAPI |
| 30 | + * @author Set Protocol |
| 31 | + * |
| 32 | + * The PerpV2LeverageAPI exposes issue and redeem functionality for Sets that contain poitions that accrue |
| 33 | + * interest per block. The getter function syncs the position balance to the current block, so subsequent blocks |
| 34 | + * will cause the position value to be slightly out of sync (a buffer is needed). This API is primarily used for Sets |
| 35 | + * that rely on the ALM contracts to manage debt. The manager can define arbitrary issuance logic |
| 36 | + * in the manager hook, as well as specify issue and redeem fees. |
| 37 | + * |
| 38 | + */ |
| 39 | +export default class PerpV2LeverageAPI { |
| 40 | + private perpV2LeverageModuleWrapper: PerpV2LeverageModuleWrapper; |
| 41 | + private assert: Assertions; |
| 42 | + |
| 43 | + public constructor(provider: Provider, perpV2LeverageModuleAddress: Address, assertions?: Assertions) { |
| 44 | + this.perpV2LeverageModuleWrapper = new PerpV2LeverageModuleWrapper(provider, perpV2LeverageModuleAddress); |
| 45 | + this.assert = assertions || new Assertions(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Initializes the PerpV2LeverageModule to the SetToken. Only callable by the SetToken's manager. |
| 50 | + * |
| 51 | + * @param setTokenAddress Address of the SetToken to initialize |
| 52 | + * @param callerAddress The address of user transferring from (optional) |
| 53 | + * @param txOpts Overrides for transaction (optional) |
| 54 | + * |
| 55 | + * @return Transaction hash of the initialize transaction |
| 56 | + */ |
| 57 | + public async initializeAsync( |
| 58 | + setTokenAddress: Address, |
| 59 | + callerAddress: Address = undefined, |
| 60 | + txOpts: TransactionOverrides = {} |
| 61 | + ): Promise<ContractTransaction> { |
| 62 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 63 | + |
| 64 | + return await this.perpV2LeverageModuleWrapper.initialize( |
| 65 | + setTokenAddress, |
| 66 | + callerAddress, |
| 67 | + txOpts, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the address of the collateral token |
| 73 | + * |
| 74 | + * @param callerAddress The address of user transferring from (optional) |
| 75 | + * @return The address of the ERC20 collateral token |
| 76 | + */ |
| 77 | + public async getCollateralTokenAsync( |
| 78 | + callerAddress: Address = undefined, |
| 79 | + ): Promise<Address> { |
| 80 | + return await this.perpV2LeverageModuleWrapper.collateralToken(callerAddress); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets decimals of the collateral token |
| 85 | + * |
| 86 | + * @param callerAddress The address of user transferring from (optional) |
| 87 | + * @return The decimals of the ERC20 collateral token |
| 88 | + */ |
| 89 | + public async getcollateralDecimalsAsync( |
| 90 | + callerAddress: Address = undefined, |
| 91 | + ): Promise<Number> { |
| 92 | + return await this.perpV2LeverageModuleWrapper.collateralDecimals(callerAddress); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns a tuple of arrays representing all positions open for the SetToken. |
| 97 | + * |
| 98 | + * @param _setToken Instance of SetToken |
| 99 | + * @param callerAddress The address of user transferring from (optional) |
| 100 | + * |
| 101 | + * @return address[] baseToken: addresses |
| 102 | + * @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18) |
| 103 | + * @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18) |
| 104 | + */ |
| 105 | + public async getPositionNotionalInfoAsync( |
| 106 | + setTokenAddress: Address, |
| 107 | + callerAddress: Address = undefined, |
| 108 | + ): Promise<(Address|BigNumber)[][]> { |
| 109 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 110 | + |
| 111 | + return await this.perpV2LeverageModuleWrapper.getPositionNotionalInfo( |
| 112 | + setTokenAddress, |
| 113 | + callerAddress, |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns a tuple of arrays representing all positions open for the SetToken. |
| 119 | + * |
| 120 | + * @param _setToken Instance of SetToken |
| 121 | + * @param callerAddress The address of user transferring from (optional) |
| 122 | + * |
| 123 | + * @return address[] baseToken: addresses |
| 124 | + * @return BigNumber[] baseUnit: baseToken balances as position unit (10**18) |
| 125 | + * @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18) |
| 126 | + */ |
| 127 | + public async getPositionUnitInfoAsync( |
| 128 | + setTokenAddress: Address, |
| 129 | + callerAddress: Address = undefined, |
| 130 | + ): Promise<(Address|BigNumber)[][]> { |
| 131 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 132 | + |
| 133 | + return await this.perpV2LeverageModuleWrapper.getPositionUnitInfo( |
| 134 | + setTokenAddress, |
| 135 | + callerAddress, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide |
| 141 | + * (rather than position specific) balance info |
| 142 | + * |
| 143 | + * @param _setToken Instance of the SetToken |
| 144 | + * @param callerAddress The address of user transferring from (optional) |
| 145 | + * |
| 146 | + * @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals) |
| 147 | + * @return BigNumber owed realized Pnl` (10**18) |
| 148 | + * @return BigNumber pending funding payments (10**18) |
| 149 | + * @return BigNumber net quote balance (10**18) |
| 150 | + */ |
| 151 | + public async getAccountInfoAsync( |
| 152 | + setTokenAddress: Address, |
| 153 | + callerAddress: Address = undefined, |
| 154 | + ): Promise<BigNumber[]> { |
| 155 | + this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress); |
| 156 | + |
| 157 | + return await this.perpV2LeverageModuleWrapper.getAccountInfo( |
| 158 | + setTokenAddress, |
| 159 | + callerAddress, |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments