-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.ts
More file actions
136 lines (112 loc) · 3.98 KB
/
Copy pathutils.ts
File metadata and controls
136 lines (112 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { NetworkType } from "@ecadlabs/beacon-types"
import { capitalize } from "@material-ui/core"
import { BeaconWallet } from "@taquito/beacon-wallet"
import { MichelCodecPacker, TezosToolkit } from "@taquito/taquito"
import { Tzip16Module } from "@taquito/tzip16"
import { EnvKey, getEnv } from "services/config"
export type Network = "mainnet" | "shadownet" | "etherlink_shadownet" | "etherlink_mainnet"
export const rpcNodes: Record<Network, string> = {
mainnet: getEnv(EnvKey.REACT_APP_RPC_NETWORK_MAINNET) || "https://mainnet.api.tez.ie",
shadownet: getEnv(EnvKey.REACT_APP_RPC_NETWORK_SHADOWNET) || "https://rpc.shadownet.teztnets.com",
etherlink_shadownet: "https://node.shadownet.etherlink.com",
etherlink_mainnet: "https://node.mainnet.etherlink.com"
}
export const networkDotColorMap: Record<Network, string> = {
mainnet: "#9EEE5D",
shadownet: "#291F79",
etherlink_mainnet: "#9EEE5D",
etherlink_shadownet: "#291F79"
}
// Infer the initial network from URL when no user selection exists yet
const inferInitialNetworkFromPath = (): Network | undefined => {
try {
const path = window?.location?.pathname || ""
if (path.startsWith("/explorer/etherlink")) return "etherlink_mainnet"
} catch (e) {
// noop — window may be undefined in some build contexts
}
return undefined
}
export const getTezosNetwork = (): Network => {
const storageNetwork = window.localStorage.getItem("homebase:network")
if (storageNetwork) {
return storageNetwork as Network
}
// If no stored selection, prefer Etherlink mainnet for Etherlink deep links
const urlInferred = inferInitialNetworkFromPath()
if (urlInferred) {
window.localStorage.setItem("homebase:network", urlInferred)
return urlInferred
}
const envNetwork = getEnv(EnvKey.REACT_APP_NETWORK).toString().toLowerCase() as Network
if (!envNetwork) {
throw new Error("No Network ENV set")
}
window.localStorage.setItem("homebase:network", envNetwork)
return envNetwork
}
export const createWallet = (network: Network) => {
const networkType = getNetworkTypeByEnvNetwork(network)
// For Shadownet, include the RPC URL so wallets know exactly which network to connect to
const networkConfig =
network === "shadownet"
? {
type: networkType,
rpcUrl: rpcNodes.shadownet
}
: { type: networkType }
return new BeaconWallet({
name: "Homebase",
iconUrl: "https://tezostaquito.io/img/favicon.png",
network: networkConfig,
walletConnectOptions: {
projectId: "1641355e825aeaa926e843dd38b04f6f", // Project ID can be customised
relayUrl: "wss://relay.walletconnect.com" // WC2 relayUrl can be customised
}
})
}
export const createTezos = (network: Network) => {
const tezos = new TezosToolkit(rpcNodes[network])
tezos.setPackerProvider(new MichelCodecPacker())
tezos.addExtension(new Tzip16Module())
return tezos
}
export const getNetworkTypeByEnvNetwork = (envNetwork: Network): NetworkType => {
switch (envNetwork) {
case "shadownet":
return NetworkType.SHADOWNET
case "mainnet":
return NetworkType.MAINNET
default:
return NetworkType.MAINNET
}
}
export const connectWithBeacon = async (
envNetwork: Network
): Promise<{
network: Network
wallet: BeaconWallet
}> => {
const wallet = createWallet(envNetwork)
// Network is already configured in createWallet via DAppClientOptions.network
// In Beacon SDK 4.7.0+, requestPermissions only accepts scopes, not network
await wallet.requestPermissions()
const accounts: any[] = JSON.parse(localStorage.getItem("beacon:accounts") as string)
const network = accounts[0].network.type as Network
return {
network,
wallet
}
}
export function getNetworkDisplayName(networkSlug: string) {
if (networkSlug.includes("_")) {
return networkSlug
.split("_")
.map(x => capitalize(x))
.join(" ")
}
if (!networkSlug.startsWith("etherlink")) {
return capitalize(`Tezos ${networkSlug}`)
}
return capitalize(networkSlug)
}