Skip to content

Commit

Permalink
Enforce lower case via type. Reexport Address type, also use internally
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Jan 30, 2025
1 parent 1adf958 commit c7ec8ba
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 137 deletions.
4 changes: 2 additions & 2 deletions src/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, getAddress, isAddress, zeroAddress } from 'viem'
import { getAddress, isAddress, zeroAddress } from 'viem'
import { chains } from './chains'

import type { ChainId, PrefixedAddress } from './types'
import type { Address, ChainId, PrefixedAddress } from './types'

export function prefixAddress(
chainId: ChainId | undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/eip712.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, expect } from 'bun:test'
import {
Address,
encodeFunctionData,
getAddress,
Hash,
Expand All @@ -17,9 +16,10 @@ import { typedDataForSafeTransaction } from './eip712'

import { deploySafe } from '../test/avatar'
import { testClient } from '../test/client'
import { Address } from './types'

const makeAddress = (number: number): Address =>
getAddress(toHex(number, { size: 20 }))
getAddress(toHex(number, { size: 20 })).toLowerCase() as Address

const safeAbi = parseAbi([
'function getTransactionHash(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, uint256 _nonce) view returns (bytes32)',
Expand Down
4 changes: 3 additions & 1 deletion src/eip712.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Address, Hex, zeroAddress } from 'viem'
import { Hex, zeroAddress } from 'viem'
import { OperationType } from '@safe-global/types-kit'

import { Address } from './types'

export function typedDataForSafeTransaction({
chainId,
safeAddress,
Expand Down
3 changes: 1 addition & 2 deletions src/execute/execute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Address,
Chain,
createWalletClient,
custom,
Expand All @@ -20,7 +19,7 @@ import {
type ExecutionPlan,
type ExecutionState,
} from './types'
import type { ChainId } from '../types'
import type { Address, ChainId } from '../types'

/**
* Executes the given plan, continuing from the given state. Mutates the state array to track execution progress.
Expand Down
22 changes: 12 additions & 10 deletions src/execute/multisend.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { concat, encodeFunctionData, encodePacked, hexToBytes } from 'viem'
import { OperationType } from '@safe-global/types-kit'
import { MetaTransactionRequest } from '../types'

import { Address, MetaTransactionRequest } from '../types'

export const encodeMultiSend = (
transactions: readonly MetaTransactionRequest[],
preferredAddresses: `0x${string}`[] = []
preferredAddresses: Address[] = []
): MetaTransactionRequest => {
if (transactions.length === 0) {
throw new Error('No transactions to encode')
Expand Down Expand Up @@ -61,35 +62,36 @@ const encodeMultiSendData = (
})
}

const MULTI_SEND_141 = '0x38869bf66a61cf6bdb996a6ae40d5853fd43b526'
const MULTI_SEND_CALLONLY_141 = '0x9641d764fc13c8b624c04430c7356c1c7c8102e2'
const MULTI_SEND_141: Address = '0x38869bf66a61cf6bdb996a6ae40d5853fd43b526'
const MULTI_SEND_CALLONLY_141: Address =
'0x9641d764fc13c8b624c04430c7356c1c7c8102e2'

const KNOWN_MULTI_SEND_ADDRESSES = [
const KNOWN_MULTI_SEND_ADDRESSES: Address[] = [
MULTI_SEND_141,
'0xa238cbeb142c10ef7ad8442c6d1f9e89e07e7761', // MultiSend 1.3.0
'0x998739bfdaadde7c933b942a68053933098f9eda', // MultiSend 1.3.0 alternative
'0x8d29be29923b68abfdd21e541b9374737b49cdad', // MultiSend 1.1.1
]
const KNOWN_MULTI_SEND_CALL_ONLY_ADDRESSES = [
const KNOWN_MULTI_SEND_CALL_ONLY_ADDRESSES: Address[] = [
MULTI_SEND_CALLONLY_141,
'0x40a2accbd92bca938b02010e17a5b8929b49130d', // MultiSendCallOnly 1.3.0
'0xa1dabef33b3b82c7814b6d82a79e50f4ac44102b', // MultiSendCallOnly 1.3.0 alternative
]

const multiSendAddress = (
transactions: readonly MetaTransactionRequest[],
preferredAddresses: `0x${string}`[] = []
): `0x${string}` => {
preferredAddresses: Address[] = []
): Address => {
const callOnly = transactions.every(
(tx) => tx.operation === OperationType.Call
)

const preferredAddress: `0x${string}` | undefined =
const preferredAddress: Address | undefined =
preferredAddresses.find((a) =>
(callOnly
? KNOWN_MULTI_SEND_CALL_ONLY_ADDRESSES
: KNOWN_MULTI_SEND_ADDRESSES
).includes(a.toLowerCase())
).includes(a)
) || preferredAddresses[0]

if (
Expand Down
3 changes: 2 additions & 1 deletion src/execute/normalizeRoute.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Address, encodeFunctionData, getAddress, parseAbi } from 'viem'
import { encodeFunctionData, getAddress, parseAbi } from 'viem'

import { prefixAddress, splitPrefixedAddress } from '../addresses'

import {
Account,
AccountType,
Address,
ChainId,
Connection,
PrefixedAddress,
Expand Down
4 changes: 2 additions & 2 deletions src/execute/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Address, createPublicClient, getAddress, http } from 'viem'
import { createPublicClient, http } from 'viem'

import { Eip1193Provider } from '@safe-global/protocol-kit'

import { chains, defaultRpc } from '../chains'
import { prefixAddress } from '../addresses'

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

export interface Options {
Expand Down
Loading

0 comments on commit c7ec8ba

Please sign in to comment.