Skip to content

Commit

Permalink
feat: Add deploymentType (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv authored Nov 8, 2024
1 parent 014f365 commit 32b408b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
42 changes: 28 additions & 14 deletions packages/protocol-kit/src/contracts/safeDeploymentContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ export async function getCompatibilityFallbackHandlerContract({
export async function getMultiSendContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<MultiSendContractImplementationType> {
const multiSendContract = await getMultiSendContractInstance(
safeVersion,
safeProvider,
customContracts?.multiSendAddress,
customContracts?.multiSendAbi
customContracts?.multiSendAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(multiSendContract.getAddress())
Expand All @@ -131,13 +133,15 @@ export async function getMultiSendContract({
export async function getMultiSendCallOnlyContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<MultiSendCallOnlyContractImplementationType> {
const multiSendCallOnlyContract = await getMultiSendCallOnlyContractInstance(
safeVersion,
safeProvider,
customContracts?.multiSendCallOnlyAddress,
customContracts?.multiSendCallOnlyAbi
customContracts?.multiSendCallOnlyAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(
Expand All @@ -152,13 +156,15 @@ export async function getMultiSendCallOnlyContract({
export async function getSignMessageLibContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<SignMessageLibContractImplementationType> {
const signMessageLibContract = await getSignMessageLibContractInstance(
safeVersion,
safeProvider,
customContracts?.signMessageLibAddress,
customContracts?.signMessageLibAbi
customContracts?.signMessageLibAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(
Expand All @@ -173,13 +179,15 @@ export async function getSignMessageLibContract({
export async function getCreateCallContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<CreateCallContractImplementationType> {
const createCallContract = await getCreateCallContractInstance(
safeVersion,
safeProvider,
customContracts?.createCallAddress,
customContracts?.createCallAbi
customContracts?.createCallAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(createCallContract.getAddress())
Expand All @@ -192,13 +200,15 @@ export async function getCreateCallContract({
export async function getSimulateTxAccessorContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<SimulateTxAccessorContractImplementationType> {
const simulateTxAccessorContract = await getSimulateTxAccessorContractInstance(
safeVersion,
safeProvider,
customContracts?.simulateTxAccessorAddress,
customContracts?.simulateTxAccessorAbi
customContracts?.simulateTxAccessorAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(
Expand All @@ -213,13 +223,15 @@ export async function getSimulateTxAccessorContract({
export async function getSafeWebAuthnSignerFactoryContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<SafeWebAuthnSignerFactoryContractImplementationType> {
const safeWebAuthnSignerFactoryContract = await getSafeWebAuthnSignerFactoryContractInstance(
safeVersion,
safeProvider,
customContracts?.safeWebAuthnSignerFactoryAddress,
customContracts?.safeWebAuthnSignerFactoryAbi
customContracts?.safeWebAuthnSignerFactoryAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(
Expand All @@ -234,13 +246,15 @@ export async function getSafeWebAuthnSignerFactoryContract({
export async function getSafeWebAuthnSharedSignerContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType
}: GetContractInstanceProps): Promise<SafeWebAuthnSharedSignerContractImplementationType> {
const safeWebAuthnSharedSignerContract = await getSafeWebAuthnSharedSignerContractInstance(
safeVersion,
safeProvider,
customContracts?.safeWebAuthnSharedSignerAddress,
customContracts?.safeWebAuthnSharedSignerAbi
customContracts?.safeWebAuthnSharedSignerAbi,
deploymentType
)

const isContractDeployed = await safeProvider.isContractDeployed(
Expand Down
6 changes: 4 additions & 2 deletions packages/protocol-kit/src/managers/contractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ class ContractManager {
this.#multiSendContract = await getMultiSendContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType: predictedSafe?.safeDeploymentConfig?.deploymentType
})

this.#multiSendCallOnlyContract = await getMultiSendCallOnlyContract({
safeProvider,
safeVersion,
customContracts
customContracts,
deploymentType: predictedSafe?.safeDeploymentConfig?.deploymentType
})
}

Expand Down
6 changes: 4 additions & 2 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ export class Safe4337Pack extends RelayKitBasePack<{
if (isBatch) {
const multiSendContract = await getMultiSendContract({
safeProvider,
safeVersion
safeVersion,
deploymentType: options.deploymentType || undefined
})

const batchData = encodeFunctionData({
Expand All @@ -315,7 +316,8 @@ export class Safe4337Pack extends RelayKitBasePack<{
predictedSafe: {
safeDeploymentConfig: {
safeVersion,
saltNonce: options.saltNonce || undefined
saltNonce: options.saltNonce || undefined,
deploymentType: options.deploymentType || undefined
},
safeAccountConfig: {
owners: options.owners,
Expand Down
3 changes: 2 additions & 1 deletion packages/relay-kit/src/packs/safe-4337/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account, Address, Chain, Hash, Hex, PublicClient, PublicRpcSchema, Transport } from 'viem'
import Safe, { SafeProviderConfig } from '@safe-global/protocol-kit'
import Safe, { DeploymentType, SafeProviderConfig } from '@safe-global/protocol-kit'
import {
EstimateGasData,
MetaTransactionData,
Expand All @@ -19,6 +19,7 @@ type PredictedSafeOptions = {
threshold: number
safeVersion?: SafeVersion
saltNonce?: string
deploymentType?: DeploymentType
}

export type SponsoredPaymasterOption = {
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk-starter-kit/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { DeploymentType } from '@safe-global/protocol-kit'

export const DEFAULT_DEPLOYMENT_TYPE: DeploymentType = 'canonical'

export enum SafeClientTxStatus {
DEPLOYED_AND_EXECUTED = 'DEPLOYED_AND_EXECUTED',
DEPLOYED_AND_PENDING_SIGNATURES = 'DEPLOYED_AND_PENDING_SIGNATURES',
Expand Down
39 changes: 32 additions & 7 deletions packages/sdk-starter-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Safe from '@safe-global/protocol-kit'
import Safe, { SafeConfig } from '@safe-global/protocol-kit'
import SafeApiKit from '@safe-global/api-kit'

import { SafeClient } from '@safe-global/sdk-starter-kit/SafeClient'
import { isValidAddress, isValidSafeConfig } from '@safe-global/sdk-starter-kit/utils'
import { SdkStarterKitConfig } from '@safe-global/sdk-starter-kit/types'
import { DEFAULT_DEPLOYMENT_TYPE } from './constants'

/**
* Initializes a Safe client with the given configuration options.
Expand All @@ -13,7 +14,7 @@ import { SdkStarterKitConfig } from '@safe-global/sdk-starter-kit/types'
*/
export async function createSafeClient(config: SdkStarterKitConfig): Promise<SafeClient> {
const protocolKit = await getProtocolKitInstance(config)
const apiKit = await getApiKitInstance(protocolKit)
const apiKit = await getApiKitInstance(protocolKit, config)

if (!protocolKit || !apiKit) throw new Error('Failed to create a kit instances')

Expand All @@ -36,7 +37,8 @@ async function getProtocolKitInstance(config: SdkStarterKitConfig): Promise<Safe
})
} else if (config.safeOptions && isValidSafeConfig(config.safeOptions)) {
// If the safe does not exist and the configuration is provided
const protocolKit = await Safe.init({
let protocolKit: Safe
const initConfig: SafeConfig = {
provider: config.provider,
signer: config.signer,
predictedSafe: {
Expand All @@ -45,10 +47,30 @@ async function getProtocolKitInstance(config: SdkStarterKitConfig): Promise<Safe
threshold: config.safeOptions.threshold
},
safeDeploymentConfig: {
saltNonce: config.safeOptions.saltNonce
saltNonce: config.safeOptions.saltNonce,
deploymentType: DEFAULT_DEPLOYMENT_TYPE
}
}
})
}

try {
protocolKit = await Safe.init(initConfig)
} catch (error) {
const isDeploymentTypeUnresolvedError =
error instanceof Error &&
error.message &&
error.message.startsWith('Invalid') &&
error.message.includes('contract address')
if (
isDeploymentTypeUnresolvedError &&
initConfig.predictedSafe.safeDeploymentConfig?.deploymentType
) {
delete initConfig.predictedSafe.safeDeploymentConfig.deploymentType
protocolKit = await Safe.init(initConfig)
} else {
throw error
}
}

const isSafeDeployed = await protocolKit.isSafeDeployed()

Expand All @@ -70,10 +92,13 @@ async function getProtocolKitInstance(config: SdkStarterKitConfig): Promise<Safe
}
}

async function getApiKitInstance(protocolKit: Safe): Promise<SafeApiKit> {
async function getApiKitInstance(
protocolKit: Safe,
config: SdkStarterKitConfig
): Promise<SafeApiKit> {
const chainId = await protocolKit.getChainId()

return new SafeApiKit({ chainId })
return new SafeApiKit({ chainId, txServiceUrl: config.txServiceUrl })
}

export * from './types'
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-starter-kit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type PredictedSafeConfig = {
export type SdkStarterKitRootConfig = {
provider: SafeProvider['provider']
signer?: SafeProvider['signer']
txServiceUrl?: string
}

export type SdkStarterKitConfig = SdkStarterKitRootConfig &
Expand Down

0 comments on commit 32b408b

Please sign in to comment.