@carrot-protocol/boost-http-client 0.1.7 → 0.2.0-data-fixes-dev-5a03722

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AnchorProvider, web3 } from "@coral-xyz/anchor";
2
- import { DepositLeverageRequest, GetMarketResponse, GetUserResponse, ModifyLeverageRequest, WithdrawLeverageRequest } from "./types";
2
+ import { GetBankResponse, GetUserResponse, GetGroupResponse } from "./types";
3
3
  export * from "./types";
4
4
  /**
5
5
  * HTTP Client for Carrot Boost API
@@ -27,31 +27,33 @@ export declare class Client {
27
27
  */
28
28
  getUser(user?: web3.PublicKey): Promise<GetUserResponse>;
29
29
  /**
30
- * Get market details for the carrot boost market
31
- * @returns Market details
30
+ * Get market details
31
+ * @returns Group Details
32
32
  */
33
- getMarket(): Promise<GetMarketResponse>;
33
+ getGroup(): Promise<GetGroupResponse>;
34
34
  /**
35
- * Create an obligation for the client
36
- * @returns Transaction signature
35
+ * Get bank details
36
+ * @param addr token mint or bank public key
37
+ * @param addrType "mint" or "bank"
38
+ * @returns Bank details
37
39
  */
38
- createObligation(): Promise<string>;
40
+ getBank(addr: web3.PublicKey, addrType: "mint" | "bank"): Promise<GetBankResponse>;
39
41
  /**
40
42
  * Deposit collateral and create a leveraged position
41
43
  * @param request Deposit leverage request parameters
42
44
  * @returns Deposit leverage operation result
43
45
  */
44
- depositLeverage(params: DepositLeverageRequest): Promise<string>;
46
+ depositLeverage(selectedTokenMint: web3.PublicKey, uiAmount: number, leverage: number, slippageBps: number): Promise<string>;
45
47
  /**
46
- * Modify the leverage of an existing position
47
- * @param request Modify leverage request parameters
48
- * @returns Modify leverage operation result
48
+ * Adjust the leverage of an existing position
49
+ * @param request Adjust leverage request parameters
50
+ * @returns Adjust leverage operation result
49
51
  */
50
- modifyLeverage(params: ModifyLeverageRequest): Promise<any>;
52
+ adjustLeverage(leverage: number, slippageBps: number): Promise<any>;
51
53
  /**
52
54
  * Withdraw from or close a leveraged position
53
55
  * @param request Withdraw leverage request parameters
54
56
  * @returns Withdraw leverage operation result
55
57
  */
56
- withdrawLeverage(params: WithdrawLeverageRequest): Promise<any>;
58
+ withdrawLeverage(selectedTokenMint: web3.PublicKey, uiAmount: number, slippageBps: number, withdrawAll: boolean): Promise<any>;
57
59
  }
package/dist/index.js CHANGED
@@ -10,28 +10,6 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
15
  };
@@ -40,7 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
18
  };
41
19
  Object.defineProperty(exports, "__esModule", { value: true });
42
20
  exports.Client = void 0;
43
- const axios_1 = __importStar(require("axios"));
21
+ const axios_1 = __importDefault(require("axios"));
44
22
  const anchor_1 = require("@coral-xyz/anchor");
45
23
  const bs58_1 = __importDefault(require("bs58"));
46
24
  // Re-export types
@@ -70,7 +48,7 @@ class Client {
70
48
  return this.provider.wallet.publicKey;
71
49
  }
72
50
  // sign and send tx to api for sending to network
73
- async send(unsignedBase64Tx, ...signedBase64Txns) {
51
+ async send(unsignedBase64Tx) {
74
52
  // deserialize into tx obj
75
53
  const txBytes = Buffer.from(unsignedBase64Tx, "base64");
76
54
  const tx = anchor_1.web3.VersionedTransaction.deserialize(new Uint8Array(txBytes));
@@ -81,7 +59,7 @@ class Client {
81
59
  const encodedAndSignedTx = Buffer.from(signedTx.serialize()).toString("base64");
82
60
  // create request obj
83
61
  const sendRequest = {
84
- txns: [encodedAndSignedTx, ...signedBase64Txns],
62
+ txns: [encodedAndSignedTx],
85
63
  };
86
64
  // send to api
87
65
  await handleApiCall(() => this.http.post(`send`, sendRequest));
@@ -106,48 +84,120 @@ class Client {
106
84
  user = this.address();
107
85
  }
108
86
  const body = await handleApiCall(() => this.http.get(`/user?user=${user.toString()}`));
109
- const response = JSON.parse(body);
110
- return response;
87
+ const jsonRawResponse = JSON.parse(body);
88
+ // get tokens still in user wallet
89
+ const wallet = {
90
+ usdcBalance: new anchor_1.BN(jsonRawResponse.wallet.usdcBalance, "hex"),
91
+ usdcBalanceUi: Number(jsonRawResponse.wallet.usdcBalanceUi),
92
+ jlpBalance: new anchor_1.BN(jsonRawResponse.wallet.jlpBalance, "hex"),
93
+ jlpBalanceUi: Number(jsonRawResponse.wallet.jlpBalanceUi),
94
+ solBalance: new anchor_1.BN(jsonRawResponse.wallet.solBalance, "hex"),
95
+ solBalanceUi: Number(jsonRawResponse.wallet.solBalanceUi),
96
+ };
97
+ // if no clend account, return undefined for clend
98
+ if (!jsonRawResponse.clendAccount) {
99
+ return {
100
+ wallet,
101
+ clendAccount: undefined,
102
+ };
103
+ }
104
+ // parse lending account balances
105
+ const balances = [];
106
+ for (const b of jsonRawResponse.clendAccount.balances) {
107
+ balances.push({
108
+ mint: new anchor_1.web3.PublicKey(b.mint),
109
+ bank: new anchor_1.web3.PublicKey(b.bank),
110
+ assetBalance: new anchor_1.BN(b.assetBalance, "hex"),
111
+ assetBalanceUi: Number(b.assetBalanceUi),
112
+ assetValue: Number(b.assetValue),
113
+ liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
114
+ liabilityBalanceUi: Number(b.liabilityBalanceUi),
115
+ liabilityValue: Number(b.liabilityValue),
116
+ price: Number(b.price),
117
+ });
118
+ }
119
+ // create clend account
120
+ const clendAccount = {
121
+ balances,
122
+ netValue: Number(jsonRawResponse.clendAccount.netValue),
123
+ netApy: Number(jsonRawResponse.clendAccount.netApy),
124
+ pnl: Number(jsonRawResponse.clendAccount.pnl),
125
+ totalAssetValue: Number(jsonRawResponse.clendAccount.totalAssetValue),
126
+ totalLiabilityValue: Number(jsonRawResponse.clendAccount.totalLiabilityValue),
127
+ healthFactorNotional: Number(jsonRawResponse.clendAccount.healthFactorNotional),
128
+ healthFactorRiskAdjusted: Number(jsonRawResponse.clendAccount.healthFactorRiskAdjusted),
129
+ notionalLeverage: Number(jsonRawResponse.clendAccount.notionalLeverage),
130
+ riskAdjustedLeverage: Number(jsonRawResponse.clendAccount.riskAdjustedLeverage),
131
+ };
132
+ return {
133
+ wallet,
134
+ clendAccount,
135
+ };
111
136
  }
112
137
  /**
113
- * Get market details for the carrot boost market
114
- * @returns Market details
138
+ * Get market details
139
+ * @returns Group Details
115
140
  */
116
- async getMarket() {
117
- const body = await handleApiCall(() => this.http.get(`market`));
118
- const response = JSON.parse(body);
141
+ async getGroup() {
142
+ const body = await handleApiCall(() => this.http.get(`/group`));
143
+ const marketJson = JSON.parse(body);
144
+ const banks = [];
145
+ for (const bankJson of marketJson.banks) {
146
+ const bank = parseBank(bankJson);
147
+ banks.push(bank);
148
+ }
149
+ const response = {
150
+ banks,
151
+ };
119
152
  return response;
120
153
  }
121
154
  /**
122
- * Create an obligation for the client
123
- * @returns Transaction signature
155
+ * Get bank details
156
+ * @param addr token mint or bank public key
157
+ * @param addrType "mint" or "bank"
158
+ * @returns Bank details
124
159
  */
125
- async createObligation() {
126
- const body = await handleApiCall(() => this.http.post("createObligation", JSON.stringify({ owner: this.address() })));
127
- const createObligationResponse = JSON.parse(body);
128
- const txSig = await this.send(createObligationResponse.unsignedBase64Tx);
129
- return txSig;
160
+ async getBank(addr, addrType) {
161
+ const body = await handleApiCall(() => this.http.get(`/bank?${addrType}=${addr.toString()}`));
162
+ const bankJson = JSON.parse(body);
163
+ const bank = parseBank(bankJson.bank);
164
+ const response = {
165
+ bank,
166
+ };
167
+ return response;
130
168
  }
131
169
  /**
132
170
  * Deposit collateral and create a leveraged position
133
171
  * @param request Deposit leverage request parameters
134
172
  * @returns Deposit leverage operation result
135
173
  */
136
- async depositLeverage(params) {
137
- const body = await handleApiCall(() => this.http.post("leverage/deposit", JSON.stringify(params)));
174
+ async depositLeverage(selectedTokenMint, uiAmount, leverage, slippageBps) {
175
+ const req = {
176
+ owner: this.address(),
177
+ selectedTokenMint,
178
+ depositAmountUi: uiAmount,
179
+ leverage,
180
+ slippageBps,
181
+ };
182
+ const body = await handleApiCall(() => this.http.post("leverage/deposit", JSON.stringify(req)));
138
183
  const depositLeverageResponse = JSON.parse(body);
139
- const txSig = await this.send(depositLeverageResponse.unsignedBase64Tx, depositLeverageResponse.signedSetBorrowRateBase64Tx);
184
+ const txSig = await this.send(depositLeverageResponse.unsignedBase64Tx);
140
185
  return txSig;
141
186
  }
142
187
  /**
143
- * Modify the leverage of an existing position
144
- * @param request Modify leverage request parameters
145
- * @returns Modify leverage operation result
188
+ * Adjust the leverage of an existing position
189
+ * @param request Adjust leverage request parameters
190
+ * @returns Adjust leverage operation result
146
191
  */
147
- async modifyLeverage(params) {
148
- const body = await handleApiCall(() => this.http.post("leverage/modify", JSON.stringify(params)));
149
- const modifyLeverageResponse = JSON.parse(body);
150
- const txSig = await this.send(modifyLeverageResponse.unsignedBase64Tx, modifyLeverageResponse.signedSetBorrowRateBase64Tx);
192
+ async adjustLeverage(leverage, slippageBps) {
193
+ const req = {
194
+ owner: this.address(),
195
+ leverage,
196
+ slippageBps,
197
+ };
198
+ const body = await handleApiCall(() => this.http.post("leverage/adjust", JSON.stringify(req)));
199
+ const adjustLeverageResponse = JSON.parse(body);
200
+ const txSig = await this.send(adjustLeverageResponse.unsignedBase64Tx);
151
201
  return txSig;
152
202
  }
153
203
  /**
@@ -155,10 +205,17 @@ class Client {
155
205
  * @param request Withdraw leverage request parameters
156
206
  * @returns Withdraw leverage operation result
157
207
  */
158
- async withdrawLeverage(params) {
159
- const body = await handleApiCall(() => this.http.post("leverage/withdraw", JSON.stringify(params)));
208
+ async withdrawLeverage(selectedTokenMint, uiAmount, slippageBps, withdrawAll) {
209
+ const req = {
210
+ owner: this.address(),
211
+ selectedTokenMint,
212
+ withdrawAmountUi: uiAmount,
213
+ slippageBps,
214
+ withdrawAll,
215
+ };
216
+ const body = await handleApiCall(() => this.http.post("leverage/withdraw", JSON.stringify(req)));
160
217
  const withdrawLeverageResponse = JSON.parse(body);
161
- const txSig = await this.send(withdrawLeverageResponse.unsignedBase64Tx, withdrawLeverageResponse.signedSetBorrowRateBase64Tx);
218
+ const txSig = await this.send(withdrawLeverageResponse.unsignedBase64Tx);
162
219
  return txSig;
163
220
  }
164
221
  }
@@ -172,23 +229,21 @@ function handleStatusCode(statusCode) {
172
229
  }
173
230
  }
174
231
  // Helper function to handle API calls
175
- async function handleApiCall(apiCall) {
232
+ async function handleApiCall(call) {
176
233
  try {
177
- const response = await apiCall();
178
- handleStatusCode(response.status);
179
- return JSON.stringify(response.data);
234
+ const { data } = await call();
235
+ return JSON.stringify(data);
180
236
  }
181
- catch (error) {
182
- if (error instanceof axios_1.AxiosError) {
183
- const simplifiedError = {
184
- status: error.response?.status,
185
- message: error.message,
186
- url: error.config?.url,
187
- };
188
- console.error(simplifiedError);
189
- throw new Error(JSON.stringify(simplifiedError));
237
+ catch (e) {
238
+ if (axios_1.default.isAxiosError(e)) {
239
+ const res = e.response;
240
+ if (res && typeof res.data === "object") {
241
+ const payload = res.data;
242
+ throw new Error(`${payload.error}${payload.details ? ` ${payload.details}` : ""}`);
243
+ }
244
+ throw new Error(`${e.message}`);
190
245
  }
191
- throw error;
246
+ throw e;
192
247
  }
193
248
  }
194
249
  function getDummyProvider() {
@@ -196,3 +251,18 @@ function getDummyProvider() {
196
251
  skipPreflight: true,
197
252
  });
198
253
  }
254
+ function parseBank(bankJson) {
255
+ return {
256
+ mint: new anchor_1.web3.PublicKey(bankJson.mint),
257
+ key: new anchor_1.web3.PublicKey(bankJson.key),
258
+ group: new anchor_1.web3.PublicKey(bankJson.group),
259
+ supplyApy: Number(bankJson.supplyApy),
260
+ borrowApy: Number(bankJson.borrowApy),
261
+ utilizationRate: Number(bankJson.utilizationRate),
262
+ assetAmount: new anchor_1.BN(bankJson.assetAmount, "hex"),
263
+ assetAmountUi: Number(bankJson.assetAmountUi),
264
+ liabilityAmount: new anchor_1.BN(bankJson.liabilityAmount, "hex"),
265
+ liabilityAmountUi: Number(bankJson.liabilityAmountUi),
266
+ price: Number(bankJson.price),
267
+ };
268
+ }
package/dist/types.d.ts CHANGED
@@ -1,45 +1,36 @@
1
1
  import { BN, web3 } from "@coral-xyz/anchor";
2
- import Decimal from "decimal.js";
3
2
  export interface SendRequest {
4
3
  txns: string[];
5
4
  }
6
- export interface CreateObligationRequest {
7
- owner: web3.PublicKey;
8
- }
9
- export interface CreateObligationResponse {
10
- unsignedBase64Tx: string;
11
- }
12
5
  /**
13
6
  * Request to deposit collateral and create a leveraged position
14
7
  */
15
8
  export interface DepositLeverageRequest {
16
9
  owner: web3.PublicKey;
17
10
  selectedTokenMint: web3.PublicKey;
18
- depositAmount: Decimal;
11
+ depositAmountUi: number;
19
12
  leverage: number;
20
- slippagePct: number;
13
+ slippageBps: number;
21
14
  }
22
15
  /**
23
16
  * Response for deposit leverage operation
24
17
  */
25
18
  export interface DepositLeverageResponse {
26
19
  unsignedBase64Tx: string;
27
- signedSetBorrowRateBase64Tx: string;
28
20
  }
29
21
  /**
30
- * Request to modify the leverage of an existing position
22
+ * Request to adjust the leverage of an existing position
31
23
  */
32
- export interface ModifyLeverageRequest {
24
+ export interface AdjustLeverageRequest {
33
25
  owner: web3.PublicKey;
34
26
  leverage: number;
35
- slippagePct: number;
27
+ slippageBps: number;
36
28
  }
37
29
  /**
38
- * Response for modify leverage operation
30
+ * Response for adjust leverage operation
39
31
  */
40
- export interface ModifyLeverageResponse {
32
+ export interface AdjustLeverageResponse {
41
33
  unsignedBase64Tx: string;
42
- signedSetBorrowRateBase64Tx: string;
43
34
  }
44
35
  /**
45
36
  * Request to withdraw from a leveraged position
@@ -47,41 +38,67 @@ export interface ModifyLeverageResponse {
47
38
  export interface WithdrawLeverageRequest {
48
39
  owner: web3.PublicKey;
49
40
  selectedTokenMint: web3.PublicKey;
50
- withdrawAmount: Decimal;
51
- slippagePct: number;
52
- closePosition: boolean;
41
+ withdrawAmountUi: number;
42
+ slippageBps: number;
43
+ withdrawAll: boolean;
53
44
  }
54
45
  /**
55
46
  * Response for withdraw leverage operation
56
47
  */
57
48
  export interface WithdrawLeverageResponse {
58
49
  unsignedBase64Tx: string;
59
- signedSetBorrowRateBase64Tx: string;
50
+ }
51
+ export interface GetGroupResponse {
52
+ banks: Bank[];
60
53
  }
61
54
  export interface GetUserResponse {
62
- netValue: number;
63
- pnl: number;
64
- netAPY: number;
65
- leverage: number;
66
- totalCollateral: number;
67
- totalDebt: number;
68
- ltv: number;
69
- liquidationLtv: number;
55
+ wallet: UserWallet;
56
+ clendAccount: ClendAccount | undefined;
57
+ }
58
+ export interface UserWallet {
70
59
  usdcBalance: BN;
71
60
  usdcBalanceUi: number;
72
61
  jlpBalance: BN;
73
62
  jlpBalanceUi: number;
74
63
  solBalance: BN;
75
64
  solBalanceUi: number;
76
- obligationCreated: boolean;
77
65
  }
78
- export interface GetMarketResponse {
79
- totalBorrowTVL: number;
80
- totalSupplyTVL: number;
81
- usdcSupplyApy: number;
82
- usdcBorrowApy: number;
83
- usdcSupplyAmount: number;
84
- usdcBorrowAmount: number;
85
- usdcPrice: number;
86
- jlpPrice: number;
66
+ export interface ClendAccount {
67
+ balances: Balance[];
68
+ netValue: number;
69
+ netApy: number;
70
+ pnl: number;
71
+ totalAssetValue: number;
72
+ totalLiabilityValue: number;
73
+ healthFactorNotional: number;
74
+ healthFactorRiskAdjusted: number;
75
+ notionalLeverage: number;
76
+ riskAdjustedLeverage: number;
77
+ }
78
+ export interface Balance {
79
+ mint: web3.PublicKey;
80
+ bank: web3.PublicKey;
81
+ assetBalance: BN;
82
+ assetBalanceUi: number;
83
+ assetValue: number;
84
+ liabilityBalance: BN;
85
+ liabilityBalanceUi: number;
86
+ liabilityValue: number;
87
+ price: number;
88
+ }
89
+ export interface GetBankResponse {
90
+ bank: Bank;
91
+ }
92
+ export interface Bank {
93
+ group: web3.PublicKey;
94
+ key: web3.PublicKey;
95
+ mint: web3.PublicKey;
96
+ supplyApy: number;
97
+ borrowApy: number;
98
+ utilizationRate: number;
99
+ assetAmount: BN;
100
+ assetAmountUi: number;
101
+ liabilityAmount: BN;
102
+ liabilityAmountUi: number;
103
+ price: number;
87
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carrot-protocol/boost-http-client",
3
- "version": "0.1.7",
3
+ "version": "0.2.0-data-fixes-dev-5a03722",
4
4
  "description": "HTTP client for Carrot Boost API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,16 +1,20 @@
1
1
  import axios, { AxiosInstance, AxiosError, AxiosResponse } from "axios";
2
- import { AnchorProvider, Wallet, web3 } from "@coral-xyz/anchor";
2
+ import { AnchorProvider, BN, Wallet, web3 } from "@coral-xyz/anchor";
3
3
  import {
4
- CreateObligationResponse,
5
4
  DepositLeverageRequest,
6
5
  DepositLeverageResponse,
7
- GetMarketResponse,
6
+ GetBankResponse,
8
7
  GetUserResponse,
9
- ModifyLeverageRequest,
10
- ModifyLeverageResponse,
8
+ AdjustLeverageRequest,
9
+ AdjustLeverageResponse,
11
10
  SendRequest,
12
11
  WithdrawLeverageRequest,
13
12
  WithdrawLeverageResponse,
13
+ UserWallet,
14
+ ClendAccount,
15
+ Balance,
16
+ Bank,
17
+ GetGroupResponse,
14
18
  } from "./types";
15
19
  import encode from "bs58";
16
20
 
@@ -48,10 +52,7 @@ export class Client {
48
52
  }
49
53
 
50
54
  // sign and send tx to api for sending to network
51
- private async send(
52
- unsignedBase64Tx: string,
53
- ...signedBase64Txns: string[]
54
- ): Promise<string> {
55
+ private async send(unsignedBase64Tx: string): Promise<string> {
55
56
  // deserialize into tx obj
56
57
  const txBytes = Buffer.from(unsignedBase64Tx, "base64");
57
58
  const tx = web3.VersionedTransaction.deserialize(new Uint8Array(txBytes));
@@ -67,7 +68,7 @@ export class Client {
67
68
 
68
69
  // create request obj
69
70
  const sendRequest: SendRequest = {
70
- txns: [encodedAndSignedTx, ...signedBase64Txns],
71
+ txns: [encodedAndSignedTx],
71
72
  };
72
73
 
73
74
  // send to api
@@ -100,40 +101,113 @@ export class Client {
100
101
  this.http.get(`/user?user=${user.toString()}`),
101
102
  );
102
103
 
103
- const response: GetUserResponse = JSON.parse(body);
104
+ const jsonRawResponse: any = JSON.parse(body);
104
105
 
105
- return response;
106
+ // get tokens still in user wallet
107
+ const wallet: UserWallet = {
108
+ usdcBalance: new BN(jsonRawResponse.wallet.usdcBalance, "hex"),
109
+ usdcBalanceUi: Number(jsonRawResponse.wallet.usdcBalanceUi),
110
+ jlpBalance: new BN(jsonRawResponse.wallet.jlpBalance, "hex"),
111
+ jlpBalanceUi: Number(jsonRawResponse.wallet.jlpBalanceUi),
112
+ solBalance: new BN(jsonRawResponse.wallet.solBalance, "hex"),
113
+ solBalanceUi: Number(jsonRawResponse.wallet.solBalanceUi),
114
+ };
115
+
116
+ // if no clend account, return undefined for clend
117
+ if (!jsonRawResponse.clendAccount) {
118
+ return {
119
+ wallet,
120
+ clendAccount: undefined,
121
+ };
122
+ }
123
+
124
+ // parse lending account balances
125
+ const balances: Balance[] = [];
126
+ for (const b of jsonRawResponse.clendAccount.balances) {
127
+ balances.push({
128
+ mint: new web3.PublicKey(b.mint),
129
+ bank: new web3.PublicKey(b.bank),
130
+ assetBalance: new BN(b.assetBalance, "hex"),
131
+ assetBalanceUi: Number(b.assetBalanceUi),
132
+ assetValue: Number(b.assetValue),
133
+ liabilityBalance: new BN(b.liabilityBalance, "hex"),
134
+ liabilityBalanceUi: Number(b.liabilityBalanceUi),
135
+ liabilityValue: Number(b.liabilityValue),
136
+ price: Number(b.price),
137
+ });
138
+ }
139
+
140
+ // create clend account
141
+ const clendAccount: ClendAccount = {
142
+ balances,
143
+ netValue: Number(jsonRawResponse.clendAccount.netValue),
144
+ netApy: Number(jsonRawResponse.clendAccount.netApy),
145
+ pnl: Number(jsonRawResponse.clendAccount.pnl),
146
+ totalAssetValue: Number(jsonRawResponse.clendAccount.totalAssetValue),
147
+ totalLiabilityValue: Number(
148
+ jsonRawResponse.clendAccount.totalLiabilityValue,
149
+ ),
150
+ healthFactorNotional: Number(
151
+ jsonRawResponse.clendAccount.healthFactorNotional,
152
+ ),
153
+ healthFactorRiskAdjusted: Number(
154
+ jsonRawResponse.clendAccount.healthFactorRiskAdjusted,
155
+ ),
156
+ notionalLeverage: Number(jsonRawResponse.clendAccount.notionalLeverage),
157
+ riskAdjustedLeverage: Number(
158
+ jsonRawResponse.clendAccount.riskAdjustedLeverage,
159
+ ),
160
+ };
161
+
162
+ return {
163
+ wallet,
164
+ clendAccount,
165
+ };
106
166
  }
107
167
 
108
168
  /**
109
- * Get market details for the carrot boost market
110
- * @returns Market details
169
+ * Get market details
170
+ * @returns Group Details
111
171
  */
112
- async getMarket(): Promise<GetMarketResponse> {
113
- const body = await handleApiCall(() => this.http.get(`market`));
172
+ async getGroup(): Promise<GetGroupResponse> {
173
+ const body = await handleApiCall(() => this.http.get(`/group`));
174
+
175
+ const marketJson: any = JSON.parse(body);
176
+ const banks: Bank[] = [];
177
+ for (const bankJson of marketJson.banks) {
178
+ const bank = parseBank(bankJson);
179
+ banks.push(bank);
180
+ }
114
181
 
115
- const response: GetMarketResponse = JSON.parse(body);
182
+ const response: GetGroupResponse = {
183
+ banks,
184
+ };
116
185
 
117
186
  return response;
118
187
  }
119
188
 
120
189
  /**
121
- * Create an obligation for the client
122
- * @returns Transaction signature
190
+ * Get bank details
191
+ * @param addr token mint or bank public key
192
+ * @param addrType "mint" or "bank"
193
+ * @returns Bank details
123
194
  */
124
- async createObligation(): Promise<string> {
195
+ async getBank(
196
+ addr: web3.PublicKey,
197
+ addrType: "mint" | "bank",
198
+ ): Promise<GetBankResponse> {
125
199
  const body = await handleApiCall(() =>
126
- this.http.post(
127
- "createObligation",
128
- JSON.stringify({ owner: this.address() }),
129
- ),
200
+ this.http.get(`/bank?${addrType}=${addr.toString()}`),
130
201
  );
131
202
 
132
- const createObligationResponse: CreateObligationResponse = JSON.parse(body);
203
+ const bankJson: any = JSON.parse(body);
204
+ const bank = parseBank(bankJson.bank);
133
205
 
134
- const txSig = await this.send(createObligationResponse.unsignedBase64Tx);
206
+ const response: GetBankResponse = {
207
+ bank,
208
+ };
135
209
 
136
- return txSig;
210
+ return response;
137
211
  }
138
212
 
139
213
  /**
@@ -141,37 +215,48 @@ export class Client {
141
215
  * @param request Deposit leverage request parameters
142
216
  * @returns Deposit leverage operation result
143
217
  */
144
- async depositLeverage(params: DepositLeverageRequest): Promise<string> {
218
+ async depositLeverage(
219
+ selectedTokenMint: web3.PublicKey,
220
+ uiAmount: number,
221
+ leverage: number,
222
+ slippageBps: number,
223
+ ): Promise<string> {
224
+ const req: DepositLeverageRequest = {
225
+ owner: this.address(),
226
+ selectedTokenMint,
227
+ depositAmountUi: uiAmount,
228
+ leverage,
229
+ slippageBps,
230
+ };
145
231
  const body = await handleApiCall(() =>
146
- this.http.post("leverage/deposit", JSON.stringify(params)),
232
+ this.http.post("leverage/deposit", JSON.stringify(req)),
147
233
  );
148
234
 
149
235
  const depositLeverageResponse: DepositLeverageResponse = JSON.parse(body);
150
236
 
151
- const txSig = await this.send(
152
- depositLeverageResponse.unsignedBase64Tx,
153
- depositLeverageResponse.signedSetBorrowRateBase64Tx,
154
- );
237
+ const txSig = await this.send(depositLeverageResponse.unsignedBase64Tx);
155
238
 
156
239
  return txSig;
157
240
  }
158
241
 
159
242
  /**
160
- * Modify the leverage of an existing position
161
- * @param request Modify leverage request parameters
162
- * @returns Modify leverage operation result
243
+ * Adjust the leverage of an existing position
244
+ * @param request Adjust leverage request parameters
245
+ * @returns Adjust leverage operation result
163
246
  */
164
- async modifyLeverage(params: ModifyLeverageRequest): Promise<any> {
247
+ async adjustLeverage(leverage: number, slippageBps: number): Promise<any> {
248
+ const req: AdjustLeverageRequest = {
249
+ owner: this.address(),
250
+ leverage,
251
+ slippageBps,
252
+ };
165
253
  const body = await handleApiCall(() =>
166
- this.http.post("leverage/modify", JSON.stringify(params)),
254
+ this.http.post("leverage/adjust", JSON.stringify(req)),
167
255
  );
168
256
 
169
- const modifyLeverageResponse: ModifyLeverageResponse = JSON.parse(body);
257
+ const adjustLeverageResponse: AdjustLeverageResponse = JSON.parse(body);
170
258
 
171
- const txSig = await this.send(
172
- modifyLeverageResponse.unsignedBase64Tx,
173
- modifyLeverageResponse.signedSetBorrowRateBase64Tx,
174
- );
259
+ const txSig = await this.send(adjustLeverageResponse.unsignedBase64Tx);
175
260
 
176
261
  return txSig;
177
262
  }
@@ -181,22 +266,38 @@ export class Client {
181
266
  * @param request Withdraw leverage request parameters
182
267
  * @returns Withdraw leverage operation result
183
268
  */
184
- async withdrawLeverage(params: WithdrawLeverageRequest): Promise<any> {
269
+ async withdrawLeverage(
270
+ selectedTokenMint: web3.PublicKey,
271
+ uiAmount: number,
272
+ slippageBps: number,
273
+ withdrawAll: boolean,
274
+ ): Promise<any> {
275
+ const req: WithdrawLeverageRequest = {
276
+ owner: this.address(),
277
+ selectedTokenMint,
278
+ withdrawAmountUi: uiAmount,
279
+ slippageBps,
280
+ withdrawAll,
281
+ };
185
282
  const body = await handleApiCall(() =>
186
- this.http.post("leverage/withdraw", JSON.stringify(params)),
283
+ this.http.post("leverage/withdraw", JSON.stringify(req)),
187
284
  );
188
285
 
189
286
  const withdrawLeverageResponse: WithdrawLeverageResponse = JSON.parse(body);
190
287
 
191
- const txSig = await this.send(
192
- withdrawLeverageResponse.unsignedBase64Tx,
193
- withdrawLeverageResponse.signedSetBorrowRateBase64Tx,
194
- );
288
+ const txSig = await this.send(withdrawLeverageResponse.unsignedBase64Tx);
195
289
 
196
290
  return txSig;
197
291
  }
198
292
  }
199
293
 
294
+ type ApiErrorPayload = {
295
+ error: string;
296
+ details?: unknown;
297
+ path: string;
298
+ timestamp: string;
299
+ };
300
+
200
301
  function handleStatusCode(statusCode: number): void {
201
302
  switch (statusCode) {
202
303
  case 200:
@@ -208,23 +309,23 @@ function handleStatusCode(statusCode: number): void {
208
309
 
209
310
  // Helper function to handle API calls
210
311
  async function handleApiCall<T>(
211
- apiCall: () => Promise<AxiosResponse<T>>,
212
- ): Promise<any> {
312
+ call: () => Promise<AxiosResponse<T>>,
313
+ ): Promise<string> {
213
314
  try {
214
- const response = await apiCall();
215
- handleStatusCode(response.status);
216
- return JSON.stringify(response.data);
217
- } catch (error) {
218
- if (error instanceof AxiosError) {
219
- const simplifiedError = {
220
- status: error.response?.status,
221
- message: error.message,
222
- url: error.config?.url,
223
- };
224
- console.error(simplifiedError);
225
- throw new Error(JSON.stringify(simplifiedError));
315
+ const { data } = await call();
316
+ return JSON.stringify(data);
317
+ } catch (e) {
318
+ if (axios.isAxiosError(e)) {
319
+ const res = e.response;
320
+ if (res && typeof res.data === "object") {
321
+ const payload = res.data as ApiErrorPayload;
322
+ throw new Error(
323
+ `${payload.error}${payload.details ? ` ${payload.details}` : ""}`,
324
+ );
325
+ }
326
+ throw new Error(`${e.message}`);
226
327
  }
227
- throw error;
328
+ throw e;
228
329
  }
229
330
  }
230
331
 
@@ -237,3 +338,19 @@ function getDummyProvider(): AnchorProvider {
237
338
  },
238
339
  );
239
340
  }
341
+
342
+ function parseBank(bankJson: any): Bank {
343
+ return {
344
+ mint: new web3.PublicKey(bankJson.mint),
345
+ key: new web3.PublicKey(bankJson.key),
346
+ group: new web3.PublicKey(bankJson.group),
347
+ supplyApy: Number(bankJson.supplyApy),
348
+ borrowApy: Number(bankJson.borrowApy),
349
+ utilizationRate: Number(bankJson.utilizationRate),
350
+ assetAmount: new BN(bankJson.assetAmount, "hex"),
351
+ assetAmountUi: Number(bankJson.assetAmountUi),
352
+ liabilityAmount: new BN(bankJson.liabilityAmount, "hex"),
353
+ liabilityAmountUi: Number(bankJson.liabilityAmountUi),
354
+ price: Number(bankJson.price),
355
+ };
356
+ }
package/src/types.ts CHANGED
@@ -6,23 +6,15 @@ export interface SendRequest {
6
6
  txns: string[];
7
7
  }
8
8
 
9
- export interface CreateObligationRequest {
10
- owner: web3.PublicKey;
11
- }
12
-
13
- export interface CreateObligationResponse {
14
- unsignedBase64Tx: string;
15
- }
16
-
17
9
  /**
18
10
  * Request to deposit collateral and create a leveraged position
19
11
  */
20
12
  export interface DepositLeverageRequest {
21
13
  owner: web3.PublicKey;
22
14
  selectedTokenMint: web3.PublicKey;
23
- depositAmount: Decimal;
15
+ depositAmountUi: number;
24
16
  leverage: number;
25
- slippagePct: number;
17
+ slippageBps: number;
26
18
  }
27
19
 
28
20
  /**
@@ -30,24 +22,22 @@ export interface DepositLeverageRequest {
30
22
  */
31
23
  export interface DepositLeverageResponse {
32
24
  unsignedBase64Tx: string;
33
- signedSetBorrowRateBase64Tx: string;
34
25
  }
35
26
 
36
27
  /**
37
- * Request to modify the leverage of an existing position
28
+ * Request to adjust the leverage of an existing position
38
29
  */
39
- export interface ModifyLeverageRequest {
30
+ export interface AdjustLeverageRequest {
40
31
  owner: web3.PublicKey;
41
32
  leverage: number;
42
- slippagePct: number;
33
+ slippageBps: number;
43
34
  }
44
35
 
45
36
  /**
46
- * Response for modify leverage operation
37
+ * Response for adjust leverage operation
47
38
  */
48
- export interface ModifyLeverageResponse {
39
+ export interface AdjustLeverageResponse {
49
40
  unsignedBase64Tx: string;
50
- signedSetBorrowRateBase64Tx: string;
51
41
  }
52
42
 
53
43
  /**
@@ -56,9 +46,9 @@ export interface ModifyLeverageResponse {
56
46
  export interface WithdrawLeverageRequest {
57
47
  owner: web3.PublicKey;
58
48
  selectedTokenMint: web3.PublicKey;
59
- withdrawAmount: Decimal;
60
- slippagePct: number;
61
- closePosition: boolean;
49
+ withdrawAmountUi: number;
50
+ slippageBps: number;
51
+ withdrawAll: boolean;
62
52
  }
63
53
 
64
54
  /**
@@ -66,34 +56,65 @@ export interface WithdrawLeverageRequest {
66
56
  */
67
57
  export interface WithdrawLeverageResponse {
68
58
  unsignedBase64Tx: string;
69
- signedSetBorrowRateBase64Tx: string;
59
+ }
60
+
61
+ export interface GetGroupResponse {
62
+ banks: Bank[];
70
63
  }
71
64
 
72
65
  export interface GetUserResponse {
73
- netValue: number;
74
- pnl: number;
75
- netAPY: number;
76
- leverage: number;
77
- totalCollateral: number;
78
- totalDebt: number;
79
- ltv: number;
80
- liquidationLtv: number;
66
+ wallet: UserWallet;
67
+ clendAccount: ClendAccount | undefined;
68
+ }
69
+
70
+ export interface UserWallet {
81
71
  usdcBalance: BN;
82
72
  usdcBalanceUi: number;
83
73
  jlpBalance: BN;
84
74
  jlpBalanceUi: number;
85
75
  solBalance: BN;
86
76
  solBalanceUi: number;
87
- obligationCreated: boolean;
88
77
  }
89
78
 
90
- export interface GetMarketResponse {
91
- totalBorrowTVL: number;
92
- totalSupplyTVL: number;
93
- usdcSupplyApy: number;
94
- usdcBorrowApy: number;
95
- usdcSupplyAmount: number;
96
- usdcBorrowAmount: number;
97
- usdcPrice: number;
98
- jlpPrice: number;
79
+ export interface ClendAccount {
80
+ balances: Balance[];
81
+ netValue: number;
82
+ netApy: number;
83
+ pnl: number;
84
+ totalAssetValue: number;
85
+ totalLiabilityValue: number;
86
+ healthFactorNotional: number;
87
+ healthFactorRiskAdjusted: number;
88
+ notionalLeverage: number;
89
+ riskAdjustedLeverage: number;
90
+ }
91
+
92
+ export interface Balance {
93
+ mint: web3.PublicKey;
94
+ bank: web3.PublicKey;
95
+ assetBalance: BN;
96
+ assetBalanceUi: number;
97
+ assetValue: number;
98
+ liabilityBalance: BN;
99
+ liabilityBalanceUi: number;
100
+ liabilityValue: number;
101
+ price: number;
102
+ }
103
+
104
+ export interface GetBankResponse {
105
+ bank: Bank;
106
+ }
107
+
108
+ export interface Bank {
109
+ group: web3.PublicKey;
110
+ key: web3.PublicKey;
111
+ mint: web3.PublicKey;
112
+ supplyApy: number;
113
+ borrowApy: number;
114
+ utilizationRate: number;
115
+ assetAmount: BN;
116
+ assetAmountUi: number;
117
+ liabilityAmount: BN;
118
+ liabilityAmountUi: number;
119
+ price: number;
99
120
  }