-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
228 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ test('behavior: user rejected request', async () => { | |
[UserRejectedRequestError: User rejected the request. | ||
Details: Failed to connect. | ||
Version: [email protected].9] | ||
Version: [email protected].11] | ||
`) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,6 @@ test('behavior: bogus token', async () => { | |
function: decimals() | ||
Docs: https://viem.sh/docs/contract/multicall.html | ||
Version: [email protected].9" | ||
Version: [email protected].11" | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,7 +397,7 @@ test('throws if allowFailure=false & a contract method fails', async () => { | |
args: (0xA0Cf798816D4b9b9866b5330EEa46a18382f251e, 69420) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9" | ||
Version: [email protected].11" | ||
`, | ||
) | ||
}) | ||
|
@@ -450,7 +450,7 @@ test('allowFailure=true & a contract method fails', async () => { | |
args: (0xA0Cf798816D4b9b9866b5330EEa46a18382f251e, 69420) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9], | ||
Version: [email protected].11], | ||
"result": undefined, | ||
"status": "failure", | ||
}, | ||
|
@@ -464,7 +464,7 @@ test('allowFailure=true & a contract method fails', async () => { | |
args: (0xA0Cf798816D4b9b9866b5330EEa46a18382f251e, 69421) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9], | ||
Version: [email protected].11], | ||
"result": undefined, | ||
"status": "failure", | ||
}, | ||
|
@@ -502,7 +502,7 @@ test('throws if allowFailure=false & encoding contract function data fails', asy | |
args: (1e+31) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9" | ||
Version: [email protected].11" | ||
`, | ||
) | ||
}) | ||
|
@@ -561,7 +561,7 @@ test('allowFailure=true & encoding contract function data fails', async () => { | |
args: (1e+31) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9], | ||
Version: [email protected].11], | ||
"result": undefined, | ||
"status": "failure", | ||
}, | ||
|
@@ -579,7 +579,7 @@ test('allowFailure=true & encoding contract function data fails', async () => { | |
args: (1e+31) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9], | ||
Version: [email protected].11], | ||
"result": undefined, | ||
"status": "failure", | ||
}, | ||
|
@@ -617,7 +617,7 @@ test('should throw if allowFailure=false & a contract has no response', async () | |
args: (0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9" | ||
Version: [email protected].11" | ||
`, | ||
) | ||
}) | ||
|
@@ -669,7 +669,7 @@ test('allowFailure=true & a contract has no response', async () => { | |
args: (0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC) | ||
Docs: https://viem.sh/docs/contract/readContract.html | ||
Version: [email protected].9], | ||
Version: [email protected].11], | ||
"result": undefined, | ||
"status": "failure", | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ test('behavior: user rejected request', async () => { | |
[UserRejectedRequestError: User rejected the request. | ||
Details: Failed to sign message. | ||
Version: [email protected].9] | ||
Version: [email protected].11] | ||
`) | ||
await disconnect(config, { connector: connector_ }) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ test('behavior: user rejected request', async () => { | |
[UserRejectedRequestError: User rejected the request. | ||
Details: Failed to sign typed data. | ||
Version: [email protected].9] | ||
Version: [email protected].11] | ||
`) | ||
await disconnect(config, { connector: connector_ }) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ test('behavior: user rejected request', async () => { | |
[UserRejectedRequestError: User rejected the request. | ||
Details: Failed to switch chain. | ||
Version: [email protected].9] | ||
Version: [email protected].11] | ||
`) | ||
await disconnect(config, { connector: connector_ }) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { expect, test } from 'vitest' | ||
import { unstable_connector } from './connector.js' | ||
|
||
test('setup', () => { | ||
expect(unstable_connector({ type: 'injected' })({})).toMatchInlineSnapshot(` | ||
{ | ||
"config": { | ||
"key": "connector", | ||
"name": "Connector", | ||
"request": [Function], | ||
"retryCount": 3, | ||
"retryDelay": 150, | ||
"timeout": undefined, | ||
"type": "connector", | ||
}, | ||
"request": [Function], | ||
"value": undefined, | ||
} | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
ChainDisconnectedError, | ||
type EIP1193Provider, | ||
ProviderDisconnectedError, | ||
type TransportConfig, | ||
createTransport, | ||
hexToNumber, | ||
} from 'viem' | ||
import type { Transport } from '../createConfig.js' | ||
|
||
export type ConnectorTransportConfig = { | ||
/** The key of the transport. */ | ||
key?: TransportConfig['key'] | ||
/** The name of the transport. */ | ||
name?: TransportConfig['name'] | ||
/** The max number of times to retry. */ | ||
retryCount?: TransportConfig['retryCount'] | ||
/** The base delay (in ms) between retries. */ | ||
retryDelay?: TransportConfig['retryDelay'] | ||
/** Type of connector to target. */ | ||
type?: | ||
| 'injected' | ||
| 'coinbase' | ||
| 'ledger' | ||
| 'metaMask' | ||
| 'safe' | ||
| 'walletConnect' | ||
| (string & {}) | ||
} | ||
|
||
export type ConnectorTransport = Transport | ||
|
||
export function unstable_connector( | ||
config: ConnectorTransportConfig, | ||
): Transport { | ||
const { | ||
key = 'connector', | ||
name = 'Connector', | ||
retryCount, | ||
retryDelay, | ||
type, | ||
} = config || {} | ||
return ({ chain, connectors, retryCount: defaultRetryCount }) => { | ||
const connector = connectors?.find((c) => c.type === type) | ||
return createTransport({ | ||
key, | ||
name, | ||
async request({ method, params }) { | ||
if (!connector) | ||
throw new ProviderDisconnectedError( | ||
new Error( | ||
`Could not find connector of type "${type}" in \`connectors\` passed to \`createConfig\`.`, | ||
), | ||
) | ||
|
||
const provider = (await connector.getProvider({ | ||
chainId: chain?.id, | ||
})) as EIP1193Provider | undefined | ||
if (!provider) | ||
throw new ProviderDisconnectedError( | ||
new Error('Provider is disconnected.'), | ||
) | ||
|
||
const chainId = hexToNumber( | ||
await provider.request({ method: 'eth_chainId' }), | ||
) | ||
if (chain && chainId !== chain.id) | ||
throw new ChainDisconnectedError( | ||
new Error( | ||
`The current chain of the connector (id: ${chainId}) does not match the target chain for the request (id: ${chain.id} – ${chain.name}).`, | ||
), | ||
) | ||
|
||
return (await provider.request({ | ||
method, | ||
params, | ||
} as any)) as any | ||
}, | ||
retryDelay, | ||
retryCount: retryCount ?? defaultRetryCount, | ||
type: 'connector', | ||
}) | ||
} | ||
} |
Oops, something went wrong.