From f88e586ff668797d828d3c25326b503d3d78ce9b Mon Sep 17 00:00:00 2001 From: Daniel <25051234+dasanra@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:17:38 +0200 Subject: [PATCH] chore: improve getChainId and getSafeVersion --- packages/protocol-kit/src/Safe.ts | 9 +-------- packages/protocol-kit/src/SafeProvider.ts | 4 ++-- packages/protocol-kit/src/contracts/BaseContract.ts | 13 ++++++++++--- .../contracts/Safe/v1.0.0/SafeContract_v1_0_0.ts | 3 +-- .../contracts/Safe/v1.1.1/SafeContract_v1_1_1.ts | 3 +-- .../contracts/Safe/v1.2.0/SafeContract_v1_2_0.ts | 6 ++---- .../contracts/Safe/v1.3.0/SafeContract_v1_3_0.ts | 5 ++--- .../contracts/Safe/v1.4.1/SafeContract_v1_4_1.ts | 5 ++--- 8 files changed, 21 insertions(+), 27 deletions(-) diff --git a/packages/protocol-kit/src/Safe.ts b/packages/protocol-kit/src/Safe.ts index c1f2c554e..0fbaa5e34 100644 --- a/packages/protocol-kit/src/Safe.ts +++ b/packages/protocol-kit/src/Safe.ts @@ -72,7 +72,6 @@ import { import { isSafeConfigWithPredictedSafe } from './utils/types' import { getCompatibilityFallbackHandlerContract, - getMultiSendCallOnlyContract, getProxyFactoryContract } from './contracts/safeDeploymentContracts' import SafeMessage from './utils/messages/SafeMessage' @@ -1523,14 +1522,8 @@ class Safe { transactions: MetaTransactionData[], transactionOptions?: TransactionOptions ): Promise { - 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', [ diff --git a/packages/protocol-kit/src/SafeProvider.ts b/packages/protocol-kit/src/SafeProvider.ts index bb01c2c1c..8803ddb34 100644 --- a/packages/protocol-kit/src/SafeProvider.ts +++ b/packages/protocol-kit/src/SafeProvider.ts @@ -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) } diff --git a/packages/protocol-kit/src/contracts/BaseContract.ts b/packages/protocol-kit/src/contracts/BaseContract.ts index ebcc0dace..8583bdcba 100644 --- a/packages/protocol-kit/src/contracts/BaseContract.ts +++ b/packages/protocol-kit/src/contracts/BaseContract.ts @@ -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' @@ -17,7 +25,6 @@ import { convertTransactionOptions } from '@safe-global/protocol-kit/utils' import { ExternalClient } from '../types' -import { ContractFunctionName, ContractFunctionArgs } from 'viem' /** * Abstract class BaseContract @@ -118,7 +125,7 @@ class BaseContract { 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 } diff --git a/packages/protocol-kit/src/contracts/Safe/v1.0.0/SafeContract_v1_0_0.ts b/packages/protocol-kit/src/contracts/Safe/v1.0.0/SafeContract_v1_0_0.ts index de2024525..b3898c309 100644 --- a/packages/protocol-kit/src/contracts/Safe/v1.0.0/SafeContract_v1_0_0.ts +++ b/packages/protocol-kit/src/contracts/Safe/v1.0.0/SafeContract_v1_0_0.ts @@ -355,8 +355,7 @@ class SafeContract_v1_0_0 * @returns {Promise} A promise that resolves to the version of the Safe contract as string. */ async getVersion(): Promise { - const [safeVersion] = await this.VERSION() - return safeVersion as SafeVersion + return Promise.resolve(this.safeVersion) } /** diff --git a/packages/protocol-kit/src/contracts/Safe/v1.1.1/SafeContract_v1_1_1.ts b/packages/protocol-kit/src/contracts/Safe/v1.1.1/SafeContract_v1_1_1.ts index 8ba8e4cd9..3da93eb42 100644 --- a/packages/protocol-kit/src/contracts/Safe/v1.1.1/SafeContract_v1_1_1.ts +++ b/packages/protocol-kit/src/contracts/Safe/v1.1.1/SafeContract_v1_1_1.ts @@ -318,8 +318,7 @@ class SafeContract_v1_1_1 * @returns {Promise} A promise that resolves to the version of the Safe contract as string. */ async getVersion(): Promise { - const [safeVersion] = await this.VERSION() - return safeVersion as SafeVersion + return Promise.resolve(this.safeVersion) } /** diff --git a/packages/protocol-kit/src/contracts/Safe/v1.2.0/SafeContract_v1_2_0.ts b/packages/protocol-kit/src/contracts/Safe/v1.2.0/SafeContract_v1_2_0.ts index b08359d6a..b5e3a4314 100644 --- a/packages/protocol-kit/src/contracts/Safe/v1.2.0/SafeContract_v1_2_0.ts +++ b/packages/protocol-kit/src/contracts/Safe/v1.2.0/SafeContract_v1_2_0.ts @@ -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)] } /** @@ -320,8 +319,7 @@ class SafeContract_v1_2_0 * @returns {Promise} A promise that resolves to the version of the Safe contract as string. */ async getVersion(): Promise { - const [safeVersion] = await this.VERSION() - return safeVersion as SafeVersion + return Promise.resolve(this.safeVersion) } /** diff --git a/packages/protocol-kit/src/contracts/Safe/v1.3.0/SafeContract_v1_3_0.ts b/packages/protocol-kit/src/contracts/Safe/v1.3.0/SafeContract_v1_3_0.ts index 9eeb75a45..ba0a38054 100644 --- a/packages/protocol-kit/src/contracts/Safe/v1.3.0/SafeContract_v1_3_0.ts +++ b/packages/protocol-kit/src/contracts/Safe/v1.3.0/SafeContract_v1_3_0.ts @@ -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)] } /** @@ -334,8 +334,7 @@ class SafeContract_v1_3_0 * @returns {Promise} A promise that resolves to the version of the Safe contract as string. */ async getVersion(): Promise { - const [safeVersion] = await this.VERSION() - return safeVersion as SafeVersion + return Promise.resolve(this.safeVersion) } /** diff --git a/packages/protocol-kit/src/contracts/Safe/v1.4.1/SafeContract_v1_4_1.ts b/packages/protocol-kit/src/contracts/Safe/v1.4.1/SafeContract_v1_4_1.ts index 124cabfb9..1d349670f 100644 --- a/packages/protocol-kit/src/contracts/Safe/v1.4.1/SafeContract_v1_4_1.ts +++ b/packages/protocol-kit/src/contracts/Safe/v1.4.1/SafeContract_v1_4_1.ts @@ -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)] } /** @@ -334,8 +334,7 @@ class SafeContract_v1_4_1 * @returns {Promise} A promise that resolves to the version of the Safe contract as string. */ async getVersion(): Promise { - const [safeVersion] = await this.VERSION() - return safeVersion as SafeVersion + return Promise.resolve(this.safeVersion) } /**