-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract eip1193 retrieval logic
- Loading branch information
1 parent
25d461a
commit 1a93b86
Showing
2 changed files
with
56 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createPublicClient, http } from 'viem' | ||
|
||
import { Eip1193Provider } from '@safe-global/protocol-kit' | ||
|
||
import { chains, defaultRpc } from '../chains' | ||
|
||
import { ChainId, PrefixedAddress } from '../types' | ||
import { SafeTransactionProperties } from './types' | ||
|
||
export interface Options { | ||
providers?: { | ||
[chainId in ChainId]?: string | Eip1193Provider | ||
} | ||
safeTransactionProperties?: { | ||
[safe: PrefixedAddress]: SafeTransactionProperties | ||
} | ||
} | ||
|
||
export function getEip1193Provider({ | ||
chainId, | ||
options, | ||
}: { | ||
chainId: ChainId | ||
options?: Options | ||
}): Eip1193Provider { | ||
const chain = chainId && chains.find((chain) => chain.chainId === chainId) | ||
if (!chain) { | ||
throw new Error(`Unsupported chainId: ${chainId}`) | ||
} | ||
|
||
const passedIn = Boolean( | ||
options && options.providers && options.providers[chainId] | ||
) | ||
|
||
let urlOrProvider | ||
if (passedIn) { | ||
urlOrProvider = options!.providers![chainId]! | ||
} else { | ||
urlOrProvider = defaultRpc[chainId]! | ||
} | ||
|
||
if (typeof urlOrProvider == 'string') { | ||
return createPublicClient({ | ||
transport: http(urlOrProvider), | ||
}) as Eip1193Provider | ||
} else { | ||
return urlOrProvider | ||
} | ||
} |
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