feat(relayer): Add relayer region to the wallet v2 settings (#64)

* feat(relayer): Add relayer region to the wallet v2 settings

* prettier linting
This commit is contained in:
crypblizz 2022-09-20 15:06:25 +02:00 committed by GitHub
parent 2cf644ec7a
commit 16d2341b8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 8 deletions

View File

@ -0,0 +1,28 @@
import { REGIONALIZED_RELAYER_ENDPOINTS } from '@/data/RelayerRegions'
import SettingsStore from '@/store/SettingsStore'
import { useSnapshot } from 'valtio'
export default function AccountPicker() {
const { relayerRegionURL } = useSnapshot(SettingsStore.state)
function onSelect(value: string) {
SettingsStore.setRelayerRegionURL(value)
}
return (
<select
value={relayerRegionURL}
onChange={e => onSelect(e.currentTarget.value)}
aria-label="relayerRegions"
>
{REGIONALIZED_RELAYER_ENDPOINTS.map((endpoint, index) => {
return (
<option key={index} value={endpoint.value}>
{endpoint.label}
</option>
)
})}
</select>
)
}

View File

@ -0,0 +1,31 @@
/**
* Types
*/
type RelayerType = {
value: string
label: string
}
/**
* Relayer Regions
*/
export const REGIONALIZED_RELAYER_ENDPOINTS: RelayerType[] = [
{
value: 'wss://relay.walletconnect.com',
label: 'Default'
},
{
value: 'wss://us-east-1.relay.walletconnect.com/',
label: 'US'
},
{
value: 'wss://eu-central-1.relay.walletconnect.com/',
label: 'EU'
},
{
value: 'wss://ap-southeast-1.relay.walletconnect.com/',
label: 'Asia Pacific'
}
]

View File

@ -4,10 +4,14 @@ import { createOrRestoreEIP155Wallet } from '@/utils/EIP155WalletUtil'
import { createOrRestoreSolanaWallet } from '@/utils/SolanaWalletUtil' import { createOrRestoreSolanaWallet } from '@/utils/SolanaWalletUtil'
import { createOrRestorePolkadotWallet } from '@/utils/PolkadotWalletUtil' import { createOrRestorePolkadotWallet } from '@/utils/PolkadotWalletUtil'
import { createSignClient } from '@/utils/WalletConnectUtil' import { createSignClient } from '@/utils/WalletConnectUtil'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useRef, useState } from 'react'
import { useSnapshot } from 'valtio'
export default function useInitialization() { export default function useInitialization() {
const [initialized, setInitialized] = useState(false) const [initialized, setInitialized] = useState(false)
const prevRelayerURLValue = useRef<string>('')
const { relayerRegionURL } = useSnapshot(SettingsStore.state)
const onInitialize = useCallback(async () => { const onInitialize = useCallback(async () => {
try { try {
@ -21,19 +25,24 @@ export default function useInitialization() {
SettingsStore.setSolanaAddress(solanaAddresses[0]) SettingsStore.setSolanaAddress(solanaAddresses[0])
SettingsStore.setPolkadotAddress(polkadotAddresses[0]) SettingsStore.setPolkadotAddress(polkadotAddresses[0])
await createSignClient() await createSignClient(relayerRegionURL)
prevRelayerURLValue.current = relayerRegionURL
setInitialized(true) setInitialized(true)
} catch (err: unknown) { } catch (err: unknown) {
alert(err) alert(err)
} }
}, []) }, [relayerRegionURL])
useEffect(() => { useEffect(() => {
if (!initialized) { if (!initialized) {
onInitialize() onInitialize()
} }
}, [initialized, onInitialize]) if (prevRelayerURLValue.current !== relayerRegionURL) {
setInitialized(false)
onInitialize()
}
}, [initialized, onInitialize, relayerRegionURL])
return initialized return initialized
} }

View File

@ -1,4 +1,5 @@
import PageHeader from '@/components/PageHeader' import PageHeader from '@/components/PageHeader'
import RelayRegionPicker from '@/components/RelayRegionPicker'
import SettingsStore from '@/store/SettingsStore' import SettingsStore from '@/store/SettingsStore'
import { cosmosWallets } from '@/utils/CosmosWalletUtil' import { cosmosWallets } from '@/utils/CosmosWalletUtil'
import { eip155Wallets } from '@/utils/EIP155WalletUtil' import { eip155Wallets } from '@/utils/EIP155WalletUtil'
@ -43,6 +44,16 @@ export default function SettingsPage() {
<Divider y={2} /> <Divider y={2} />
<Row justify="space-between" align="center">
<Text h4 css={{ marginBottom: '$5' }}>
Relayer Region
</Text>
<RelayRegionPicker/>
</Row>
<Divider y={2} />
<Text css={{ color: '$yellow500', marginBottom: '$5', textAlign: 'left', padding: 0 }}> <Text css={{ color: '$yellow500', marginBottom: '$5', textAlign: 'left', padding: 0 }}>
Warning: mnemonics and secret keys are provided for development purposes only and should not Warning: mnemonics and secret keys are provided for development purposes only and should not
be used elsewhere! be used elsewhere!

View File

@ -10,6 +10,7 @@ interface State {
cosmosAddress: string cosmosAddress: string
solanaAddress: string solanaAddress: string
polkadotAddress: string polkadotAddress: string
relayerRegionURL: string
} }
/** /**
@ -21,7 +22,8 @@ const state = proxy<State>({
eip155Address: '', eip155Address: '',
cosmosAddress: '', cosmosAddress: '',
solanaAddress: '', solanaAddress: '',
polkadotAddress: '' polkadotAddress: '',
relayerRegionURL: ''
}) })
/** /**
@ -50,6 +52,10 @@ const SettingsStore = {
state.polkadotAddress = polkadotAddress state.polkadotAddress = polkadotAddress
}, },
setRelayerRegionURL(relayerRegionURL: string) {
state.relayerRegionURL = relayerRegionURL
},
toggleTestNets() { toggleTestNets() {
state.testNets = !state.testNets state.testNets = !state.testNets
if (state.testNets) { if (state.testNets) {

View File

@ -2,10 +2,10 @@ import SignClient from '@walletconnect/sign-client'
export let signClient: SignClient export let signClient: SignClient
export async function createSignClient() { export async function createSignClient(relayerRegionURL: string) {
signClient = await SignClient.init({ signClient = await SignClient.init({
projectId: process.env.NEXT_PUBLIC_PROJECT_ID, projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
relayUrl: process.env.NEXT_PUBLIC_RELAY_URL ?? 'wss://relay.walletconnect.com', relayUrl: relayerRegionURL ?? process.env.NEXT_PUBLIC_RELAYER_URL,
metadata: { metadata: {
name: 'React Wallet', name: 'React Wallet',
description: 'React Wallet for WalletConnect', description: 'React Wallet for WalletConnect',