Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Update endpoints #15

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions pyth-common-js/src/PriceServiceConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { makeWebsocketUrl } from "./utils";
export type DurationInMs = number;

export type PriceServiceConnectionConfig = {
/* Optional websocket endpoint of the price service if it has host/port other than the endpoint. */
wsEndpoint?: string;
/* Timeout of each request (for all of retries). Default: 5000ms */
timeout?: DurationInMs;
/**
Expand Down Expand Up @@ -63,7 +61,7 @@ export class PriceServiceConnection {
/**
* Constructs a new Connection.
*
* @param endpoint endpoint URL to the price service. Example: https://website/example
* @param endpoint endpoint URL to the price service. Example: https://website/example/
* @param config Optional PriceServiceConnectionConfig for custom configurations.
*/
constructor(endpoint: string, config?: PriceServiceConnectionConfig) {
Expand All @@ -82,7 +80,7 @@ export class PriceServiceConnection {
this.logger?.error(error);
};

this.wsEndpoint = config?.wsEndpoint || makeWebsocketUrl(endpoint);
this.wsEndpoint = makeWebsocketUrl(endpoint);
}

/**
Expand All @@ -99,7 +97,7 @@ export class PriceServiceConnection {
return [];
}

const response = await this.httpClient.get("/latest_price_feeds", {
const response = await this.httpClient.get("/api/latest_price_feeds", {
params: {
ids: priceIds,
},
Expand All @@ -122,7 +120,7 @@ export class PriceServiceConnection {
* @returns Array of base64 encoded VAAs.
*/
protected async getLatestVaas(priceIds: HexString[]): Promise<string[]> {
const response = await this.httpClient.get("/latest_vaas", {
const response = await this.httpClient.get("/api/latest_vaas", {
params: {
ids: priceIds,
},
Expand Down
7 changes: 0 additions & 7 deletions pyth-common-js/src/examples/PriceServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ const argv = yargs(hideBin(process.argv))
type: "string",
required: true,
})
.option("wsEndpoint", {
description:
"Optional web socket endpoint for the price service if it's different than endpoint. e.g: wss://endpoint/example",
type: "string",
required: false,
})
.option("price-ids", {
description:
"Space separated price feed ids (in hex without leading 0x) to fetch." +
Expand All @@ -33,7 +27,6 @@ const argv = yargs(hideBin(process.argv))

async function run() {
const connection = new PriceServiceConnection(argv.endpoint, {
wsEndpoint: argv.wsEndpoint,
logger: console, // Providing logger will allow the connection to log it's events.
});

Expand Down
12 changes: 1 addition & 11 deletions pyth-common-js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@
* @returns Ws(s) protocol endpoint of the same address
*/
export function makeWebsocketUrl(endpoint: string) {
const url = new URL(endpoint);
const url = new URL("ws", endpoint);
const useHttps = url.protocol === "https:";

url.protocol = useHttps ? "wss:" : "ws:";
url.host = "";

// Only shift the port by +1 as a convention for ws(s) only if given endpoint
// is explictly specifying the endpoint port (HTTP-based RPC), assuming
// we're directly trying to connect to solana-validator's ws listening port.
// When the endpoint omits the port, we're connecting to the protocol
// default ports: http(80) or https(443) and it's assumed we're behind a reverse
// proxy which manages WebSocket upgrade and backend port redirection.
if (url.port !== "") {
url.port = String(Number(url.port) + 1);
}
return url.toString();
}