@carrot-protocol/http-client 0.2.6-spl1-dev-fee4913 → 0.2.6-spl1-dev-0bcb5aa
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/package.json +5 -1
- package/carrot-protocol-http-client-0.2.6.tgz +0 -0
- package/src/index.ts +0 -297
- package/tsconfig.json +0 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carrot-protocol/http-client",
|
|
3
|
-
"version": "0.2.6-spl1-dev-
|
|
3
|
+
"version": "0.2.6-spl1-dev-0bcb5aa",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"fmt:check": "prettier --check src/",
|
|
10
10
|
"fmt": "prettier --write src/"
|
|
11
11
|
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
12
16
|
"author": "",
|
|
13
17
|
"license": "ISC",
|
|
14
18
|
"dependencies": {
|
|
Binary file
|
package/src/index.ts
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
import fetch from "cross-fetch";
|
|
2
|
-
import { web3, BN, AnchorProvider } from "@coral-xyz/anchor";
|
|
3
|
-
import { encode } from "bs58";
|
|
4
|
-
import { Vault, VaultPerformance } from "@carrot-protocol/common";
|
|
5
|
-
|
|
6
|
-
export class Client {
|
|
7
|
-
readonly baseUrl: string;
|
|
8
|
-
readonly provider: AnchorProvider;
|
|
9
|
-
private headers = { "Content-Type": "application/json" };
|
|
10
|
-
|
|
11
|
-
constructor(baseUrl: string, provider: AnchorProvider) {
|
|
12
|
-
this.baseUrl = baseUrl;
|
|
13
|
-
this.provider = provider;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async index(): Promise<void> {
|
|
17
|
-
const response = await fetch(this.baseUrl);
|
|
18
|
-
checkResponse(response);
|
|
19
|
-
|
|
20
|
-
const body = await response.json();
|
|
21
|
-
|
|
22
|
-
console.log(JSON.stringify(body, undefined, 2));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async getVault(vault: web3.PublicKey): Promise<Vault> {
|
|
26
|
-
const url = new URL(`${this.baseUrl}/vault?vault=${vault.toString()}`);
|
|
27
|
-
const response = await fetch(url, {
|
|
28
|
-
method: "GET",
|
|
29
|
-
headers: this.headers,
|
|
30
|
-
});
|
|
31
|
-
checkResponse(response);
|
|
32
|
-
|
|
33
|
-
const rawBody = await response.json();
|
|
34
|
-
|
|
35
|
-
let body: Vault = JSON.parse(JSON.stringify(rawBody));
|
|
36
|
-
body.address = new web3.PublicKey(body.address);
|
|
37
|
-
body.authority = new web3.PublicKey(body.authority);
|
|
38
|
-
body.shares = new web3.PublicKey(body.shares);
|
|
39
|
-
body.sharesSupply = new BN(body.sharesSupply);
|
|
40
|
-
|
|
41
|
-
body.assets = body.assets.map((asset) => ({
|
|
42
|
-
...asset,
|
|
43
|
-
assetId: Number(asset.assetId),
|
|
44
|
-
balanceUsd: Number(asset.balanceUsd),
|
|
45
|
-
mint: new web3.PublicKey(asset.mint),
|
|
46
|
-
ata: new web3.PublicKey(asset.ata),
|
|
47
|
-
oracle: new web3.PublicKey(asset.oracle),
|
|
48
|
-
ataAmount: new BN(asset.ataAmount, "hex"),
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
body.strategies = body.strategies.map((strategy) => ({
|
|
52
|
-
address: new web3.PublicKey(strategy.address),
|
|
53
|
-
record: {
|
|
54
|
-
strategyId: Number(strategy.record.strategyId),
|
|
55
|
-
assetId: Number(strategy.record.assetId),
|
|
56
|
-
balance: new BN(strategy.record.balance, "hex"),
|
|
57
|
-
balanceUsd: Number(strategy.record.balanceUsd),
|
|
58
|
-
netEarnings: new BN(strategy.record.netEarnings, "hex"),
|
|
59
|
-
},
|
|
60
|
-
metadata: {
|
|
61
|
-
...strategy.metadata,
|
|
62
|
-
strategyId: Number(strategy.metadata.strategyId),
|
|
63
|
-
assetMint: new web3.PublicKey(strategy.metadata.assetMint),
|
|
64
|
-
vault: new web3.PublicKey(strategy.metadata.vault),
|
|
65
|
-
},
|
|
66
|
-
strategyType: strategy.strategyType,
|
|
67
|
-
}));
|
|
68
|
-
|
|
69
|
-
return body;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async getVaultPerformance(vault: web3.PublicKey): Promise<VaultPerformance> {
|
|
73
|
-
const url = new URL(
|
|
74
|
-
`${this.baseUrl}/performance?vault=${vault.toString()}`,
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
const response = await fetch(url, {
|
|
78
|
-
method: "GET",
|
|
79
|
-
headers: this.headers,
|
|
80
|
-
});
|
|
81
|
-
checkResponse(response);
|
|
82
|
-
|
|
83
|
-
const rawBody = await response.json();
|
|
84
|
-
|
|
85
|
-
let body: VaultPerformance = JSON.parse(JSON.stringify(rawBody));
|
|
86
|
-
body.apy = Number(body.apy);
|
|
87
|
-
|
|
88
|
-
for (let strats of body.strategyAPY) {
|
|
89
|
-
strats.apy = Number(strats.apy);
|
|
90
|
-
strats.balanceUsd = Number(strats.balanceUsd);
|
|
91
|
-
strats.strategy = new web3.PublicKey(strats.strategy);
|
|
92
|
-
strats.vaultWeight = Number(strats.vaultWeight);
|
|
93
|
-
strats.assetMint = new web3.PublicKey(strats.assetMint);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return body;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async getUser(vault: web3.PublicKey): Promise<UserResponse> {
|
|
100
|
-
const url = new URL(
|
|
101
|
-
`${this.baseUrl}/user?vault=${vault.toString()}&user=${this.provider.publicKey.toString()}`,
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const response = await fetch(url, {
|
|
105
|
-
method: "GET",
|
|
106
|
-
headers: this.headers,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
checkResponse(response);
|
|
110
|
-
|
|
111
|
-
const body: UserResponse = JSON.parse(
|
|
112
|
-
JSON.stringify(await response.json()),
|
|
113
|
-
);
|
|
114
|
-
body.solAmount = new BN(body.solAmount, "hex");
|
|
115
|
-
body.sharesAmount = new BN(body.sharesAmount, "hex");
|
|
116
|
-
|
|
117
|
-
body.assets = body.assets.map((asset) => ({
|
|
118
|
-
...asset,
|
|
119
|
-
mint: new web3.PublicKey(asset.mint),
|
|
120
|
-
amount: new BN(asset.amount, "hex"),
|
|
121
|
-
}));
|
|
122
|
-
|
|
123
|
-
return body;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async issue(
|
|
127
|
-
vault: web3.PublicKey,
|
|
128
|
-
assetMint: web3.PublicKey,
|
|
129
|
-
amount: BN,
|
|
130
|
-
): Promise<string> {
|
|
131
|
-
const url = new URL(`${this.baseUrl}/issue`);
|
|
132
|
-
|
|
133
|
-
const body: IssueRequest = {
|
|
134
|
-
user: this.provider.publicKey,
|
|
135
|
-
vault,
|
|
136
|
-
amount,
|
|
137
|
-
assetMint,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const response = await fetch(url, {
|
|
141
|
-
method: "POST",
|
|
142
|
-
headers: this.headers,
|
|
143
|
-
body: JSON.stringify(body),
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
checkResponse(response);
|
|
147
|
-
|
|
148
|
-
const responseBody = await response.json();
|
|
149
|
-
|
|
150
|
-
const issueResponse: IssueResponse = JSON.parse(
|
|
151
|
-
JSON.stringify(responseBody),
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
const txSig = await this.send(issueResponse.tx);
|
|
155
|
-
|
|
156
|
-
return txSig;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async redeem(vault: web3.PublicKey, assetMint: web3.PublicKey, amount: BN) {
|
|
160
|
-
const url = new URL(`${this.baseUrl}/redeem`);
|
|
161
|
-
|
|
162
|
-
const body: RedeemRequest = {
|
|
163
|
-
user: this.provider.publicKey,
|
|
164
|
-
vault,
|
|
165
|
-
amount,
|
|
166
|
-
assetMint,
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const response = await fetch(url, {
|
|
170
|
-
method: "POST",
|
|
171
|
-
headers: this.headers,
|
|
172
|
-
body: JSON.stringify(body),
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
checkResponse(response);
|
|
176
|
-
|
|
177
|
-
const responseBody = await response.json();
|
|
178
|
-
|
|
179
|
-
const redeemResponse: RedeemResponse = JSON.parse(
|
|
180
|
-
JSON.stringify(responseBody),
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
const txSig = await this.send(redeemResponse.tx);
|
|
184
|
-
|
|
185
|
-
return txSig;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private async send(base64Tx: string): Promise<string> {
|
|
189
|
-
const txBytes = Buffer.from(base64Tx, "base64");
|
|
190
|
-
const tx = web3.VersionedTransaction.deserialize(new Uint8Array(txBytes));
|
|
191
|
-
const signedTx = await this.provider.wallet.signTransaction(tx);
|
|
192
|
-
const txSig = signedTx.signatures[0];
|
|
193
|
-
|
|
194
|
-
const encodedAndSignedTx = Buffer.from(signedTx.serialize()).toString(
|
|
195
|
-
"base64",
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
const sendRequest: SendRequest = {
|
|
199
|
-
tx: encodedAndSignedTx,
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
const url = new URL(`${this.baseUrl}/send`);
|
|
203
|
-
|
|
204
|
-
const response = await fetch(url, {
|
|
205
|
-
method: "POST",
|
|
206
|
-
headers: this.headers,
|
|
207
|
-
body: JSON.stringify(sendRequest),
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
checkResponse(response);
|
|
211
|
-
|
|
212
|
-
if (response.status !== 200) {
|
|
213
|
-
throw new Error(`unexpected http status of ${response.status}`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return encode(txSig);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export interface SendRequest {
|
|
221
|
-
tx: string;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export interface RedeemRequest {
|
|
225
|
-
vault: web3.PublicKey;
|
|
226
|
-
assetMint: web3.PublicKey;
|
|
227
|
-
amount: BN;
|
|
228
|
-
user: web3.PublicKey;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export interface IssueRequest {
|
|
232
|
-
vault: web3.PublicKey;
|
|
233
|
-
assetMint: web3.PublicKey;
|
|
234
|
-
amount: BN;
|
|
235
|
-
user: web3.PublicKey;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export interface RedeemResponse {
|
|
239
|
-
tx: string;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export interface IssueResponse {
|
|
243
|
-
tx: string;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export interface UserResponse {
|
|
247
|
-
solAmount: BN;
|
|
248
|
-
solAmountUi: number;
|
|
249
|
-
sharesAmount: BN;
|
|
250
|
-
sharesAmountUi: number;
|
|
251
|
-
assets: UserAssetBalance[];
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
export interface UserAssetBalance {
|
|
255
|
-
mint: web3.PublicKey;
|
|
256
|
-
amount: BN;
|
|
257
|
-
amountUi: number;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export async function prepareUnsignedTx(
|
|
261
|
-
connection: web3.Connection,
|
|
262
|
-
payer: web3.PublicKey,
|
|
263
|
-
ixns: web3.TransactionInstruction[],
|
|
264
|
-
lutAddr?: web3.PublicKey,
|
|
265
|
-
additionalSigner?: AnchorProvider,
|
|
266
|
-
): Promise<string> {
|
|
267
|
-
const lutAccounts: web3.AddressLookupTableAccount[] = [];
|
|
268
|
-
if (lutAddr) {
|
|
269
|
-
const account = (await connection.getAddressLookupTable(lutAddr)).value!;
|
|
270
|
-
lutAccounts.push(account);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const recentBh = await connection.getLatestBlockhash({
|
|
274
|
-
commitment: "confirmed",
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
const msg = new web3.TransactionMessage({
|
|
278
|
-
payerKey: payer,
|
|
279
|
-
recentBlockhash: recentBh.blockhash,
|
|
280
|
-
instructions: ixns,
|
|
281
|
-
}).compileToV0Message(lutAccounts);
|
|
282
|
-
|
|
283
|
-
const tx = new web3.VersionedTransaction(msg);
|
|
284
|
-
let base64Tx = Buffer.from(tx.serialize()).toString("base64");
|
|
285
|
-
if (additionalSigner) {
|
|
286
|
-
const signedTx = await additionalSigner.wallet.signTransaction(tx);
|
|
287
|
-
base64Tx = Buffer.from(signedTx.serialize()).toString("base64");
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return base64Tx;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function checkResponse(response: Response): void {
|
|
294
|
-
if (response.status !== 200) {
|
|
295
|
-
throw new Error(`unexpected http status of ${response.status}`);
|
|
296
|
-
}
|
|
297
|
-
}
|
package/tsconfig.json
DELETED