Skip to content

Commit a888ba3

Browse files
authored
chore: env variables for rpcs (#1407)
* chore: env variables for rpcs * Remove console log
1 parent 77db9ee commit a888ba3

File tree

3 files changed

+38
-78
lines changed

3 files changed

+38
-78
lines changed

governance/xc_admin/packages/xc_admin_frontend/hooks/useMultisig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UPGRADE_MULTISIG,
1313
} from 'xc_admin_common'
1414
import { ClusterContext } from '../contexts/ClusterContext'
15-
import { pythClusterApiUrls } from '../utils/pythClusterApiUrl'
15+
import { deriveWsUrl, pythClusterApiUrls } from '../utils/pythClusterApiUrl'
1616

1717
export interface MultisigHookData {
1818
isLoading: boolean
@@ -69,9 +69,9 @@ export const useMultisig = (): MultisigHookData => {
6969

7070
const connection = useMemo(() => {
7171
const urls = pythClusterApiUrls(multisigCluster)
72-
return new Connection(urls[urlsIndex].rpcUrl, {
72+
return new Connection(urls[urlsIndex], {
7373
commitment: 'confirmed',
74-
wsEndpoint: urls[urlsIndex].wsUrl,
74+
wsEndpoint: deriveWsUrl(urls[urlsIndex]),
7575
})
7676
}, [urlsIndex, multisigCluster])
7777

governance/xc_admin/packages/xc_admin_frontend/hooks/usePyth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Connection, PublicKey } from '@solana/web3.js'
1313
import assert from 'assert'
1414
import { useContext, useEffect, useRef, useState } from 'react'
1515
import { ClusterContext } from '../contexts/ClusterContext'
16-
import { pythClusterApiUrls } from '../utils/pythClusterApiUrl'
16+
import { deriveWsUrl, pythClusterApiUrls } from '../utils/pythClusterApiUrl'
1717

1818
const ONES = '11111111111111111111111111111111'
1919

@@ -67,9 +67,9 @@ const usePyth = (): PythHookData => {
6767
useEffect(() => {
6868
let cancelled = false
6969
const urls = pythClusterApiUrls(cluster)
70-
const connection = new Connection(urls[urlsIndex].rpcUrl, {
70+
const connection = new Connection(urls[urlsIndex], {
7171
commitment: 'confirmed',
72-
wsEndpoint: urls[urlsIndex].wsUrl,
72+
wsEndpoint: deriveWsUrl(urls[urlsIndex]),
7373
})
7474

7575
connectionRef.current = connection
Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,40 @@
1-
import { PythCluster } from '@pythnetwork/client/lib/cluster'
1+
import {
2+
PythCluster,
3+
getPythClusterApiUrl,
4+
} from '@pythnetwork/client/lib/cluster'
25

3-
const CLUSTER_URLS: Record<PythCluster, any> = {
6+
const CLUSTER_URLS: Record<PythCluster, string[]> = {
47
'mainnet-beta': [
5-
{
6-
rpcUrl: 'http://mainnet.xyz.pyth.network',
7-
wsUrl: 'ws://mainnet.xyz.pyth.network',
8-
},
9-
{
10-
rpcUrl:
11-
'https://pyth-network.rpcpool.com/' +
12-
(process.env.NEXT_PUBLIC_RPC_POOL_TOKEN || ''),
13-
wsUrl:
14-
'wss://pyth-network.rpcpool.com/' +
15-
(process.env.NEXT_PUBLIC_RPC_POOL_TOKEN || ''),
16-
},
17-
{
18-
rpcUrl: 'http://pyth-rpc1.certus.one:8899/',
19-
wsUrl: 'ws://pyth-rpc1.certus.one:8900/',
20-
},
21-
{
22-
rpcUrl: 'http://pyth-rpc2.certus.one:8899/',
23-
wsUrl: 'ws://pyth-rpc2.certus.one:8900/',
24-
},
25-
{
26-
rpcUrl: 'https://api.mainnet-beta.solana.com/',
27-
wsUrl: 'wss://api.mainnet-beta.solana.com/',
28-
},
8+
process.env.NEXT_PUBLIC_MAINNET_RPC || getPythClusterApiUrl('mainnet-beta'),
9+
'https://pyth-network.rpcpool.com/' +
10+
(process.env.NEXT_PUBLIC_RPC_POOL_TOKEN || ''),
11+
'http://pyth-rpc1.certus.one:8899/',
12+
'http://pyth-rpc2.certus.one:8899/',
13+
'https://api.mainnet-beta.solana.com/',
2914
],
3015
devnet: [
31-
{
32-
rpcUrl: 'http://devnet.xyz.pyth.network',
33-
wsUrl: 'ws://devnet.xyz.pyth.network',
34-
},
35-
{
36-
rpcUrl: 'https://api.devnet.solana.com/',
37-
wsUrl: 'wss://api.devnet.solana.com/',
38-
},
16+
process.env.NEXT_PUBLIC_DEVNET_RPC || getPythClusterApiUrl('devnet'),
17+
'https://api.devnet.solana.com/',
3918
],
4019
testnet: [
41-
{
42-
rpcUrl: 'http://testnet.xyz.pyth.network',
43-
wsUrl: 'ws://testnet.xyz.pyth.network',
44-
},
45-
{
46-
rpcUrl: 'https://api.testnet.solana.com/',
47-
wsUrl: 'wss://api.testnet.solana.com/',
48-
},
20+
process.env.NEXT_PUBLIC_TESTNET_RPC || getPythClusterApiUrl('testnet'),
21+
'https://api.testnet.solana.com/',
4922
],
5023
'pythtest-conformance': [
51-
{
52-
rpcUrl: 'http://pythtest.xyz.pyth.network',
53-
wsUrl: 'ws://pythtest.xyz.pyth.network',
54-
},
55-
{
56-
rpcUrl: 'https://api.pythtest.pyth.network/',
57-
wsUrl: 'wss://api.pythtest.pyth.network/',
58-
},
24+
process.env.NEXT_PUBLIC_PYTHTEST_RPC ||
25+
getPythClusterApiUrl('pythtest-conformance'),
26+
'https://api.pythtest.pyth.network/',
5927
],
6028
'pythtest-crosschain': [
61-
{
62-
rpcUrl: 'http://pythtest.xyz.pyth.network',
63-
wsUrl: 'ws://pythtest.xyz.pyth.network',
64-
},
65-
{
66-
rpcUrl: 'https://api.pythtest.pyth.network/',
67-
wsUrl: 'wss://api.pythtest.pyth.network/',
68-
},
29+
process.env.NEXT_PUBLIC_PYTHTEST_RPC ||
30+
getPythClusterApiUrl('pythtest-crosschain'),
31+
'https://api.pythtest.pyth.network/',
6932
],
7033
pythnet: [
71-
{
72-
rpcUrl: 'http://pythnet.xyz.pyth.network',
73-
wsUrl: 'ws://pythnet.xyz.pyth.network',
74-
},
75-
{
76-
rpcUrl: 'https://pythnet.rpcpool.com/',
77-
wsUrl: 'wss://pythnet.rpcpool.com/',
78-
},
79-
],
80-
localnet: [
81-
{
82-
rpcUrl: 'http://localhost:8899/',
83-
wsUrl: 'ws://localhost:8900/',
84-
},
34+
process.env.NEXT_PUBLIC_PYTHNET_RPC || getPythClusterApiUrl('pythnet'),
35+
'https://pythnet.rpcpool.com/',
8536
],
37+
localnet: ['http://localhost:8899/'],
8638
}
8739

8840
export function pythClusterApiUrls(cluster: PythCluster) {
@@ -92,3 +44,11 @@ export function pythClusterApiUrls(cluster: PythCluster) {
9244
return []
9345
}
9446
}
47+
48+
export function deriveWsUrl(httpUrl: string) {
49+
if (httpUrl.startsWith('https://')) {
50+
return 'wss://' + httpUrl.slice(8)
51+
} else {
52+
return 'ws://' + httpUrl.slice(7)
53+
}
54+
}

0 commit comments

Comments
 (0)