Skip to content

Commit bdaf44a

Browse files
authored
Add PerpV2LeverageAPI and tests (#101)
1 parent 7c396e9 commit bdaf44a

File tree

9 files changed

+430
-26
lines changed

9 files changed

+430
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "set.js",
3-
"version": "0.4.8",
3+
"version": "0.4.9",
44
"description": "A javascript library for interacting with the Set Protocol v2",
55
"keywords": [
66
"set.js",

src/Set.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 Set Labs Inc.
2+
Copyright 2022 Set Labs Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import {
3232
DebtIssuanceV2API,
3333
SlippageIssuanceAPI,
3434
} from './api/index';
35+
import PerpV2LeverageAPI from './api/PerpV2LeverageAPI';
3536

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

@@ -112,6 +113,13 @@ class Set {
112113
*/
113114
public slippageIssuance: SlippageIssuanceAPI;
114115

116+
/**
117+
* An instance of the PerpV2LeverageAPI class. Contains getters for fetching
118+
* positional (per Set) and notional (across all Sets) units for collateral, vAssets, and debt.
119+
* Initially used for Perpetual Leverage Tokens.
120+
*/
121+
public perpV2Leverage: PerpV2LeverageAPI;
122+
115123
/**
116124
* An instance of the BlockchainAPI class. Contains interfaces for
117125
* interacting with the blockchain
@@ -145,6 +153,7 @@ class Set {
145153
this.debtIssuance = new DebtIssuanceAPI(ethersProvider, config.debtIssuanceModuleAddress);
146154
this.debtIssuanceV2 = new DebtIssuanceV2API(ethersProvider, config.debtIssuanceModuleV2Address);
147155
this.slippageIssuance = new SlippageIssuanceAPI(ethersProvider, config.slippageIssuanceModuleAddress);
156+
this.perpV2Leverage = new PerpV2LeverageAPI(ethersProvider, config.perpV2LeverageModuleAddress);
148157
this.blockchain = new BlockchainAPI(ethersProvider, assertions);
149158
}
150159
}

src/api/PerpV2LeverageAPI.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}

src/api/SlippageIssuanceAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2021 Set Labs Inc.
2+
Copyright 2022 Set Labs Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import PriceOracleAPI from './PriceOracleAPI';
1010
import DebtIssuanceAPI from './DebtIssuanceAPI';
1111
import DebtIssuanceV2API from './DebtIssuanceV2API';
1212
import SlippageIssuanceAPI from './SlippageIssuanceAPI';
13+
import PerpV2LeverageAPI from './PerpV2LeverageAPI';
1314
import {
1415
TradeQuoter,
1516
CoinGeckoDataService,
@@ -29,6 +30,7 @@ export {
2930
DebtIssuanceAPI,
3031
DebtIssuanceV2API,
3132
SlippageIssuanceAPI,
33+
PerpV2LeverageAPI,
3234
TradeQuoter,
3335
CoinGeckoDataService,
3436
GasOracleService

src/types/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface SetJSConfig {
2121
zeroExApiKey?: string;
2222
debtIssuanceModuleV2Address: Address;
2323
slippageIssuanceModuleAddress: Address;
24+
perpV2LeverageModuleAddress: Address;
2425
}
2526

2627
export type SetDetails = {

src/wrappers/set-protocol-v2/PerpV2LeverageModuleWrapper.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export default class PerpV2LeverageModuleWrapper {
6767
/**
6868
* Gets the address of the collateral token
6969
*
70-
* @param callerAddress Address of the method caller
71-
* @return The address of the ERC20 collateral token
70+
* @param callerAddress Address of the method caller
71+
* @return The address of the ERC20 collateral token
7272
*/
7373
public async collateralToken(
7474
callerAddress: Address = undefined,
@@ -84,8 +84,8 @@ export default class PerpV2LeverageModuleWrapper {
8484
/**
8585
* Gets decimals of the collateral token
8686
*
87-
* @param callerAddress Address of the method caller
88-
* @return The decimals of the ERC20 collateral token
87+
* @param callerAddress Address of the method caller
88+
* @return The decimals of the ERC20 collateral token
8989
*/
9090
public async collateralDecimals(
9191
callerAddress: Address = undefined,
@@ -99,13 +99,14 @@ export default class PerpV2LeverageModuleWrapper {
9999
}
100100

101101
/**
102-
* Returns a PositionUnitNotionalInfo array representing all positions open for the SetToken.
102+
* Returns a tuple of arrays representing all positions open for the SetToken.
103103
*
104-
* @param _setToken Instance of SetToken
104+
* @param _setToken Instance of SetToken
105+
* @param callerAddress Address of the method caller
105106
*
106-
* @return address[] baseToken: addresses
107-
* @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
108-
* @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
107+
* @return address[] baseToken: addresses
108+
* @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
109+
* @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
109110
*/
110111
public async getPositionNotionalInfo(
111112
setTokenAddress: Address,
@@ -122,13 +123,14 @@ export default class PerpV2LeverageModuleWrapper {
122123
}
123124

124125
/**
125-
* Returns a PositionUnitInfo array representing all positions open for the SetToken.
126+
* Returns a tuple of arrays representing all positions open for the SetToken.
126127
*
127-
* @param _setToken Instance of SetToken
128+
* @param _setToken Instance of SetToken
129+
* @param callerAddress Address of the method caller
128130
*
129-
* @return address[] baseToken: addresses
130-
* @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
131-
* @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
131+
* @return address[] baseToken: addresses
132+
* @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
133+
* @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
132134
*/
133135
public async getPositionUnitInfo(
134136
setTokenAddress: Address,
@@ -148,12 +150,13 @@ export default class PerpV2LeverageModuleWrapper {
148150
* Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide
149151
* (rather than position specific) balance info
150152
*
151-
* @param _setToken Instance of the SetToken
153+
* @param _setToken Instance of the SetToken
154+
* @param callerAddress Address of the method caller
152155
*
153-
* @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
154-
* @return BigNumber owed realized Pnl` (10**18)
155-
* @return BigNumber pending funding payments (10**18)
156-
* @return BigNumber net quote balance (10**18)
156+
* @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
157+
* @return BigNumber owed realized Pnl` (10**18)
158+
* @return BigNumber pending funding payments (10**18)
159+
* @return BigNumber net quote balance (10**18)
157160
*/
158161
public async getAccountInfo(
159162
setTokenAddress: Address,

src/wrappers/set-protocol-v2/SlippageIssuanceModuleWrapper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2021 Set Labs Inc.
2+
Copyright 2022 Set Labs Inc.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -24,13 +24,13 @@ import { generateTxOpts } from '../../utils/transactions';
2424
import ContractWrapper from './ContractWrapper';
2525

2626
/**
27-
* @title SlippageIssuanceModuleV2Wrapper
27+
* @title SlippageIssuanceModuleWrapper
2828
* @author Set Protocol
2929
*
30-
* The SlippageIssuanceModuleV2Wrapper forwards functionality from the SlippageIssuanceModule contract
30+
* The SlippageIssuanceModuleWrapper forwards functionality from the SlippageIssuanceModule contract
3131
*
3232
*/
33-
export default class SlippageIssuanceModuleV2Wrapper {
33+
export default class SlippageIssuanceModuleWrapper {
3434
private provider: Provider;
3535
private contracts: ContractWrapper;
3636

0 commit comments

Comments
 (0)