@carrot-protocol/boost-http-client 0.2.0-mrgn-fork1-dev-37e1e0b → 0.2.0-mrgn-fork1-dev-4dad661

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, GetBankResponse, GetUserResponse, AdjustLeverageRequest, WithdrawLeverageRequest } from "./types";
2
+ import { DepositLeverageRequest, GetBankResponse, GetUserResponse, AdjustLeverageRequest, WithdrawLeverageRequest, GetGroupResponse } from "./types";
3
3
  export * from "./types";
4
4
  /**
5
5
  * HTTP Client for Carrot Boost API
@@ -26,12 +26,18 @@ export declare class Client {
26
26
  * @returns User details
27
27
  */
28
28
  getUser(user?: web3.PublicKey): Promise<GetUserResponse>;
29
+ /**
30
+ * Get market details
31
+ * @returns Group Details
32
+ */
33
+ getGroup(): Promise<GetGroupResponse>;
29
34
  /**
30
35
  * Get bank details
31
- * @param mint token mint of the bank public key
36
+ * @param addr token mint or bank public key
37
+ * @param addrType "mint" or "bank"
32
38
  * @returns Bank details
33
39
  */
34
- getBank(mint: web3.PublicKey): Promise<GetBankResponse>;
40
+ getBank(addr: web3.PublicKey, addrType: "mint" | "bank"): Promise<GetBankResponse>;
35
41
  /**
36
42
  * Deposit collateral and create a leveraged position
37
43
  * @param request Deposit leverage request parameters
package/dist/index.js CHANGED
@@ -106,18 +106,86 @@ class Client {
106
106
  user = this.address();
107
107
  }
108
108
  const body = await handleApiCall(() => this.http.get(`/user?user=${user.toString()}`));
109
- console.log(JSON.stringify(body));
110
- const response = JSON.parse(body);
109
+ const jsonRawResponse = JSON.parse(body);
110
+ // get tokens still in user wallet
111
+ const wallet = {
112
+ usdcBalance: new anchor_1.BN(jsonRawResponse.wallet.usdcBalance, "hex"),
113
+ usdcBalanceUi: Number(jsonRawResponse.wallet.usdcBalanceUi),
114
+ jlpBalance: new anchor_1.BN(jsonRawResponse.wallet.jlpBalance, "hex"),
115
+ jlpBalanceUi: Number(jsonRawResponse.wallet.jlpBalanceUi),
116
+ solBalance: new anchor_1.BN(jsonRawResponse.wallet.solBalance, "hex"),
117
+ solBalanceUi: Number(jsonRawResponse.wallet.solBalanceUi),
118
+ };
119
+ // if no clend account, return undefined for clend
120
+ if (!jsonRawResponse.clendAccount) {
121
+ return {
122
+ wallet,
123
+ clendAccount: undefined,
124
+ };
125
+ }
126
+ // parse lending account balances
127
+ const balances = [];
128
+ for (const b of jsonRawResponse.clendAccount.balances) {
129
+ balances.push({
130
+ mint: new anchor_1.web3.PublicKey(b.mint),
131
+ bank: new anchor_1.web3.PublicKey(b.bank),
132
+ assetBalance: new anchor_1.BN(b.assetBalance, "hex"),
133
+ assetBalanceUi: Number(b.assetBalanceUi),
134
+ assetValue: Number(b.assetValue),
135
+ liabilityBalance: new anchor_1.BN(b.liabilityBalance, "hex"),
136
+ liabilityBalanceUi: Number(b.liabilityBalanceUi),
137
+ liabilityValue: Number(b.liabilityValue),
138
+ price: Number(b.price),
139
+ });
140
+ }
141
+ // create clend account
142
+ const clendAccount = {
143
+ balances,
144
+ netValue: Number(jsonRawResponse.clendAccount.netValue),
145
+ netApy: Number(jsonRawResponse.clendAccount.netApy),
146
+ pnl: Number(jsonRawResponse.clendAccount.pnl),
147
+ totalAssetValue: Number(jsonRawResponse.clendAccount.totalAssetValue),
148
+ totalLiabilityValue: Number(jsonRawResponse.clendAccount.totalLiabilityValue),
149
+ healthFactorNotional: Number(jsonRawResponse.clendAccount.healthFactorNotional),
150
+ healthFactorRiskAdjusted: Number(jsonRawResponse.clendAccount.healthFactorRiskAdjusted),
151
+ notionalLeverage: Number(jsonRawResponse.clendAccount.notionalLeverage),
152
+ riskAdjustedLeverage: Number(jsonRawResponse.clendAccount.riskAdjustedLeverage),
153
+ };
154
+ return {
155
+ wallet,
156
+ clendAccount,
157
+ };
158
+ }
159
+ /**
160
+ * Get market details
161
+ * @returns Group Details
162
+ */
163
+ async getGroup() {
164
+ const body = await handleApiCall(() => this.http.get(`/group`));
165
+ const marketJson = JSON.parse(body);
166
+ const banks = [];
167
+ for (const bankJson of marketJson.banks) {
168
+ const bank = parseBank(bankJson);
169
+ banks.push(bank);
170
+ }
171
+ const response = {
172
+ banks,
173
+ };
111
174
  return response;
112
175
  }
113
176
  /**
114
177
  * Get bank details
115
- * @param mint token mint of the bank public key
178
+ * @param addr token mint or bank public key
179
+ * @param addrType "mint" or "bank"
116
180
  * @returns Bank details
117
181
  */
118
- async getBank(mint) {
119
- const body = await handleApiCall(() => this.http.get(`/bank?mint=${mint.toString()}`));
120
- const response = JSON.parse(body);
182
+ async getBank(addr, addrType) {
183
+ const body = await handleApiCall(() => this.http.get(`/bank?${addrType}=${addr.toString()}`));
184
+ const bankJson = JSON.parse(body);
185
+ const bank = parseBank(bankJson.bank);
186
+ const response = {
187
+ bank,
188
+ };
121
189
  return response;
122
190
  }
123
191
  /**
@@ -188,3 +256,18 @@ function getDummyProvider() {
188
256
  skipPreflight: true,
189
257
  });
190
258
  }
259
+ function parseBank(bankJson) {
260
+ return {
261
+ mint: new anchor_1.web3.PublicKey(bankJson.mint),
262
+ key: new anchor_1.web3.PublicKey(bankJson.key),
263
+ group: new anchor_1.web3.PublicKey(bankJson.group),
264
+ supplyApy: Number(bankJson.supplyApy),
265
+ borrowApy: Number(bankJson.borrowApy),
266
+ utilizationRate: Number(bankJson.utilizationRate),
267
+ assetAmount: new anchor_1.BN(bankJson.assetAmount, "hex"),
268
+ assetAmountUi: Number(bankJson.assetAmountUi),
269
+ liabilityAmount: new anchor_1.BN(bankJson.liabilityAmount, "hex"),
270
+ liabilityAmountUi: Number(bankJson.liabilityAmountUi),
271
+ price: Number(bankJson.price),
272
+ };
273
+ }
package/dist/types.d.ts CHANGED
@@ -54,21 +54,57 @@ export interface WithdrawLeverageRequest {
54
54
  export interface WithdrawLeverageResponse {
55
55
  unsignedBase64Tx: string;
56
56
  }
57
+ export interface GetGroupResponse {
58
+ banks: Bank[];
59
+ }
57
60
  export interface GetUserResponse {
61
+ wallet: UserWallet;
62
+ clendAccount: ClendAccount | undefined;
63
+ }
64
+ export interface UserWallet {
58
65
  usdcBalance: BN;
59
66
  usdcBalanceUi: number;
60
67
  jlpBalance: BN;
61
68
  jlpBalanceUi: number;
62
69
  solBalance: BN;
63
70
  solBalanceUi: number;
64
- clendAccount: any | undefined;
71
+ }
72
+ export interface ClendAccount {
73
+ balances: Balance[];
74
+ netValue: number;
75
+ netApy: number;
76
+ pnl: number;
77
+ totalAssetValue: number;
78
+ totalLiabilityValue: number;
79
+ healthFactorNotional: number;
80
+ healthFactorRiskAdjusted: number;
81
+ notionalLeverage: number;
82
+ riskAdjustedLeverage: number;
83
+ }
84
+ export interface Balance {
85
+ mint: web3.PublicKey;
86
+ bank: web3.PublicKey;
87
+ assetBalance: BN;
88
+ assetBalanceUi: number;
89
+ assetValue: number;
90
+ liabilityBalance: BN;
91
+ liabilityBalanceUi: number;
92
+ liabilityValue: number;
93
+ price: number;
65
94
  }
66
95
  export interface GetBankResponse {
67
- totalBorrowTVL: number;
68
- totalSupplyTVL: number;
96
+ bank: Bank;
97
+ }
98
+ export interface Bank {
99
+ group: web3.PublicKey;
100
+ key: web3.PublicKey;
101
+ mint: web3.PublicKey;
69
102
  supplyApy: number;
70
103
  borrowApy: number;
71
- supplyAmount: number;
72
- borrowAmount: number;
104
+ utilizationRate: number;
105
+ assetAmount: BN;
106
+ assetAmountUi: number;
107
+ liabilityAmount: BN;
108
+ liabilityAmountUi: number;
73
109
  price: number;
74
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carrot-protocol/boost-http-client",
3
- "version": "0.2.0-mrgn-fork1-dev-37e1e0b",
3
+ "version": "0.2.0-mrgn-fork1-dev-4dad661",
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,5 +1,5 @@
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
4
  DepositLeverageRequest,
5
5
  DepositLeverageResponse,
@@ -10,6 +10,11 @@ import {
10
10
  SendRequest,
11
11
  WithdrawLeverageRequest,
12
12
  WithdrawLeverageResponse,
13
+ UserWallet,
14
+ ClendAccount,
15
+ Balance,
16
+ Bank,
17
+ GetGroupResponse,
13
18
  } from "./types";
14
19
  import encode from "bs58";
15
20
 
@@ -95,24 +100,112 @@ export class Client {
95
100
  const body = await handleApiCall(() =>
96
101
  this.http.get(`/user?user=${user.toString()}`),
97
102
  );
98
- console.log(JSON.stringify(body));
99
103
 
100
- const response: GetUserResponse = JSON.parse(body);
104
+ const jsonRawResponse: any = JSON.parse(body);
105
+
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
+ };
166
+ }
167
+
168
+ /**
169
+ * Get market details
170
+ * @returns Group Details
171
+ */
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
+ }
181
+
182
+ const response: GetGroupResponse = {
183
+ banks,
184
+ };
101
185
 
102
186
  return response;
103
187
  }
104
188
 
105
189
  /**
106
190
  * Get bank details
107
- * @param mint token mint of the bank public key
191
+ * @param addr token mint or bank public key
192
+ * @param addrType "mint" or "bank"
108
193
  * @returns Bank details
109
194
  */
110
- async getBank(mint: web3.PublicKey): Promise<GetBankResponse> {
195
+ async getBank(
196
+ addr: web3.PublicKey,
197
+ addrType: "mint" | "bank",
198
+ ): Promise<GetBankResponse> {
111
199
  const body = await handleApiCall(() =>
112
- this.http.get(`/bank?mint=${mint.toString()}`),
200
+ this.http.get(`/bank?${addrType}=${addr.toString()}`),
113
201
  );
114
202
 
115
- const response: GetBankResponse = JSON.parse(body);
203
+ const bankJson: any = JSON.parse(body);
204
+ const bank = parseBank(bankJson.bank);
205
+
206
+ const response: GetBankResponse = {
207
+ bank,
208
+ };
116
209
 
117
210
  return response;
118
211
  }
@@ -209,3 +302,19 @@ function getDummyProvider(): AnchorProvider {
209
302
  },
210
303
  );
211
304
  }
305
+
306
+ function parseBank(bankJson: any): Bank {
307
+ return {
308
+ mint: new web3.PublicKey(bankJson.mint),
309
+ key: new web3.PublicKey(bankJson.key),
310
+ group: new web3.PublicKey(bankJson.group),
311
+ supplyApy: Number(bankJson.supplyApy),
312
+ borrowApy: Number(bankJson.borrowApy),
313
+ utilizationRate: Number(bankJson.utilizationRate),
314
+ assetAmount: new BN(bankJson.assetAmount, "hex"),
315
+ assetAmountUi: Number(bankJson.assetAmountUi),
316
+ liabilityAmount: new BN(bankJson.liabilityAmount, "hex"),
317
+ liabilityAmountUi: Number(bankJson.liabilityAmountUi),
318
+ price: Number(bankJson.price),
319
+ };
320
+ }
package/src/types.ts CHANGED
@@ -66,22 +66,63 @@ export interface WithdrawLeverageResponse {
66
66
  unsignedBase64Tx: string;
67
67
  }
68
68
 
69
+ export interface GetGroupResponse {
70
+ banks: Bank[];
71
+ }
72
+
69
73
  export interface GetUserResponse {
74
+ wallet: UserWallet;
75
+ clendAccount: ClendAccount | undefined;
76
+ }
77
+
78
+ export interface UserWallet {
70
79
  usdcBalance: BN;
71
80
  usdcBalanceUi: number;
72
81
  jlpBalance: BN;
73
82
  jlpBalanceUi: number;
74
83
  solBalance: BN;
75
84
  solBalanceUi: number;
76
- clendAccount: any | undefined;
85
+ }
86
+
87
+ export interface ClendAccount {
88
+ balances: Balance[];
89
+ netValue: number;
90
+ netApy: number;
91
+ pnl: number;
92
+ totalAssetValue: number;
93
+ totalLiabilityValue: number;
94
+ healthFactorNotional: number;
95
+ healthFactorRiskAdjusted: number;
96
+ notionalLeverage: number;
97
+ riskAdjustedLeverage: number;
98
+ }
99
+
100
+ export interface Balance {
101
+ mint: web3.PublicKey;
102
+ bank: web3.PublicKey;
103
+ assetBalance: BN;
104
+ assetBalanceUi: number;
105
+ assetValue: number;
106
+ liabilityBalance: BN;
107
+ liabilityBalanceUi: number;
108
+ liabilityValue: number;
109
+ price: number;
77
110
  }
78
111
 
79
112
  export interface GetBankResponse {
80
- totalBorrowTVL: number;
81
- totalSupplyTVL: number;
113
+ bank: Bank;
114
+ }
115
+
116
+ export interface Bank {
117
+ group: web3.PublicKey;
118
+ key: web3.PublicKey;
119
+ mint: web3.PublicKey;
82
120
  supplyApy: number;
83
121
  borrowApy: number;
84
- supplyAmount: number;
85
- borrowAmount: number;
122
+ utilizationRate: number;
123
+ assetAmount: BN;
124
+ assetAmountUi: number;
125
+ liabilityAmount: BN;
126
+ liabilityAmountUi: number;
86
127
  price: number;
87
128
  }