Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: derive cluster ws url like web3.js #1362

Merged
merged 1 commit into from
Mar 11, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ incremented for features.

* ts: Fix the loss of strict typing using the `methods` namespace on builder functions ([#1539](https://github.com/project-serum/anchor/pull/1539)).
* spl: Update `spl/governance` to use new errors ([#1582](https://github.com/project-serum/anchor/pull/1582)).
* client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).

### Breaking

Expand Down
25 changes: 17 additions & 8 deletions client/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ impl FromStr for Cluster {
"d" | "devnet" => Ok(Cluster::Devnet),
"l" | "localnet" => Ok(Cluster::Localnet),
"g" | "debug" => Ok(Cluster::Debug),
url if url.contains("http") => {
let http_url = url;
_ if s.starts_with("http") => {
let http_url = s;

// Taken from:
// https://github.com/solana-labs/solana/blob/aea8f0df1610248d29d8ca3bc0d60e9fabc99e31/web3.js/src/util/url.ts

// Websocket port is always +1 the http port.
let mut ws_url = Url::parse(http_url)?;
if let Some(port) = ws_url.port() {
ws_url.set_port(Some(port + 1))
.map_err(|_| anyhow!("Unable to set port"))?;
} else {
ws_url.set_port(Some(8900))
.map_err(|_| anyhow!("Unable to set port"))?;
}
if ws_url.scheme() == "https" {
ws_url.set_scheme("wss")
Expand Down Expand Up @@ -134,7 +133,7 @@ mod tests {
let url = "http://my-url.com/";
let cluster = Cluster::from_str(url).unwrap();
assert_eq!(
Cluster::Custom(url.to_string(), "ws://my-url.com:8900/".to_string()),
Cluster::Custom(url.to_string(), "ws://my-url.com/".to_string()),
cluster
);
}
Expand All @@ -153,7 +152,17 @@ mod tests {
let url = "https://my-url.com/";
let cluster = Cluster::from_str(url).unwrap();
assert_eq!(
Cluster::Custom(url.to_string(), "wss://my-url.com:8900/".to_string()),
Cluster::Custom(url.to_string(), "wss://my-url.com/".to_string()),
cluster
);
}

#[test]
fn test_upper_case() {
let url = "http://my-url.com/FooBar";
let cluster = Cluster::from_str(url).unwrap();
assert_eq!(
Cluster::Custom(url.to_string(), "ws://my-url.com/FooBar".to_string()),
cluster
);
}
Expand Down