From 4e0cea92236746e2964cf42887abc6a6759f00c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yago=20P=C3=A9rez=20V=C3=A1zquez?= Date: Fri, 20 Sep 2024 16:21:27 +0200 Subject: [PATCH] Add some changes to allow to choose the deploymentType --- packages/protocol-kit/src/Safe.ts | 4 +++- packages/protocol-kit/src/SafeProvider.ts | 9 +++++++++ packages/protocol-kit/src/contracts/BaseContract.ts | 6 +++++- packages/protocol-kit/src/contracts/config.ts | 3 +++ packages/protocol-kit/src/types/safeConfig.ts | 1 + packages/protocol-kit/tests/e2e/contractInfo.test.ts | 2 +- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/protocol-kit/src/Safe.ts b/packages/protocol-kit/src/Safe.ts index b8440d14e..3612051bf 100644 --- a/packages/protocol-kit/src/Safe.ts +++ b/packages/protocol-kit/src/Safe.ts @@ -122,7 +122,7 @@ class Safe { * @throws "MultiSendCallOnly contract is not deployed on the current network" */ async #initializeProtocolKit(config: SafeConfig) { - const { provider, signer, isL1SafeSingleton, contractNetworks } = config + const { provider, signer, isL1SafeSingleton, contractNetworks, predictedSafe } = config this.#safeProvider = await SafeProvider.init( provider, @@ -131,6 +131,8 @@ class Safe { contractNetworks ) + this.#safeProvider.deploymentType = predictedSafe?.safeDeploymentConfig?.deploymentType + if (isSafeConfigWithPredictedSafe(config)) { this.#predictedSafe = config.predictedSafe this.#contractManager = await ContractManager.init( diff --git a/packages/protocol-kit/src/SafeProvider.ts b/packages/protocol-kit/src/SafeProvider.ts index f85755151..a03c91c97 100644 --- a/packages/protocol-kit/src/SafeProvider.ts +++ b/packages/protocol-kit/src/SafeProvider.ts @@ -94,6 +94,7 @@ class SafeProvider { #externalProvider: ExternalClient signer?: SafeSigner provider: Eip1193Provider | HttpTransport | SocketTransport + #deploymentType?: 'canonical' | 'eip155' | 'zksync' constructor({ provider, @@ -181,6 +182,14 @@ class SafeProvider { } } + get deploymentType(): 'canonical' | 'eip155' | 'zksync' | undefined { + return this.#deploymentType + } + + set deploymentType(deploymentType: 'canonical' | 'eip155' | 'zksync' | undefined) { + this.#deploymentType = deploymentType + } + async getExternalSigner(): Promise { const { transport, chain = await this.#getChain() } = this.getExternalProvider() diff --git a/packages/protocol-kit/src/contracts/BaseContract.ts b/packages/protocol-kit/src/contracts/BaseContract.ts index ebcc0dace..d89f11f05 100644 --- a/packages/protocol-kit/src/contracts/BaseContract.ts +++ b/packages/protocol-kit/src/contracts/BaseContract.ts @@ -75,12 +75,16 @@ class BaseContract { throw new Error(`Invalid ${contractName.replace('Version', '')} contract address`) } + const customDeploymentTypeAddress = safeProvider.deploymentType + ? deployment?.deployments[safeProvider.deploymentType]?.address + : undefined + this.chainId = chainId this.contractName = contractName this.safeVersion = safeVersion this.contractAddress = Array.isArray(contractAddress) && contractAddress.length - ? contractAddress[0] + ? contractAddress.find((a) => a === customDeploymentTypeAddress) || contractAddress[0] : contractAddress.toString() this.contractAbi = customContractAbi || diff --git a/packages/protocol-kit/src/contracts/config.ts b/packages/protocol-kit/src/contracts/config.ts index c6bf72b2f..04ecf8be9 100644 --- a/packages/protocol-kit/src/contracts/config.ts +++ b/packages/protocol-kit/src/contracts/config.ts @@ -160,6 +160,7 @@ export function getContractInfo(contractAddress: string): ContractInfo | undefin const deployment = contractFunctions[contractName as contractName]( filters ) as SingletonDeployment + if (deployment && deployment.networkAddresses) { for (const [, address] of Object.entries(deployment.networkAddresses)) { if (address.toLowerCase() === contractAddress.toLowerCase()) { @@ -168,10 +169,12 @@ export function getContractInfo(contractAddress: string): ContractInfo | undefin | 'eip155' | 'zksync' )[] + const type = types.find( (t) => deployment.deployments[t]?.address.toLowerCase() === contractAddress.toLowerCase() ) + if (type) { return { version: safeVersion, diff --git a/packages/protocol-kit/src/types/safeConfig.ts b/packages/protocol-kit/src/types/safeConfig.ts index 767cf770b..801215a5e 100644 --- a/packages/protocol-kit/src/types/safeConfig.ts +++ b/packages/protocol-kit/src/types/safeConfig.ts @@ -17,6 +17,7 @@ export type SafeAccountConfig = { export type SafeDeploymentConfig = { saltNonce?: string safeVersion?: SafeVersion + deploymentType?: 'canonical' | 'eip155' | 'zksync' } export type PredictedSafeProps = { diff --git a/packages/protocol-kit/tests/e2e/contractInfo.test.ts b/packages/protocol-kit/tests/e2e/contractInfo.test.ts index 59d6bb9da..c83484ae7 100644 --- a/packages/protocol-kit/tests/e2e/contractInfo.test.ts +++ b/packages/protocol-kit/tests/e2e/contractInfo.test.ts @@ -20,7 +20,7 @@ describe('Contract Info', () => { }) }) - describe.only('create', async () => { + describe('create', async () => { it('should return undefined for a contract address not related to Safe', async () => { chai.expect( protocolKit.getContractInfo({