@carrot-protocol/boost-http-client 0.2.16-token22-dev-158437a → 0.3.0-feat-get-account-endpoint-dev-427172d
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 +10 -3
- package/dist/index.js +115 -131
- package/dist/types.d.ts +8 -27
- package/package.json +1 -1
- package/src/index.ts +153 -190
- package/src/types.ts +10 -31
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnchorProvider, web3 } from "@coral-xyz/anchor";
|
|
2
|
-
import { GetBankResponse, GetUserResponse, GetGroupResponse, GetGroupsResponse } from "./types";
|
|
2
|
+
import { GetBankResponse, GetUserResponse, GetGroupResponse, GetGroupsResponse, GetAccountResponse, GetAccountRequest } from "./types";
|
|
3
3
|
export * from "./types";
|
|
4
4
|
export * from "./utils";
|
|
5
5
|
export * as Common from "@carrot-protocol/clend-common";
|
|
@@ -22,6 +22,15 @@ export declare class Client {
|
|
|
22
22
|
* @returns Index details
|
|
23
23
|
*/
|
|
24
24
|
index(): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Fetch and parse a single clend account by pubkey
|
|
27
|
+
*/
|
|
28
|
+
getAccount(account: GetAccountRequest["account"], getClendAccountSummary: GetAccountRequest["getClendAccountSummary"]): Promise<GetAccountResponse>;
|
|
29
|
+
/**
|
|
30
|
+
* Helper to parse a clend account summary array from API response
|
|
31
|
+
*/
|
|
32
|
+
private parseClendAccountSummary;
|
|
33
|
+
private parseClendAccountWithSummary;
|
|
25
34
|
getUser(user: web3.PublicKey, groups: web3.PublicKey[], getClendAccountSummary: boolean): Promise<GetUserResponse>;
|
|
26
35
|
/**
|
|
27
36
|
* Get all groups
|
|
@@ -41,8 +50,6 @@ export declare class Client {
|
|
|
41
50
|
* @returns Bank details
|
|
42
51
|
*/
|
|
43
52
|
getBank(bankAddress: web3.PublicKey): Promise<GetBankResponse>;
|
|
44
|
-
deposit(clendGroup: web3.PublicKey, clendAccount: web3.PublicKey | null, inputTokenMint: web3.PublicKey, uiAmount: number): Promise<string>;
|
|
45
|
-
withdraw(clendAccount: web3.PublicKey, outputTokenMint: web3.PublicKey, uiAmount: number, withdrawAll: boolean): Promise<string>;
|
|
46
53
|
/**
|
|
47
54
|
* Deposit collateral and create a leveraged position
|
|
48
55
|
* @param request Deposit leverage request parameters
|
package/dist/index.js
CHANGED
|
@@ -99,6 +99,120 @@ class Client {
|
|
|
99
99
|
async index() {
|
|
100
100
|
return handleApiCall(() => this.http.get(""));
|
|
101
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Fetch and parse a single clend account by pubkey
|
|
104
|
+
*/
|
|
105
|
+
async getAccount(account, getClendAccountSummary) {
|
|
106
|
+
const body = await handleApiCall(() => this.http.get(`/account?account=${account.toString()}&getClendAccountSummary=${getClendAccountSummary}`));
|
|
107
|
+
const jsonRawResponse = JSON.parse(body);
|
|
108
|
+
return this.parseClendAccountWithSummary(jsonRawResponse, getClendAccountSummary);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Helper to parse a clend account summary array from API response
|
|
112
|
+
*/
|
|
113
|
+
parseClendAccountSummary(summaryArr) {
|
|
114
|
+
return summaryArr.map((s) => {
|
|
115
|
+
let input = undefined;
|
|
116
|
+
if (s.input) {
|
|
117
|
+
input = {
|
|
118
|
+
apiPath: s.input.apiPath,
|
|
119
|
+
selectedTokenMint: s.input.selectedTokenMint
|
|
120
|
+
? new anchor_1.web3.PublicKey(s.input.selectedTokenMint)
|
|
121
|
+
: null,
|
|
122
|
+
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
123
|
+
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
124
|
+
slippageBps: s.input.slippageBps ? Number(s.input.slippageBps) : null,
|
|
125
|
+
openPosition: s.input.openPosition != null ? Boolean(s.input.openPosition) : null,
|
|
126
|
+
closePosition: s.input.closePosition != null
|
|
127
|
+
? Boolean(s.input.closePosition)
|
|
128
|
+
: null,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const events = (s.events || []).map((event) => {
|
|
132
|
+
let closeBalance = null;
|
|
133
|
+
if (event.closeBalance !== null && event.closeBalance !== undefined) {
|
|
134
|
+
closeBalance = Boolean(event.closeBalance);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
eventIndex: event.eventIndex,
|
|
138
|
+
eventName: event.eventName,
|
|
139
|
+
bank: event.bank ? new anchor_1.web3.PublicKey(event.bank) : null,
|
|
140
|
+
mint: event.mint ? new anchor_1.web3.PublicKey(event.mint) : null,
|
|
141
|
+
amount: event.amount ? new anchor_1.BN(event.amount, "hex") : null,
|
|
142
|
+
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
143
|
+
value: event.value ? Number(event.value) : null,
|
|
144
|
+
price: event.price ? Number(event.price) : null,
|
|
145
|
+
closeBalance,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
const txSummary = {
|
|
149
|
+
txSig: s.txSig,
|
|
150
|
+
time: s.time,
|
|
151
|
+
clendAccount: new anchor_1.web3.PublicKey(s.clendAccount),
|
|
152
|
+
clendAccountAuthority: new anchor_1.web3.PublicKey(s.clendAccountAuthority),
|
|
153
|
+
clendGroup: new anchor_1.web3.PublicKey(s.clendGroup),
|
|
154
|
+
action: s.action,
|
|
155
|
+
input,
|
|
156
|
+
events,
|
|
157
|
+
};
|
|
158
|
+
return txSummary;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
parseClendAccountWithSummary(accountData, getClendAccountSummary) {
|
|
162
|
+
// A. Parse the main ClendAccount object
|
|
163
|
+
const rawClendAccount = accountData.clendAccount;
|
|
164
|
+
const clendAccountBalances = (rawClendAccount.balances || []).map((b) => {
|
|
165
|
+
const liquidation = b.liquidation
|
|
166
|
+
? {
|
|
167
|
+
price: Number(b.liquidation.price),
|
|
168
|
+
changePercentage: Number(b.liquidation.changePercentage),
|
|
169
|
+
}
|
|
170
|
+
: null;
|
|
171
|
+
const emissionsOutstandingAndUnclaimed = new anchor_1.BN(b.emissionsOutstandingAndUnclaimed, "hex");
|
|
172
|
+
return {
|
|
173
|
+
mint: new anchor_1.web3.PublicKey(b.mint),
|
|
174
|
+
bank: new anchor_1.web3.PublicKey(b.bank),
|
|
175
|
+
tokenYieldApy: Number(b.tokenYieldApy),
|
|
176
|
+
assetBalance: new anchor_1.BN(b.assetBalance, "hex"),
|
|
177
|
+
assetBalanceUi: Number(b.assetBalanceUi),
|
|
178
|
+
assetValue: Number(b.assetValue),
|
|
179
|
+
assetEmissionsApy: Number(b.assetEmissionsApy),
|
|
180
|
+
liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
|
|
181
|
+
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
182
|
+
liabilityValue: Number(b.liabilityValue),
|
|
183
|
+
liabilityBorrowCostApy: Number(b.liabilityBorrowCostApy),
|
|
184
|
+
liabilityEmissionsApy: Number(b.liabilityEmissionsApy),
|
|
185
|
+
emissionsOutstandingAndUnclaimed,
|
|
186
|
+
emissionsOutstandingAndUnclaimedUi: Number(b.emissionsOutstandingAndUnclaimedUi),
|
|
187
|
+
price: Number(b.price),
|
|
188
|
+
liquidation,
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
const clendAccount = {
|
|
192
|
+
key: new anchor_1.web3.PublicKey(rawClendAccount.key),
|
|
193
|
+
group: new anchor_1.web3.PublicKey(rawClendAccount.group),
|
|
194
|
+
balances: clendAccountBalances,
|
|
195
|
+
netValue: Number(rawClendAccount.netValue),
|
|
196
|
+
netApy: Number(rawClendAccount.netApy),
|
|
197
|
+
pnl: Number(rawClendAccount.pnl),
|
|
198
|
+
totalEmissionsApy: Number(rawClendAccount.totalEmissionsApy),
|
|
199
|
+
totalAssetValue: Number(rawClendAccount.totalAssetValue),
|
|
200
|
+
totalLiabilityValue: Number(rawClendAccount.totalLiabilityValue),
|
|
201
|
+
healthFactorNotional: Number(rawClendAccount.healthFactorNotional),
|
|
202
|
+
healthFactorRiskAdjusted: Number(rawClendAccount.healthFactorRiskAdjusted),
|
|
203
|
+
notionalLeverage: Number(rawClendAccount.notionalLeverage),
|
|
204
|
+
riskAdjustedLeverage: Number(rawClendAccount.riskAdjustedLeverage),
|
|
205
|
+
notionalLtv: Number(rawClendAccount.notionalLtv),
|
|
206
|
+
riskAdjustedLtv: Number(rawClendAccount.riskAdjustedLtv),
|
|
207
|
+
};
|
|
208
|
+
// B. Parse the associated Summary array for the account
|
|
209
|
+
let summary = [];
|
|
210
|
+
if (getClendAccountSummary && accountData.summary) {
|
|
211
|
+
summary = this.parseClendAccountSummary(accountData.summary);
|
|
212
|
+
}
|
|
213
|
+
// C. Combine the parsed account and its summary
|
|
214
|
+
return { clendAccount, summary };
|
|
215
|
+
}
|
|
102
216
|
async getUser(user, groups, getClendAccountSummary) {
|
|
103
217
|
// Make the API call to fetch the raw user data as a string.
|
|
104
218
|
const body = await handleApiCall(() => this.http.get(`/user?user=${user.toString()}&groups=${groups.map((g) => g.toString()).join(",")}&getClendAccountSummary=${getClendAccountSummary}`));
|
|
@@ -117,112 +231,7 @@ class Client {
|
|
|
117
231
|
// 2. Parse Clend Accounts and their Summaries
|
|
118
232
|
// This is the main refactored section. It iterates through the clendAccounts array
|
|
119
233
|
// from the response, which contains both the account data and its summary.
|
|
120
|
-
const clendAccounts = (jsonRawResponse.clendAccounts || []).map((accountData) =>
|
|
121
|
-
// A. Parse the main ClendAccount object
|
|
122
|
-
const rawClendAccount = accountData.clendAccount;
|
|
123
|
-
const clendAccountBalances = (rawClendAccount.balances || []).map((b) => {
|
|
124
|
-
const liquidation = b.liquidation
|
|
125
|
-
? {
|
|
126
|
-
price: Number(b.liquidation.price),
|
|
127
|
-
changePercentage: Number(b.liquidation.changePercentage),
|
|
128
|
-
}
|
|
129
|
-
: null;
|
|
130
|
-
const emissionsOutstandingAndUnclaimed = new anchor_1.BN(b.emissionsOutstandingAndUnclaimed, "hex");
|
|
131
|
-
return {
|
|
132
|
-
mint: new anchor_1.web3.PublicKey(b.mint),
|
|
133
|
-
bank: new anchor_1.web3.PublicKey(b.bank),
|
|
134
|
-
tokenYieldApy: Number(b.tokenYieldApy),
|
|
135
|
-
assetBalance: new anchor_1.BN(b.assetBalance, "hex"),
|
|
136
|
-
assetBalanceUi: Number(b.assetBalanceUi),
|
|
137
|
-
assetValue: Number(b.assetValue),
|
|
138
|
-
assetEmissionsApy: Number(b.assetEmissionsApy),
|
|
139
|
-
liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
|
|
140
|
-
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
141
|
-
liabilityValue: Number(b.liabilityValue),
|
|
142
|
-
liabilityBorrowCostApy: Number(b.liabilityBorrowCostApy),
|
|
143
|
-
liabilityEmissionsApy: Number(b.liabilityEmissionsApy),
|
|
144
|
-
emissionsOutstandingAndUnclaimed,
|
|
145
|
-
emissionsOutstandingAndUnclaimedUi: Number(b.emissionsOutstandingAndUnclaimedUi),
|
|
146
|
-
price: Number(b.price),
|
|
147
|
-
liquidation,
|
|
148
|
-
};
|
|
149
|
-
});
|
|
150
|
-
const clendAccount = {
|
|
151
|
-
key: new anchor_1.web3.PublicKey(rawClendAccount.key),
|
|
152
|
-
group: new anchor_1.web3.PublicKey(rawClendAccount.group),
|
|
153
|
-
balances: clendAccountBalances,
|
|
154
|
-
netValue: Number(rawClendAccount.netValue),
|
|
155
|
-
netApy: Number(rawClendAccount.netApy),
|
|
156
|
-
pnl: Number(rawClendAccount.pnl),
|
|
157
|
-
totalEmissionsApy: Number(rawClendAccount.totalEmissionsApy),
|
|
158
|
-
totalAssetValue: Number(rawClendAccount.totalAssetValue),
|
|
159
|
-
totalLiabilityValue: Number(rawClendAccount.totalLiabilityValue),
|
|
160
|
-
healthFactorNotional: Number(rawClendAccount.healthFactorNotional),
|
|
161
|
-
healthFactorRiskAdjusted: Number(rawClendAccount.healthFactorRiskAdjusted),
|
|
162
|
-
notionalLeverage: Number(rawClendAccount.notionalLeverage),
|
|
163
|
-
riskAdjustedLeverage: Number(rawClendAccount.riskAdjustedLeverage),
|
|
164
|
-
notionalLtv: Number(rawClendAccount.notionalLtv),
|
|
165
|
-
riskAdjustedLtv: Number(rawClendAccount.riskAdjustedLtv),
|
|
166
|
-
};
|
|
167
|
-
// B. Parse the associated Summary array for the account
|
|
168
|
-
let summary = [];
|
|
169
|
-
if (getClendAccountSummary && accountData.summary) {
|
|
170
|
-
summary = accountData.summary.map((s) => {
|
|
171
|
-
let input = undefined;
|
|
172
|
-
if (s.input) {
|
|
173
|
-
input = {
|
|
174
|
-
apiPath: s.input.apiPath,
|
|
175
|
-
selectedTokenMint: s.input.selectedTokenMint
|
|
176
|
-
? new anchor_1.web3.PublicKey(s.input.selectedTokenMint)
|
|
177
|
-
: null,
|
|
178
|
-
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
179
|
-
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
180
|
-
slippageBps: s.input.slippageBps
|
|
181
|
-
? Number(s.input.slippageBps)
|
|
182
|
-
: null,
|
|
183
|
-
openPosition: s.input.openPosition != null
|
|
184
|
-
? Boolean(s.input.openPosition)
|
|
185
|
-
: null,
|
|
186
|
-
closePosition: s.input.closePosition != null
|
|
187
|
-
? Boolean(s.input.closePosition)
|
|
188
|
-
: null,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
const events = (s.events || []).map((event) => {
|
|
192
|
-
let closeBalance = null;
|
|
193
|
-
// Only convert to boolean if the value is not null/undefined to preserve the null state.
|
|
194
|
-
if (event.closeBalance !== null &&
|
|
195
|
-
event.closeBalance !== undefined) {
|
|
196
|
-
closeBalance = Boolean(event.closeBalance);
|
|
197
|
-
}
|
|
198
|
-
return {
|
|
199
|
-
eventIndex: event.eventIndex,
|
|
200
|
-
eventName: event.eventName,
|
|
201
|
-
bank: event.bank ? new anchor_1.web3.PublicKey(event.bank) : null,
|
|
202
|
-
mint: event.mint ? new anchor_1.web3.PublicKey(event.mint) : null,
|
|
203
|
-
amount: event.amount ? new anchor_1.BN(event.amount, "hex") : null,
|
|
204
|
-
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
205
|
-
value: event.value ? Number(event.value) : null,
|
|
206
|
-
price: event.price ? Number(event.price) : null,
|
|
207
|
-
closeBalance,
|
|
208
|
-
};
|
|
209
|
-
});
|
|
210
|
-
const txSummary = {
|
|
211
|
-
txSig: s.txSig,
|
|
212
|
-
time: s.time,
|
|
213
|
-
clendAccount: new anchor_1.web3.PublicKey(s.clendAccount),
|
|
214
|
-
clendAccountAuthority: new anchor_1.web3.PublicKey(s.clendAccountAuthority),
|
|
215
|
-
clendGroup: new anchor_1.web3.PublicKey(s.clendGroup),
|
|
216
|
-
action: s.action,
|
|
217
|
-
input,
|
|
218
|
-
events,
|
|
219
|
-
};
|
|
220
|
-
return txSummary;
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
// C. Combine the parsed account and its summary
|
|
224
|
-
return { clendAccount, summary };
|
|
225
|
-
});
|
|
234
|
+
const clendAccounts = (jsonRawResponse.clendAccounts || []).map((accountData) => this.parseClendAccountWithSummary(accountData, getClendAccountSummary));
|
|
226
235
|
// 3. Return the final, correctly structured response object.
|
|
227
236
|
return {
|
|
228
237
|
wallet,
|
|
@@ -289,31 +298,6 @@ class Client {
|
|
|
289
298
|
};
|
|
290
299
|
return response;
|
|
291
300
|
}
|
|
292
|
-
async deposit(clendGroup, clendAccount, inputTokenMint, uiAmount) {
|
|
293
|
-
const req = {
|
|
294
|
-
owner: this.address(),
|
|
295
|
-
clendGroup,
|
|
296
|
-
clendAccount,
|
|
297
|
-
inputTokenMint,
|
|
298
|
-
depositAmountUi: uiAmount,
|
|
299
|
-
};
|
|
300
|
-
const body = await handleApiCall(() => this.http.post("deposit", JSON.stringify(req)));
|
|
301
|
-
const depositResponse = JSON.parse(body);
|
|
302
|
-
const txSig = await this.send(depositResponse.unsignedBase64Tx, depositResponse.userRequestId);
|
|
303
|
-
return txSig;
|
|
304
|
-
}
|
|
305
|
-
async withdraw(clendAccount, outputTokenMint, uiAmount, withdrawAll) {
|
|
306
|
-
const req = {
|
|
307
|
-
clendAccount,
|
|
308
|
-
outputTokenMint,
|
|
309
|
-
withdrawAmountUi: uiAmount,
|
|
310
|
-
withdrawAll,
|
|
311
|
-
};
|
|
312
|
-
const body = await handleApiCall(() => this.http.post("withdraw", JSON.stringify(req)));
|
|
313
|
-
const withdrawResponse = JSON.parse(body);
|
|
314
|
-
const txSig = await this.send(withdrawResponse.unsignedBase64Tx, withdrawResponse.userRequestId);
|
|
315
|
-
return txSig;
|
|
316
|
-
}
|
|
317
301
|
/**
|
|
318
302
|
* Deposit collateral and create a leveraged position
|
|
319
303
|
* @param request Deposit leverage request parameters
|
package/dist/types.d.ts
CHANGED
|
@@ -3,33 +3,6 @@ export interface SendRequest {
|
|
|
3
3
|
userRequestId: string;
|
|
4
4
|
txns: string[];
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
7
|
-
* Request to deposit collateral
|
|
8
|
-
*/
|
|
9
|
-
export interface DepositRequest {
|
|
10
|
-
owner: web3.PublicKey;
|
|
11
|
-
clendGroup: web3.PublicKey;
|
|
12
|
-
clendAccount: web3.PublicKey | null;
|
|
13
|
-
inputTokenMint: web3.PublicKey;
|
|
14
|
-
depositAmountUi: number;
|
|
15
|
-
}
|
|
16
|
-
export interface DepositResponse {
|
|
17
|
-
userRequestId: string;
|
|
18
|
-
unsignedBase64Tx: string;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Request to withdraw collateral
|
|
22
|
-
*/
|
|
23
|
-
export interface WithdrawRequest {
|
|
24
|
-
clendAccount: web3.PublicKey;
|
|
25
|
-
outputTokenMint: web3.PublicKey;
|
|
26
|
-
withdrawAmountUi: number;
|
|
27
|
-
withdrawAll: boolean;
|
|
28
|
-
}
|
|
29
|
-
export interface WithdrawResponse {
|
|
30
|
-
userRequestId: string;
|
|
31
|
-
unsignedBase64Tx: string;
|
|
32
|
-
}
|
|
33
6
|
/**
|
|
34
7
|
* Request to deposit collateral and create a leveraged position
|
|
35
8
|
*/
|
|
@@ -111,6 +84,14 @@ export interface GroupAndBanks {
|
|
|
111
84
|
groupName: string;
|
|
112
85
|
banks: Bank[];
|
|
113
86
|
}
|
|
87
|
+
export interface GetAccountRequest {
|
|
88
|
+
account: web3.PublicKey;
|
|
89
|
+
getClendAccountSummary: boolean;
|
|
90
|
+
}
|
|
91
|
+
export interface GetAccountResponse {
|
|
92
|
+
clendAccount: ClendAccount;
|
|
93
|
+
summary: ClendAccountTxSummary[];
|
|
94
|
+
}
|
|
114
95
|
export interface GetUserRequest {
|
|
115
96
|
groups: web3.PublicKey[];
|
|
116
97
|
user: web3.PublicKey;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -27,10 +27,8 @@ import {
|
|
|
27
27
|
BankEmissionsMode,
|
|
28
28
|
WithdrawEmissionsRequest,
|
|
29
29
|
WithdrawEmissionsResponse,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
WithdrawRequest,
|
|
33
|
-
WithdrawResponse,
|
|
30
|
+
GetAccountResponse,
|
|
31
|
+
GetAccountRequest,
|
|
34
32
|
} from "./types";
|
|
35
33
|
import encode from "bs58";
|
|
36
34
|
|
|
@@ -110,6 +108,155 @@ export class Client {
|
|
|
110
108
|
return handleApiCall(() => this.http.get(""));
|
|
111
109
|
}
|
|
112
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Fetch and parse a single clend account by pubkey
|
|
113
|
+
*/
|
|
114
|
+
async getAccount(
|
|
115
|
+
account: GetAccountRequest["account"],
|
|
116
|
+
getClendAccountSummary: GetAccountRequest["getClendAccountSummary"],
|
|
117
|
+
): Promise<GetAccountResponse> {
|
|
118
|
+
const body = await handleApiCall(() =>
|
|
119
|
+
this.http.get(
|
|
120
|
+
`/account?account=${account.toString()}&getClendAccountSummary=${getClendAccountSummary}`,
|
|
121
|
+
),
|
|
122
|
+
);
|
|
123
|
+
const jsonRawResponse: any = JSON.parse(body);
|
|
124
|
+
return this.parseClendAccountWithSummary(
|
|
125
|
+
jsonRawResponse,
|
|
126
|
+
getClendAccountSummary,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Helper to parse a clend account summary array from API response
|
|
132
|
+
*/
|
|
133
|
+
private parseClendAccountSummary(summaryArr: any[]): ClendAccountTxSummary[] {
|
|
134
|
+
return summaryArr.map((s: any) => {
|
|
135
|
+
let input: UserRequest | undefined = undefined;
|
|
136
|
+
if (s.input) {
|
|
137
|
+
input = {
|
|
138
|
+
apiPath: s.input.apiPath,
|
|
139
|
+
selectedTokenMint: s.input.selectedTokenMint
|
|
140
|
+
? new web3.PublicKey(s.input.selectedTokenMint)
|
|
141
|
+
: null,
|
|
142
|
+
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
143
|
+
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
144
|
+
slippageBps: s.input.slippageBps ? Number(s.input.slippageBps) : null,
|
|
145
|
+
openPosition:
|
|
146
|
+
s.input.openPosition != null ? Boolean(s.input.openPosition) : null,
|
|
147
|
+
closePosition:
|
|
148
|
+
s.input.closePosition != null
|
|
149
|
+
? Boolean(s.input.closePosition)
|
|
150
|
+
: null,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const events: ClendAccountEvent[] = (s.events || []).map((event: any) => {
|
|
155
|
+
let closeBalance: boolean | null = null;
|
|
156
|
+
if (event.closeBalance !== null && event.closeBalance !== undefined) {
|
|
157
|
+
closeBalance = Boolean(event.closeBalance);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
eventIndex: event.eventIndex,
|
|
162
|
+
eventName: event.eventName,
|
|
163
|
+
bank: event.bank ? new web3.PublicKey(event.bank) : null,
|
|
164
|
+
mint: event.mint ? new web3.PublicKey(event.mint) : null,
|
|
165
|
+
amount: event.amount ? new BN(event.amount, "hex") : null,
|
|
166
|
+
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
167
|
+
value: event.value ? Number(event.value) : null,
|
|
168
|
+
price: event.price ? Number(event.price) : null,
|
|
169
|
+
closeBalance,
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const txSummary: ClendAccountTxSummary = {
|
|
174
|
+
txSig: s.txSig,
|
|
175
|
+
time: s.time,
|
|
176
|
+
clendAccount: new web3.PublicKey(s.clendAccount),
|
|
177
|
+
clendAccountAuthority: new web3.PublicKey(s.clendAccountAuthority),
|
|
178
|
+
clendGroup: new web3.PublicKey(s.clendGroup),
|
|
179
|
+
action: s.action,
|
|
180
|
+
input,
|
|
181
|
+
events,
|
|
182
|
+
};
|
|
183
|
+
return txSummary;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private parseClendAccountWithSummary(
|
|
188
|
+
accountData: any,
|
|
189
|
+
getClendAccountSummary: boolean,
|
|
190
|
+
): GetAccountResponse {
|
|
191
|
+
// A. Parse the main ClendAccount object
|
|
192
|
+
const rawClendAccount = accountData.clendAccount;
|
|
193
|
+
const clendAccountBalances: ClendAccountBalance[] = (
|
|
194
|
+
rawClendAccount.balances || []
|
|
195
|
+
).map((b: any) => {
|
|
196
|
+
const liquidation: ClendAccountAssetLiquidation | null = b.liquidation
|
|
197
|
+
? {
|
|
198
|
+
price: Number(b.liquidation.price),
|
|
199
|
+
changePercentage: Number(b.liquidation.changePercentage),
|
|
200
|
+
}
|
|
201
|
+
: null;
|
|
202
|
+
|
|
203
|
+
const emissionsOutstandingAndUnclaimed = new BN(
|
|
204
|
+
b.emissionsOutstandingAndUnclaimed,
|
|
205
|
+
"hex",
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
mint: new web3.PublicKey(b.mint),
|
|
210
|
+
bank: new web3.PublicKey(b.bank),
|
|
211
|
+
tokenYieldApy: Number(b.tokenYieldApy),
|
|
212
|
+
assetBalance: new BN(b.assetBalance, "hex"),
|
|
213
|
+
assetBalanceUi: Number(b.assetBalanceUi),
|
|
214
|
+
assetValue: Number(b.assetValue),
|
|
215
|
+
assetEmissionsApy: Number(b.assetEmissionsApy),
|
|
216
|
+
liabilityBalance: new BN(b.liabilityBalance, "hex"),
|
|
217
|
+
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
218
|
+
liabilityValue: Number(b.liabilityValue),
|
|
219
|
+
liabilityBorrowCostApy: Number(b.liabilityBorrowCostApy),
|
|
220
|
+
liabilityEmissionsApy: Number(b.liabilityEmissionsApy),
|
|
221
|
+
emissionsOutstandingAndUnclaimed,
|
|
222
|
+
emissionsOutstandingAndUnclaimedUi: Number(
|
|
223
|
+
b.emissionsOutstandingAndUnclaimedUi,
|
|
224
|
+
),
|
|
225
|
+
price: Number(b.price),
|
|
226
|
+
liquidation,
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const clendAccount: ClendAccount = {
|
|
231
|
+
key: new web3.PublicKey(rawClendAccount.key),
|
|
232
|
+
group: new web3.PublicKey(rawClendAccount.group),
|
|
233
|
+
balances: clendAccountBalances,
|
|
234
|
+
netValue: Number(rawClendAccount.netValue),
|
|
235
|
+
netApy: Number(rawClendAccount.netApy),
|
|
236
|
+
pnl: Number(rawClendAccount.pnl),
|
|
237
|
+
totalEmissionsApy: Number(rawClendAccount.totalEmissionsApy),
|
|
238
|
+
totalAssetValue: Number(rawClendAccount.totalAssetValue),
|
|
239
|
+
totalLiabilityValue: Number(rawClendAccount.totalLiabilityValue),
|
|
240
|
+
healthFactorNotional: Number(rawClendAccount.healthFactorNotional),
|
|
241
|
+
healthFactorRiskAdjusted: Number(
|
|
242
|
+
rawClendAccount.healthFactorRiskAdjusted,
|
|
243
|
+
),
|
|
244
|
+
notionalLeverage: Number(rawClendAccount.notionalLeverage),
|
|
245
|
+
riskAdjustedLeverage: Number(rawClendAccount.riskAdjustedLeverage),
|
|
246
|
+
notionalLtv: Number(rawClendAccount.notionalLtv),
|
|
247
|
+
riskAdjustedLtv: Number(rawClendAccount.riskAdjustedLtv),
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// B. Parse the associated Summary array for the account
|
|
251
|
+
let summary: ClendAccountTxSummary[] = [];
|
|
252
|
+
if (getClendAccountSummary && accountData.summary) {
|
|
253
|
+
summary = this.parseClendAccountSummary(accountData.summary);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// C. Combine the parsed account and its summary
|
|
257
|
+
return { clendAccount, summary };
|
|
258
|
+
}
|
|
259
|
+
|
|
113
260
|
async getUser(
|
|
114
261
|
user: web3.PublicKey,
|
|
115
262
|
groups: web3.PublicKey[],
|
|
@@ -143,137 +290,8 @@ export class Client {
|
|
|
143
290
|
// This is the main refactored section. It iterates through the clendAccounts array
|
|
144
291
|
// from the response, which contains both the account data and its summary.
|
|
145
292
|
const clendAccounts = (jsonRawResponse.clendAccounts || []).map(
|
|
146
|
-
(accountData: any) =>
|
|
147
|
-
|
|
148
|
-
const rawClendAccount = accountData.clendAccount;
|
|
149
|
-
const clendAccountBalances: ClendAccountBalance[] = (
|
|
150
|
-
rawClendAccount.balances || []
|
|
151
|
-
).map((b: any) => {
|
|
152
|
-
const liquidation: ClendAccountAssetLiquidation | null = b.liquidation
|
|
153
|
-
? {
|
|
154
|
-
price: Number(b.liquidation.price),
|
|
155
|
-
changePercentage: Number(b.liquidation.changePercentage),
|
|
156
|
-
}
|
|
157
|
-
: null;
|
|
158
|
-
|
|
159
|
-
const emissionsOutstandingAndUnclaimed = new BN(
|
|
160
|
-
b.emissionsOutstandingAndUnclaimed,
|
|
161
|
-
"hex",
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
mint: new web3.PublicKey(b.mint),
|
|
166
|
-
bank: new web3.PublicKey(b.bank),
|
|
167
|
-
tokenYieldApy: Number(b.tokenYieldApy),
|
|
168
|
-
assetBalance: new BN(b.assetBalance, "hex"),
|
|
169
|
-
assetBalanceUi: Number(b.assetBalanceUi),
|
|
170
|
-
assetValue: Number(b.assetValue),
|
|
171
|
-
assetEmissionsApy: Number(b.assetEmissionsApy),
|
|
172
|
-
liabilityBalance: new BN(b.liabilityBalance, "hex"),
|
|
173
|
-
liabilityBalanceUi: Number(b.liabilityBalanceUi),
|
|
174
|
-
liabilityValue: Number(b.liabilityValue),
|
|
175
|
-
liabilityBorrowCostApy: Number(b.liabilityBorrowCostApy),
|
|
176
|
-
liabilityEmissionsApy: Number(b.liabilityEmissionsApy),
|
|
177
|
-
emissionsOutstandingAndUnclaimed,
|
|
178
|
-
emissionsOutstandingAndUnclaimedUi: Number(
|
|
179
|
-
b.emissionsOutstandingAndUnclaimedUi,
|
|
180
|
-
),
|
|
181
|
-
price: Number(b.price),
|
|
182
|
-
liquidation,
|
|
183
|
-
};
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
const clendAccount: ClendAccount = {
|
|
187
|
-
key: new web3.PublicKey(rawClendAccount.key),
|
|
188
|
-
group: new web3.PublicKey(rawClendAccount.group),
|
|
189
|
-
balances: clendAccountBalances,
|
|
190
|
-
netValue: Number(rawClendAccount.netValue),
|
|
191
|
-
netApy: Number(rawClendAccount.netApy),
|
|
192
|
-
pnl: Number(rawClendAccount.pnl),
|
|
193
|
-
totalEmissionsApy: Number(rawClendAccount.totalEmissionsApy),
|
|
194
|
-
totalAssetValue: Number(rawClendAccount.totalAssetValue),
|
|
195
|
-
totalLiabilityValue: Number(rawClendAccount.totalLiabilityValue),
|
|
196
|
-
healthFactorNotional: Number(rawClendAccount.healthFactorNotional),
|
|
197
|
-
healthFactorRiskAdjusted: Number(
|
|
198
|
-
rawClendAccount.healthFactorRiskAdjusted,
|
|
199
|
-
),
|
|
200
|
-
notionalLeverage: Number(rawClendAccount.notionalLeverage),
|
|
201
|
-
riskAdjustedLeverage: Number(rawClendAccount.riskAdjustedLeverage),
|
|
202
|
-
notionalLtv: Number(rawClendAccount.notionalLtv),
|
|
203
|
-
riskAdjustedLtv: Number(rawClendAccount.riskAdjustedLtv),
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
// B. Parse the associated Summary array for the account
|
|
207
|
-
let summary: ClendAccountTxSummary[] = [];
|
|
208
|
-
if (getClendAccountSummary && accountData.summary) {
|
|
209
|
-
summary = accountData.summary.map((s: any) => {
|
|
210
|
-
let input: UserRequest | undefined = undefined;
|
|
211
|
-
if (s.input) {
|
|
212
|
-
input = {
|
|
213
|
-
apiPath: s.input.apiPath,
|
|
214
|
-
selectedTokenMint: s.input.selectedTokenMint
|
|
215
|
-
? new web3.PublicKey(s.input.selectedTokenMint)
|
|
216
|
-
: null,
|
|
217
|
-
amountUi: s.input.amountUi ? Number(s.input.amountUi) : null,
|
|
218
|
-
leverage: s.input.leverage ? Number(s.input.leverage) : null,
|
|
219
|
-
slippageBps: s.input.slippageBps
|
|
220
|
-
? Number(s.input.slippageBps)
|
|
221
|
-
: null,
|
|
222
|
-
openPosition:
|
|
223
|
-
s.input.openPosition != null
|
|
224
|
-
? Boolean(s.input.openPosition)
|
|
225
|
-
: null,
|
|
226
|
-
closePosition:
|
|
227
|
-
s.input.closePosition != null
|
|
228
|
-
? Boolean(s.input.closePosition)
|
|
229
|
-
: null,
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const events: ClendAccountEvent[] = (s.events || []).map(
|
|
234
|
-
(event: any) => {
|
|
235
|
-
let closeBalance: boolean | null = null;
|
|
236
|
-
// Only convert to boolean if the value is not null/undefined to preserve the null state.
|
|
237
|
-
if (
|
|
238
|
-
event.closeBalance !== null &&
|
|
239
|
-
event.closeBalance !== undefined
|
|
240
|
-
) {
|
|
241
|
-
closeBalance = Boolean(event.closeBalance);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return {
|
|
245
|
-
eventIndex: event.eventIndex,
|
|
246
|
-
eventName: event.eventName,
|
|
247
|
-
bank: event.bank ? new web3.PublicKey(event.bank) : null,
|
|
248
|
-
mint: event.mint ? new web3.PublicKey(event.mint) : null,
|
|
249
|
-
amount: event.amount ? new BN(event.amount, "hex") : null,
|
|
250
|
-
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
251
|
-
value: event.value ? Number(event.value) : null,
|
|
252
|
-
price: event.price ? Number(event.price) : null,
|
|
253
|
-
closeBalance,
|
|
254
|
-
};
|
|
255
|
-
},
|
|
256
|
-
);
|
|
257
|
-
|
|
258
|
-
const txSummary: ClendAccountTxSummary = {
|
|
259
|
-
txSig: s.txSig,
|
|
260
|
-
time: s.time,
|
|
261
|
-
clendAccount: new web3.PublicKey(s.clendAccount),
|
|
262
|
-
clendAccountAuthority: new web3.PublicKey(
|
|
263
|
-
s.clendAccountAuthority,
|
|
264
|
-
),
|
|
265
|
-
clendGroup: new web3.PublicKey(s.clendGroup),
|
|
266
|
-
action: s.action,
|
|
267
|
-
input,
|
|
268
|
-
events,
|
|
269
|
-
};
|
|
270
|
-
return txSummary;
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// C. Combine the parsed account and its summary
|
|
275
|
-
return { clendAccount, summary };
|
|
276
|
-
},
|
|
293
|
+
(accountData: any) =>
|
|
294
|
+
this.parseClendAccountWithSummary(accountData, getClendAccountSummary),
|
|
277
295
|
);
|
|
278
296
|
|
|
279
297
|
// 3. Return the final, correctly structured response object.
|
|
@@ -360,61 +378,6 @@ export class Client {
|
|
|
360
378
|
return response;
|
|
361
379
|
}
|
|
362
380
|
|
|
363
|
-
async deposit(
|
|
364
|
-
clendGroup: web3.PublicKey,
|
|
365
|
-
clendAccount: web3.PublicKey | null,
|
|
366
|
-
inputTokenMint: web3.PublicKey,
|
|
367
|
-
uiAmount: number,
|
|
368
|
-
): Promise<string> {
|
|
369
|
-
const req: DepositRequest = {
|
|
370
|
-
owner: this.address(),
|
|
371
|
-
clendGroup,
|
|
372
|
-
clendAccount,
|
|
373
|
-
inputTokenMint,
|
|
374
|
-
depositAmountUi: uiAmount,
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
const body = await handleApiCall(() =>
|
|
378
|
-
this.http.post("deposit", JSON.stringify(req)),
|
|
379
|
-
);
|
|
380
|
-
|
|
381
|
-
const depositResponse: DepositResponse = JSON.parse(body);
|
|
382
|
-
|
|
383
|
-
const txSig = await this.send(
|
|
384
|
-
depositResponse.unsignedBase64Tx,
|
|
385
|
-
depositResponse.userRequestId,
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
return txSig;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
async withdraw(
|
|
392
|
-
clendAccount: web3.PublicKey,
|
|
393
|
-
outputTokenMint: web3.PublicKey,
|
|
394
|
-
uiAmount: number,
|
|
395
|
-
withdrawAll: boolean,
|
|
396
|
-
): Promise<string> {
|
|
397
|
-
const req: WithdrawRequest = {
|
|
398
|
-
clendAccount,
|
|
399
|
-
outputTokenMint,
|
|
400
|
-
withdrawAmountUi: uiAmount,
|
|
401
|
-
withdrawAll,
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
const body = await handleApiCall(() =>
|
|
405
|
-
this.http.post("withdraw", JSON.stringify(req)),
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
const withdrawResponse: WithdrawResponse = JSON.parse(body);
|
|
409
|
-
|
|
410
|
-
const txSig = await this.send(
|
|
411
|
-
withdrawResponse.unsignedBase64Tx,
|
|
412
|
-
withdrawResponse.userRequestId,
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
return txSig;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
381
|
/**
|
|
419
382
|
* Deposit collateral and create a leveraged position
|
|
420
383
|
* @param request Deposit leverage request parameters
|
package/src/types.ts
CHANGED
|
@@ -6,37 +6,6 @@ export interface SendRequest {
|
|
|
6
6
|
txns: string[];
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
* Request to deposit collateral
|
|
11
|
-
*/
|
|
12
|
-
export interface DepositRequest {
|
|
13
|
-
owner: web3.PublicKey;
|
|
14
|
-
clendGroup: web3.PublicKey;
|
|
15
|
-
clendAccount: web3.PublicKey | null;
|
|
16
|
-
inputTokenMint: web3.PublicKey;
|
|
17
|
-
depositAmountUi: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface DepositResponse {
|
|
21
|
-
userRequestId: string;
|
|
22
|
-
unsignedBase64Tx: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Request to withdraw collateral
|
|
27
|
-
*/
|
|
28
|
-
export interface WithdrawRequest {
|
|
29
|
-
clendAccount: web3.PublicKey;
|
|
30
|
-
outputTokenMint: web3.PublicKey;
|
|
31
|
-
withdrawAmountUi: number;
|
|
32
|
-
withdrawAll: boolean;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface WithdrawResponse {
|
|
36
|
-
userRequestId: string;
|
|
37
|
-
unsignedBase64Tx: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
9
|
/**
|
|
41
10
|
* Request to deposit collateral and create a leveraged position
|
|
42
11
|
*/
|
|
@@ -129,6 +98,16 @@ export interface GroupAndBanks {
|
|
|
129
98
|
banks: Bank[];
|
|
130
99
|
}
|
|
131
100
|
|
|
101
|
+
export interface GetAccountRequest {
|
|
102
|
+
account: web3.PublicKey;
|
|
103
|
+
getClendAccountSummary: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface GetAccountResponse {
|
|
107
|
+
clendAccount: ClendAccount;
|
|
108
|
+
summary: ClendAccountTxSummary[];
|
|
109
|
+
}
|
|
110
|
+
|
|
132
111
|
export interface GetUserRequest {
|
|
133
112
|
groups: web3.PublicKey[];
|
|
134
113
|
user: web3.PublicKey;
|