@carrot-protocol/boost-http-client 0.2.15-group-refactor1-dev-2702e47 → 0.2.15-group-refactor1-dev-57892a5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +108 -97
- package/dist/types.d.ts +13 -11
- package/package.json +1 -1
- package/src/index.ts +125 -111
- package/src/types.ts +14 -11
package/dist/index.d.ts
CHANGED
|
@@ -57,5 +57,5 @@ export declare class Client {
|
|
|
57
57
|
* @param request Withdraw leverage request parameters
|
|
58
58
|
* @returns Withdraw leverage operation result
|
|
59
59
|
*/
|
|
60
|
-
withdrawLeverage(clendGroup: web3.PublicKey,
|
|
60
|
+
withdrawLeverage(clendGroup: web3.PublicKey, outputTokenMint: web3.PublicKey, assetTokenMint: web3.PublicKey, liabilityTokenMint: web3.PublicKey, uiAmount: number, slippageBps: number, withdrawAll: boolean): Promise<any>;
|
|
61
61
|
}
|
package/dist/index.js
CHANGED
|
@@ -112,113 +112,124 @@ class Client {
|
|
|
112
112
|
}
|
|
113
113
|
const body = await handleApiCall(() => this.http.get(`/user?user=${user.toString()}&getClendAccountSummary=${request.getClendAccountSummary}`));
|
|
114
114
|
const jsonRawResponse = JSON.parse(body);
|
|
115
|
-
//
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
jlpBalance: new anchor_1.BN(jsonRawResponse.wallet.jlpBalance, "hex"),
|
|
120
|
-
jlpBalanceUi: Number(jsonRawResponse.wallet.jlpBalanceUi),
|
|
121
|
-
solBalance: new anchor_1.BN(jsonRawResponse.wallet.solBalance, "hex"),
|
|
122
|
-
solBalanceUi: Number(jsonRawResponse.wallet.solBalanceUi),
|
|
123
|
-
};
|
|
124
|
-
// if no clend account, return undefined for clend
|
|
125
|
-
if (!jsonRawResponse.clendAccount) {
|
|
126
|
-
return {
|
|
127
|
-
wallet,
|
|
128
|
-
clendAccount: undefined,
|
|
129
|
-
summary: [],
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
// parse lending account balances
|
|
133
|
-
const balances = [];
|
|
134
|
-
for (const b of jsonRawResponse.clendAccount.balances) {
|
|
135
|
-
balances.push({
|
|
115
|
+
// parse balances
|
|
116
|
+
const walletBalances = [];
|
|
117
|
+
for (const b of jsonRawResponse.wallet.balances) {
|
|
118
|
+
walletBalances.push({
|
|
136
119
|
mint: new anchor_1.web3.PublicKey(b.mint),
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
assetBalanceUi: Number(b.assetBalanceUi),
|
|
140
|
-
assetValue: Number(b.assetValue),
|
|
141
|
-
liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
|
|
142
|
-
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
143
|
-
liabilityValue: Number(b.liabilityValue),
|
|
144
|
-
price: Number(b.price),
|
|
120
|
+
balance: new anchor_1.BN(b.balance, "hex"),
|
|
121
|
+
balanceUi: Number(b.balanceUi),
|
|
145
122
|
});
|
|
146
123
|
}
|
|
147
|
-
//
|
|
148
|
-
const
|
|
149
|
-
balances,
|
|
150
|
-
netValue: Number(jsonRawResponse.clendAccount.netValue),
|
|
151
|
-
netApy: Number(jsonRawResponse.clendAccount.netApy),
|
|
152
|
-
pnl: Number(jsonRawResponse.clendAccount.pnl),
|
|
153
|
-
totalAssetValue: Number(jsonRawResponse.clendAccount.totalAssetValue),
|
|
154
|
-
totalLiabilityValue: Number(jsonRawResponse.clendAccount.totalLiabilityValue),
|
|
155
|
-
healthFactorNotional: Number(jsonRawResponse.clendAccount.healthFactorNotional),
|
|
156
|
-
healthFactorRiskAdjusted: Number(jsonRawResponse.clendAccount.healthFactorRiskAdjusted),
|
|
157
|
-
notionalLeverage: Number(jsonRawResponse.clendAccount.notionalLeverage),
|
|
158
|
-
riskAdjustedLeverage: Number(jsonRawResponse.clendAccount.riskAdjustedLeverage),
|
|
159
|
-
liquidationPrice: Number(jsonRawResponse.clendAccount.liquidationPrice),
|
|
160
|
-
liquidationPriceChangePercentage: Number(jsonRawResponse.clendAccount.liquidationPriceChangePercentage),
|
|
161
|
-
notionalLtv: Number(jsonRawResponse.clendAccount.notionalLtv),
|
|
162
|
-
riskAdjustedLtv: Number(jsonRawResponse.clendAccount.riskAdjustedLtv),
|
|
124
|
+
// get tokens still in user wallet
|
|
125
|
+
const wallet = {
|
|
126
|
+
balances: walletBalances,
|
|
163
127
|
};
|
|
164
|
-
// get tx summary for each account
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
selectedTokenMint: s.input.selectedTokenMint
|
|
182
|
-
? new anchor_1.web3.PublicKey(s.input.selectedTokenMint)
|
|
183
|
-
: null,
|
|
184
|
-
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
185
|
-
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
186
|
-
slippageBps: s.input.slippageBps ? Number(s.input.slippageBps) : null,
|
|
187
|
-
openPosition: s.input.openPosition
|
|
188
|
-
? Boolean(s.input.openPosition)
|
|
189
|
-
: null,
|
|
190
|
-
closePosition: s.input.closePosition
|
|
191
|
-
? Boolean(s.input.closePosition)
|
|
192
|
-
: null,
|
|
128
|
+
// get tx summary for each account and group them
|
|
129
|
+
const summaryByAccount = new Map();
|
|
130
|
+
if (request.getClendAccountSummary && jsonRawResponse.summary) {
|
|
131
|
+
for (const s of jsonRawResponse.summary) {
|
|
132
|
+
const accountAddress = new anchor_1.web3.PublicKey(s.clendAccount).toBase58();
|
|
133
|
+
if (!summaryByAccount.has(accountAddress)) {
|
|
134
|
+
summaryByAccount.set(accountAddress, []);
|
|
135
|
+
}
|
|
136
|
+
const txSummary = {
|
|
137
|
+
txSig: s.txSig,
|
|
138
|
+
time: s.time,
|
|
139
|
+
clendAccount: new anchor_1.web3.PublicKey(s.clendAccount),
|
|
140
|
+
clendAccountAuthority: new anchor_1.web3.PublicKey(s.clendAccountAuthority),
|
|
141
|
+
clendGroup: new anchor_1.web3.PublicKey(s.clendGroup),
|
|
142
|
+
action: s.action,
|
|
143
|
+
input: undefined,
|
|
144
|
+
events: [],
|
|
193
145
|
};
|
|
194
|
-
|
|
146
|
+
// parse inputs if they exist
|
|
147
|
+
if (s.input) {
|
|
148
|
+
const input = {
|
|
149
|
+
apiPath: s.input.apiPath,
|
|
150
|
+
selectedTokenMint: s.input.selectedTokenMint
|
|
151
|
+
? new anchor_1.web3.PublicKey(s.input.selectedTokenMint)
|
|
152
|
+
: null,
|
|
153
|
+
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
154
|
+
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
155
|
+
slippageBps: s.input.slippageBps
|
|
156
|
+
? Number(s.input.slippageBps)
|
|
157
|
+
: null,
|
|
158
|
+
openPosition: s.input.openPosition
|
|
159
|
+
? Boolean(s.input.openPosition)
|
|
160
|
+
: null,
|
|
161
|
+
closePosition: s.input.closePosition
|
|
162
|
+
? Boolean(s.input.closePosition)
|
|
163
|
+
: null,
|
|
164
|
+
};
|
|
165
|
+
txSummary.input = input;
|
|
166
|
+
}
|
|
167
|
+
// get events for each summary
|
|
168
|
+
for (const event of s.events) {
|
|
169
|
+
let closeBalance = null;
|
|
170
|
+
if (event.closeBalance !== null) {
|
|
171
|
+
closeBalance = Boolean(event.closeBalance);
|
|
172
|
+
}
|
|
173
|
+
// parse amount
|
|
174
|
+
const clendAccountEvent = {
|
|
175
|
+
eventIndex: event.eventIndex,
|
|
176
|
+
eventName: event.eventName,
|
|
177
|
+
bank: event.bank ? new anchor_1.web3.PublicKey(event.bank) : null,
|
|
178
|
+
mint: event.mint ? new anchor_1.web3.PublicKey(event.mint) : null,
|
|
179
|
+
amount: event.amount ? new anchor_1.BN(event.amount, "hex") : null,
|
|
180
|
+
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
181
|
+
value: event.value ? Number(event.value) : null,
|
|
182
|
+
price: event.price ? Number(event.price) : null,
|
|
183
|
+
closeBalance,
|
|
184
|
+
};
|
|
185
|
+
txSummary.events.push(clendAccountEvent);
|
|
186
|
+
}
|
|
187
|
+
summaryByAccount.get(accountAddress).push(txSummary);
|
|
195
188
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
189
|
+
}
|
|
190
|
+
// if no clend account, return undefined for clend
|
|
191
|
+
const clendAccounts = [];
|
|
192
|
+
if (jsonRawResponse.clendAccounts) {
|
|
193
|
+
for (const [address, ca] of Object.entries(jsonRawResponse.clendAccounts)) {
|
|
194
|
+
// parse lending account balances
|
|
195
|
+
const clendAccountBalances = [];
|
|
196
|
+
for (const b of ca.balances) {
|
|
197
|
+
clendAccountBalances.push({
|
|
198
|
+
mint: new anchor_1.web3.PublicKey(b.mint),
|
|
199
|
+
bank: new anchor_1.web3.PublicKey(b.bank),
|
|
200
|
+
assetBalance: new anchor_1.BN(b.assetBalance, "hex"),
|
|
201
|
+
assetBalanceUi: Number(b.assetBalanceUi),
|
|
202
|
+
assetValue: Number(b.assetValue),
|
|
203
|
+
liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
|
|
204
|
+
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
205
|
+
liabilityValue: Number(b.liabilityValue),
|
|
206
|
+
price: Number(b.price),
|
|
207
|
+
});
|
|
201
208
|
}
|
|
202
|
-
//
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
209
|
+
// create clend account
|
|
210
|
+
const clendAccount = {
|
|
211
|
+
balances: clendAccountBalances,
|
|
212
|
+
netValue: Number(ca.netValue),
|
|
213
|
+
netApy: Number(ca.netApy),
|
|
214
|
+
pnl: Number(ca.pnl),
|
|
215
|
+
totalAssetValue: Number(ca.totalAssetValue),
|
|
216
|
+
totalLiabilityValue: Number(ca.totalLiabilityValue),
|
|
217
|
+
healthFactorNotional: Number(ca.healthFactorNotional),
|
|
218
|
+
healthFactorRiskAdjusted: Number(ca.healthFactorRiskAdjusted),
|
|
219
|
+
notionalLeverage: Number(ca.notionalLeverage),
|
|
220
|
+
riskAdjustedLeverage: Number(ca.riskAdjustedLeverage),
|
|
221
|
+
liquidationPrice: Number(ca.liquidationPrice),
|
|
222
|
+
liquidationPriceChangePercentage: Number(ca.liquidationPriceChangePercentage),
|
|
223
|
+
notionalLtv: Number(ca.notionalLtv),
|
|
224
|
+
riskAdjustedLtv: Number(ca.riskAdjustedLtv),
|
|
213
225
|
};
|
|
214
|
-
|
|
226
|
+
const summary = summaryByAccount.get(address) || [];
|
|
227
|
+
clendAccounts.push({ clendAccount, summary });
|
|
215
228
|
}
|
|
216
|
-
summary.push(txSummary);
|
|
217
229
|
}
|
|
218
230
|
return {
|
|
219
231
|
wallet,
|
|
220
|
-
|
|
221
|
-
summary,
|
|
232
|
+
clendAccounts,
|
|
222
233
|
};
|
|
223
234
|
}
|
|
224
235
|
/**
|
|
@@ -302,11 +313,11 @@ class Client {
|
|
|
302
313
|
* @param request Withdraw leverage request parameters
|
|
303
314
|
* @returns Withdraw leverage operation result
|
|
304
315
|
*/
|
|
305
|
-
async withdrawLeverage(clendGroup,
|
|
316
|
+
async withdrawLeverage(clendGroup, outputTokenMint, assetTokenMint, liabilityTokenMint, uiAmount, slippageBps, withdrawAll) {
|
|
306
317
|
const req = {
|
|
307
318
|
owner: this.address(),
|
|
308
319
|
clendGroup,
|
|
309
|
-
|
|
320
|
+
outputTokenMint,
|
|
310
321
|
assetTokenMint,
|
|
311
322
|
liabilityTokenMint,
|
|
312
323
|
withdrawAmountUi: uiAmount,
|
package/dist/types.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface AdjustLeverageResponse {
|
|
|
47
47
|
export interface WithdrawLeverageRequest {
|
|
48
48
|
owner: web3.PublicKey;
|
|
49
49
|
clendGroup: web3.PublicKey;
|
|
50
|
-
|
|
50
|
+
outputTokenMint: web3.PublicKey;
|
|
51
51
|
assetTokenMint: web3.PublicKey;
|
|
52
52
|
liabilityTokenMint: web3.PublicKey;
|
|
53
53
|
withdrawAmountUi: number;
|
|
@@ -70,19 +70,21 @@ export interface GetUserRequest {
|
|
|
70
70
|
}
|
|
71
71
|
export interface GetUserResponse {
|
|
72
72
|
wallet: UserWallet;
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
clendAccounts: {
|
|
74
|
+
clendAccount: ClendAccount;
|
|
75
|
+
summary: ClendAccountTxSummary[];
|
|
76
|
+
}[];
|
|
75
77
|
}
|
|
76
78
|
export interface UserWallet {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
balances: UserBalance[];
|
|
80
|
+
}
|
|
81
|
+
export interface UserBalance {
|
|
82
|
+
mint: web3.PublicKey;
|
|
83
|
+
balance: BN;
|
|
84
|
+
balanceUi: number;
|
|
83
85
|
}
|
|
84
86
|
export interface ClendAccount {
|
|
85
|
-
balances:
|
|
87
|
+
balances: ClendAccountBalance[];
|
|
86
88
|
netValue: number;
|
|
87
89
|
netApy: number;
|
|
88
90
|
pnl: number;
|
|
@@ -97,7 +99,7 @@ export interface ClendAccount {
|
|
|
97
99
|
notionalLtv: number;
|
|
98
100
|
riskAdjustedLtv: number;
|
|
99
101
|
}
|
|
100
|
-
export interface
|
|
102
|
+
export interface ClendAccountBalance {
|
|
101
103
|
mint: web3.PublicKey;
|
|
102
104
|
bank: web3.PublicKey;
|
|
103
105
|
assetBalance: BN;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -12,13 +12,14 @@ import {
|
|
|
12
12
|
WithdrawLeverageResponse,
|
|
13
13
|
UserWallet,
|
|
14
14
|
ClendAccount,
|
|
15
|
-
|
|
15
|
+
ClendAccountBalance,
|
|
16
16
|
Bank,
|
|
17
17
|
GetGroupResponse,
|
|
18
18
|
ClendAccountEvent,
|
|
19
19
|
GetUserRequest,
|
|
20
20
|
ClendAccountTxSummary,
|
|
21
21
|
UserRequest,
|
|
22
|
+
UserBalance,
|
|
22
23
|
} from "./types";
|
|
23
24
|
import encode from "bs58";
|
|
24
25
|
|
|
@@ -118,131 +119,144 @@ export class Client {
|
|
|
118
119
|
|
|
119
120
|
const jsonRawResponse: any = JSON.parse(body);
|
|
120
121
|
|
|
121
|
-
//
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
jlpBalance: new BN(jsonRawResponse.wallet.jlpBalance, "hex"),
|
|
126
|
-
jlpBalanceUi: Number(jsonRawResponse.wallet.jlpBalanceUi),
|
|
127
|
-
solBalance: new BN(jsonRawResponse.wallet.solBalance, "hex"),
|
|
128
|
-
solBalanceUi: Number(jsonRawResponse.wallet.solBalanceUi),
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// if no clend account, return undefined for clend
|
|
132
|
-
if (!jsonRawResponse.clendAccount) {
|
|
133
|
-
return {
|
|
134
|
-
wallet,
|
|
135
|
-
clendAccount: undefined,
|
|
136
|
-
summary: [],
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// parse lending account balances
|
|
141
|
-
const balances: Balance[] = [];
|
|
142
|
-
for (const b of jsonRawResponse.clendAccount.balances) {
|
|
143
|
-
balances.push({
|
|
122
|
+
// parse balances
|
|
123
|
+
const walletBalances: UserBalance[] = [];
|
|
124
|
+
for (const b of jsonRawResponse.wallet.balances) {
|
|
125
|
+
walletBalances.push({
|
|
144
126
|
mint: new web3.PublicKey(b.mint),
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
assetBalanceUi: Number(b.assetBalanceUi),
|
|
148
|
-
assetValue: Number(b.assetValue),
|
|
149
|
-
liabilityBalance: new BN(b.liabilityBalance, "hex"),
|
|
150
|
-
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
151
|
-
liabilityValue: Number(b.liabilityValue),
|
|
152
|
-
price: Number(b.price),
|
|
127
|
+
balance: new BN(b.balance, "hex"),
|
|
128
|
+
balanceUi: Number(b.balanceUi),
|
|
153
129
|
});
|
|
154
130
|
}
|
|
155
131
|
|
|
156
|
-
//
|
|
157
|
-
const
|
|
158
|
-
balances,
|
|
159
|
-
netValue: Number(jsonRawResponse.clendAccount.netValue),
|
|
160
|
-
netApy: Number(jsonRawResponse.clendAccount.netApy),
|
|
161
|
-
pnl: Number(jsonRawResponse.clendAccount.pnl),
|
|
162
|
-
totalAssetValue: Number(jsonRawResponse.clendAccount.totalAssetValue),
|
|
163
|
-
totalLiabilityValue: Number(
|
|
164
|
-
jsonRawResponse.clendAccount.totalLiabilityValue,
|
|
165
|
-
),
|
|
166
|
-
healthFactorNotional: Number(
|
|
167
|
-
jsonRawResponse.clendAccount.healthFactorNotional,
|
|
168
|
-
),
|
|
169
|
-
healthFactorRiskAdjusted: Number(
|
|
170
|
-
jsonRawResponse.clendAccount.healthFactorRiskAdjusted,
|
|
171
|
-
),
|
|
172
|
-
notionalLeverage: Number(jsonRawResponse.clendAccount.notionalLeverage),
|
|
173
|
-
riskAdjustedLeverage: Number(
|
|
174
|
-
jsonRawResponse.clendAccount.riskAdjustedLeverage,
|
|
175
|
-
),
|
|
176
|
-
liquidationPrice: Number(jsonRawResponse.clendAccount.liquidationPrice),
|
|
177
|
-
liquidationPriceChangePercentage: Number(
|
|
178
|
-
jsonRawResponse.clendAccount.liquidationPriceChangePercentage,
|
|
179
|
-
),
|
|
180
|
-
notionalLtv: Number(jsonRawResponse.clendAccount.notionalLtv),
|
|
181
|
-
riskAdjustedLtv: Number(jsonRawResponse.clendAccount.riskAdjustedLtv),
|
|
132
|
+
// get tokens still in user wallet
|
|
133
|
+
const wallet: UserWallet = {
|
|
134
|
+
balances: walletBalances,
|
|
182
135
|
};
|
|
183
136
|
|
|
184
|
-
// get tx summary for each account
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
selectedTokenMint: s.input.selectedTokenMint
|
|
203
|
-
? new web3.PublicKey(s.input.selectedTokenMint)
|
|
204
|
-
: null,
|
|
205
|
-
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
206
|
-
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
207
|
-
slippageBps: s.input.slippageBps ? Number(s.input.slippageBps) : null,
|
|
208
|
-
openPosition: s.input.openPosition
|
|
209
|
-
? Boolean(s.input.openPosition)
|
|
210
|
-
: null,
|
|
211
|
-
closePosition: s.input.closePosition
|
|
212
|
-
? Boolean(s.input.closePosition)
|
|
213
|
-
: null,
|
|
137
|
+
// get tx summary for each account and group them
|
|
138
|
+
const summaryByAccount = new Map<string, ClendAccountTxSummary[]>();
|
|
139
|
+
if (request.getClendAccountSummary && jsonRawResponse.summary) {
|
|
140
|
+
for (const s of jsonRawResponse.summary) {
|
|
141
|
+
const accountAddress = new web3.PublicKey(s.clendAccount).toBase58();
|
|
142
|
+
if (!summaryByAccount.has(accountAddress)) {
|
|
143
|
+
summaryByAccount.set(accountAddress, []);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const txSummary: ClendAccountTxSummary = {
|
|
147
|
+
txSig: s.txSig,
|
|
148
|
+
time: s.time,
|
|
149
|
+
clendAccount: new web3.PublicKey(s.clendAccount),
|
|
150
|
+
clendAccountAuthority: new web3.PublicKey(s.clendAccountAuthority),
|
|
151
|
+
clendGroup: new web3.PublicKey(s.clendGroup),
|
|
152
|
+
action: s.action,
|
|
153
|
+
input: undefined,
|
|
154
|
+
events: [],
|
|
214
155
|
};
|
|
215
|
-
|
|
156
|
+
|
|
157
|
+
// parse inputs if they exist
|
|
158
|
+
if (s.input) {
|
|
159
|
+
const input: UserRequest = {
|
|
160
|
+
apiPath: s.input.apiPath,
|
|
161
|
+
selectedTokenMint: s.input.selectedTokenMint
|
|
162
|
+
? new web3.PublicKey(s.input.selectedTokenMint)
|
|
163
|
+
: null,
|
|
164
|
+
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
165
|
+
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
166
|
+
slippageBps: s.input.slippageBps
|
|
167
|
+
? Number(s.input.slippageBps)
|
|
168
|
+
: null,
|
|
169
|
+
openPosition: s.input.openPosition
|
|
170
|
+
? Boolean(s.input.openPosition)
|
|
171
|
+
: null,
|
|
172
|
+
closePosition: s.input.closePosition
|
|
173
|
+
? Boolean(s.input.closePosition)
|
|
174
|
+
: null,
|
|
175
|
+
};
|
|
176
|
+
txSummary.input = input;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// get events for each summary
|
|
180
|
+
for (const event of s.events) {
|
|
181
|
+
let closeBalance: boolean | null = null;
|
|
182
|
+
if (event.closeBalance !== null) {
|
|
183
|
+
closeBalance = Boolean(event.closeBalance);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// parse amount
|
|
187
|
+
const clendAccountEvent: ClendAccountEvent = {
|
|
188
|
+
eventIndex: event.eventIndex,
|
|
189
|
+
eventName: event.eventName,
|
|
190
|
+
bank: event.bank ? new web3.PublicKey(event.bank) : null,
|
|
191
|
+
mint: event.mint ? new web3.PublicKey(event.mint) : null,
|
|
192
|
+
amount: event.amount ? new BN(event.amount, "hex") : null,
|
|
193
|
+
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
194
|
+
value: event.value ? Number(event.value) : null,
|
|
195
|
+
price: event.price ? Number(event.price) : null,
|
|
196
|
+
closeBalance,
|
|
197
|
+
};
|
|
198
|
+
txSummary.events.push(clendAccountEvent);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
summaryByAccount.get(accountAddress)!.push(txSummary);
|
|
216
202
|
}
|
|
203
|
+
}
|
|
217
204
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
205
|
+
// if no clend account, return undefined for clend
|
|
206
|
+
const clendAccounts: {
|
|
207
|
+
clendAccount: ClendAccount;
|
|
208
|
+
summary: ClendAccountTxSummary[];
|
|
209
|
+
}[] = [];
|
|
210
|
+
if (jsonRawResponse.clendAccounts) {
|
|
211
|
+
for (const [address, ca] of Object.entries(
|
|
212
|
+
jsonRawResponse.clendAccounts,
|
|
213
|
+
)) {
|
|
214
|
+
// parse lending account balances
|
|
215
|
+
const clendAccountBalances: ClendAccountBalance[] = [];
|
|
216
|
+
for (const b of (ca as any).balances) {
|
|
217
|
+
clendAccountBalances.push({
|
|
218
|
+
mint: new web3.PublicKey(b.mint),
|
|
219
|
+
bank: new web3.PublicKey(b.bank),
|
|
220
|
+
assetBalance: new BN(b.assetBalance, "hex"),
|
|
221
|
+
assetBalanceUi: Number(b.assetBalanceUi),
|
|
222
|
+
assetValue: Number(b.assetValue),
|
|
223
|
+
liabilityBalance: new BN(b.liabilityBalance, "hex"),
|
|
224
|
+
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
225
|
+
liabilityValue: Number(b.liabilityValue),
|
|
226
|
+
price: Number(b.price),
|
|
227
|
+
});
|
|
223
228
|
}
|
|
224
229
|
|
|
225
|
-
//
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
230
|
+
// create clend account
|
|
231
|
+
const clendAccount: ClendAccount = {
|
|
232
|
+
balances: clendAccountBalances,
|
|
233
|
+
netValue: Number((ca as any).netValue),
|
|
234
|
+
netApy: Number((ca as any).netApy),
|
|
235
|
+
pnl: Number((ca as any).pnl),
|
|
236
|
+
totalAssetValue: Number((ca as any).totalAssetValue),
|
|
237
|
+
totalLiabilityValue: Number((ca as any).totalLiabilityValue),
|
|
238
|
+
healthFactorNotional: Number((ca as any).healthFactorNotional),
|
|
239
|
+
healthFactorRiskAdjusted: Number(
|
|
240
|
+
(ca as any).healthFactorRiskAdjusted,
|
|
241
|
+
),
|
|
242
|
+
notionalLeverage: Number((ca as any).notionalLeverage),
|
|
243
|
+
riskAdjustedLeverage: Number((ca as any).riskAdjustedLeverage),
|
|
244
|
+
liquidationPrice: Number((ca as any).liquidationPrice),
|
|
245
|
+
liquidationPriceChangePercentage: Number(
|
|
246
|
+
(ca as any).liquidationPriceChangePercentage,
|
|
247
|
+
),
|
|
248
|
+
notionalLtv: Number((ca as any).notionalLtv),
|
|
249
|
+
riskAdjustedLtv: Number((ca as any).riskAdjustedLtv),
|
|
236
250
|
};
|
|
237
|
-
|
|
251
|
+
|
|
252
|
+
const summary = summaryByAccount.get(address) || [];
|
|
253
|
+
clendAccounts.push({ clendAccount, summary });
|
|
238
254
|
}
|
|
239
|
-
summary.push(txSummary);
|
|
240
255
|
}
|
|
241
256
|
|
|
242
257
|
return {
|
|
243
258
|
wallet,
|
|
244
|
-
|
|
245
|
-
summary,
|
|
259
|
+
clendAccounts,
|
|
246
260
|
};
|
|
247
261
|
}
|
|
248
262
|
|
|
@@ -378,7 +392,7 @@ export class Client {
|
|
|
378
392
|
*/
|
|
379
393
|
async withdrawLeverage(
|
|
380
394
|
clendGroup: web3.PublicKey,
|
|
381
|
-
|
|
395
|
+
outputTokenMint: web3.PublicKey,
|
|
382
396
|
assetTokenMint: web3.PublicKey,
|
|
383
397
|
liabilityTokenMint: web3.PublicKey,
|
|
384
398
|
uiAmount: number,
|
|
@@ -388,7 +402,7 @@ export class Client {
|
|
|
388
402
|
const req: WithdrawLeverageRequest = {
|
|
389
403
|
owner: this.address(),
|
|
390
404
|
clendGroup,
|
|
391
|
-
|
|
405
|
+
outputTokenMint,
|
|
392
406
|
assetTokenMint,
|
|
393
407
|
liabilityTokenMint,
|
|
394
408
|
withdrawAmountUi: uiAmount,
|
package/src/types.ts
CHANGED
|
@@ -54,7 +54,7 @@ export interface AdjustLeverageResponse {
|
|
|
54
54
|
export interface WithdrawLeverageRequest {
|
|
55
55
|
owner: web3.PublicKey;
|
|
56
56
|
clendGroup: web3.PublicKey;
|
|
57
|
-
|
|
57
|
+
outputTokenMint: web3.PublicKey;
|
|
58
58
|
assetTokenMint: web3.PublicKey;
|
|
59
59
|
liabilityTokenMint: web3.PublicKey;
|
|
60
60
|
withdrawAmountUi: number;
|
|
@@ -81,21 +81,24 @@ export interface GetUserRequest {
|
|
|
81
81
|
|
|
82
82
|
export interface GetUserResponse {
|
|
83
83
|
wallet: UserWallet;
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
clendAccounts: {
|
|
85
|
+
clendAccount: ClendAccount;
|
|
86
|
+
summary: ClendAccountTxSummary[];
|
|
87
|
+
}[];
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
export interface UserWallet {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
balances: UserBalance[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface UserBalance {
|
|
95
|
+
mint: web3.PublicKey;
|
|
96
|
+
balance: BN;
|
|
97
|
+
balanceUi: number;
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
export interface ClendAccount {
|
|
98
|
-
balances:
|
|
101
|
+
balances: ClendAccountBalance[];
|
|
99
102
|
netValue: number;
|
|
100
103
|
netApy: number;
|
|
101
104
|
pnl: number;
|
|
@@ -111,7 +114,7 @@ export interface ClendAccount {
|
|
|
111
114
|
riskAdjustedLtv: number;
|
|
112
115
|
}
|
|
113
116
|
|
|
114
|
-
export interface
|
|
117
|
+
export interface ClendAccountBalance {
|
|
115
118
|
mint: web3.PublicKey;
|
|
116
119
|
bank: web3.PublicKey;
|
|
117
120
|
assetBalance: BN;
|