Skip to content

Commit

Permalink
update to newer package version
Browse files Browse the repository at this point in the history
  • Loading branch information
JFrankfurt committed Aug 1, 2023
1 parent 742b05d commit 018df9c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 49 deletions.
8 changes: 3 additions & 5 deletions example/components/ConnectWithSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ function ChainSelect({
}}
disabled={switchChain === undefined}
>
<option hidden disabled selected={activeChainId === undefined}>
<option hidden disabled>
Select chain
</option>
<option value={-1} selected={activeChainId === -1}>
Default
</option>
<option value={-1}>Default</option>
{chainIds.map((chainId) => (
<option key={chainId} value={chainId} selected={chainId === activeChainId}>
<option key={chainId} value={chainId}>
{CHAINS[chainId]?.name ?? chainId}
</option>
))}
Expand Down
2 changes: 1 addition & 1 deletion packages/walletconnect-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"start": "tsc --watch"
},
"dependencies": {
"@walletconnect/ethereum-provider": "^2.9.1",
"@walletconnect/ethereum-provider": "^2.9.2",
"@walletconnect/modal": "^2.6.1",
"@web3-react/types": "^8.2.0",
"eventemitter3": "^4.0.7"
Expand Down
32 changes: 27 additions & 5 deletions packages/walletconnect-v2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type WalletConnectOptions = Omit<Parameters<typeof WalletConnectProvider.
rpc?: { [chainId: number]: string | string[] }
}

export type ArrayOneOrMore<T> = {
0: T
} & Array<T>

/**
* Options to configure the WalletConnect connector.
*/
Expand All @@ -48,10 +52,9 @@ export class WalletConnect extends Connector {
public provider?: WalletConnectProvider
public readonly events = new EventEmitter3()

private readonly options: Omit<WalletConnectOptions, 'rpcMap' | 'chains'>
private readonly options: Omit<WalletConnectOptions, 'rpcMap'>

private readonly rpcMap?: Record<number, string | string[]>
private readonly chains: number[]
private readonly defaultChainId?: number
private readonly timeout: number

Expand All @@ -60,10 +63,9 @@ export class WalletConnect extends Connector {
constructor({ actions, options, defaultChainId, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) {
super(actions, onError)

const { rpcMap, rpc, chains, ...rest } = options
const { rpcMap, rpc, ...rest } = options

this.options = rest
this.chains = chains
this.defaultChainId = defaultChainId
this.rpcMap = rpcMap || rpc
this.timeout = timeout
Expand Down Expand Up @@ -91,12 +93,32 @@ export class WalletConnect extends Connector {
): Promise<WalletConnectProvider> {
if (this.eagerConnection) return this.eagerConnection

function isArrayOneOrMore<T>(input: T[]): input is ArrayOneOrMore<T> {
return input.length > 0
}

const rpcMap = this.rpcMap ? getBestUrlMap(this.rpcMap, this.timeout) : undefined
const chains = desiredChainId ? getChainsWithDefault(this.chains, desiredChainId) : this.chains
const chains = desiredChainId ? getChainsWithDefault(this.options.chains, desiredChainId) : this.options.chains

let optionalChains: ArrayOneOrMore<number> = [this.defaultChainId || 1]

if (
this.options.optionalChains &&
this.options.optionalChains.length > 0 &&
isArrayOneOrMore(this.options.optionalChains)
) {
optionalChains = this.options.optionalChains
}

console.log('this.options.chains', this.options.chains)
console.log('chains', chains)
console.log('this.options.optionalChains', this.options.optionalChains)
console.log('optionalChains', optionalChains)

return (this.eagerConnection = import('@walletconnect/ethereum-provider').then(async (ethProviderModule) => {
const provider = (this.provider = await ethProviderModule.default.init({
...this.options,
optionalChains,
chains,
rpcMap: await rpcMap,
}))
Expand Down
7 changes: 6 additions & 1 deletion packages/walletconnect-v2/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ArrayOneOrMore } from '.'

/**
* @param rpcMap - Map of chainIds to rpc url(s).
* @param timeout - Timeout, in milliseconds, after which to consider network calls failed.
Expand Down Expand Up @@ -85,7 +87,10 @@ async function getBestUrl(urls: string | string[], timeout: number): Promise<str
* @param chains - An array of chain IDs.
* @param defaultChainId - The chain ID to treat as the default (it will be the first element in the returned array).
*/
export function getChainsWithDefault(chains: number[], defaultChainId: number) {
export function getChainsWithDefault(
chains: number[] | ArrayOneOrMore<number> = [],
defaultChainId: number
): ArrayOneOrMore<number> {
const idx = chains.indexOf(defaultChainId)
if (idx === -1) {
throw new Error(
Expand Down
74 changes: 37 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2730,10 +2730,10 @@
"@walletconnect/types" "^1.8.0"
"@walletconnect/utils" "^1.8.0"

"@walletconnect/[email protected].1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.9.1.tgz#1a333933750f5f933d9b7788a8dae44ce1173063"
integrity sha512-xyWeP0eLhEEDQAVJSmqs4n/AClKUM+8os2ZFe7BTuw1tFYjeLNVDtKCHziVOSTh8wEChMsKSGKA4zerQoH8mAQ==
"@walletconnect/[email protected].2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.9.2.tgz#c46734ca63771b28fd77606fd521930b7ecfc5e1"
integrity sha512-VARMPAx8sIgodeyngDHbealP3B621PQqjqKsByFUTOep8ZI1/R/20zU+cmq6j9RCrL+kLKZcrZqeVzs8Z7OlqQ==
dependencies:
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
Expand All @@ -2746,8 +2746,8 @@
"@walletconnect/relay-auth" "^1.0.4"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.9.1"
"@walletconnect/utils" "2.9.1"
"@walletconnect/types" "2.9.2"
"@walletconnect/utils" "2.9.2"
events "^3.3.0"
lodash.isequal "4.5.0"
uint8arrays "^3.1.0"
Expand Down Expand Up @@ -2803,19 +2803,19 @@
eip1193-provider "1.0.1"
eventemitter3 "4.0.7"

"@walletconnect/ethereum-provider@^2.9.1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/ethereum-provider/-/ethereum-provider-2.9.1.tgz#6f41acb38259a59f383d490396aa9c0933839d0b"
integrity sha512-JiMatBFVgzJSQrckpbOoOsmQmKnbTn9wzmU10MBPe9W6ZV2mf2JuxW0luWKLpQkuCmM9mL6+nCKX7nfW9V6qrQ==
"@walletconnect/ethereum-provider@^2.9.2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/ethereum-provider/-/ethereum-provider-2.9.2.tgz#fb3a6fca279bb4e98e75baa2fb9730545d41bb99"
integrity sha512-eO1dkhZffV1g7vpG19XUJTw09M/bwGUwwhy1mJ3AOPbOSbMPvwiCuRz2Kbtm1g9B0Jv15Dl+TvJ9vTgYF8zoZg==
dependencies:
"@walletconnect/jsonrpc-http-connection" "^1.0.7"
"@walletconnect/jsonrpc-provider" "^1.0.13"
"@walletconnect/jsonrpc-types" "^1.0.3"
"@walletconnect/jsonrpc-utils" "^1.0.8"
"@walletconnect/sign-client" "2.9.1"
"@walletconnect/types" "2.9.1"
"@walletconnect/universal-provider" "2.9.1"
"@walletconnect/utils" "2.9.1"
"@walletconnect/sign-client" "2.9.2"
"@walletconnect/types" "2.9.2"
"@walletconnect/universal-provider" "2.9.2"
"@walletconnect/utils" "2.9.2"
events "^3.3.0"

"@walletconnect/events@^1.0.1":
Expand Down Expand Up @@ -2991,19 +2991,19 @@
dependencies:
tslib "1.14.1"

"@walletconnect/[email protected].1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.9.1.tgz#e4aa9c7b15849f450fdd1b03754a7517cb5c8811"
integrity sha512-Z7tFRrJ9btA1vU427vsjUS6cPlHQVcTWdKH90khEc2lv3dB6mU8FNO0VJsw+I2D7CW7WaMWF3nnj6Z1FfotbDg==
"@walletconnect/[email protected].2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.9.2.tgz#ff4c81c082c2078878367d07f24bcb20b1f7ab9e"
integrity sha512-anRwnXKlR08lYllFMEarS01hp1gr6Q9XUgvacr749hoaC/AwGVlxYFdM8+MyYr3ozlA+2i599kjbK/mAebqdXg==
dependencies:
"@walletconnect/core" "2.9.1"
"@walletconnect/core" "2.9.2"
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.9.1"
"@walletconnect/utils" "2.9.1"
"@walletconnect/types" "2.9.2"
"@walletconnect/utils" "2.9.2"
events "^3.3.0"

"@walletconnect/signer-connection@^1.8.0":
Expand Down Expand Up @@ -3034,10 +3034,10 @@
dependencies:
tslib "1.14.1"

"@walletconnect/[email protected].1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.9.1.tgz#cb32ff396cc8880a7395f28716d1e82f407e1372"
integrity sha512-xbGgTPuD6xsb7YMvCESBIH55cjB86QAnnVL50a/ED42YkQzDsOdJ0VGTbrm0tG5cxUOF933rpxZQjxGdP+ovww==
"@walletconnect/[email protected].2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.9.2.tgz#d5fd5a61dc0f41cbdca59d1885b85207ac7bf8c5"
integrity sha512-7Rdn30amnJEEal4hk83cdwHUuxI1SWQ+K7fFFHBMqkuHLGi3tpMY6kpyfDxnUScYEZXqgRps4Jo5qQgnRqVM7A==
dependencies:
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
Expand All @@ -3051,25 +3051,25 @@
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195"
integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg==

"@walletconnect/[email protected].1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.9.1.tgz#b9034dfa748f11b692d45b584f202cfcfdd289ea"
integrity sha512-Ychf+/J0Ql3UvaiPVEGtdpYXXDa87e6hP+NUEl/+nF41x3dlH0P1zoIgX5sWbpGP8HRaKd8Qsm0N6S7RalC+LQ==
"@walletconnect/[email protected].2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.9.2.tgz#40e54e98bc48b1f2f5f77eb5b7f05462093a8506"
integrity sha512-JmaolkO8D31UdRaQCHwlr8uIFUI5BYhBzqYFt54Mc6gbIa1tijGOmdyr6YhhFO70LPmS6gHIjljwOuEllmlrxw==
dependencies:
"@walletconnect/jsonrpc-http-connection" "^1.0.7"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "^1.0.2"
"@walletconnect/jsonrpc-utils" "^1.0.7"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/sign-client" "2.9.1"
"@walletconnect/types" "2.9.1"
"@walletconnect/utils" "2.9.1"
"@walletconnect/sign-client" "2.9.2"
"@walletconnect/types" "2.9.2"
"@walletconnect/utils" "2.9.2"
events "^3.3.0"

"@walletconnect/[email protected].1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.9.1.tgz#92abc24b3af3ead42a3864e019dbf2f651ab2e47"
integrity sha512-tXeQVebF5oPBvhdmuUyVSkSIBYx/egIi4czav1QrnUpwrUS1LsrFhyWBxSbhN7TXY287ULWkEf6aFpWOHdp5EA==
"@walletconnect/[email protected].2":
version "2.9.2"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.9.2.tgz#035bdb859ee81a4bcc6420f56114cc5ec3e30afb"
integrity sha512-D44hwXET/8JhhIjqljY6qxSu7xXnlPrf63UN/Qfl98vDjWlYVcDl2+JIQRxD9GPastw0S8XZXdRq59XDXLuZBg==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
Expand All @@ -3079,7 +3079,7 @@
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.9.1"
"@walletconnect/types" "2.9.2"
"@walletconnect/window-getters" "^1.0.1"
"@walletconnect/window-metadata" "^1.0.1"
detect-browser "5.3.0"
Expand Down

0 comments on commit 018df9c

Please sign in to comment.