Skip to content

Commit

Permalink
Some changes to fix the BaseContract deploymenttype search
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Sep 23, 2024
1 parent 4e0cea9 commit c10fd16
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class Safe {
contractNetworks
)

this.#safeProvider.deploymentType = predictedSafe?.safeDeploymentConfig?.deploymentType

if (isSafeConfigWithPredictedSafe(config)) {
this.#predictedSafe = config.predictedSafe
this.#contractManager = await ContractManager.init(
Expand All @@ -158,6 +156,7 @@ class Safe {

const safeVersion = await this.getContractVersion()
this.#safeProvider = await SafeProvider.init(provider, signer, safeVersion, contractNetworks)
this.#safeProvider.deploymentType = predictedSafe?.safeDeploymentConfig?.deploymentType

this.#ownerManager = new OwnerManager(this.#safeProvider, this.#contractManager.safeContract)
this.#moduleManager = new ModuleManager(this.#safeProvider, this.#contractManager.safeContract)
Expand Down
14 changes: 8 additions & 6 deletions packages/protocol-kit/src/contracts/BaseContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class BaseContract<ContractAbiType extends Abi> {
) {
const deployment = getContractDeployment(safeVersion, chainId, contractName)

const contractAddress =
customContractAddress || deployment?.networkAddresses[chainId.toString()]
let contractAddress = customContractAddress || deployment?.networkAddresses[chainId.toString()]

if (!contractAddress) {
throw new Error(`Invalid ${contractName.replace('Version', '')} contract address`)
Expand All @@ -79,13 +78,16 @@ class BaseContract<ContractAbiType extends Abi> {
? deployment?.deployments[safeProvider.deploymentType]?.address
: undefined

this.chainId = chainId
this.contractName = contractName
this.safeVersion = safeVersion
this.contractAddress =
contractAddress =
Array.isArray(contractAddress) && contractAddress.length
? contractAddress.find((a) => a === customDeploymentTypeAddress) || contractAddress[0]
: contractAddress.toString()

this.chainId = chainId
this.contractName = contractName
this.safeVersion = safeVersion
this.contractAddress = contractAddress

this.contractAbi =
customContractAbi ||
(deployment?.abi as unknown as ContractAbiType) || // this cast is required because abi is set as any[] in safe-deployments
Expand Down
41 changes: 22 additions & 19 deletions packages/protocol-kit/src/contracts/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
DeploymentFilter,
SingletonDeployment,
getCompatibilityFallbackHandlerDeployment,
getCreateCallDeployment,
getMultiSendCallOnlyDeployment,
getMultiSendDeployment,
getProxyFactoryDeployment,
getSafeL2SingletonDeployment,
getSafeSingletonDeployment,
getSignMessageLibDeployment,
getSimulateTxAccessorDeployment
SingletonDeploymentV2,
getCompatibilityFallbackHandlerDeployments,
getCreateCallDeployments,
getMultiSendCallOnlyDeployments,
getMultiSendDeployments,
getProxyFactoryDeployments,
getSafeL2SingletonDeployments,
getSafeSingletonDeployments,
getSignMessageLibDeployments,
getSimulateTxAccessorDeployments
} from '@safe-global/safe-deployments'
import {
Deployment,
Expand Down Expand Up @@ -110,17 +111,19 @@ export const safeDeploymentsL1ChainIds = [

const contractFunctions: Record<
contractName,
(filter?: DeploymentFilter) => SingletonDeployment | undefined | Deployment
(
filter?: DeploymentFilter
) => SingletonDeployment | SingletonDeploymentV2 | undefined | Deployment
> = {
safeSingletonVersion: getSafeSingletonDeployment,
safeSingletonL2Version: getSafeL2SingletonDeployment,
safeProxyFactoryVersion: getProxyFactoryDeployment,
compatibilityFallbackHandler: getCompatibilityFallbackHandlerDeployment,
multiSendVersion: getMultiSendDeployment,
multiSendCallOnlyVersion: getMultiSendCallOnlyDeployment,
signMessageLibVersion: getSignMessageLibDeployment,
createCallVersion: getCreateCallDeployment,
simulateTxAccessorVersion: getSimulateTxAccessorDeployment,
safeSingletonVersion: getSafeSingletonDeployments,
safeSingletonL2Version: getSafeL2SingletonDeployments,
safeProxyFactoryVersion: getProxyFactoryDeployments,
compatibilityFallbackHandler: getCompatibilityFallbackHandlerDeployments,
multiSendVersion: getMultiSendDeployments,
multiSendCallOnlyVersion: getMultiSendCallOnlyDeployments,
signMessageLibVersion: getSignMessageLibDeployments,
createCallVersion: getCreateCallDeployments,
simulateTxAccessorVersion: getSimulateTxAccessorDeployments,
safeWebAuthnSignerFactoryVersion: getSafeWebAuthnSignerFactoryDeployment,
safeWebAuthnSharedSignerVersion: getSafeWebAuthnShareSignerDeployment
}
Expand Down
66 changes: 66 additions & 0 deletions packages/protocol-kit/tests/e2e/utilsContracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,5 +651,71 @@ describe('Contract utils', () => {
chai.expect(mainnetPredictedSafeAddress).to.be.equal(expectedMainnetSafeAddress)
}
)

itif(safeVersionDeployed === '1.3.0')(
'returns the same predicted address based on the deploymentType for different chains',
async () => {
const { accounts } = await setupTests()
const [owner] = accounts
const safeVersion = safeVersionDeployed

const safeAccountConfig: SafeAccountConfig = {
owners: [owner.address],
threshold: 1
}

const safeDeploymentConfig: SafeDeploymentConfig = {
safeVersion,
saltNonce: PREDETERMINED_SALT_NONCE
}

const protocolKitPolygonMainnet = await Safe.init({
provider: 'https://polygon-bor-rpc.publicnode.com',
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig
}
})

const protocolKitGnosis = await Safe.init({
provider: 'https://rpc.gnosischain.com',
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig
}
})

const protocolKitBNB = await Safe.init({
provider: 'https://bsc.meowrpc.com',
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig: {
...safeDeploymentConfig,
deploymentType: 'canonical'
}
}
})

const protocolKitSepolia = await Safe.init({
provider: 'https://sepolia.gateway.tenderly.co',
predictedSafe: {
safeAccountConfig,
safeDeploymentConfig: {
...safeDeploymentConfig,
deploymentType: 'canonical'
}
}
})

const gnosisChainPredictedAddress = await protocolKitGnosis.getAddress()
const polygonChainPredictedAddress = await protocolKitPolygonMainnet.getAddress()
const bnbChainPredictedAddress = await protocolKitBNB.getAddress()
const sepoliaChainPredictedAddress = await protocolKitSepolia.getAddress()

chai.expect(gnosisChainPredictedAddress).to.be.equal(polygonChainPredictedAddress)
chai.expect(polygonChainPredictedAddress).to.be.equal(bnbChainPredictedAddress)
chai.expect(bnbChainPredictedAddress).to.be.equal(sepoliaChainPredictedAddress)
}
)
})
})

0 comments on commit c10fd16

Please sign in to comment.