Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Add support for websocket rpc connections #131

Open
itsahedge opened this issue Apr 29, 2023 · 2 comments
Open

Add support for websocket rpc connections #131

itsahedge opened this issue Apr 29, 2023 · 2 comments

Comments

@itsahedge
Copy link
Contributor

itsahedge commented Apr 29, 2023

As of version 2.0.2, the Go SDK does not support websocket rpc connections because in the getDefaultRpc() function, it checks:

if strings.HasPrefix(rpcUrlorName, "http") {
	return rpcUrlorName, nil
} else {
	return "", fmt.Errorf("invalid rpc url or chain name: %s", rpcUrlorName)
}

Adding websocket support would make it easier to handle listening on contract events.

example: wss://goerli.infura.io/ws/v3/{your-infura-project-id}

solution:

func getDefaultRpcUrl(rpcUrlorName string) (string, error) {
	switch rpcUrlorName {
	case "mumbai":
		return defaultRpc("mumbai")
	case "goerli":
		return defaultRpc("goerli")
	case "polygon":
		return defaultRpc("polygon")
	case "mainnet", "ethereum":
		return defaultRpc("ethereum")
	case "fantom":
		return defaultRpc("fantom")
	case "avalanche":
		return defaultRpc("avalanche")
	case "optimism":
		return defaultRpc("optimism")
	case "optimism-goerli":
		return defaultRpc("optimism-goerli")
	case "arbitrum":
		return defaultRpc("arbitrum")
	case "arbitrum-goerli":
		return defaultRpc("arbitrum-goerli")
	default:
		if strings.HasPrefix(rpcUrlorName, "http") || strings.HasPrefix(rpcUrlorName, "wss") {
			return rpcUrlorName, nil
		} else {
			return "", fmt.Errorf("invalid rpc url or chain name: %s", rpcUrlorName)
		}
	}
}
@joaquim-verges
Copy link
Member

Thank you for reporting this @itsahedge - would you like to put up a PR for this? Contributions are always welcome!

@itsahedge
Copy link
Contributor Author

|| strings.HasPrefix(rpcUrlorName, "wss")

#132

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants