@carrot-protocol/boost-http-client 0.2.16-group-refactor1-dev-50c976e → 0.2.16-token22-dev-158437a
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 +16 -8
- package/dist/index.js +220 -134
- package/dist/types.d.ts +100 -20
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -9
- package/package.json +2 -2
- package/src/index.ts +329 -155
- package/src/types.ts +116 -21
- package/src/utils.ts +0 -8
package/src/index.ts
CHANGED
|
@@ -12,13 +12,25 @@ 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,
|
|
23
|
+
ClendAccountAssetLiquidation,
|
|
24
|
+
GetGroupsResponse,
|
|
25
|
+
GroupAndBanks,
|
|
26
|
+
BankEmissions,
|
|
27
|
+
BankEmissionsMode,
|
|
28
|
+
WithdrawEmissionsRequest,
|
|
29
|
+
WithdrawEmissionsResponse,
|
|
30
|
+
DepositRequest,
|
|
31
|
+
DepositResponse,
|
|
32
|
+
WithdrawRequest,
|
|
33
|
+
WithdrawResponse,
|
|
22
34
|
} from "./types";
|
|
23
35
|
import encode from "bs58";
|
|
24
36
|
|
|
@@ -98,153 +110,207 @@ export class Client {
|
|
|
98
110
|
return handleApiCall(() => this.http.get(""));
|
|
99
111
|
}
|
|
100
112
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// use loaded wallet if not provided
|
|
108
|
-
let user = request.user;
|
|
109
|
-
if (!user) {
|
|
110
|
-
user = this.address();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
+
async getUser(
|
|
114
|
+
user: web3.PublicKey,
|
|
115
|
+
groups: web3.PublicKey[],
|
|
116
|
+
getClendAccountSummary: boolean,
|
|
117
|
+
): Promise<GetUserResponse> {
|
|
118
|
+
// Make the API call to fetch the raw user data as a string.
|
|
113
119
|
const body = await handleApiCall(() =>
|
|
114
120
|
this.http.get(
|
|
115
|
-
`/user?user=${user.toString()}&
|
|
121
|
+
`/user?user=${user.toString()}&groups=${groups.map((g) => g.toString()).join(",")}&getClendAccountSummary=${getClendAccountSummary}`,
|
|
116
122
|
),
|
|
117
123
|
);
|
|
118
124
|
|
|
125
|
+
// Parse the raw string body into a JSON object.
|
|
119
126
|
const jsonRawResponse: any = JSON.parse(body);
|
|
120
127
|
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
};
|
|
128
|
+
// 1. Parse Wallet Balances
|
|
129
|
+
// This section correctly iterates through the wallet balances and creates typed objects.
|
|
130
|
+
const walletBalances: UserBalance[] = (
|
|
131
|
+
jsonRawResponse.wallet.balances || []
|
|
132
|
+
).map((b: any) => ({
|
|
133
|
+
mint: new web3.PublicKey(b.mint),
|
|
134
|
+
balance: new BN(b.balance, "hex"),
|
|
135
|
+
balanceUi: Number(b.balanceUi),
|
|
136
|
+
}));
|
|
130
137
|
|
|
131
|
-
|
|
132
|
-
|
|
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({
|
|
144
|
-
mint: new web3.PublicKey(b.mint),
|
|
145
|
-
bank: new web3.PublicKey(b.bank),
|
|
146
|
-
assetBalance: new BN(b.assetBalance, "hex"),
|
|
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),
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// create clend account
|
|
157
|
-
const clendAccount: ClendAccount = {
|
|
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),
|
|
138
|
+
const wallet: UserWallet = {
|
|
139
|
+
balances: walletBalances,
|
|
182
140
|
};
|
|
183
141
|
|
|
184
|
-
//
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
:
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
:
|
|
142
|
+
// 2. Parse Clend Accounts and their Summaries
|
|
143
|
+
// This is the main refactored section. It iterates through the clendAccounts array
|
|
144
|
+
// from the response, which contains both the account data and its summary.
|
|
145
|
+
const clendAccounts = (jsonRawResponse.clendAccounts || []).map(
|
|
146
|
+
(accountData: any) => {
|
|
147
|
+
// A. Parse the main ClendAccount object
|
|
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),
|
|
214
204
|
};
|
|
215
|
-
txSummary.input = input;
|
|
216
|
-
}
|
|
217
205
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
+
});
|
|
223
272
|
}
|
|
224
273
|
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
bank: event.bank ? new web3.PublicKey(event.bank) : null,
|
|
230
|
-
mint: event.mint ? new web3.PublicKey(event.mint) : null,
|
|
231
|
-
amount: event.amount ? new BN(event.amount, "hex") : null,
|
|
232
|
-
amountUi: event.amountUi ? Number(event.amountUi) : null,
|
|
233
|
-
value: event.value ? Number(event.value) : null,
|
|
234
|
-
price: event.price ? Number(event.price) : null,
|
|
235
|
-
closeBalance,
|
|
236
|
-
};
|
|
237
|
-
txSummary.events.push(clendAccountEvent);
|
|
238
|
-
}
|
|
239
|
-
summary.push(txSummary);
|
|
240
|
-
}
|
|
274
|
+
// C. Combine the parsed account and its summary
|
|
275
|
+
return { clendAccount, summary };
|
|
276
|
+
},
|
|
277
|
+
);
|
|
241
278
|
|
|
279
|
+
// 3. Return the final, correctly structured response object.
|
|
242
280
|
return {
|
|
243
281
|
wallet,
|
|
244
|
-
|
|
245
|
-
summary,
|
|
282
|
+
clendAccounts,
|
|
246
283
|
};
|
|
247
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Get all groups
|
|
287
|
+
* @param includeBankData Whether to include bank data in the response, if not strictly necessary keep false
|
|
288
|
+
* @returns All groups
|
|
289
|
+
*/
|
|
290
|
+
async getGroups(includeBankData: boolean): Promise<GetGroupsResponse> {
|
|
291
|
+
const body = await handleApiCall(() =>
|
|
292
|
+
this.http.get(`/groups?includeBankData=${includeBankData}`),
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const marketJson: any = JSON.parse(body);
|
|
296
|
+
const groups: GroupAndBanks[] = [];
|
|
297
|
+
for (const groupJson of marketJson.groups) {
|
|
298
|
+
const group: web3.PublicKey = new web3.PublicKey(groupJson.group);
|
|
299
|
+
const groupName = groupJson.groupName;
|
|
300
|
+
const banks: Bank[] = [];
|
|
301
|
+
for (const bankJson of groupJson.banks) {
|
|
302
|
+
const bank = parseBank(bankJson);
|
|
303
|
+
banks.push(bank);
|
|
304
|
+
}
|
|
305
|
+
groups.push({ group, groupName, banks });
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const response: GetGroupsResponse = {
|
|
309
|
+
groups,
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
return response;
|
|
313
|
+
}
|
|
248
314
|
|
|
249
315
|
/**
|
|
250
316
|
* Get market details
|
|
@@ -257,14 +323,18 @@ export class Client {
|
|
|
257
323
|
);
|
|
258
324
|
|
|
259
325
|
const marketJson: any = JSON.parse(body);
|
|
260
|
-
const
|
|
261
|
-
|
|
326
|
+
const group: GroupAndBanks = {
|
|
327
|
+
group: new web3.PublicKey(marketJson.group.group),
|
|
328
|
+
groupName: marketJson.group.groupName,
|
|
329
|
+
banks: [],
|
|
330
|
+
};
|
|
331
|
+
for (const bankJson of marketJson.group.banks) {
|
|
262
332
|
const bank = parseBank(bankJson);
|
|
263
|
-
banks.push(bank);
|
|
333
|
+
group.banks.push(bank);
|
|
264
334
|
}
|
|
265
335
|
|
|
266
336
|
const response: GetGroupResponse = {
|
|
267
|
-
|
|
337
|
+
group,
|
|
268
338
|
};
|
|
269
339
|
|
|
270
340
|
return response;
|
|
@@ -290,20 +360,80 @@ export class Client {
|
|
|
290
360
|
return response;
|
|
291
361
|
}
|
|
292
362
|
|
|
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
|
+
|
|
293
418
|
/**
|
|
294
419
|
* Deposit collateral and create a leveraged position
|
|
295
420
|
* @param request Deposit leverage request parameters
|
|
296
421
|
* @returns Deposit leverage operation result
|
|
297
422
|
*/
|
|
298
423
|
async depositLeverage(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
424
|
+
clendGroup: web3.PublicKey,
|
|
425
|
+
clendAccount: web3.PublicKey | null,
|
|
426
|
+
inputTokenMint: web3.PublicKey,
|
|
427
|
+
assetTokenMint: web3.PublicKey,
|
|
428
|
+
liabilityTokenMint: web3.PublicKey,
|
|
302
429
|
uiAmount: number,
|
|
303
430
|
leverage: number,
|
|
304
431
|
slippageBps: number,
|
|
305
432
|
): Promise<string> {
|
|
306
|
-
if (
|
|
433
|
+
if (
|
|
434
|
+
!inputTokenMint.equals(assetTokenMint) &&
|
|
435
|
+
!inputTokenMint.equals(liabilityTokenMint)
|
|
436
|
+
) {
|
|
307
437
|
throw new Error(
|
|
308
438
|
"Input mint must be the same as the asset or liability mint",
|
|
309
439
|
);
|
|
@@ -311,9 +441,11 @@ export class Client {
|
|
|
311
441
|
|
|
312
442
|
const req: DepositLeverageRequest = {
|
|
313
443
|
owner: this.address(),
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
444
|
+
clendGroup,
|
|
445
|
+
clendAccount,
|
|
446
|
+
inputTokenMint,
|
|
447
|
+
assetTokenMint,
|
|
448
|
+
liabilityTokenMint,
|
|
317
449
|
depositAmountUi: uiAmount,
|
|
318
450
|
leverage,
|
|
319
451
|
slippageBps,
|
|
@@ -337,9 +469,17 @@ export class Client {
|
|
|
337
469
|
* @param request Adjust leverage request parameters
|
|
338
470
|
* @returns Adjust leverage operation result
|
|
339
471
|
*/
|
|
340
|
-
async adjustLeverage(
|
|
472
|
+
async adjustLeverage(
|
|
473
|
+
clendAccount: web3.PublicKey,
|
|
474
|
+
assetTokenMint: web3.PublicKey,
|
|
475
|
+
liabilityTokenMint: web3.PublicKey,
|
|
476
|
+
leverage: number,
|
|
477
|
+
slippageBps: number,
|
|
478
|
+
): Promise<string> {
|
|
341
479
|
const req: AdjustLeverageRequest = {
|
|
342
|
-
|
|
480
|
+
clendAccount,
|
|
481
|
+
assetTokenMint,
|
|
482
|
+
liabilityTokenMint,
|
|
343
483
|
leverage,
|
|
344
484
|
slippageBps,
|
|
345
485
|
};
|
|
@@ -363,14 +503,19 @@ export class Client {
|
|
|
363
503
|
* @returns Withdraw leverage operation result
|
|
364
504
|
*/
|
|
365
505
|
async withdrawLeverage(
|
|
366
|
-
|
|
506
|
+
clendAccount: web3.PublicKey,
|
|
507
|
+
outputTokenMint: web3.PublicKey,
|
|
508
|
+
assetTokenMint: web3.PublicKey,
|
|
509
|
+
liabilityTokenMint: web3.PublicKey,
|
|
367
510
|
uiAmount: number,
|
|
368
511
|
slippageBps: number,
|
|
369
512
|
withdrawAll: boolean,
|
|
370
|
-
): Promise<
|
|
513
|
+
): Promise<string> {
|
|
371
514
|
const req: WithdrawLeverageRequest = {
|
|
372
|
-
|
|
373
|
-
|
|
515
|
+
clendAccount,
|
|
516
|
+
outputTokenMint,
|
|
517
|
+
assetTokenMint,
|
|
518
|
+
liabilityTokenMint,
|
|
374
519
|
withdrawAmountUi: uiAmount,
|
|
375
520
|
slippageBps,
|
|
376
521
|
withdrawAll,
|
|
@@ -388,6 +533,30 @@ export class Client {
|
|
|
388
533
|
|
|
389
534
|
return txSig;
|
|
390
535
|
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Withdraw emissions from a bank
|
|
539
|
+
* @returns Withdraw emissions operation result
|
|
540
|
+
*/
|
|
541
|
+
async withdrawEmissions(): Promise<string> {
|
|
542
|
+
const req: WithdrawEmissionsRequest = {
|
|
543
|
+
owner: this.address(),
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
const body = await handleApiCall(() =>
|
|
547
|
+
this.http.post("emissions/withdraw", JSON.stringify(req)),
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
const withdrawEmissionsResponse: WithdrawEmissionsResponse =
|
|
551
|
+
JSON.parse(body);
|
|
552
|
+
|
|
553
|
+
const txSig = await this.send(
|
|
554
|
+
withdrawEmissionsResponse.unsignedBase64Tx,
|
|
555
|
+
withdrawEmissionsResponse.userRequestId,
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
return txSig;
|
|
559
|
+
}
|
|
391
560
|
}
|
|
392
561
|
|
|
393
562
|
type ApiErrorPayload = {
|
|
@@ -397,15 +566,6 @@ type ApiErrorPayload = {
|
|
|
397
566
|
timestamp: string;
|
|
398
567
|
};
|
|
399
568
|
|
|
400
|
-
function handleStatusCode(statusCode: number): void {
|
|
401
|
-
switch (statusCode) {
|
|
402
|
-
case 200:
|
|
403
|
-
break;
|
|
404
|
-
default:
|
|
405
|
-
throw new Error(`unexpected status code: ${statusCode}`);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
569
|
// Helper function to handle API calls
|
|
410
570
|
async function handleApiCall<T>(
|
|
411
571
|
call: () => Promise<AxiosResponse<T>>,
|
|
@@ -439,6 +599,18 @@ function getDummyProvider(): AnchorProvider {
|
|
|
439
599
|
}
|
|
440
600
|
|
|
441
601
|
function parseBank(bankJson: any): Bank {
|
|
602
|
+
let bankEmissions: BankEmissions | null = null;
|
|
603
|
+
if (bankJson.emissions) {
|
|
604
|
+
bankEmissions = {
|
|
605
|
+
emissionsMode: bankJson.emissions.emissionsMode as BankEmissionsMode,
|
|
606
|
+
emissionsApy: Number(bankJson.emissions.emissionsApy),
|
|
607
|
+
emissionsMint: new web3.PublicKey(bankJson.emissions.emissionsMint),
|
|
608
|
+
emissionsTokenPrice: Number(bankJson.emissions.emissionsTokenPrice),
|
|
609
|
+
emissionsRate: new BN(bankJson.emissions.emissionsRate, "hex"),
|
|
610
|
+
emissionsRemainingUi: Number(bankJson.emissions.emissionsRemainingUi),
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
442
614
|
return {
|
|
443
615
|
mint: new web3.PublicKey(bankJson.mint),
|
|
444
616
|
key: new web3.PublicKey(bankJson.key),
|
|
@@ -452,6 +624,7 @@ function parseBank(bankJson: any): Bank {
|
|
|
452
624
|
assetMaintWeight: Number(bankJson.assetMaintWeight),
|
|
453
625
|
totalAssetShares: Number(bankJson.totalAssetShares),
|
|
454
626
|
assetShareValue: Number(bankJson.assetShareValue),
|
|
627
|
+
tokenYieldApy: Number(bankJson.tokenYieldApy),
|
|
455
628
|
liabilityAmount: new BN(bankJson.liabilityAmount, "hex"),
|
|
456
629
|
liabilityAmountUi: Number(bankJson.liabilityAmountUi),
|
|
457
630
|
liabilityInitWeight: Number(bankJson.liabilityInitWeight),
|
|
@@ -463,5 +636,6 @@ function parseBank(bankJson: any): Bank {
|
|
|
463
636
|
depositLimitUi: Number(bankJson.depositLimitUi),
|
|
464
637
|
borrowLimit: new BN(bankJson.borrowLimit, "hex"),
|
|
465
638
|
borrowLimitUi: Number(bankJson.borrowLimitUi),
|
|
639
|
+
emissions: bankEmissions,
|
|
466
640
|
};
|
|
467
641
|
}
|