enhance the usage of config
- use optimism goerli as default - use any wallet provider in dev builds
This commit is contained in:
parent
08676b6d65
commit
c92ea71397
|
@ -6,6 +6,7 @@ import { Modal } from './Modal'
|
|||
import { LinkExternal } from './Link'
|
||||
import statusLogo from '../assets/images/statusLogo.svg'
|
||||
import { ColumnFlexDiv } from '../constants/styles'
|
||||
import { useConfig } from '../providers/config'
|
||||
|
||||
export type ConnectButtonProps = {
|
||||
text?: string
|
||||
|
@ -15,13 +16,14 @@ export type ConnectButtonProps = {
|
|||
export function ConnectButton({ text, className }: ConnectButtonProps) {
|
||||
const [showModal, setShowModal] = useState(false)
|
||||
const { activateBrowserWallet } = useEthers()
|
||||
const { config } = useConfig()
|
||||
|
||||
const activateStatusWallet = () => {
|
||||
if ((window as any).ethereum?.isStatus) {
|
||||
activateBrowserWallet()
|
||||
} else {
|
||||
const activateWallet = () => {
|
||||
if (config.statusWalletRequired && !((window as any).ethereum?.isStatus)) {
|
||||
setShowModal(true)
|
||||
return
|
||||
}
|
||||
activateBrowserWallet()
|
||||
}
|
||||
|
||||
return (
|
||||
|
@ -31,7 +33,7 @@ export function ConnectButton({ text, className }: ConnectButtonProps) {
|
|||
<StatusModal />{' '}
|
||||
</Modal>
|
||||
)}
|
||||
<ProposeButton className={className} onClick={activateStatusWallet}>
|
||||
<ProposeButton className={className} onClick={activateWallet}>
|
||||
{!text ? 'Connect to Vote' : text}
|
||||
</ProposeButton>
|
||||
</div>
|
||||
|
|
|
@ -46,7 +46,7 @@ export async function receiveWakuFeature(waku: WakuLight | undefined, topic: str
|
|||
if (!top5.find((featuredComm) => featuredComm[0] === el.publicKey)) {
|
||||
merge(wakuFeatured, { [el.publicKey]: { votes: { [el.voter]: el.sntAmount } } })
|
||||
}
|
||||
} else {
|
||||
} else { // bug: this element's vote is ignored
|
||||
top5 = getTop(wakuFeatured, 5)
|
||||
top5.forEach((featuredComm) => {
|
||||
wakuFeatured[featuredComm[0]].votes = {}
|
||||
|
|
|
@ -9,17 +9,21 @@ import { ConfigProvider } from './providers/config/provider'
|
|||
import { WakuProvider } from './providers/waku/provider'
|
||||
import { CommunitiesProvider } from './providers/communities/provider'
|
||||
import { WakuFeatureProvider } from './providers/wakuFeature/provider'
|
||||
import { contracts, CustomChainId, getDAppConfig } from './providers/config/config'
|
||||
|
||||
const config = {
|
||||
readOnlyChainId: ChainId.Ropsten,
|
||||
readOnlyUrls: {
|
||||
readOnlyChainId: getDAppConfig(process.env.ENV).defaultChainId,
|
||||
readonlyUrls: {
|
||||
[ChainId.Ropsten]: 'https://ropsten.infura.io/v3/b4451d780cc64a078ccf2181e872cfcf',
|
||||
[CustomChainId.OptimismGoerli]: 'https://optimism-goerli.infura.io/v3/4c90c025caca4de2b1419633554e6bca',
|
||||
},
|
||||
multicallAddresses: {
|
||||
...DEFAULT_CONFIG.multicallAddresses,
|
||||
1337: process.env.GANACHE_MULTICALL_CONTRACT ?? '0x0000000000000000000000000000000000000000',
|
||||
[ChainId.Ropsten]: contracts[ChainId.Ropsten].multicallContract,
|
||||
[CustomChainId.OptimismGoerli]: contracts[CustomChainId.OptimismGoerli].multicallContract,
|
||||
[ChainId.Localhost]: contracts[ChainId.Localhost].multicallContract,
|
||||
},
|
||||
supportedChains: [...DEFAULT_CONFIG.supportedChains, 1337],
|
||||
supportedChains: [...DEFAULT_CONFIG.supportedChains, CustomChainId.OptimismGoerli],
|
||||
notifications: {
|
||||
checkInterval: 500,
|
||||
expirationPeriod: 50000,
|
||||
|
@ -28,17 +32,17 @@ const config = {
|
|||
|
||||
render(
|
||||
<React.StrictMode>
|
||||
<WakuProvider>
|
||||
<DAppProvider config={config}>
|
||||
<ConfigProvider>
|
||||
<ConfigProvider>
|
||||
<WakuProvider>
|
||||
<DAppProvider config={config}>
|
||||
<CommunitiesProvider>
|
||||
<WakuFeatureProvider>
|
||||
<App />
|
||||
</WakuFeatureProvider>
|
||||
</CommunitiesProvider>
|
||||
</ConfigProvider>
|
||||
</DAppProvider>
|
||||
</WakuProvider>
|
||||
</DAppProvider>
|
||||
</WakuProvider>
|
||||
</ConfigProvider>
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
)
|
||||
|
|
|
@ -1,59 +1,80 @@
|
|||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { ChainId, MULTICALL_ADDRESSES } from '@usedapp/core/src/constants'
|
||||
import { Fleet } from 'js-waku/lib/predefined_bootstrap_nodes'
|
||||
|
||||
const version = '0.0.5'
|
||||
const rapid = true // VOTING_LENGTH = 2min, TIME_BETWEEN_VOTING = 30s
|
||||
|
||||
export interface Config {
|
||||
numberPerPage: number
|
||||
fleet: Fleet
|
||||
wakuTopic: string
|
||||
wakuFeatureTopic: string
|
||||
defaultChainId: number
|
||||
contracts: {
|
||||
[chainID: number]: {
|
||||
[name: string]: string
|
||||
}
|
||||
}
|
||||
statusWalletRequired: boolean
|
||||
}
|
||||
|
||||
interface EnvConfigs {
|
||||
interface DAppConfigs {
|
||||
[env: string]: Config
|
||||
}
|
||||
|
||||
const contracts = {
|
||||
3: {
|
||||
subgraph: '',
|
||||
export enum CustomChainId {
|
||||
OptimismGoerli = 420
|
||||
}
|
||||
|
||||
export const contracts = {
|
||||
[ChainId.Ropsten]: {
|
||||
votingContract: '0x120aEb4726F8B188DFBd7733E6A0Af85a445c8Bf',
|
||||
directoryContract: '0x950071851cB75F56BA90Fe718C3dDb723Bf852e2',
|
||||
tokenContract: '0x80ee48b5ba5c3EA556b7fF6D850d2fB2c4bc7412',
|
||||
multicallContract: MULTICALL_ADDRESSES[ChainId.Ropsten]
|
||||
},
|
||||
1337: {
|
||||
subgraph: 'http://localhost:8000/subgraphs/name/HistorySubgraph',
|
||||
[CustomChainId.OptimismGoerli]: {
|
||||
votingContract: rapid ? '0x86037004278B7e6BC5f2a34ce5DFAd7c06555e7c' : '0x6F4F83bF868585f25090b111Fe762d9Dba3B839D',
|
||||
directoryContract: rapid ? '0x5C6CCc458020977dFE324FA8924F71A4d38c6742' : '0xaEA4F79e020B041C92316303D6889E32C2285D0b',
|
||||
tokenContract: '0xf8E655fd30696Beab513ACce4c75430a992301A7',
|
||||
multicallContract: '0x805e246abef0D2C76E619E122c79b1EF2EfBd8b7'
|
||||
},
|
||||
[ChainId.Localhost]: {
|
||||
votingContract: process.env.GANACHE_VOTING_CONTRACT ?? '0x0000000000000000000000000000000000000000',
|
||||
directoryContract: process.env.GANACHE_DIRECTORY_CONTRACT ?? '0x0000000000000000000000000000000000000000',
|
||||
tokenContract: process.env.GANACHE_TOKEN_CONTRACT ?? '0x0000000000000000000000000000000000000000',
|
||||
multicallContract: process.env.GANACHE_MULTICALL_CONTRACT ?? '0x0000000000000000000000000000000000000000',
|
||||
},
|
||||
}
|
||||
|
||||
const version = '0.0.5'
|
||||
|
||||
export const config: EnvConfigs = {
|
||||
export const config: DAppConfigs = {
|
||||
localhost: {
|
||||
wakuTopic: `/myApp/localhost/${uuidv4()}/${version}/votingRoom/proto/`,
|
||||
wakuFeatureTopic: `/myApp/localhost/${uuidv4()}/${version}/feature/proto/`,
|
||||
numberPerPage: 2,
|
||||
fleet: Fleet.Test,
|
||||
wakuTopic: `/communitiesCuration/localhost/${uuidv4()}/${version}/directory/proto/`,
|
||||
wakuFeatureTopic: `/communitiesCuration/localhost/${uuidv4()}/${version}/featured/proto/`,
|
||||
defaultChainId: ChainId.Localhost,
|
||||
contracts,
|
||||
statusWalletRequired: false,
|
||||
},
|
||||
development: {
|
||||
wakuTopic: `/myApp/development/${version}/votingRoom/proto/`,
|
||||
wakuFeatureTopic: `/myApp/development/${version}/feature/proto/`,
|
||||
numberPerPage: 3,
|
||||
fleet: Fleet.Test,
|
||||
wakuTopic: `/communitiesCuration/development/${version}/directory/proto/`,
|
||||
wakuFeatureTopic: `/communitiesCuration/development/${version}/featured/proto/`,
|
||||
defaultChainId: CustomChainId.OptimismGoerli,
|
||||
contracts,
|
||||
statusWalletRequired: false,
|
||||
},
|
||||
production: {
|
||||
wakuTopic: `/myApp/production/${version}/votingRoom/proto/`,
|
||||
wakuFeatureTopic: `/myApp/production/${version}/feature/proto/`,
|
||||
numberPerPage: 4,
|
||||
fleet: Fleet.Prod,
|
||||
wakuTopic: `/communitiesCuration/${version}/directory/proto/`,
|
||||
wakuFeatureTopic: `/communitiesCuration/${version}/featured/proto/`,
|
||||
defaultChainId: ChainId.Ropsten,
|
||||
contracts,
|
||||
statusWalletRequired: true,
|
||||
},
|
||||
}
|
||||
|
||||
export const getEnvConfig = (env: string | undefined) => {
|
||||
export const getDAppConfig = (env: string | undefined) => {
|
||||
if (env) {
|
||||
return config[env]
|
||||
} else {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import React, { ReactNode, useReducer, createContext, useContext } from 'react'
|
||||
import merge from 'lodash/merge'
|
||||
import { getEnvConfig, Config } from './config'
|
||||
import { getDAppConfig, Config } from './config'
|
||||
|
||||
const ConfigContext = createContext<{ config: Config; updateConfig: (config: Partial<Config>) => void }>({
|
||||
config: getEnvConfig(process.env.ENV),
|
||||
config: getDAppConfig(process.env.ENV),
|
||||
updateConfig: () => undefined,
|
||||
})
|
||||
|
||||
|
@ -16,7 +16,7 @@ interface ConfigProviderProps {
|
|||
}
|
||||
|
||||
export function ConfigProvider({ children }: ConfigProviderProps) {
|
||||
const [reducedConfig, dispatch] = useReducer(configReducer, { ...getEnvConfig(process.env.ENV) })
|
||||
const [reducedConfig, dispatch] = useReducer(configReducer, { ...getDAppConfig(process.env.ENV) })
|
||||
return <ConfigContext.Provider value={{ config: reducedConfig, updateConfig: dispatch }} children={children} />
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
|||
|
||||
import type { WakuLight } from 'js-waku/lib/interfaces'
|
||||
|
||||
export async function connectWaku(setWaku: (waku: WakuLight) => void) {
|
||||
export async function connectWaku(setWaku: (waku: WakuLight) => void, fleet: Fleet) {
|
||||
// todo: handle disconnects
|
||||
const newWaku = await createLightNode({
|
||||
defaultBootstrap: false,
|
||||
|
@ -15,8 +15,7 @@ export async function connectWaku(setWaku: (waku: WakuLight) => void) {
|
|||
// @ts-ignore
|
||||
emitSelf: true,
|
||||
libp2p: {
|
||||
// todo: use config or proces.env.ENV to select fleet
|
||||
peerDiscovery: [new PeerDiscoveryStaticPeers(getPredefinedBootstrapNodes(Fleet.Test), { maxPeers: 1 })],
|
||||
peerDiscovery: [new PeerDiscoveryStaticPeers(getPredefinedBootstrapNodes(fleet), { maxPeers: 1 })],
|
||||
},
|
||||
})
|
||||
await newWaku.start()
|
||||
|
|
|
@ -2,6 +2,7 @@ import React, { ReactNode, createContext, useContext, useState } from 'react'
|
|||
import { connectWaku } from './connect'
|
||||
|
||||
import type { WakuLight } from 'js-waku/lib/interfaces'
|
||||
import { useConfig } from '../config';
|
||||
|
||||
const WakuContext = createContext<{ waku: WakuLight | undefined; setWaku: (waku: WakuLight) => void }>({
|
||||
waku: undefined,
|
||||
|
@ -11,9 +12,10 @@ const WakuContext = createContext<{ waku: WakuLight | undefined; setWaku: (waku:
|
|||
export function useWaku() {
|
||||
const { setWaku, waku } = useContext(WakuContext)
|
||||
const [creatingWaku, setCreatingWaku] = useState(false)
|
||||
const { config } = useConfig()
|
||||
if (!waku && !creatingWaku) {
|
||||
setCreatingWaku(true)
|
||||
connectWaku(setWaku)
|
||||
connectWaku(setWaku, config.fleet)
|
||||
}
|
||||
return { waku }
|
||||
}
|
||||
|
|
|
@ -5,23 +5,10 @@ import { expect } from 'chai'
|
|||
import merge from 'lodash/merge'
|
||||
|
||||
describe('ConfigProvider', () => {
|
||||
it('success', () => {
|
||||
const { result } = renderHook(useConfig, { wrapper: ConfigProvider })
|
||||
expect(result.current.config).to.deep.eq(config.development)
|
||||
act(() => {
|
||||
result.current.updateConfig({ numberPerPage: 5 })
|
||||
})
|
||||
})
|
||||
|
||||
it('updateConfig', () => {
|
||||
const { result } = renderHook(useConfig, { wrapper: ConfigProvider })
|
||||
expect(result.current.config).to.deep.eq(config.development)
|
||||
|
||||
act(() => {
|
||||
result.current.updateConfig({ numberPerPage: 5 })
|
||||
})
|
||||
expect(result.current.config).to.deep.eq({ ...config.development, numberPerPage: 5 })
|
||||
|
||||
const newContracts = {
|
||||
contracts: {
|
||||
1: {
|
||||
|
@ -33,7 +20,6 @@ describe('ConfigProvider', () => {
|
|||
|
||||
act(() => {
|
||||
result.current.updateConfig(newContracts)
|
||||
result.current.updateConfig({ numberPerPage: config.development.numberPerPage })
|
||||
})
|
||||
merge({}, config.development, newContracts)
|
||||
expect(result.current.config).to.deep.equal(merge({}, config.development, newContracts))
|
||||
|
|
Loading…
Reference in New Issue