Skip to content

Commit

Permalink
chore: improve getChainId and getSafeVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
dasanra committed Sep 23, 2024
1 parent 4ec32bf commit f88e586
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 27 deletions.
9 changes: 1 addition & 8 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import {
import { isSafeConfigWithPredictedSafe } from './utils/types'
import {
getCompatibilityFallbackHandlerContract,
getMultiSendCallOnlyContract,
getProxyFactoryContract
} from './contracts/safeDeploymentContracts'
import SafeMessage from './utils/messages/SafeMessage'
Expand Down Expand Up @@ -1523,14 +1522,8 @@ class Safe {
transactions: MetaTransactionData[],
transactionOptions?: TransactionOptions
): Promise<Transaction> {
const chainId = await this.#safeProvider.getChainId()

// we use the MultiSend contract to create the batch, see: https://github.com/safe-global/safe-contracts/blob/main/contracts/libraries/MultiSendCallOnly.sol
const multiSendCallOnlyContract = await getMultiSendCallOnlyContract({
safeProvider: this.#safeProvider,
safeVersion: await this.getContractVersion(),
customContracts: this.#contractManager.contractNetworks?.[chainId.toString()]
})
const multiSendCallOnlyContract = this.#contractManager.multiSendCallOnlyContract

// multiSend method with the transactions encoded
const batchData = multiSendCallOnlyContract.encode('multiSend', [
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol-kit/src/SafeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ class SafeProvider {
throw new Error('SafeProvider must be initialized with a signer to use this method')
}

// The address on the `WalletClient` is the one we are passing so we let viem make assertions about that account
// For viem, in this context a typeof account === 'string' to singMessage is assumed to be a json-rpc account (returned by parseAccount function)
// The address on the `WalletClient` is the one we are passing so we let Viem make assertions about that account
// For Viem, in this context a typeof account === 'string' to signMessage is assumed to be a json-rpc account (returned by parseAccount function)
if (sameString(signer.account.address, account)) {
return await signer?.signMessage!({
message: { raw: toBytes(message) }
Expand Down
13 changes: 10 additions & 3 deletions packages/protocol-kit/src/contracts/BaseContract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Abi } from 'abitype'
import { Transport, encodeFunctionData, WalletClient, Hash, Chain } from 'viem'
import {
ContractFunctionName,
ContractFunctionArgs,
Transport,
encodeFunctionData,
WalletClient,
Hash,
Chain
} from 'viem'
import { estimateContractGas, getTransactionReceipt } from 'viem/actions'
import { contractName, getContractDeployment } from '@safe-global/protocol-kit/contracts/config'
import SafeProvider from '@safe-global/protocol-kit/SafeProvider'
Expand All @@ -17,7 +25,6 @@ import {
convertTransactionOptions
} from '@safe-global/protocol-kit/utils'
import { ExternalClient } from '../types'
import { ContractFunctionName, ContractFunctionArgs } from 'viem'

/**
* Abstract class BaseContract
Expand Down Expand Up @@ -118,7 +125,7 @@ class BaseContract<ContractAbiType extends Abi> {
if (!signer || !signerAddress) throw new Error('Invalid signer')

const account = signer || signerAddress
const txOptions = await convertTransactionOptions(options)
const txOptions = convertTransactionOptions(options)
return { chain, ...txOptions, account } // Needs to be in this order to override the `account` if necessary
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ class SafeContract_v1_0_0
* @returns {Promise<SafeVersion>} A promise that resolves to the version of the Safe contract as string.
*/
async getVersion(): Promise<SafeVersion> {
const [safeVersion] = await this.VERSION()
return safeVersion as SafeVersion
return Promise.resolve(this.safeVersion)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ class SafeContract_v1_1_1
* @returns {Promise<SafeVersion>} A promise that resolves to the version of the Safe contract as string.
*/
async getVersion(): Promise<SafeVersion> {
const [safeVersion] = await this.VERSION()
return safeVersion as SafeVersion
return Promise.resolve(this.safeVersion)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ class SafeContract_v1_2_0
* @returns Array[chainId]
*/
async getChainId(): Promise<[bigint]> {
const chainId = await this.runner!.getChainId()
return [BigInt(chainId)]
return [await Promise.resolve(this.chainId)]
}

/**
Expand Down Expand Up @@ -320,8 +319,7 @@ class SafeContract_v1_2_0
* @returns {Promise<SafeVersion>} A promise that resolves to the version of the Safe contract as string.
*/
async getVersion(): Promise<SafeVersion> {
const [safeVersion] = await this.VERSION()
return safeVersion as SafeVersion
return Promise.resolve(this.safeVersion)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class SafeContract_v1_3_0
* @returns Array[chainId]
*/
async getChainId(): Promise<[bigint]> {
return [await this.read('getChainId')]
return [await Promise.resolve(this.chainId)]
}

/**
Expand All @@ -334,8 +334,7 @@ class SafeContract_v1_3_0
* @returns {Promise<SafeVersion>} A promise that resolves to the version of the Safe contract as string.
*/
async getVersion(): Promise<SafeVersion> {
const [safeVersion] = await this.VERSION()
return safeVersion as SafeVersion
return Promise.resolve(this.safeVersion)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class SafeContract_v1_4_1
* @returns Array[chainId]
*/
async getChainId(): Promise<[bigint]> {
return [await this.read('getChainId')]
return [await Promise.resolve(this.chainId)]
}

/**
Expand All @@ -334,8 +334,7 @@ class SafeContract_v1_4_1
* @returns {Promise<SafeVersion>} A promise that resolves to the version of the Safe contract as string.
*/
async getVersion(): Promise<SafeVersion> {
const [safeVersion] = await this.VERSION()
return safeVersion as SafeVersion
return Promise.resolve(this.safeVersion)
}

/**
Expand Down

0 comments on commit f88e586

Please sign in to comment.