@carrot-protocol/http-client 0.2.6 → 0.2.7-es2020-dev-83c2b79
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.js +35 -43
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,61 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 {
|
|
1
|
+
import fetch from "cross-fetch";
|
|
2
|
+
import { web3, BN } from "@coral-xyz/anchor";
|
|
3
|
+
import { encode } from "bs58";
|
|
4
|
+
export class Client {
|
|
12
5
|
constructor(baseUrl, provider) {
|
|
13
6
|
this.headers = { "Content-Type": "application/json" };
|
|
14
7
|
this.baseUrl = baseUrl;
|
|
15
8
|
this.provider = provider;
|
|
16
9
|
}
|
|
17
10
|
async index() {
|
|
18
|
-
const response = await (
|
|
11
|
+
const response = await fetch(this.baseUrl);
|
|
19
12
|
checkResponse(response);
|
|
20
13
|
const body = await response.json();
|
|
21
14
|
console.log(JSON.stringify(body, undefined, 2));
|
|
22
15
|
}
|
|
23
16
|
async getVault(vault) {
|
|
24
17
|
const url = new URL(`${this.baseUrl}/vault?vault=${vault.toString()}`);
|
|
25
|
-
const response = await (
|
|
18
|
+
const response = await fetch(url, {
|
|
26
19
|
method: "GET",
|
|
27
20
|
headers: this.headers,
|
|
28
21
|
});
|
|
29
22
|
checkResponse(response);
|
|
30
23
|
const rawBody = await response.json();
|
|
31
24
|
let body = JSON.parse(JSON.stringify(rawBody));
|
|
32
|
-
body.address = new
|
|
33
|
-
body.authority = new
|
|
34
|
-
body.shares = new
|
|
35
|
-
body.sharesSupply = new
|
|
25
|
+
body.address = new web3.PublicKey(body.address);
|
|
26
|
+
body.authority = new web3.PublicKey(body.authority);
|
|
27
|
+
body.shares = new web3.PublicKey(body.shares);
|
|
28
|
+
body.sharesSupply = new BN(body.sharesSupply);
|
|
36
29
|
body.assets = body.assets.map((asset) => ({
|
|
37
30
|
...asset,
|
|
38
31
|
assetId: Number(asset.assetId),
|
|
39
32
|
balanceUsd: Number(asset.balanceUsd),
|
|
40
|
-
mint: new
|
|
41
|
-
ata: new
|
|
42
|
-
oracle: new
|
|
43
|
-
ataAmount: new
|
|
33
|
+
mint: new web3.PublicKey(asset.mint),
|
|
34
|
+
ata: new web3.PublicKey(asset.ata),
|
|
35
|
+
oracle: new web3.PublicKey(asset.oracle),
|
|
36
|
+
ataAmount: new BN(asset.ataAmount, "hex"),
|
|
44
37
|
}));
|
|
45
38
|
body.strategies = body.strategies.map((strategy) => ({
|
|
46
|
-
address: new
|
|
39
|
+
address: new web3.PublicKey(strategy.address),
|
|
47
40
|
record: {
|
|
48
41
|
strategyId: Number(strategy.record.strategyId),
|
|
49
42
|
assetId: Number(strategy.record.assetId),
|
|
50
|
-
balance: new
|
|
43
|
+
balance: new BN(strategy.record.balance, "hex"),
|
|
51
44
|
balanceUsd: Number(strategy.record.balanceUsd),
|
|
52
|
-
netEarnings: new
|
|
45
|
+
netEarnings: new BN(strategy.record.netEarnings, "hex"),
|
|
53
46
|
},
|
|
54
47
|
metadata: {
|
|
55
48
|
...strategy.metadata,
|
|
56
49
|
strategyId: Number(strategy.metadata.strategyId),
|
|
57
|
-
assetMint: new
|
|
58
|
-
vault: new
|
|
50
|
+
assetMint: new web3.PublicKey(strategy.metadata.assetMint),
|
|
51
|
+
vault: new web3.PublicKey(strategy.metadata.vault),
|
|
59
52
|
},
|
|
60
53
|
strategyType: strategy.strategyType,
|
|
61
54
|
}));
|
|
@@ -63,7 +56,7 @@ class Client {
|
|
|
63
56
|
}
|
|
64
57
|
async getVaultPerformance(vault) {
|
|
65
58
|
const url = new URL(`${this.baseUrl}/performance?vault=${vault.toString()}`);
|
|
66
|
-
const response = await (
|
|
59
|
+
const response = await fetch(url, {
|
|
67
60
|
method: "GET",
|
|
68
61
|
headers: this.headers,
|
|
69
62
|
});
|
|
@@ -74,26 +67,26 @@ class Client {
|
|
|
74
67
|
for (let strats of body.strategyAPY) {
|
|
75
68
|
strats.apy = Number(strats.apy);
|
|
76
69
|
strats.balanceUsd = Number(strats.balanceUsd);
|
|
77
|
-
strats.strategy = new
|
|
70
|
+
strats.strategy = new web3.PublicKey(strats.strategy);
|
|
78
71
|
strats.vaultWeight = Number(strats.vaultWeight);
|
|
79
|
-
strats.assetMint = new
|
|
72
|
+
strats.assetMint = new web3.PublicKey(strats.assetMint);
|
|
80
73
|
}
|
|
81
74
|
return body;
|
|
82
75
|
}
|
|
83
76
|
async getUser(vault) {
|
|
84
77
|
const url = new URL(`${this.baseUrl}/user?vault=${vault.toString()}&user=${this.provider.publicKey.toString()}`);
|
|
85
|
-
const response = await (
|
|
78
|
+
const response = await fetch(url, {
|
|
86
79
|
method: "GET",
|
|
87
80
|
headers: this.headers,
|
|
88
81
|
});
|
|
89
82
|
checkResponse(response);
|
|
90
83
|
const body = JSON.parse(JSON.stringify(await response.json()));
|
|
91
|
-
body.solAmount = new
|
|
92
|
-
body.sharesAmount = new
|
|
84
|
+
body.solAmount = new BN(body.solAmount, "hex");
|
|
85
|
+
body.sharesAmount = new BN(body.sharesAmount, "hex");
|
|
93
86
|
body.assets = body.assets.map((asset) => ({
|
|
94
87
|
...asset,
|
|
95
|
-
mint: new
|
|
96
|
-
amount: new
|
|
88
|
+
mint: new web3.PublicKey(asset.mint),
|
|
89
|
+
amount: new BN(asset.amount, "hex"),
|
|
97
90
|
}));
|
|
98
91
|
return body;
|
|
99
92
|
}
|
|
@@ -105,7 +98,7 @@ class Client {
|
|
|
105
98
|
amount,
|
|
106
99
|
assetMint,
|
|
107
100
|
};
|
|
108
|
-
const response = await (
|
|
101
|
+
const response = await fetch(url, {
|
|
109
102
|
method: "POST",
|
|
110
103
|
headers: this.headers,
|
|
111
104
|
body: JSON.stringify(body),
|
|
@@ -124,7 +117,7 @@ class Client {
|
|
|
124
117
|
amount,
|
|
125
118
|
assetMint,
|
|
126
119
|
};
|
|
127
|
-
const response = await (
|
|
120
|
+
const response = await fetch(url, {
|
|
128
121
|
method: "POST",
|
|
129
122
|
headers: this.headers,
|
|
130
123
|
body: JSON.stringify(body),
|
|
@@ -137,7 +130,7 @@ class Client {
|
|
|
137
130
|
}
|
|
138
131
|
async send(base64Tx) {
|
|
139
132
|
const txBytes = Buffer.from(base64Tx, "base64");
|
|
140
|
-
const tx =
|
|
133
|
+
const tx = web3.VersionedTransaction.deserialize(new Uint8Array(txBytes));
|
|
141
134
|
const signedTx = await this.provider.wallet.signTransaction(tx);
|
|
142
135
|
const txSig = signedTx.signatures[0];
|
|
143
136
|
const encodedAndSignedTx = Buffer.from(signedTx.serialize()).toString("base64");
|
|
@@ -145,7 +138,7 @@ class Client {
|
|
|
145
138
|
tx: encodedAndSignedTx,
|
|
146
139
|
};
|
|
147
140
|
const url = new URL(`${this.baseUrl}/send`);
|
|
148
|
-
const response = await (
|
|
141
|
+
const response = await fetch(url, {
|
|
149
142
|
method: "POST",
|
|
150
143
|
headers: this.headers,
|
|
151
144
|
body: JSON.stringify(sendRequest),
|
|
@@ -154,11 +147,10 @@ class Client {
|
|
|
154
147
|
if (response.status !== 200) {
|
|
155
148
|
throw new Error(`unexpected http status of ${response.status}`);
|
|
156
149
|
}
|
|
157
|
-
return
|
|
150
|
+
return encode(txSig);
|
|
158
151
|
}
|
|
159
152
|
}
|
|
160
|
-
|
|
161
|
-
async function prepareUnsignedTx(connection, payer, ixns, lutAddr, additionalSigner) {
|
|
153
|
+
export async function prepareUnsignedTx(connection, payer, ixns, lutAddr, additionalSigner) {
|
|
162
154
|
const lutAccounts = [];
|
|
163
155
|
if (lutAddr) {
|
|
164
156
|
const account = (await connection.getAddressLookupTable(lutAddr)).value;
|
|
@@ -167,12 +159,12 @@ async function prepareUnsignedTx(connection, payer, ixns, lutAddr, additionalSig
|
|
|
167
159
|
const recentBh = await connection.getLatestBlockhash({
|
|
168
160
|
commitment: "confirmed",
|
|
169
161
|
});
|
|
170
|
-
const msg = new
|
|
162
|
+
const msg = new web3.TransactionMessage({
|
|
171
163
|
payerKey: payer,
|
|
172
164
|
recentBlockhash: recentBh.blockhash,
|
|
173
165
|
instructions: ixns,
|
|
174
166
|
}).compileToV0Message(lutAccounts);
|
|
175
|
-
const tx = new
|
|
167
|
+
const tx = new web3.VersionedTransaction(msg);
|
|
176
168
|
let base64Tx = Buffer.from(tx.serialize()).toString("base64");
|
|
177
169
|
if (additionalSigner) {
|
|
178
170
|
const signedTx = await additionalSigner.wallet.signTransaction(tx);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carrot-protocol/http-client",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7-es2020-dev-83c2b79",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "rm -rf dist node_modules && npm
|
|
8
|
+
"build": "rm -rf dist node_modules && npm i && tsc && npm pack",
|
|
9
9
|
"fmt:check": "prettier --check src/",
|
|
10
10
|
"fmt": "prettier --write src/"
|
|
11
11
|
},
|