Skip to content

Commit 7d8a33e

Browse files
authored
Use dotenv for cluster selection (solana-labs#25)
1 parent 3534449 commit 7d8a33e

9 files changed

+30
-29
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/store
44
/.cargo
55
/dist
6-
cluster.txt
6+
.env

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ script:
3232
- npm run build:program-c
3333
- npm run build:program-rust
3434
- npm run test:program-rust
35-
- npm run cluster:local
35+
- npm run cluster:localnet
3636
- npm run start

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ $ npm run cluster:devnet
258258
259259
To point back to the local cluster:
260260
```bash
261-
$ npm run cluster:local
261+
$ npm run cluster:localnet
262262
```
263263
264264
## Expand your skills with advanced examples

cluster-devnet.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LIVE=1
2+
CLUSTER=devnet

cluster-mainnet-beta.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LIVE=1
2+
CLUSTER=mainnet-beta

cluster-testnet.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LIVE=1
2+
CLUSTER=testnet

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"build:program-rust": "./src/program-rust/do.sh build && npm run clean:store",
2626
"clean:program-rust": "./src/program-rust/do.sh clean && rm -rf ./dist && npm run clean:store",
2727
"test:program-rust": "./src/program-rust/do.sh test",
28-
"cluster:local": "npm run clean:store && rm -f cluster.txt",
29-
"cluster:devnet": "npm run clean:store && echo devnet > cluster.txt",
30-
"cluster:testnet": "npm run clean:store && echo testnet > cluster.txt",
31-
"cluster:mainnet-beta": "npm run clean:store && echo mainnet-beta > cluster.txt",
28+
"cluster:localnet": "npm run clean:store && rm -f .env",
29+
"cluster:devnet": "npm run clean:store && cp cluster-devnet.env .env",
30+
"cluster:testnet": "npm run clean:store && cp cluster-testnet.env .env",
31+
"cluster:mainnet-beta": "npm run clean:store && cp cluster-mainnet-beta.env .env",
3232
"localnet:update": "solana-localnet update",
3333
"localnet:up": "set -x; solana-localnet down; set -e; solana-localnet up",
3434
"localnet:down": "solana-localnet down",
@@ -57,6 +57,7 @@
5757
"body-parser": "^1.18.3",
5858
"buffer-layout": "^1.2.0",
5959
"css-loader": "^3.1.0",
60+
"dotenv": "8.2.0",
6061
"eslint": "^6.1.0",
6162
"eslint-loader": "^4.0.0",
6263
"eslint-plugin-import": "^2.13.0",

url.js

+11-22
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,30 @@
22
// environment. By default, `LIVE=1` will connect to the devnet cluster.
33

44
import {clusterApiUrl, Cluster} from '@solana/web3.js';
5-
import fs from 'mz/fs';
5+
import dotenv from 'dotenv';
66

77
function chooseCluster(): Cluster | undefined {
8-
let cluster;
9-
if (process.env.LIVE) {
10-
cluster = process.env.CLUSTER;
11-
} else if (fs.existsSync('cluster.txt')) {
12-
cluster = fs
13-
.readFileSync('cluster.txt')
14-
.toString()
15-
.replace(/(\r\n|\n|\r| )/gm, '');
16-
}
17-
18-
if (cluster) {
19-
switch (cluster) {
20-
case 'devnet':
21-
case 'testnet':
22-
case 'mainnet-beta': {
23-
return cluster;
24-
}
25-
default:
26-
throw 'Unknown cluster: ' + cluster;
8+
dotenv.config();
9+
if (!process.env.LIVE) return;
10+
switch (process.env.CLUSTER) {
11+
case 'devnet':
12+
case 'testnet':
13+
case 'mainnet-beta': {
14+
return process.env.CLUSTER;
2715
}
2816
}
17+
throw 'Unknown cluster "' + process.env.CLUSTER + '", check the .env file';
2918
}
3019

3120
export const cluster = chooseCluster();
3221

3322
export const url =
3423
process.env.RPC_URL ||
35-
(cluster ? clusterApiUrl(cluster, false) : 'http://localhost:8899');
24+
(process.env.LIVE ? clusterApiUrl(cluster, false) : 'http://localhost:8899');
3625

3726
export const urlTls =
3827
process.env.RPC_URL ||
39-
(cluster ? clusterApiUrl(cluster, true) : 'http://localhost:8899');
28+
(process.env.LIVE ? clusterApiUrl(cluster, true) : 'http://localhost:8899');
4029

4130
export let walletUrl =
4231
process.env.WALLET_URL || 'https://solana-example-webwallet.herokuapp.com/';

0 commit comments

Comments
 (0)