Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error when not supplying a provider #13

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 4 additions & 65 deletions src/execute/plan.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import assert from 'assert'
import {
Address,
decodeFunctionData,
encodeFunctionData,
getAddress,
hashTypedData,
parseAbi,
zeroAddress,
} from 'viem'
import { OperationType } from '@safe-global/types-kit'
import { decodeFunctionData, hashTypedData, parseAbi, zeroAddress } from 'viem'
import { Eip1193Provider } from '@safe-global/protocol-kit'

import { encodeMultiSend } from './multisend'
import { createPreApprovedSignature } from './signatures'
import { encodeMultiSend } from './multisend'
import { prepareSafeTransaction } from './safeTransaction'

import { formatPrefixedAddress, splitPrefixedAddress } from '../addresses'
import { splitPrefixedAddress } from '../addresses'
import { typedDataForSafeTransaction } from '../eip712'

import encodeApproveHash from '../encode/approveHash'
Expand Down Expand Up @@ -374,59 +366,6 @@ function shouldPropose(waypoint: Waypoint | StartingPoint, options?: Options) {
return proposeOnly || !canExecute
}

async function prepareSafeTransaction({
chainId,
safe,
transaction,
options,
}: {
chainId: ChainId
safe: Address
transaction: MetaTransactionRequest
options: Options
}): Promise<SafeTransactionRequest> {
if (!options.providers || !options.providers[chainId]) {
throw new Error('Provider is required')
}

const provider = options.providers[chainId] as Eip1193Provider
const defaults =
options?.safeTransactionProperties?.[formatPrefixedAddress(chainId, safe)]

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

const nonce = BigInt(
(await provider.request({
method: 'eth_call',
params: [
{
to: safe,
data: encodeFunctionData({
abi: avatarAbi,
functionName: 'nonce',
args: [],
}),
},
],
})) as string
)

return {
to: transaction.to,
value: transaction.value,
data: transaction.data,
operation: transaction.operation ?? OperationType.Call,
safeTxGas: BigInt(defaults?.safeTxGas || 0),
baseGas: BigInt(defaults?.baseGas || 0),
gasPrice: BigInt(defaults?.gasPrice || 0),
gasToken: getAddress(defaults?.gasToken || zeroAddress) as `0x${string}`,
refundReceiver: getAddress(
defaults?.refundReceiver || zeroAddress
) as `0x${string}`,
nonce: Number(defaults?.nonce || nonce),
}
}

function unwrapExecuteTransaction(
action: ExecuteTransactionAction
): MetaTransactionRequest {
Expand Down
114 changes: 114 additions & 0 deletions src/execute/safeTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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 {
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,
safe,
transaction,
options,
}: {
chainId: ChainId
safe: Address
transaction: MetaTransactionRequest
options?: Options
}): Promise<SafeTransactionRequest> {
const provider = getEip1193Provider({ chainId, options })
const defaults =
options?.safeTransactionProperties?.[formatPrefixedAddress(chainId, safe)]

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

const nonce = BigInt(
(await provider.request({
method: 'eth_call',
params: [
{
to: safe,
data: encodeFunctionData({
abi: avatarAbi,
functionName: 'nonce',
args: [],
}),
},
'latest',
],
})) as string
)

return {
to: transaction.to,
value: transaction.value,
data: transaction.data,
operation: transaction.operation ?? OperationType.Call,
safeTxGas: BigInt(defaults?.safeTxGas || 0),
baseGas: BigInt(defaults?.baseGas || 0),
gasPrice: BigInt(defaults?.gasPrice || 0),
gasToken: getAddress(defaults?.gasToken || zeroAddress) as `0x${string}`,
refundReceiver: getAddress(
defaults?.refundReceiver || zeroAddress
) as `0x${string}`,
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
}
}
Loading