-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[dapp-kit] Add ability to configure networks to SuiClientProvider #13342
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,70 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { SuiClient, getFullnodeUrl } from '@mysten/sui.js/client'; | ||
import { SuiClientContext } from '../hooks/useSuiClient.js'; | ||
import { useMemo } from 'react'; | ||
import { SuiClient, getFullnodeUrl, isSuiClient } from '@mysten/sui.js/client'; | ||
import type { SuiClientOptions } from '@mysten/sui.js/client'; | ||
import { createContext, useMemo, useState } from 'react'; | ||
|
||
export interface SuiClientProviderProps { | ||
type NetworkConfig = SuiClient | SuiClientOptions; | ||
type NetworkConfigs<T extends NetworkConfig = NetworkConfig> = Record<string, T>; | ||
|
||
export interface SuiClientProviderContext { | ||
client: SuiClient; | ||
networks: NetworkConfigs; | ||
selectedNetwork: string; | ||
selectNetwork: (network: string) => void; | ||
} | ||
|
||
export const SuiClientContext = createContext<SuiClientProviderContext | null>(null); | ||
|
||
export interface SuiClientProviderProps<T extends NetworkConfigs> { | ||
networks?: T; | ||
createClient?: (name: keyof T, config: T[keyof T]) => SuiClient; | ||
defaultNetwork?: keyof T & string; | ||
children: React.ReactNode; | ||
client?: SuiClient; | ||
url?: string; | ||
queryKeyPrefix: string; | ||
} | ||
|
||
export const SuiClientProvider = (props: SuiClientProviderProps) => { | ||
const ctx = useMemo(() => { | ||
const client = | ||
props.client ?? | ||
new SuiClient({ | ||
url: props.url ?? getFullnodeUrl('devnet'), | ||
}); | ||
const DEFAULT_NETWORKS = { | ||
devnet: { url: getFullnodeUrl('devnet') }, | ||
}; | ||
|
||
const DEFAULT_CREATE_CLIENT = function createClient( | ||
_name: string, | ||
config: NetworkConfig | SuiClient, | ||
) { | ||
if (isSuiClient(config)) { | ||
return config; | ||
} | ||
|
||
return new SuiClient(config); | ||
}; | ||
|
||
export function SuiClientProvider<T extends NetworkConfigs>(props: SuiClientProviderProps<T>) { | ||
const networks = props.networks ?? (DEFAULT_NETWORKS as never); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as never casting here is weirding me out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change this to |
||
const createClient = | ||
(props.createClient as typeof DEFAULT_CREATE_CLIENT) ?? DEFAULT_CREATE_CLIENT; | ||
|
||
const [selectedNetwork, setSelectedNetwork] = useState<keyof T & string>( | ||
props.defaultNetwork ?? (Object.keys(networks)[0] as keyof T & string), | ||
); | ||
|
||
const [client, setClient] = useState<SuiClient>(() => { | ||
hayes-mysten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return createClient(selectedNetwork, networks[selectedNetwork]); | ||
}); | ||
|
||
const ctx = useMemo((): SuiClientProviderContext => { | ||
hayes-mysten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
client, | ||
queryKey: (key: unknown[]) => [props.queryKeyPrefix, ...key], | ||
networks, | ||
selectedNetwork, | ||
selectNetwork: (network) => { | ||
if (network !== selectedNetwork) { | ||
setSelectedNetwork(network); | ||
setClient(createClient(network, networks[network])); | ||
hayes-mysten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
}; | ||
}, [props.client, props.url, props.queryKeyPrefix]); | ||
}, [client, setClient, createClient, selectedNetwork, networks]); | ||
|
||
return <SuiClientContext.Provider value={ctx}>{props.children}</SuiClientContext.Provider>; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { screen } from '@testing-library/dom'; | ||
import { SuiClientProvider } from '../../src/components/SuiClientProvider.js'; | ||
import { useSuiClient, useSuiClientContext } from 'dapp-kit/src/index.js'; | ||
import { SuiClient } from '@mysten/sui.js/client'; | ||
import { useState } from 'react'; | ||
|
||
describe('SuiClientProvider', () => { | ||
it('renders without crashing', () => { | ||
render( | ||
<SuiClientProvider> | ||
<div>Test</div> | ||
</SuiClientProvider>, | ||
); | ||
expect(screen.getByText('Test')).toBeInTheDocument(); | ||
}); | ||
|
||
it('provides a SuiClient instance to its children', () => { | ||
const ChildComponent = () => { | ||
const client = useSuiClient(); | ||
expect(client).toBeInstanceOf(SuiClient); | ||
return <div>Test</div>; | ||
}; | ||
|
||
render( | ||
<SuiClientProvider> | ||
<ChildComponent /> | ||
</SuiClientProvider>, | ||
); | ||
}); | ||
|
||
it('can accept pre-configured SuiClients', () => { | ||
const suiClient = new SuiClient({ url: 'http://localhost:8080' }); | ||
const ChildComponent = () => { | ||
const client = useSuiClient(); | ||
expect(client).toBeInstanceOf(SuiClient); | ||
expect(client).toBe(suiClient); | ||
return <div>Test</div>; | ||
}; | ||
|
||
render( | ||
<SuiClientProvider networks={{ localnet: suiClient }}> | ||
<ChildComponent /> | ||
</SuiClientProvider>, | ||
); | ||
|
||
expect(screen.getByText('Test')).toBeInTheDocument(); | ||
}); | ||
|
||
test('can create sui clients with custom options', async () => { | ||
function NetworkSelector() { | ||
const ctx = useSuiClientContext(); | ||
|
||
return ( | ||
<div> | ||
{Object.keys(ctx.networks).map((network) => ( | ||
<button key={network} onClick={() => ctx.selectNetwork(network)}> | ||
{`select ${network}`} | ||
</button> | ||
))} | ||
</div> | ||
); | ||
} | ||
function CustomConfigProvider() { | ||
const [selectedNetwork, setSelectedNetwork] = useState<string>(); | ||
|
||
return ( | ||
<SuiClientProvider | ||
networks={{ | ||
a: { | ||
url: 'http://localhost:8080', | ||
custom: setSelectedNetwork, | ||
}, | ||
b: { | ||
url: 'http://localhost:8080', | ||
custom: setSelectedNetwork, | ||
}, | ||
}} | ||
createClient={(name, { custom, ...config }) => { | ||
custom(name); | ||
return new SuiClient(config); | ||
}} | ||
> | ||
<div>{`selected network: ${selectedNetwork}`}</div> | ||
<NetworkSelector /> | ||
</SuiClientProvider> | ||
); | ||
} | ||
|
||
const user = userEvent.setup(); | ||
|
||
render(<CustomConfigProvider />); | ||
|
||
expect(screen.getByText('selected network: a')).toBeInTheDocument(); | ||
|
||
await user.click(screen.getByText('select b')); | ||
|
||
expect(screen.getByText('selected network: b')).toBeInTheDocument(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import '@testing-library/jest-dom'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I said this in another comment but IMO this default either should be more comprehensive, or only be localnet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed this to localnet for now