Skip to content

Commit 5e03355

Browse files
committed
feat(data): provide function to get supported networks
re #314
1 parent 92a6a7a commit 5e03355

File tree

8 files changed

+43
-18
lines changed

8 files changed

+43
-18
lines changed

packages/cli/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GroupResponse, SemaphoreEthers, SemaphoreSubgraph } from "@semaphore-protocol/data"
1+
import { GroupResponse, SemaphoreEthers, SemaphoreSubgraph, getSupportedNetworks } from "@semaphore-protocol/data"
22
import chalk from "chalk"
33
import { program } from "commander"
44
import figlet from "figlet"
@@ -16,7 +16,7 @@ import Spinner from "./spinner.js"
1616
const packagePath = `${dirname(fileURLToPath(import.meta.url))}/..`
1717
const { description, version } = JSON.parse(readFileSync(`${packagePath}/package.json`, "utf8"))
1818

19-
const supportedNetworks = ["sepolia", "goerli", "mumbai", "optimism-goerli", "arbitrum", "arbitrum-goerli"]
19+
const supportedNetworks = getSupportedNetworks()
2020

2121
const supportedTemplates = [
2222
{

packages/data/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ yarn add @semaphore-protocol/data
7070

7171
## 📜 Usage
7272

73-
\# **new SemaphoreSubgraph**(networkOrSubgraphURL: _Network_ | _string_ = "goerli"): _SemaphoreSubgraph_
73+
\# **getSupportedNetworks**(): _string[]_
74+
75+
```typescript
76+
const supportedNetworks = getSupportedNetworks()
77+
```
78+
79+
\# **new SemaphoreSubgraph**(networkOrSubgraphURL: SupportedNetwork | ValueOf\<SupportedNetwork> | string = "sepolia"): _SemaphoreSubgraph_
7480

7581
```typescript
7682
import { SemaphoreSubgraph } from "@semaphore-protocol/data"

packages/data/src/ethers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
import checkParameter from "./checkParameter"
1313
import getEvents from "./getEvents"
1414
import SemaphoreABI from "./semaphoreABI.json"
15-
import { EthersOptions, GroupResponse, Network } from "./types"
15+
import { EthersOptions, GroupResponse, EthersNetwork } from "./types"
1616

1717
export default class SemaphoreEthers {
18-
private _network: Network | string
18+
private _network: EthersNetwork | string
1919
private _options: EthersOptions
2020
private _contract: Contract
2121

@@ -24,7 +24,7 @@ export default class SemaphoreEthers {
2424
* @param networkOrEthereumURL Ethereum network or custom URL.
2525
* @param options Ethers options.
2626
*/
27-
constructor(networkOrEthereumURL: Network | string = "sepolia", options: EthersOptions = {}) {
27+
constructor(networkOrEthereumURL: EthersNetwork | string = "sepolia", options: EthersOptions = {}) {
2828
checkParameter(networkOrEthereumURL, "networkOrSubgraphURL", "string")
2929

3030
if (options.provider) {
@@ -114,7 +114,7 @@ export default class SemaphoreEthers {
114114
* Returns the Ethereum network or custom URL.
115115
* @returns Ethereum network or custom URL.
116116
*/
117-
get network(): Network | string {
117+
get network(): EthersNetwork | string {
118118
return this._network
119119
}
120120

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SupportedNetwork } from "./types"
2+
3+
/**
4+
* Returns the list of Semaphore supported networks.
5+
* @returns Semaphore supported networks.
6+
*/
7+
export default function getSupportedNetworks(): string[] {
8+
return Object.values(SupportedNetwork)
9+
}

packages/data/src/getURL.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Network } from "./types"
1+
import { SupportedNetwork } from "./types"
22

33
/**
44
* Returns the subgraph URL related to the network passed as a parameter.
5-
* @param network Semaphore supported network.
5+
* @param supportedNetwork Semaphore supported network.
66
* @returns Subgraph URL.
77
*/
88
export default function getURL(supportedNetwork: SupportedNetwork | string): string {
@@ -13,8 +13,8 @@ export default function getURL(supportedNetwork: SupportedNetwork | string): str
1313
case "optimism-goerli":
1414
case "arbitrum-goerli":
1515
case "arbitrum":
16-
return `https://api.studio.thegraph.com/query/14377/semaphore-${network}/v3.6.1`
16+
return `https://api.studio.thegraph.com/query/14377/semaphore-${supportedNetwork}/v3.6.1`
1717
default:
18-
throw new TypeError(`Network '${network}' is not supported`)
18+
throw new TypeError(`Network '${supportedNetwork}' is not supported`)
1919
}
2020
}

packages/data/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import SemaphoreSubgraph from "./subgraph"
21
import SemaphoreEthers from "./ethers"
2+
import getSupportedNetworks from "./getSupportedNetworks"
3+
import SemaphoreSubgraph from "./subgraph"
34

4-
export { SemaphoreSubgraph, SemaphoreEthers }
55
export * from "./types"
6+
export { SemaphoreSubgraph, SemaphoreEthers, getSupportedNetworks }

packages/data/src/subgraph.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AxiosRequestConfig } from "axios"
22
import checkParameter from "./checkParameter"
33
import getURL from "./getURL"
44
import request from "./request"
5-
import { GroupResponse, GroupOptions, Network } from "./types"
5+
import { GroupOptions, GroupResponse, SupportedNetwork } from "./types"
66
import { jsDateToGraphqlDate } from "./utils"
77

88
export default class SemaphoreSubgraph {
@@ -12,15 +12,15 @@ export default class SemaphoreSubgraph {
1212
* Initializes the subgraph object with one of the supported networks or a custom URL.
1313
* @param networkOrSubgraphURL Supported Semaphore network or custom Subgraph URL.
1414
*/
15-
constructor(networkOrSubgraphURL: Network | string = "goerli") {
15+
constructor(networkOrSubgraphURL: SupportedNetwork | string = SupportedNetwork.SEPOLIA) {
1616
checkParameter(networkOrSubgraphURL, "networkOrSubgraphURL", "string")
1717

18-
if (networkOrSubgraphURL.startsWith("http")) {
18+
if (typeof networkOrSubgraphURL === "string" && networkOrSubgraphURL.startsWith("http")) {
1919
this._url = networkOrSubgraphURL
2020
return
2121
}
2222

23-
this._url = getURL(networkOrSubgraphURL as Network)
23+
this._url = getURL(networkOrSubgraphURL)
2424
}
2525

2626
/**

packages/data/src/types/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export type Network =
1+
export enum SupportedNetwork {
2+
SEPOLIA = "sepolia",
3+
GOERLI = "goerli",
4+
MUMBAI = "mumbai",
5+
OPTIMISM_GOERLI = "optimism-goerli",
6+
ARBITRUM_GOERLI = "arbitrum-goerli",
7+
ARBITRUM = "arbitrum"
8+
}
9+
10+
export type EthersNetwork =
211
| "homestead"
312
| "matic"
413
| "goerli"

0 commit comments

Comments
 (0)