@haven-fi/solauto-sdk 1.0.332 → 1.0.333
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/utils/priceUtils.d.ts +2 -1
- package/dist/utils/priceUtils.d.ts.map +1 -1
- package/dist/utils/priceUtils.js +28 -9
- package/dist/utils/switchboardUtils.d.ts +0 -1
- package/dist/utils/switchboardUtils.d.ts.map +1 -1
- package/dist/utils/switchboardUtils.js +0 -16
- package/package.json +1 -1
- package/src/utils/priceUtils.ts +51 -12
- package/src/utils/switchboardUtils.ts +1 -30
@@ -2,6 +2,7 @@ import { PublicKey } from "@solana/web3.js";
|
|
2
2
|
import { PublicKey as UmiPublicKey } from "@metaplex-foundation/umi";
|
3
3
|
export declare function fetchTokenPrices(mints: PublicKey[]): Promise<number[]>;
|
4
4
|
export declare function getPythPrices(mints: PublicKey[]): Promise<number[]>;
|
5
|
-
export declare function
|
5
|
+
export declare function getSwitchboardPrices(mints: PublicKey[]): Promise<number[]>;
|
6
6
|
export declare function getJupTokenPrices(mints: PublicKey[]): Promise<number[]>;
|
7
|
+
export declare function safeGetPrice(mint: PublicKey | UmiPublicKey | undefined): number | undefined;
|
7
8
|
//# sourceMappingURL=priceUtils.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"priceUtils.d.ts","sourceRoot":"","sources":["../../src/utils/priceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;
|
1
|
+
{"version":3,"file":"priceUtils.d.ts","sourceRoot":"","sources":["../../src/utils/priceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAarE,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAuC5E;AAED,wBAAsB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,qBAwCrD;AAED,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC,MAAM,EAAE,CAAC,CAmCnB;AAED,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,qBAoBzD;AAED,wBAAgB,YAAY,CAC1B,IAAI,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GACzC,MAAM,GAAG,SAAS,CAKpB"}
|
package/dist/utils/priceUtils.js
CHANGED
@@ -2,14 +2,15 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.fetchTokenPrices = fetchTokenPrices;
|
4
4
|
exports.getPythPrices = getPythPrices;
|
5
|
-
exports.
|
5
|
+
exports.getSwitchboardPrices = getSwitchboardPrices;
|
6
6
|
exports.getJupTokenPrices = getJupTokenPrices;
|
7
|
+
exports.safeGetPrice = safeGetPrice;
|
7
8
|
const pythConstants_1 = require("../constants/pythConstants");
|
8
9
|
const numberUtils_1 = require("./numberUtils");
|
9
10
|
const solautoConstants_1 = require("../constants/solautoConstants");
|
10
11
|
const switchboardConstants_1 = require("../constants/switchboardConstants");
|
11
12
|
const generalUtils_1 = require("./generalUtils");
|
12
|
-
const
|
13
|
+
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
13
14
|
async function fetchTokenPrices(mints) {
|
14
15
|
const currentTime = (0, generalUtils_1.currentUnixSeconds)();
|
15
16
|
if (!mints.some((mint) => !(mint.toString() in solautoConstants_1.PRICES) ||
|
@@ -20,8 +21,9 @@ async function fetchTokenPrices(mints) {
|
|
20
21
|
const switchboardMints = mints.filter((x) => x.toString() in switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS);
|
21
22
|
const [pythData, switchboardData] = await Promise.all([
|
22
23
|
(0, generalUtils_1.zip)(pythMints, await getPythPrices(pythMints)),
|
23
|
-
(0, generalUtils_1.zip)(switchboardMints, await
|
24
|
+
(0, generalUtils_1.zip)(switchboardMints, await getSwitchboardPrices(switchboardMints)),
|
24
25
|
]);
|
26
|
+
(0, generalUtils_1.consoleLog)([...pythData, ...switchboardData]);
|
25
27
|
const prices = mints.map((mint) => {
|
26
28
|
const item = [...pythData, ...switchboardData].find((data) => data[0].equals(mint));
|
27
29
|
return item ? item[1] : 0;
|
@@ -62,22 +64,39 @@ async function getPythPrices(mints) {
|
|
62
64
|
}, 5, 200);
|
63
65
|
return prices;
|
64
66
|
}
|
65
|
-
function
|
66
|
-
if (
|
67
|
-
return
|
67
|
+
async function getSwitchboardPrices(mints) {
|
68
|
+
if (mints.length === 0) {
|
69
|
+
return [];
|
68
70
|
}
|
69
|
-
|
71
|
+
const crossbar = new on_demand_1.CrossbarClient("https://crossbar.switchboard.xyz");
|
72
|
+
let prices = await (0, generalUtils_1.retryWithExponentialBackoff)(async () => {
|
73
|
+
const res = await crossbar.simulateSolanaFeeds("mainnet", mints.map((x) => switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS[x.toString()]));
|
74
|
+
const prices = res.flatMap((x) => x.results[0]);
|
75
|
+
if (prices.find((x) => !x || x === -Infinity)) {
|
76
|
+
throw new Error("Unable to fetch Switchboard prices");
|
77
|
+
}
|
78
|
+
return prices;
|
79
|
+
}, 8, 250);
|
80
|
+
const missingPrices = (0, generalUtils_1.zip)(mints, prices).filter((x) => !x[1]);
|
81
|
+
const jupPrices = (0, generalUtils_1.zip)(missingPrices.map((x) => x[0]), await getJupTokenPrices(missingPrices.map((x) => x[0])));
|
82
|
+
prices = prices.map((x, i) => x ? x : jupPrices.find((y) => y[0].toString() === mints[i].toString())[1]);
|
83
|
+
return prices;
|
70
84
|
}
|
71
|
-
// LEGACY, NOT USED
|
72
85
|
async function getJupTokenPrices(mints) {
|
73
86
|
if (mints.length == 0) {
|
74
87
|
return [];
|
75
88
|
}
|
76
89
|
const data = await (0, generalUtils_1.retryWithExponentialBackoff)(async () => {
|
77
90
|
const res = (await fetch("https://api.jup.ag/price/v2?ids=" +
|
78
|
-
mints.map((x) => x.toString()).join(",")
|
91
|
+
mints.map((x) => x.toString()).join(","))).json();
|
79
92
|
return res;
|
80
93
|
}, 6);
|
81
94
|
const prices = Object.values(data.data).map((x) => parseFloat(x.price));
|
82
95
|
return prices;
|
83
96
|
}
|
97
|
+
function safeGetPrice(mint) {
|
98
|
+
if (mint && mint?.toString() in solautoConstants_1.PRICES) {
|
99
|
+
return solautoConstants_1.PRICES[mint.toString()].price;
|
100
|
+
}
|
101
|
+
return undefined;
|
102
|
+
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"switchboardUtils.d.ts","sourceRoot":"","sources":["../../src/utils/switchboardUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EACV,SAAS,EAGV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAkB,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAEtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,MAAM,EAAsB,MAAM,0BAA0B,CAAC;AAOtE,wBAAgB,WAAW,CACzB,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,SAAS,EACf,MAAM,CAAC,EAAE,SAAS,YAsBnB;AAED,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,GACd,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsB5C;AAED,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,EAAE,CAAC,CAuB/D
|
1
|
+
{"version":3,"file":"switchboardUtils.d.ts","sourceRoot":"","sources":["../../src/utils/switchboardUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EACV,SAAS,EAGV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAkB,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAEtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,MAAM,EAAsB,MAAM,0BAA0B,CAAC;AAOtE,wBAAgB,WAAW,CACzB,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,SAAS,EACf,MAAM,CAAC,EAAE,SAAS,YAsBnB;AAED,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,GACd,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsB5C;AAED,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,EAAE,CAAC,CAuB/D"}
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getPullFeed = getPullFeed;
|
7
7
|
exports.buildSwbSubmitResponseTx = buildSwbSubmitResponseTx;
|
8
8
|
exports.getSwitchboardFeedData = getSwitchboardFeedData;
|
9
|
-
exports.getSwitchboardPrices = getSwitchboardPrices;
|
10
9
|
const anchor_1 = require("@coral-xyz/anchor");
|
11
10
|
const switchboard_json_1 = __importDefault(require("../idls/switchboard.json"));
|
12
11
|
const web3_js_1 = require("@solana/web3.js");
|
@@ -56,18 +55,3 @@ async function getSwitchboardFeedData(conn, mints) {
|
|
56
55
|
}));
|
57
56
|
return results;
|
58
57
|
}
|
59
|
-
async function getSwitchboardPrices(mints) {
|
60
|
-
if (mints.length === 0) {
|
61
|
-
return [];
|
62
|
-
}
|
63
|
-
const crossbar = new on_demand_1.CrossbarClient("https://crossbar.switchboard.xyz");
|
64
|
-
const results = await (0, generalUtils_1.retryWithExponentialBackoff)(async () => {
|
65
|
-
const res = await crossbar.simulateSolanaFeeds("mainnet", mints.map((x) => switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS[x.toString()]));
|
66
|
-
const prices = res.flatMap((x) => x.results[0]);
|
67
|
-
if (prices.find((x) => !x || x === -Infinity)) {
|
68
|
-
throw new Error("Unable to fetch Switchboard prices");
|
69
|
-
}
|
70
|
-
return prices;
|
71
|
-
}, 8, 250);
|
72
|
-
return results;
|
73
|
-
}
|
package/package.json
CHANGED
package/src/utils/priceUtils.ts
CHANGED
@@ -5,15 +5,14 @@ import { fromBaseUnit, toBaseUnit } from "./numberUtils";
|
|
5
5
|
import { PRICES } from "../constants/solautoConstants";
|
6
6
|
import { SWITCHBOARD_PRICE_FEED_IDS } from "../constants/switchboardConstants";
|
7
7
|
import {
|
8
|
+
consoleLog,
|
8
9
|
currentUnixSeconds,
|
9
10
|
retryWithExponentialBackoff,
|
10
11
|
zip,
|
11
12
|
} from "./generalUtils";
|
12
|
-
import {
|
13
|
+
import { CrossbarClient } from "@switchboard-xyz/on-demand";
|
13
14
|
|
14
|
-
export async function fetchTokenPrices(
|
15
|
-
mints: PublicKey[]
|
16
|
-
): Promise<number[]> {
|
15
|
+
export async function fetchTokenPrices(mints: PublicKey[]): Promise<number[]> {
|
17
16
|
const currentTime = currentUnixSeconds();
|
18
17
|
if (
|
19
18
|
!mints.some(
|
@@ -35,6 +34,8 @@ export async function fetchTokenPrices(
|
|
35
34
|
zip(switchboardMints, await getSwitchboardPrices(switchboardMints)),
|
36
35
|
]);
|
37
36
|
|
37
|
+
consoleLog([...pythData, ...switchboardData]);
|
38
|
+
|
38
39
|
const prices = mints.map((mint) => {
|
39
40
|
const item = [...pythData, ...switchboardData].find((data) =>
|
40
41
|
data[0].equals(mint)
|
@@ -94,16 +95,45 @@ export async function getPythPrices(mints: PublicKey[]) {
|
|
94
95
|
return prices;
|
95
96
|
}
|
96
97
|
|
97
|
-
export function
|
98
|
-
|
99
|
-
): number
|
100
|
-
if (
|
101
|
-
return
|
98
|
+
export async function getSwitchboardPrices(
|
99
|
+
mints: PublicKey[]
|
100
|
+
): Promise<number[]> {
|
101
|
+
if (mints.length === 0) {
|
102
|
+
return [];
|
102
103
|
}
|
103
|
-
|
104
|
+
|
105
|
+
const crossbar = new CrossbarClient("https://crossbar.switchboard.xyz");
|
106
|
+
let prices = await retryWithExponentialBackoff(
|
107
|
+
async () => {
|
108
|
+
const res = await crossbar.simulateSolanaFeeds(
|
109
|
+
"mainnet",
|
110
|
+
mints.map((x) => SWITCHBOARD_PRICE_FEED_IDS[x.toString()])
|
111
|
+
);
|
112
|
+
|
113
|
+
const prices = res.flatMap((x) => x.results[0]);
|
114
|
+
if (prices.find((x) => !x || x === -Infinity)) {
|
115
|
+
throw new Error("Unable to fetch Switchboard prices");
|
116
|
+
}
|
117
|
+
|
118
|
+
return prices;
|
119
|
+
},
|
120
|
+
8,
|
121
|
+
250
|
122
|
+
);
|
123
|
+
|
124
|
+
const missingPrices = zip(mints, prices).filter((x) => !x[1]);
|
125
|
+
const jupPrices = zip(
|
126
|
+
missingPrices.map((x) => x[0]),
|
127
|
+
await getJupTokenPrices(missingPrices.map((x) => x[0]))
|
128
|
+
);
|
129
|
+
|
130
|
+
prices = prices.map((x, i) =>
|
131
|
+
x ? x : jupPrices.find((y) => y[0].toString() === mints[i].toString())![1]
|
132
|
+
);
|
133
|
+
|
134
|
+
return prices;
|
104
135
|
}
|
105
136
|
|
106
|
-
// LEGACY, NOT USED
|
107
137
|
export async function getJupTokenPrices(mints: PublicKey[]) {
|
108
138
|
if (mints.length == 0) {
|
109
139
|
return [];
|
@@ -113,7 +143,7 @@ export async function getJupTokenPrices(mints: PublicKey[]) {
|
|
113
143
|
const res = (
|
114
144
|
await fetch(
|
115
145
|
"https://api.jup.ag/price/v2?ids=" +
|
116
|
-
mints.map((x) => x.toString()).join(",")
|
146
|
+
mints.map((x) => x.toString()).join(",")
|
117
147
|
)
|
118
148
|
).json();
|
119
149
|
return res;
|
@@ -125,3 +155,12 @@ export async function getJupTokenPrices(mints: PublicKey[]) {
|
|
125
155
|
|
126
156
|
return prices;
|
127
157
|
}
|
158
|
+
|
159
|
+
export function safeGetPrice(
|
160
|
+
mint: PublicKey | UmiPublicKey | undefined
|
161
|
+
): number | undefined {
|
162
|
+
if (mint && mint?.toString() in PRICES) {
|
163
|
+
return PRICES[mint!.toString()].price;
|
164
|
+
}
|
165
|
+
return undefined;
|
166
|
+
}
|
@@ -97,33 +97,4 @@ export async function getSwitchboardFeedData(
|
|
97
97
|
);
|
98
98
|
|
99
99
|
return results;
|
100
|
-
}
|
101
|
-
|
102
|
-
export async function getSwitchboardPrices(
|
103
|
-
mints: PublicKey[]
|
104
|
-
): Promise<number[]> {
|
105
|
-
if (mints.length === 0) {
|
106
|
-
return [];
|
107
|
-
}
|
108
|
-
|
109
|
-
const crossbar = new CrossbarClient("https://crossbar.switchboard.xyz");
|
110
|
-
const results = await retryWithExponentialBackoff(
|
111
|
-
async () => {
|
112
|
-
const res = await crossbar.simulateSolanaFeeds(
|
113
|
-
"mainnet",
|
114
|
-
mints.map((x) => SWITCHBOARD_PRICE_FEED_IDS[x.toString()])
|
115
|
-
);
|
116
|
-
|
117
|
-
const prices = res.flatMap((x) => x.results[0]);
|
118
|
-
if (prices.find((x) => !x || x === -Infinity)) {
|
119
|
-
throw new Error("Unable to fetch Switchboard prices");
|
120
|
-
}
|
121
|
-
|
122
|
-
return prices;
|
123
|
-
},
|
124
|
-
8,
|
125
|
-
250
|
126
|
-
);
|
127
|
-
|
128
|
-
return results;
|
129
|
-
}
|
100
|
+
}
|