Skip to content

Commit

Permalink
feat: extract eip1193 retrieval logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Jan 8, 2025
1 parent 25d461a commit 1a93b86
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
49 changes: 49 additions & 0 deletions src/execute/options.ts
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
}
}
55 changes: 7 additions & 48 deletions src/execute/safeTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
import {
Address,
createPublicClient,
encodeFunctionData,
getAddress,
http,
parseAbi,
zeroAddress,
} from 'viem'
import { OperationType } from '@safe-global/types-kit'
import { Eip1193Provider } from '@safe-global/protocol-kit'

import { formatPrefixedAddress } from '../addresses'

import { chains, defaultRpc } from '../chains'
import { getEip1193Provider, Options } from './options'

import {
ChainId,
MetaTransactionRequest,
PrefixedAddress,
SafeTransactionRequest,
} from '../types'
import { SafeTransactionProperties } from './types'

interface Options {
providers?: {
[chainId in ChainId]?: string | Eip1193Provider
}
safeTransactionProperties?: {
[safe: PrefixedAddress]: SafeTransactionProperties
}
}

export async function prepareSafeTransaction({
chainId,
Expand All @@ -43,8 +29,13 @@ export async function prepareSafeTransaction({
options?: Options
}): Promise<SafeTransactionRequest> {
const provider = getEip1193Provider({ chainId, options })

const key1 = formatPrefixedAddress(chainId, safe)
const key2 = key1.toLowerCase() as PrefixedAddress

const defaults =
options?.safeTransactionProperties?.[formatPrefixedAddress(chainId, safe)]
options?.safeTransactionProperties?.[key1] ||
options?.safeTransactionProperties?.[key2]

const avatarAbi = parseAbi(['function nonce() view returns (uint256)'])

Expand Down Expand Up @@ -80,35 +71,3 @@ export async function prepareSafeTransaction({
nonce: Number(defaults?.nonce || nonce),
}
}

function getEip1193Provider({
chainId,
options,
}: {
chainId: ChainId
options?: Options
}): Eip1193Provider {
const chain = chainId && chains.find((chain) => chain.chainId === chainId)
if (!chain) {
throw new Error(`Unsupported chain ID: ${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
}
}

0 comments on commit 1a93b86

Please sign in to comment.