Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "set.js",
"version": "0.4.8",
"version": "0.4.9",
"description": "A javascript library for interacting with the Set Protocol v2",
"keywords": [
"set.js",
Expand Down
11 changes: 10 additions & 1 deletion src/Set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 Set Labs Inc.
Copyright 2022 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@ import {
DebtIssuanceV2API,
SlippageIssuanceAPI,
} from './api/index';
import PerpV2LeverageAPI from './api/PerpV2LeverageAPI';

const ethersProviders = require('ethers').providers;

Expand Down Expand Up @@ -112,6 +113,13 @@ class Set {
*/
public slippageIssuance: SlippageIssuanceAPI;

/**
* An instance of the PerpV2LeverageAPI class. Contains getters for fetching
* positional (per Set) and notional (across all Sets) units for collateral, vAssets, and debt.
* Initially used for Perpetual Leverage Tokens.
*/
public perpV2Leverage: PerpV2LeverageAPI;

/**
* An instance of the BlockchainAPI class. Contains interfaces for
* interacting with the blockchain
Expand Down Expand Up @@ -145,6 +153,7 @@ class Set {
this.debtIssuance = new DebtIssuanceAPI(ethersProvider, config.debtIssuanceModuleAddress);
this.debtIssuanceV2 = new DebtIssuanceV2API(ethersProvider, config.debtIssuanceModuleV2Address);
this.slippageIssuance = new SlippageIssuanceAPI(ethersProvider, config.slippageIssuanceModuleAddress);
this.perpV2Leverage = new PerpV2LeverageAPI(ethersProvider, config.perpV2LeverageModuleAddress);
this.blockchain = new BlockchainAPI(ethersProvider, assertions);
}
}
Expand Down
162 changes: 162 additions & 0 deletions src/api/PerpV2LeverageAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright 2022 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

import { ContractTransaction } from 'ethers';
import { Provider } from '@ethersproject/providers';
import { Address } from '@setprotocol/set-protocol-v2/utils/types';
import { TransactionOverrides } from '@setprotocol/set-protocol-v2/dist/typechain';
import { BigNumber } from 'ethers/lib/ethers';

import PerpV2LeverageModuleWrapper from '../wrappers/set-protocol-v2/PerpV2LeverageModuleWrapper';
import Assertions from '../assertions';

/**
* @title PerpV2LeverageAPI
* @author Set Protocol
*
* The PerpV2LeverageAPI exposes issue and redeem functionality for Sets that contain poitions that accrue
* interest per block. The getter function syncs the position balance to the current block, so subsequent blocks
* will cause the position value to be slightly out of sync (a buffer is needed). This API is primarily used for Sets
* that rely on the ALM contracts to manage debt. The manager can define arbitrary issuance logic
* in the manager hook, as well as specify issue and redeem fees.
*
*/
export default class PerpV2LeverageAPI {
private perpV2LeverageModuleWrapper: PerpV2LeverageModuleWrapper;
private assert: Assertions;

public constructor(provider: Provider, perpV2LeverageModuleAddress: Address, assertions?: Assertions) {
this.perpV2LeverageModuleWrapper = new PerpV2LeverageModuleWrapper(provider, perpV2LeverageModuleAddress);
this.assert = assertions || new Assertions();
}

/**
* Initializes the PerpV2LeverageModule to the SetToken. Only callable by the SetToken's manager.
*
* @param setTokenAddress Address of the SetToken to initialize
* @param callerAddress The address of user transferring from (optional)
* @param txOpts Overrides for transaction (optional)
*
* @return Transaction hash of the initialize transaction
*/
public async initializeAsync(
setTokenAddress: Address,
callerAddress: Address = undefined,
txOpts: TransactionOverrides = {}
): Promise<ContractTransaction> {
this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress);

return await this.perpV2LeverageModuleWrapper.initialize(
setTokenAddress,
callerAddress,
txOpts,
);
}

/**
* Gets the address of the collateral token
*
* @param callerAddress The address of user transferring from (optional)
* @return The address of the ERC20 collateral token
*/
public async getCollateralTokenAsync(
callerAddress: Address = undefined,
): Promise<Address> {
return await this.perpV2LeverageModuleWrapper.collateralToken(callerAddress);
}

/**
* Gets decimals of the collateral token
*
* @param callerAddress The address of user transferring from (optional)
* @return The decimals of the ERC20 collateral token
*/
public async getcollateralDecimalsAsync(
callerAddress: Address = undefined,
): Promise<Number> {
return await this.perpV2LeverageModuleWrapper.collateralDecimals(callerAddress);
}

/**
* Returns a tuple of arrays representing all positions open for the SetToken.
*
* @param _setToken Instance of SetToken
* @param callerAddress The address of user transferring from (optional)
*
* @return address[] baseToken: addresses
* @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
* @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
*/
public async getPositionNotionalInfoAsync(
setTokenAddress: Address,
callerAddress: Address = undefined,
): Promise<(Address|BigNumber)[][]> {
this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress);

return await this.perpV2LeverageModuleWrapper.getPositionNotionalInfo(
setTokenAddress,
callerAddress,
);
}

/**
* Returns a tuple of arrays representing all positions open for the SetToken.
*
* @param _setToken Instance of SetToken
* @param callerAddress The address of user transferring from (optional)
*
* @return address[] baseToken: addresses
* @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
* @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
*/
public async getPositionUnitInfoAsync(
setTokenAddress: Address,
callerAddress: Address = undefined,
): Promise<(Address|BigNumber)[][]> {
this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress);

return await this.perpV2LeverageModuleWrapper.getPositionUnitInfo(
setTokenAddress,
callerAddress,
);
}

/**
* Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide
* (rather than position specific) balance info
*
* @param _setToken Instance of the SetToken
* @param callerAddress The address of user transferring from (optional)
*
* @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
* @return BigNumber owed realized Pnl` (10**18)
* @return BigNumber pending funding payments (10**18)
* @return BigNumber net quote balance (10**18)
*/
public async getAccountInfoAsync(
setTokenAddress: Address,
callerAddress: Address = undefined,
): Promise<BigNumber[]> {
this.assert.schema.isValidAddress('setTokenAddress', setTokenAddress);

return await this.perpV2LeverageModuleWrapper.getAccountInfo(
setTokenAddress,
callerAddress,
);
}
}
2 changes: 1 addition & 1 deletion src/api/SlippageIssuanceAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Set Labs Inc.
Copyright 2022 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import PriceOracleAPI from './PriceOracleAPI';
import DebtIssuanceAPI from './DebtIssuanceAPI';
import DebtIssuanceV2API from './DebtIssuanceV2API';
import SlippageIssuanceAPI from './SlippageIssuanceAPI';
import PerpV2LeverageAPI from './PerpV2LeverageAPI';
import {
TradeQuoter,
CoinGeckoDataService,
Expand All @@ -29,6 +30,7 @@ export {
DebtIssuanceAPI,
DebtIssuanceV2API,
SlippageIssuanceAPI,
PerpV2LeverageAPI,
TradeQuoter,
CoinGeckoDataService,
GasOracleService
Expand Down
1 change: 1 addition & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SetJSConfig {
zeroExApiKey?: string;
debtIssuanceModuleV2Address: Address;
slippageIssuanceModuleAddress: Address;
perpV2LeverageModuleAddress: Address;
}

export type SetDetails = {
Expand Down
41 changes: 22 additions & 19 deletions src/wrappers/set-protocol-v2/PerpV2LeverageModuleWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export default class PerpV2LeverageModuleWrapper {
/**
* Gets the address of the collateral token
*
* @param callerAddress Address of the method caller
* @return The address of the ERC20 collateral token
* @param callerAddress Address of the method caller

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0xEa5C920973eBfff95270dd52c8045c4e6eBFc847

* @return The address of the ERC20 collateral token
*/
public async collateralToken(
callerAddress: Address = undefined,
Expand All @@ -84,8 +84,8 @@ export default class PerpV2LeverageModuleWrapper {
/**
* Gets decimals of the collateral token
*
* @param callerAddress Address of the method caller
* @return The decimals of the ERC20 collateral token
* @param callerAddress Address of the method caller
* @return The decimals of the ERC20 collateral token
*/
public async collateralDecimals(
callerAddress: Address = undefined,
Expand All @@ -99,13 +99,14 @@ export default class PerpV2LeverageModuleWrapper {
}

/**
* Returns a PositionUnitNotionalInfo array representing all positions open for the SetToken.
* Returns a tuple of arrays representing all positions open for the SetToken.
*
* @param _setToken Instance of SetToken
* @param _setToken Instance of SetToken
* @param callerAddress Address of the method caller
*
* @return address[] baseToken: addresses
* @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
* @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
* @return address[] baseToken: addresses
* @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
* @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
*/
public async getPositionNotionalInfo(
setTokenAddress: Address,
Expand All @@ -122,13 +123,14 @@ export default class PerpV2LeverageModuleWrapper {
}

/**
* Returns a PositionUnitInfo array representing all positions open for the SetToken.
* Returns a tuple of arrays representing all positions open for the SetToken.
*
* @param _setToken Instance of SetToken
* @param _setToken Instance of SetToken
* @param callerAddress Address of the method caller
*
* @return address[] baseToken: addresses
* @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
* @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
* @return address[] baseToken: addresses
* @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
* @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
*/
public async getPositionUnitInfo(
setTokenAddress: Address,
Expand All @@ -148,12 +150,13 @@ export default class PerpV2LeverageModuleWrapper {
* Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide
* (rather than position specific) balance info
*
* @param _setToken Instance of the SetToken
* @param _setToken Instance of the SetToken
* @param callerAddress Address of the method caller
*
* @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
* @return BigNumber owed realized Pnl` (10**18)
* @return BigNumber pending funding payments (10**18)
* @return BigNumber net quote balance (10**18)
* @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
* @return BigNumber owed realized Pnl` (10**18)
* @return BigNumber pending funding payments (10**18)
* @return BigNumber net quote balance (10**18)
*/
public async getAccountInfo(
setTokenAddress: Address,
Expand Down
8 changes: 4 additions & 4 deletions src/wrappers/set-protocol-v2/SlippageIssuanceModuleWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Set Labs Inc.
Copyright 2022 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -24,13 +24,13 @@ import { generateTxOpts } from '../../utils/transactions';
import ContractWrapper from './ContractWrapper';

/**
* @title SlippageIssuanceModuleV2Wrapper
* @title SlippageIssuanceModuleWrapper
* @author Set Protocol
*
* The SlippageIssuanceModuleV2Wrapper forwards functionality from the SlippageIssuanceModule contract
* The SlippageIssuanceModuleWrapper forwards functionality from the SlippageIssuanceModule contract
*
*/
export default class SlippageIssuanceModuleV2Wrapper {
export default class SlippageIssuanceModuleWrapper {
private provider: Provider;
private contracts: ContractWrapper;

Expand Down
Loading