• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-core-sdk / 12008523214

25 Nov 2024 10:55AM UTC coverage: 75.515%. Remained the same
12008523214

push

github

web-flow
feat(protocol-kit): Add react native compatibility (#1033)

241 of 390 branches covered (61.79%)

Branch coverage included in aggregate %.

786 of 970 relevant lines covered (81.03%)

3.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.14
/packages/sdk-starter-kit/src/index.ts
1
import Safe, { SafeConfig } from '@safe-global/protocol-kit'
1✔
2
import SafeApiKit from '@safe-global/api-kit'
1✔
3

4
import { SafeClient } from '@safe-global/sdk-starter-kit/SafeClient'
1✔
5
import { isValidAddress, isValidSafeConfig } from '@safe-global/sdk-starter-kit/utils'
1✔
6
import { SdkStarterKitConfig } from '@safe-global/sdk-starter-kit/types'
7
import { DEFAULT_DEPLOYMENT_TYPE } from './constants'
1✔
8

9
/**
10
 * Initializes a Safe client with the given configuration options.
11
 *
12
 * @param config - The Safe client configuration options.
13
 * @returns A Safe client instance.
14
 */
15
export async function createSafeClient(config: SdkStarterKitConfig): Promise<SafeClient> {
1✔
16
  const protocolKit = await getProtocolKitInstance(config)
2✔
17
  const apiKit = await getApiKitInstance(protocolKit, config)
2✔
18

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

21
  return new SafeClient(protocolKit, apiKit)
2✔
22
}
23

24
/**
25
 * Get the Safe protocol kit instance.
26
 *
27
 * @param config - The SDK Starter kit configuration options.
28
 * @returns A protocolKit instance.
29
 */
30
async function getProtocolKitInstance(config: SdkStarterKitConfig): Promise<Safe> {
31
  if (config.safeAddress && isValidAddress(config.safeAddress)) {
2!
32
    // If the safe already exist
33
    return Safe.init({
2✔
34
      provider: config.provider,
35
      signer: config.signer,
36
      safeAddress: config.safeAddress
37
    })
38
  } else if (config.safeOptions && isValidSafeConfig(config.safeOptions)) {
×
39
    // If the safe does not exist and the configuration is provided
40
    let protocolKit: Safe
41
    const initConfig: SafeConfig = {
×
42
      provider: config.provider,
43
      signer: config.signer,
44
      predictedSafe: {
45
        safeAccountConfig: {
46
          owners: config.safeOptions.owners,
47
          threshold: config.safeOptions.threshold
48
        },
49
        safeDeploymentConfig: {
50
          saltNonce: config.safeOptions.saltNonce,
51
          deploymentType: DEFAULT_DEPLOYMENT_TYPE
52
        }
53
      }
54
    }
55

56
    try {
×
57
      protocolKit = await Safe.init(initConfig)
×
58
    } catch (error) {
59
      const isDeploymentTypeUnresolvedError =
60
        error instanceof Error &&
×
61
        error.message &&
62
        error.message.startsWith('Invalid') &&
63
        error.message.includes('contract address')
64
      if (
×
65
        isDeploymentTypeUnresolvedError &&
×
66
        initConfig.predictedSafe.safeDeploymentConfig?.deploymentType
67
      ) {
68
        delete initConfig.predictedSafe.safeDeploymentConfig.deploymentType
×
69
        protocolKit = await Safe.init(initConfig)
×
70
      } else {
71
        throw error
×
72
      }
73
    }
74

75
    const isSafeDeployed = await protocolKit.isSafeDeployed()
×
76

77
    // When the safe is deployed, which can be true given the predicted safe address based on the options,
78
    // we need to re-initialize the Safe client with the safeAddress
79
    if (isSafeDeployed) {
×
80
      return Safe.init({
×
81
        provider: config.provider,
82
        signer: config.signer,
83
        safeAddress: await protocolKit.getAddress()
84
      })
85
    }
86

87
    return protocolKit
×
88
  } else {
89
    throw new Error(
×
90
      'Invalid configuration: either a valid safeAddress or valid safeOptions must be provided.'
91
    )
92
  }
93
}
94

95
async function getApiKitInstance(
96
  protocolKit: Safe,
97
  config: SdkStarterKitConfig
98
): Promise<SafeApiKit> {
99
  const chainId = await protocolKit.getChainId()
2✔
100

101
  return new SafeApiKit({ chainId, txServiceUrl: config.txServiceUrl })
2✔
102
}
103

104
export * from './types'
1✔
105
export * from './extensions'
1✔
106
export * from './SafeClient'
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc