Skip to content

Commit c809026

Browse files
feature(#1030): Add ability to create StaticJsonRpcProvider with ConnectionInfo (#1031)
* feature: Add ability to create StaticJsonRpcProvider with ConnectionInfo * feature(#1030): update package version * Increment version Co-authored-by: Aaron Barnard <abarnard@protonmail.com>
1 parent 20ca7e9 commit c809026

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ node_modules
33
dist/
44
package-lock.json
55
.rpt2_cache
6-
.vscode
6+
.vscode

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/common",
3-
"version": "2.1.0",
3+
"version": "2.1.1-alpha.1",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/common/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ethers, BigNumber } from 'ethers'
2+
import type { ConnectionInfo } from 'ethers/lib/utils'
23
import type EventEmitter from 'eventemitter3'
34
import type { TypedData as EIP712TypedData } from 'eip-712'
45
export type { TypedData as EIP712TypedData } from 'eip-712'
@@ -428,6 +429,7 @@ export interface Chain {
428429
token: TokenSymbol // eg ETH, BNB, MATIC
429430
color?: string
430431
icon?: string // svg string
432+
providerConnectionInfo?: ConnectionInfo
431433
}
432434

433435
export type TokenSymbol = string // eg ETH

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.2.11",
3+
"version": "2.2.12-alpha.1",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",
@@ -41,7 +41,7 @@
4141
"typescript": "^4.5.5"
4242
},
4343
"dependencies": {
44-
"@web3-onboard/common": "^2.1.0",
44+
"@web3-onboard/common": "^2.1.1-alpha.1",
4545
"bowser": "^2.11.0",
4646
"ethers": "5.5.3",
4747
"eventemitter3": "^4.0.7",

packages/core/src/provider.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ export const ethersProviders: {
2525
[key: string]: providers.StaticJsonRpcProvider
2626
} = {}
2727

28+
export function getProvider(chain: Chain): providers.StaticJsonRpcProvider {
29+
if (!chain) return null
30+
31+
if (!ethersProviders[chain.rpcUrl]) {
32+
ethersProviders[chain.rpcUrl] = new providers.StaticJsonRpcProvider(
33+
chain.providerConnectionInfo?.url
34+
? chain.providerConnectionInfo
35+
: chain.rpcUrl
36+
)
37+
}
38+
39+
return ethersProviders[chain.rpcUrl]
40+
}
41+
2842
export function requestAccounts(
2943
provider: EIP1193Provider
3044
): Promise<ProviderAccounts> {
@@ -231,13 +245,7 @@ export async function getEns(
231245
// chain we don't recognize and don't have a rpcUrl for requests
232246
if (!chain) return null
233247

234-
if (!ethersProviders[chain.rpcUrl]) {
235-
ethersProviders[chain.rpcUrl] = new providers.StaticJsonRpcProvider(
236-
chain.rpcUrl
237-
)
238-
}
239-
240-
const provider = ethersProviders[chain.rpcUrl]
248+
const provider = getProvider(chain);
241249

242250
try {
243251
const name = await provider.lookupAddress(address)
@@ -277,13 +285,7 @@ export async function getBalance(
277285
// chain we don't recognize and don't have a rpcUrl for requests
278286
if (!chain) return null
279287

280-
if (!ethersProviders[chain.rpcUrl]) {
281-
ethersProviders[chain.rpcUrl] = new providers.StaticJsonRpcProvider(
282-
chain.rpcUrl
283-
)
284-
}
285-
286-
const provider = ethersProviders[chain.rpcUrl]
288+
const provider = getProvider(chain);
287289

288290
try {
289291
const balanceWei = await provider.getBalance(address)

packages/core/src/validation.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,30 @@ const chainId = Joi.string().pattern(/^0x[0-9a-fA-F]+$/)
1414
const chainNamespace = Joi.string().valid('evm')
1515
const unknownObject = Joi.object().unknown()
1616

17+
// const address = Joi.string().regex(/^0x[a-fA-F0-9]{40}$/)
18+
/** Related to ConnectionInfo from 'ethers/lib/utils' */
19+
const providerConnectionInfo = Joi.object({
20+
url: Joi.string().required(),
21+
headers: Joi.object(),
22+
user: Joi.string(),
23+
password: Joi.string(),
24+
allowInsecureAuthentication: Joi.boolean(),
25+
allowGzip: Joi.boolean(),
26+
throttleLimit: Joi.number(),
27+
throttleSlotInterval: Joi.number(),
28+
throttleCallback: Joi.function(),
29+
timeout: Joi.number()
30+
})
31+
1732
const chain = Joi.object({
1833
namespace: chainNamespace,
1934
id: chainId.required(),
2035
rpcUrl: Joi.string().required(),
2136
label: Joi.string().required(),
2237
token: Joi.string().required(),
2338
icon: Joi.string(),
24-
color: Joi.string()
39+
color: Joi.string(),
40+
providerConnectionInfo: providerConnectionInfo
2541
})
2642

2743
const connectedChain = Joi.object({
@@ -214,4 +230,4 @@ export function validateLocale(data: string): ValidateReturn {
214230
export function validateUpdateBalances(data:
215231
WalletState[]): ValidateReturn {
216232
return validate(wallets, data)
217-
}
233+
}

0 commit comments

Comments
 (0)