@carrot-protocol/http-client 0.2.4-add-typings-dev-adbd3c1 → 0.2.4-add-typings-dev-878b388

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.
@@ -0,0 +1,49 @@
1
+ import { web3, BN, AnchorProvider } from "@coral-xyz/anchor";
2
+ import { Vault, VaultPerformance } from "@carrot-protocol/common/src";
3
+ export declare class Client {
4
+ readonly baseUrl: string;
5
+ readonly provider: AnchorProvider;
6
+ private headers;
7
+ constructor(baseUrl: string, provider: AnchorProvider);
8
+ index(): Promise<void>;
9
+ getVault(vault: web3.PublicKey): Promise<Vault>;
10
+ getVaultPerformance(vault: web3.PublicKey): Promise<VaultPerformance>;
11
+ getUser(vault: web3.PublicKey): Promise<UserResponse>;
12
+ issue(vault: web3.PublicKey, assetMint: web3.PublicKey, amount: BN): Promise<string>;
13
+ redeem(vault: web3.PublicKey, assetMint: web3.PublicKey, amount: BN): Promise<string>;
14
+ private send;
15
+ }
16
+ export interface SendRequest {
17
+ tx: string;
18
+ }
19
+ export interface RedeemRequest {
20
+ vault: web3.PublicKey;
21
+ assetMint: web3.PublicKey;
22
+ amount: BN;
23
+ user: web3.PublicKey;
24
+ }
25
+ export interface IssueRequest {
26
+ vault: web3.PublicKey;
27
+ assetMint: web3.PublicKey;
28
+ amount: BN;
29
+ user: web3.PublicKey;
30
+ }
31
+ export interface RedeemResponse {
32
+ tx: string;
33
+ }
34
+ export interface IssueResponse {
35
+ tx: string;
36
+ }
37
+ export interface UserResponse {
38
+ solAmount: BN;
39
+ solAmountUi: number;
40
+ sharesAmount: BN;
41
+ sharesAmountUi: number;
42
+ assets: UserAssetBalance[];
43
+ }
44
+ export interface UserAssetBalance {
45
+ mint: web3.PublicKey;
46
+ amount: BN;
47
+ amountUi: number;
48
+ }
49
+ export declare function prepareUnsignedTx(connection: web3.Connection, payer: web3.PublicKey, ixns: web3.TransactionInstruction[], lutAddr?: web3.PublicKey, additionalSigner?: AnchorProvider): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Client = void 0;
7
+ exports.prepareUnsignedTx = prepareUnsignedTx;
8
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
9
+ const anchor_1 = require("@coral-xyz/anchor");
10
+ const bs58_1 = require("bs58");
11
+ class Client {
12
+ constructor(baseUrl, provider) {
13
+ this.headers = { "Content-Type": "application/json" };
14
+ this.baseUrl = baseUrl;
15
+ this.provider = provider;
16
+ }
17
+ async index() {
18
+ const response = await (0, cross_fetch_1.default)(this.baseUrl);
19
+ checkResponse(response);
20
+ const body = await response.json();
21
+ console.log(JSON.stringify(body, undefined, 2));
22
+ }
23
+ async getVault(vault) {
24
+ const url = new URL(`${this.baseUrl}/vault?vault=${vault.toString()}`);
25
+ const response = await (0, cross_fetch_1.default)(url, {
26
+ method: "GET",
27
+ headers: this.headers,
28
+ });
29
+ checkResponse(response);
30
+ const rawBody = await response.json();
31
+ let body = JSON.parse(JSON.stringify(rawBody));
32
+ body.address = new anchor_1.web3.PublicKey(body.address);
33
+ body.authority = new anchor_1.web3.PublicKey(body.authority);
34
+ body.shares = new anchor_1.web3.PublicKey(body.shares);
35
+ body.sharesSupply = new anchor_1.BN(body.sharesSupply);
36
+ body.assets = body.assets.map((asset) => ({
37
+ ...asset,
38
+ assetId: Number(asset.assetId),
39
+ balanceUsd: Number(asset.balanceUsd),
40
+ mint: new anchor_1.web3.PublicKey(asset.mint),
41
+ ata: new anchor_1.web3.PublicKey(asset.ata),
42
+ oracle: new anchor_1.web3.PublicKey(asset.oracle),
43
+ ataAmount: new anchor_1.BN(asset.ataAmount, "hex"),
44
+ }));
45
+ body.strategies = body.strategies.map((strategy) => ({
46
+ address: new anchor_1.web3.PublicKey(strategy.address),
47
+ record: {
48
+ strategyId: Number(strategy.record.strategyId),
49
+ assetId: Number(strategy.record.assetId),
50
+ balance: new anchor_1.BN(strategy.record.balance, "hex"),
51
+ balanceUsd: Number(strategy.record.balanceUsd),
52
+ netEarnings: new anchor_1.BN(strategy.record.netEarnings, "hex"),
53
+ },
54
+ metadata: {
55
+ ...strategy.metadata,
56
+ strategyId: Number(strategy.metadata.strategyId),
57
+ assetMint: new anchor_1.web3.PublicKey(strategy.metadata.assetMint),
58
+ vault: new anchor_1.web3.PublicKey(strategy.metadata.vault),
59
+ },
60
+ strategyType: strategy.strategyType,
61
+ }));
62
+ return body;
63
+ }
64
+ async getVaultPerformance(vault) {
65
+ const url = new URL(`${this.baseUrl}/performance?vault=${vault.toString()}`);
66
+ const response = await (0, cross_fetch_1.default)(url, {
67
+ method: "GET",
68
+ headers: this.headers,
69
+ });
70
+ checkResponse(response);
71
+ const rawBody = await response.json();
72
+ let body = JSON.parse(JSON.stringify(rawBody));
73
+ body.apy = Number(body.apy);
74
+ for (let strats of body.strategyAPY) {
75
+ strats.apy = Number(strats.apy);
76
+ strats.balanceUsd = Number(strats.balanceUsd);
77
+ strats.strategy = new anchor_1.web3.PublicKey(strats.strategy);
78
+ strats.vaultWeight = Number(strats.vaultWeight);
79
+ strats.assetMint = new anchor_1.web3.PublicKey(strats.assetMint);
80
+ }
81
+ return body;
82
+ }
83
+ async getUser(vault) {
84
+ const url = new URL(`${this.baseUrl}/user?vault=${vault.toString()}&user=${this.provider.publicKey.toString()}`);
85
+ const response = await (0, cross_fetch_1.default)(url, {
86
+ method: "GET",
87
+ headers: this.headers,
88
+ });
89
+ checkResponse(response);
90
+ const body = JSON.parse(JSON.stringify(await response.json()));
91
+ body.solAmount = new anchor_1.BN(body.solAmount, "hex");
92
+ body.sharesAmount = new anchor_1.BN(body.sharesAmount, "hex");
93
+ body.assets = body.assets.map((asset) => ({
94
+ ...asset,
95
+ mint: new anchor_1.web3.PublicKey(asset.mint),
96
+ amount: new anchor_1.BN(asset.amount, "hex"),
97
+ }));
98
+ return body;
99
+ }
100
+ async issue(vault, assetMint, amount) {
101
+ const url = new URL(`${this.baseUrl}/issue`);
102
+ const body = {
103
+ user: this.provider.publicKey,
104
+ vault,
105
+ amount,
106
+ assetMint,
107
+ };
108
+ const response = await (0, cross_fetch_1.default)(url, {
109
+ method: "POST",
110
+ headers: this.headers,
111
+ body: JSON.stringify(body),
112
+ });
113
+ checkResponse(response);
114
+ const responseBody = await response.json();
115
+ const issueResponse = JSON.parse(JSON.stringify(responseBody));
116
+ const txSig = await this.send(issueResponse.tx);
117
+ return txSig;
118
+ }
119
+ async redeem(vault, assetMint, amount) {
120
+ const url = new URL(`${this.baseUrl}/redeem`);
121
+ const body = {
122
+ user: this.provider.publicKey,
123
+ vault,
124
+ amount,
125
+ assetMint,
126
+ };
127
+ const response = await (0, cross_fetch_1.default)(url, {
128
+ method: "POST",
129
+ headers: this.headers,
130
+ body: JSON.stringify(body),
131
+ });
132
+ checkResponse(response);
133
+ const responseBody = await response.json();
134
+ const redeemResponse = JSON.parse(JSON.stringify(responseBody));
135
+ const txSig = await this.send(redeemResponse.tx);
136
+ return txSig;
137
+ }
138
+ async send(base64Tx) {
139
+ const txBytes = Buffer.from(base64Tx, "base64");
140
+ const tx = anchor_1.web3.VersionedTransaction.deserialize(new Uint8Array(txBytes));
141
+ const signedTx = await this.provider.wallet.signTransaction(tx);
142
+ const txSig = signedTx.signatures[0];
143
+ const encodedAndSignedTx = Buffer.from(signedTx.serialize()).toString("base64");
144
+ const sendRequest = {
145
+ tx: encodedAndSignedTx,
146
+ };
147
+ const url = new URL(`${this.baseUrl}/send`);
148
+ const response = await (0, cross_fetch_1.default)(url, {
149
+ method: "POST",
150
+ headers: this.headers,
151
+ body: JSON.stringify(sendRequest),
152
+ });
153
+ checkResponse(response);
154
+ if (response.status !== 200) {
155
+ throw new Error(`unexpected http status of ${response.status}`);
156
+ }
157
+ return (0, bs58_1.encode)(txSig);
158
+ }
159
+ }
160
+ exports.Client = Client;
161
+ async function prepareUnsignedTx(connection, payer, ixns, lutAddr, additionalSigner) {
162
+ const lutAccounts = [];
163
+ if (lutAddr) {
164
+ const account = (await connection.getAddressLookupTable(lutAddr)).value;
165
+ lutAccounts.push(account);
166
+ }
167
+ const recentBh = await connection.getLatestBlockhash({
168
+ commitment: "confirmed",
169
+ });
170
+ const msg = new anchor_1.web3.TransactionMessage({
171
+ payerKey: payer,
172
+ recentBlockhash: recentBh.blockhash,
173
+ instructions: ixns,
174
+ }).compileToV0Message(lutAccounts);
175
+ const tx = new anchor_1.web3.VersionedTransaction(msg);
176
+ let base64Tx = Buffer.from(tx.serialize()).toString("base64");
177
+ if (additionalSigner) {
178
+ const signedTx = await additionalSigner.wallet.signTransaction(tx);
179
+ base64Tx = Buffer.from(signedTx.serialize()).toString("base64");
180
+ }
181
+ return base64Tx;
182
+ }
183
+ function checkResponse(response) {
184
+ if (response.status !== 200) {
185
+ throw new Error(`unexpected http status of ${response.status}`);
186
+ }
187
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@carrot-protocol/http-client",
3
- "version": "0.2.4-add-typings-dev-adbd3c1",
3
+ "version": "0.2.4-add-typings-dev-878b388",
4
4
  "description": "",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "build": "rm -rf *.tgz && npm i && npm pack",
8
+ "build": "rm -rf dist node_modules && npm ci && tsc && npm pack",
9
9
  "fmt:check": "prettier --check src/",
10
10
  "fmt": "prettier --write src/"
11
11
  },
@@ -13,7 +13,7 @@
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
15
  "@coral-xyz/anchor": "^0.29.0",
16
- "@carrot/common": "file:../common",
16
+ "@carrot-protocol/common": "0.2.3",
17
17
  "bs58": "^5.0.0",
18
18
  "cross-fetch": "^4.0.0"
19
19
  },
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fetch from "cross-fetch";
2
2
  import { web3, BN, AnchorProvider } from "@coral-xyz/anchor";
3
3
  import { encode } from "bs58";
4
- import { Vault, VaultPerformance } from "../../common/src";
4
+ import { Vault, VaultPerformance } from "@carrot-protocol/common/src";
5
5
 
6
6
  export class Client {
7
7
  readonly baseUrl: string;
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "rootDir": "./src",
5
+ "declaration": true,
6
+ "module": "commonjs",
7
+ "target": "es2020",
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "strict": true
11
+ },
12
+ "include": ["src/**/*"]
13
+ }