@carrot-protocol/boost-http-client 0.2.16-group-refactor1-dev-50c976e → 0.2.16-token22-dev-998073e
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 +219 -134
- package/dist/types.d.ts +99 -20
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -9
- package/package.json +2 -2
- package/src/index.ts +327 -155
- package/src/types.ts +115 -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,152 +110,206 @@ 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
|
-
|
|
282
|
+
clendAccounts,
|
|
283
|
+
};
|
|
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,
|
|
246
310
|
};
|
|
311
|
+
|
|
312
|
+
return response;
|
|
247
313
|
}
|
|
248
314
|
|
|
249
315
|
/**
|
|
@@ -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,78 @@ 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
|
+
): Promise<string> {
|
|
396
|
+
const req: WithdrawRequest = {
|
|
397
|
+
clendAccount,
|
|
398
|
+
outputTokenMint,
|
|
399
|
+
withdrawAmountUi: uiAmount,
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const body = await handleApiCall(() =>
|
|
403
|
+
this.http.post("withdraw", JSON.stringify(req)),
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
const withdrawResponse: WithdrawResponse = JSON.parse(body);
|
|
407
|
+
|
|
408
|
+
const txSig = await this.send(
|
|
409
|
+
withdrawResponse.unsignedBase64Tx,
|
|
410
|
+
withdrawResponse.userRequestId,
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
return txSig;
|
|
414
|
+
}
|
|
415
|
+
|
|
293
416
|
/**
|
|
294
417
|
* Deposit collateral and create a leveraged position
|
|
295
418
|
* @param request Deposit leverage request parameters
|
|
296
419
|
* @returns Deposit leverage operation result
|
|
297
420
|
*/
|
|
298
421
|
async depositLeverage(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
422
|
+
clendGroup: web3.PublicKey,
|
|
423
|
+
clendAccount: web3.PublicKey | null,
|
|
424
|
+
inputTokenMint: web3.PublicKey,
|
|
425
|
+
assetTokenMint: web3.PublicKey,
|
|
426
|
+
liabilityTokenMint: web3.PublicKey,
|
|
302
427
|
uiAmount: number,
|
|
303
428
|
leverage: number,
|
|
304
429
|
slippageBps: number,
|
|
305
430
|
): Promise<string> {
|
|
306
|
-
if (
|
|
431
|
+
if (
|
|
432
|
+
!inputTokenMint.equals(assetTokenMint) &&
|
|
433
|
+
!inputTokenMint.equals(liabilityTokenMint)
|
|
434
|
+
) {
|
|
307
435
|
throw new Error(
|
|
308
436
|
"Input mint must be the same as the asset or liability mint",
|
|
309
437
|
);
|
|
@@ -311,9 +439,11 @@ export class Client {
|
|
|
311
439
|
|
|
312
440
|
const req: DepositLeverageRequest = {
|
|
313
441
|
owner: this.address(),
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
442
|
+
clendGroup,
|
|
443
|
+
clendAccount,
|
|
444
|
+
inputTokenMint,
|
|
445
|
+
assetTokenMint,
|
|
446
|
+
liabilityTokenMint,
|
|
317
447
|
depositAmountUi: uiAmount,
|
|
318
448
|
leverage,
|
|
319
449
|
slippageBps,
|
|
@@ -337,9 +467,17 @@ export class Client {
|
|
|
337
467
|
* @param request Adjust leverage request parameters
|
|
338
468
|
* @returns Adjust leverage operation result
|
|
339
469
|
*/
|
|
340
|
-
async adjustLeverage(
|
|
470
|
+
async adjustLeverage(
|
|
471
|
+
clendAccount: web3.PublicKey,
|
|
472
|
+
assetTokenMint: web3.PublicKey,
|
|
473
|
+
liabilityTokenMint: web3.PublicKey,
|
|
474
|
+
leverage: number,
|
|
475
|
+
slippageBps: number,
|
|
476
|
+
): Promise<string> {
|
|
341
477
|
const req: AdjustLeverageRequest = {
|
|
342
|
-
|
|
478
|
+
clendAccount,
|
|
479
|
+
assetTokenMint,
|
|
480
|
+
liabilityTokenMint,
|
|
343
481
|
leverage,
|
|
344
482
|
slippageBps,
|
|
345
483
|
};
|
|
@@ -363,14 +501,19 @@ export class Client {
|
|
|
363
501
|
* @returns Withdraw leverage operation result
|
|
364
502
|
*/
|
|
365
503
|
async withdrawLeverage(
|
|
366
|
-
|
|
504
|
+
clendAccount: web3.PublicKey,
|
|
505
|
+
outputTokenMint: web3.PublicKey,
|
|
506
|
+
assetTokenMint: web3.PublicKey,
|
|
507
|
+
liabilityTokenMint: web3.PublicKey,
|
|
367
508
|
uiAmount: number,
|
|
368
509
|
slippageBps: number,
|
|
369
510
|
withdrawAll: boolean,
|
|
370
|
-
): Promise<
|
|
511
|
+
): Promise<string> {
|
|
371
512
|
const req: WithdrawLeverageRequest = {
|
|
372
|
-
|
|
373
|
-
|
|
513
|
+
clendAccount,
|
|
514
|
+
outputTokenMint,
|
|
515
|
+
assetTokenMint,
|
|
516
|
+
liabilityTokenMint,
|
|
374
517
|
withdrawAmountUi: uiAmount,
|
|
375
518
|
slippageBps,
|
|
376
519
|
withdrawAll,
|
|
@@ -388,6 +531,30 @@ export class Client {
|
|
|
388
531
|
|
|
389
532
|
return txSig;
|
|
390
533
|
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Withdraw emissions from a bank
|
|
537
|
+
* @returns Withdraw emissions operation result
|
|
538
|
+
*/
|
|
539
|
+
async withdrawEmissions(): Promise<string> {
|
|
540
|
+
const req: WithdrawEmissionsRequest = {
|
|
541
|
+
owner: this.address(),
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
const body = await handleApiCall(() =>
|
|
545
|
+
this.http.post("emissions/withdraw", JSON.stringify(req)),
|
|
546
|
+
);
|
|
547
|
+
|
|
548
|
+
const withdrawEmissionsResponse: WithdrawEmissionsResponse =
|
|
549
|
+
JSON.parse(body);
|
|
550
|
+
|
|
551
|
+
const txSig = await this.send(
|
|
552
|
+
withdrawEmissionsResponse.unsignedBase64Tx,
|
|
553
|
+
withdrawEmissionsResponse.userRequestId,
|
|
554
|
+
);
|
|
555
|
+
|
|
556
|
+
return txSig;
|
|
557
|
+
}
|
|
391
558
|
}
|
|
392
559
|
|
|
393
560
|
type ApiErrorPayload = {
|
|
@@ -397,15 +564,6 @@ type ApiErrorPayload = {
|
|
|
397
564
|
timestamp: string;
|
|
398
565
|
};
|
|
399
566
|
|
|
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
567
|
// Helper function to handle API calls
|
|
410
568
|
async function handleApiCall<T>(
|
|
411
569
|
call: () => Promise<AxiosResponse<T>>,
|
|
@@ -439,6 +597,18 @@ function getDummyProvider(): AnchorProvider {
|
|
|
439
597
|
}
|
|
440
598
|
|
|
441
599
|
function parseBank(bankJson: any): Bank {
|
|
600
|
+
let bankEmissions: BankEmissions | null = null;
|
|
601
|
+
if (bankJson.emissions) {
|
|
602
|
+
bankEmissions = {
|
|
603
|
+
emissionsMode: bankJson.emissions.emissionsMode as BankEmissionsMode,
|
|
604
|
+
emissionsApy: Number(bankJson.emissions.emissionsApy),
|
|
605
|
+
emissionsMint: new web3.PublicKey(bankJson.emissions.emissionsMint),
|
|
606
|
+
emissionsTokenPrice: Number(bankJson.emissions.emissionsTokenPrice),
|
|
607
|
+
emissionsRate: new BN(bankJson.emissions.emissionsRate, "hex"),
|
|
608
|
+
emissionsRemainingUi: Number(bankJson.emissions.emissionsRemainingUi),
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
|
|
442
612
|
return {
|
|
443
613
|
mint: new web3.PublicKey(bankJson.mint),
|
|
444
614
|
key: new web3.PublicKey(bankJson.key),
|
|
@@ -452,6 +622,7 @@ function parseBank(bankJson: any): Bank {
|
|
|
452
622
|
assetMaintWeight: Number(bankJson.assetMaintWeight),
|
|
453
623
|
totalAssetShares: Number(bankJson.totalAssetShares),
|
|
454
624
|
assetShareValue: Number(bankJson.assetShareValue),
|
|
625
|
+
tokenYieldApy: Number(bankJson.tokenYieldApy),
|
|
455
626
|
liabilityAmount: new BN(bankJson.liabilityAmount, "hex"),
|
|
456
627
|
liabilityAmountUi: Number(bankJson.liabilityAmountUi),
|
|
457
628
|
liabilityInitWeight: Number(bankJson.liabilityInitWeight),
|
|
@@ -463,5 +634,6 @@ function parseBank(bankJson: any): Bank {
|
|
|
463
634
|
depositLimitUi: Number(bankJson.depositLimitUi),
|
|
464
635
|
borrowLimit: new BN(bankJson.borrowLimit, "hex"),
|
|
465
636
|
borrowLimitUi: Number(bankJson.borrowLimitUi),
|
|
637
|
+
emissions: bankEmissions,
|
|
466
638
|
};
|
|
467
639
|
}
|