Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 5697198

Browse files
committed
add useMetamask and default readonly rpc
1 parent 88ed397 commit 5697198

File tree

7 files changed

+93
-37
lines changed

7 files changed

+93
-37
lines changed

src/Provider.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,27 @@ export const ThirdwebProvider = <
232232
} as WagmiproviderProps;
233233
}, [walletConnectors, _supporrtedChains, dAppMeta]);
234234

235+
const defaultSdkReadUrl =
236+
_rpcUrlMap[(desiredChainId || -1) as keyof typeof _rpcUrlMap];
237+
238+
const sdkOptionsWithDefaults = useMemo(() => {
239+
const opts: SDKOptions = sdkOptions;
240+
return {
241+
...opts,
242+
readonlySettings: {
243+
...(opts?.readonlySettings || {}),
244+
rpcUrl: opts?.readonlySettings?.rpcUrl
245+
? opts.readonlySettings.rpcUrl
246+
: defaultSdkReadUrl,
247+
},
248+
};
249+
}, [sdkOptions, defaultSdkReadUrl]);
250+
235251
return (
236252
<WagmiProvider {...wagmiProps}>
237253
<ThirdwebSDKProvider
238254
desiredChainId={desiredChainId}
239-
sdkOptions={sdkOptions}
255+
sdkOptions={sdkOptionsWithDefaults}
240256
storageInterface={storageInterface}
241257
>
242258
{children}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import invariant from "tiny-invariant";
2+
import { useConnect } from "../useConnect";
3+
4+
export function useMetamask() {
5+
const [connectors, connect] = useConnect();
6+
if (connectors.loading) {
7+
return () => Promise.reject("Metamask connector not ready to be used, yet");
8+
}
9+
const injectedConnector = connectors.data.connectors.find(
10+
(c) => c.id === "injected",
11+
);
12+
invariant(
13+
injectedConnector,
14+
"Metamask connector not found, please make sure it is provided to your <ThirdwebProvider />",
15+
);
16+
17+
return () => connect(injectedConnector);
18+
}

src/hooks/modules/useContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useSDK } from "../../Provider";
1010
export function useContract<TContractType extends ContractType>(
1111
contractType: TContractType,
1212
contractAddress?: string,
13-
): ContractForContractType<TContractType> {
13+
): ContractForContractType<TContractType> | undefined {
1414
const sdk = useSDK();
1515
if (!sdk || !contractAddress) {
1616
return undefined;

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from "./hooks/useConnect";
55
export * from "./hooks/useSigner";
66
export * from "./hooks/useAddress";
77
export * from "./hooks/useReadonlySDK";
8+
export * from "./hooks/connectors/useMetamask";
89

910
// re-exports
1011
export { defaultChains, defaultL2Chains, useAccount, useNetwork } from "wagmi";

wiki/DAppMetaData.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#### Defined in
2020

21-
[src/Provider.tsx:48](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L48)
21+
[src/Provider.tsx:48](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L48)
2222

2323
___
2424

@@ -28,7 +28,7 @@ ___
2828

2929
#### Defined in
3030

31-
[src/Provider.tsx:51](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L51)
31+
[src/Provider.tsx:51](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L51)
3232

3333
___
3434

@@ -38,7 +38,7 @@ ___
3838

3939
#### Defined in
4040

41-
[src/Provider.tsx:49](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L49)
41+
[src/Provider.tsx:49](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L49)
4242

4343
___
4444

@@ -48,7 +48,7 @@ ___
4848

4949
#### Defined in
5050

51-
[src/Provider.tsx:47](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L47)
51+
[src/Provider.tsx:47](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L47)
5252

5353
___
5454

@@ -58,4 +58,4 @@ ___
5858

5959
#### Defined in
6060

61-
[src/Provider.tsx:50](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L50)
61+
[src/Provider.tsx:50](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L50)

wiki/Exports.md

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [useEdition](../wiki/Exports#useedition)
3131
- [useEditionDrop](../wiki/Exports#useeditiondrop)
3232
- [useMarketplace](../wiki/Exports#usemarketplace)
33+
- [useMetamask](../wiki/Exports#usemetamask)
3334
- [useNFTCollection](../wiki/Exports#usenftcollection)
3435
- [useNFTDrop](../wiki/Exports#usenftdrop)
3536
- [useNetwork](../wiki/Exports#usenetwork)
@@ -54,7 +55,7 @@
5455

5556
#### Defined in
5657

57-
[src/Provider.tsx:41](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L41)
58+
[src/Provider.tsx:41](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L41)
5859

5960
___
6061

@@ -64,7 +65,7 @@ ___
6465

6566
#### Defined in
6667

67-
[src/Provider.tsx:19](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L19)
68+
[src/Provider.tsx:19](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L19)
6869

6970
___
7071

@@ -74,7 +75,7 @@ ___
7475

7576
#### Defined in
7677

77-
[src/Provider.tsx:24](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L24)
78+
[src/Provider.tsx:24](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L24)
7879

7980
___
8081

@@ -84,7 +85,7 @@ ___
8485

8586
#### Defined in
8687

87-
[src/Provider.tsx:36](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L36)
88+
[src/Provider.tsx:36](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L36)
8889

8990
___
9091

@@ -94,7 +95,7 @@ ___
9495

9596
#### Defined in
9697

97-
[src/Provider.tsx:28](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L28)
98+
[src/Provider.tsx:28](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L28)
9899

99100
## Variables
100101

@@ -140,7 +141,7 @@ node_modules/wagmi-core/dist/declarations/src/constants/chains.d.ts:10
140141

141142
#### Defined in
142143

143-
[src/Provider.tsx:114](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L114)
144+
[src/Provider.tsx:114](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L114)
144145

145146
___
146147

@@ -174,7 +175,7 @@ ___
174175

175176
#### Defined in
176177

177-
[src/hooks/useAddress.ts:3](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/useAddress.ts#L3)
178+
[src/hooks/useAddress.ts:3](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/useAddress.ts#L3)
178179

179180
___
180181

@@ -190,7 +191,7 @@ readonly [{ `data`: { `connected`: `boolean` ; `connector`: `undefined` \| `Conn
190191

191192
#### Defined in
192193

193-
[src/hooks/useConnect.ts:6](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/useConnect.ts#L6)
194+
[src/hooks/useConnect.ts:6](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/useConnect.ts#L6)
194195

195196
___
196197

@@ -210,7 +211,7 @@ ___
210211

211212
#### Defined in
212213

213-
[src/hooks/useDisconnect.ts:3](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/useDisconnect.ts#L3)
214+
[src/hooks/useDisconnect.ts:3](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/useDisconnect.ts#L3)
214215

215216
___
216217

@@ -230,7 +231,7 @@ ___
230231

231232
#### Defined in
232233

233-
[src/hooks/modules/useEdition.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useEdition.ts#L4)
234+
[src/hooks/modules/useEdition.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useEdition.ts#L4)
234235

235236
___
236237

@@ -250,7 +251,7 @@ ___
250251

251252
#### Defined in
252253

253-
[src/hooks/modules/useEditionDrop.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useEditionDrop.ts#L4)
254+
[src/hooks/modules/useEditionDrop.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useEditionDrop.ts#L4)
254255

255256
___
256257

@@ -270,7 +271,27 @@ ___
270271

271272
#### Defined in
272273

273-
[src/hooks/modules/useMarketplace.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useMarketplace.ts#L4)
274+
[src/hooks/modules/useMarketplace.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useMarketplace.ts#L4)
275+
276+
___
277+
278+
### useMetamask
279+
280+
**useMetamask**(): () => `Promise`<{ `data?`: `ConnectorData`<`any`\> ; `error?`: `Error` }\>
281+
282+
#### Returns
283+
284+
`fn`
285+
286+
▸ (): `Promise`<{ `data?`: `ConnectorData`<`any`\> ; `error?`: `Error` }\>
287+
288+
##### Returns
289+
290+
`Promise`<{ `data?`: `ConnectorData`<`any`\> ; `error?`: `Error` }\>
291+
292+
#### Defined in
293+
294+
src/hooks/connectors/useMetamask.ts:4
274295

275296
___
276297

@@ -290,7 +311,7 @@ ___
290311

291312
#### Defined in
292313

293-
[src/hooks/modules/useNFTCollection.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useNFTCollection.ts#L4)
314+
[src/hooks/modules/useNFTCollection.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useNFTCollection.ts#L4)
294315

295316
___
296317

@@ -310,7 +331,7 @@ ___
310331

311332
#### Defined in
312333

313-
[src/hooks/modules/useNFTDrop.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useNFTDrop.ts#L4)
334+
[src/hooks/modules/useNFTDrop.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useNFTDrop.ts#L4)
314335

315336
___
316337

@@ -344,7 +365,7 @@ ___
344365

345366
#### Defined in
346367

347-
[src/hooks/modules/usePack.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/usePack.ts#L4)
368+
[src/hooks/modules/usePack.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/usePack.ts#L4)
348369

349370
___
350371

@@ -357,7 +378,7 @@ ___
357378
| Name | Type |
358379
| :------ | :------ |
359380
| `readonlyRpcUrl` | `string` |
360-
| `sdkOptions` | `undefined` \| { `gasSettings?`: { `maxPriceInGwei?`: `number` ; `speed?`: ``"safeLow"`` \| ``"standard"`` \| ``"fast"`` \| ``"fastest"`` } ; `gasless?`: { `openzeppelin`: { `relayerForwarderAddress?`: `string` ; `relayerUrl`: `string` } } \| { `biconomy`: { `apiId`: `string` ; `apiKey`: `string` ; `deadlineSeconds?`: `number` } } ; `readonlySettings?`: { `chainId?`: `number` ; `rpcUrl`: `string` } } |
381+
| `sdkOptions` | `undefined` \| { `gasSettings?`: { `maxPriceInGwei?`: `number` ; `speed?`: ``"standard"`` \| ``"fast"`` \| ``"fastest"`` } ; `gasless?`: { `openzeppelin`: { `relayerForwarderAddress?`: `string` ; `relayerUrl`: `string` } } \| { `biconomy`: { `apiId`: `string` ; `apiKey`: `string` ; `deadlineSeconds?`: `number` } } ; `readonlySettings?`: { `chainId?`: `number` ; `rpcUrl`: `string` } } |
361382
| `storageInterface?` | `IStorage` |
362383

363384
#### Returns
@@ -366,7 +387,7 @@ ___
366387

367388
#### Defined in
368389

369-
[src/hooks/useReadonlySDK.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/useReadonlySDK.ts#L4)
390+
[src/hooks/useReadonlySDK.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/useReadonlySDK.ts#L4)
370391

371392
___
372393

@@ -380,7 +401,7 @@ ___
380401

381402
#### Defined in
382403

383-
[src/Provider.tsx:294](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L294)
404+
[src/Provider.tsx:310](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L310)
384405

385406
___
386407

@@ -394,7 +415,7 @@ ___
394415

395416
#### Defined in
396417

397-
[src/hooks/useSigner.ts:5](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/useSigner.ts#L5)
418+
[src/hooks/useSigner.ts:5](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/useSigner.ts#L5)
398419

399420
___
400421

@@ -414,7 +435,7 @@ ___
414435

415436
#### Defined in
416437

417-
[src/hooks/modules/useTokenModule.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useTokenModule.ts#L4)
438+
[src/hooks/modules/useTokenModule.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useTokenModule.ts#L4)
418439

419440
___
420441

@@ -434,4 +455,4 @@ ___
434455

435456
#### Defined in
436457

437-
[src/hooks/modules/useVoteModule.ts:4](https://github.com/thirdweb-dev/react/blob/945f587/src/hooks/modules/useVoteModule.ts#L4)
458+
[src/hooks/modules/useVoteModule.ts:4](https://github.com/thirdweb-dev/react/blob/88ed397/src/hooks/modules/useVoteModule.ts#L4)

wiki/ThirdwebProviderProps.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If not provided, will default to the rpcUrls of the chain objects for the suppor
2929

3030
#### Defined in
3131

32-
[src/Provider.tsx:76](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L76)
32+
[src/Provider.tsx:76](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L76)
3333

3434
___
3535

@@ -42,7 +42,7 @@ Defaults to just the name being passed as `thirdweb powered dApp`.
4242

4343
#### Defined in
4444

45-
[src/Provider.tsx:81](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L81)
45+
[src/Provider.tsx:81](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L81)
4646

4747
___
4848

@@ -56,7 +56,7 @@ When passing a chainId, it **must** be part of the `supportedChains` array.
5656

5757
#### Defined in
5858

59-
[src/Provider.tsx:87](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L87)
59+
[src/Provider.tsx:87](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L87)
6060

6161
___
6262

@@ -71,17 +71,17 @@ comes with sensible defaults
7171

7272
| Name | Type |
7373
| :------ | :------ |
74-
| `gasSettings?` | { `maxPriceInGwei?`: `number` ; `speed?`: ``"safeLow"`` \| ``"standard"`` \| ``"fast"`` \| ``"fastest"`` } |
74+
| `gasSettings?` | { `maxPriceInGwei?`: `number` ; `speed?`: ``"standard"`` \| ``"fast"`` \| ``"fastest"`` } |
7575
| `gasSettings.maxPriceInGwei?` | `number` |
76-
| `gasSettings.speed?` | ``"safeLow"`` \| ``"standard"`` \| ``"fast"`` \| ``"fastest"`` |
76+
| `gasSettings.speed?` | ``"standard"`` \| ``"fast"`` \| ``"fastest"`` |
7777
| `gasless?` | { `openzeppelin`: { `relayerForwarderAddress?`: `string` ; `relayerUrl`: `string` } } \| { `biconomy`: { `apiId`: `string` ; `apiKey`: `string` ; `deadlineSeconds?`: `number` } } |
7878
| `readonlySettings?` | { `chainId?`: `number` ; `rpcUrl`: `string` } |
7979
| `readonlySettings.chainId?` | `number` |
8080
| `readonlySettings.rpcUrl` | `string` |
8181

8282
#### Defined in
8383

84-
[src/Provider.tsx:61](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L61)
84+
[src/Provider.tsx:61](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L61)
8585

8686
___
8787

@@ -93,7 +93,7 @@ The storage interface to use with the sdk.
9393

9494
#### Defined in
9595

96-
[src/Provider.tsx:94](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L94)
96+
[src/Provider.tsx:94](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L94)
9797

9898
___
9999

@@ -106,7 +106,7 @@ If not provided, all chains supported by the SDK will be supported by default
106106

107107
#### Defined in
108108

109-
[src/Provider.tsx:66](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L66)
109+
[src/Provider.tsx:66](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L66)
110110

111111
___
112112

@@ -119,4 +119,4 @@ If not provided, will default to metamask (injected), wallet connect and walletl
119119

120120
#### Defined in
121121

122-
[src/Provider.tsx:71](https://github.com/thirdweb-dev/react/blob/945f587/src/Provider.tsx#L71)
122+
[src/Provider.tsx:71](https://github.com/thirdweb-dev/react/blob/88ed397/src/Provider.tsx#L71)

0 commit comments

Comments
 (0)