Skip to content

Commit

Permalink
Merge 8e32572 into 17b9607
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed May 9, 2024
2 parents 17b9607 + 8e32572 commit 561e52b
Show file tree
Hide file tree
Showing 36 changed files with 280 additions and 278 deletions.
14 changes: 7 additions & 7 deletions guides/integrating-the-safe-core-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ const safeService = new SafeApiKit({
```js
import Safe, { SafeFactory } from '@safe-global/protocol-kit'

const safeFactory = await SafeFactory.create({ provider, signer })
const safeFactory = await SafeFactory.init({ provider, signer })

const safeSdk = await Safe.create({ provider, signer, safeAddress })
const safeSdk = await Safe.init({ provider, signer, safeAddress })
```

There are two versions of the Safe contracts: [Safe.sol](https://github.com/safe-global/safe-contracts/blob/v1.4.1/contracts/Safe.sol) that does not trigger events in order to save gas and [SafeL2.sol](https://github.com/safe-global/safe-contracts/blob/v1.4.1/contracts/SafeL2.sol) that does, which is more appropriate for L2 networks.

By default `Safe.sol` will be only used on Ethereum Mainnet. For the rest of the networks where the Safe contracts are already deployed, the `SafeL2.sol` contract will be used unless you add the property `isL1SafeSingleton` to force the use of the `Safe.sol` contract.

```js
const safeFactory = await SafeFactory.create({ provider, signer, isL1SafeSingleton: true })
const safeFactory = await SafeFactory.init({ provider, signer, isL1SafeSingleton: true })

const safeSdk = await Safe.create({ provider, signer, safeAddress, isL1SafeSingleton: true })
const safeSdk = await Safe.init({ provider, signer, safeAddress, isL1SafeSingleton: true })
```

If the Safe contracts are not deployed to your current network, the property `contractNetworks` will be required to point to the addresses of the Safe contracts previously deployed by you.
Expand Down Expand Up @@ -100,16 +100,16 @@ const contractNetworks: ContractNetworksConfig = {
}
}

const safeFactory = await SafeFactory.create({ provider, signer, contractNetworks })
const safeFactory = await SafeFactory.init({ provider, signer, contractNetworks })

const safeSdk = await Safe.create({ provider, signer, safeAddress, contractNetworks })
const safeSdk = await Safe.init({ provider, signer, safeAddress, contractNetworks })
```

The `SafeFactory` constructor also accepts the property `safeVersion` to specify the Safe contract version that will be deployed. This string can take the values `1.0.0`, `1.1.1`, `1.2.0`, `1.3.0` or `1.4.1`. If not specified, the `DEFAULT_SAFE_VERSION` value will be used.

```js
const safeVersion = 'X.Y.Z'
const safeFactory = await SafeFactory.create({ provider, signer, safeVersion })
const safeFactory = await SafeFactory.init({ provider, signer, safeVersion })
```

## <a name="deploy-safe">3. Deploy a new Safe</a>
Expand Down
12 changes: 6 additions & 6 deletions packages/account-abstraction-kit/src/AccountAbstraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('AccountAbstraction', () => {
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
)
expect(SafeMock.create).toHaveBeenCalledTimes(1)
expect(SafeMock.create).toHaveBeenCalledWith(
expect(SafeMock.init).toHaveBeenCalledTimes(1)
expect(SafeMock.init).toHaveBeenCalledWith(
expect.objectContaining({
safeAddress: predictSafeAddress
})
Expand All @@ -60,8 +60,8 @@ describe('AccountAbstraction', () => {
safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 }
})
)
expect(SafeMock.create).toHaveBeenCalledTimes(1)
expect(SafeMock.create).toHaveBeenCalledWith(
expect(SafeMock.init).toHaveBeenCalledTimes(1)
expect(SafeMock.init).toHaveBeenCalledWith(
expect.objectContaining({
predictedSafe: { safeAccountConfig: { owners: ['0xSignerAddress'], threshold: 1 } }
})
Expand All @@ -75,7 +75,7 @@ describe('AccountAbstraction', () => {
`There's no signer available with the provided config (provider, signer)`
)

expect(SafeMock.create).not.toHaveBeenCalled()
expect(SafeMock.init).not.toHaveBeenCalled()
})
})

Expand All @@ -97,7 +97,7 @@ describe('AccountAbstraction', () => {

beforeEach(async () => {
jest.clearAllMocks()
SafeMock.create = () => Promise.resolve(safeInstanceMock as unknown as Safe)
SafeMock.init = () => Promise.resolve(safeInstanceMock as unknown as Safe)
accountAbstraction = await initAccountAbstraction()
})

Expand Down
4 changes: 2 additions & 2 deletions packages/account-abstraction-kit/src/AccountAbstraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class AccountAbstraction {
const isSafeDeployed = await safeProvider.isContractDeployed(safeAddress)

if (isSafeDeployed) {
this.protocolKit = await Safe.create({
this.protocolKit = await Safe.init({
provider: this.#provider,
signer: this.#signer,
safeAddress
})
} else {
this.protocolKit = await Safe.create({
this.protocolKit = await Safe.init({
provider: this.#provider,
signer: this.#signer,
predictedSafe: { safeAccountConfig }
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/tests/utils/setupKits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function getProtocolKit({
safeAddress: GetKitsOptions['safeAddress']
}): Promise<Safe> {
const provider = getEip1193Provider()
const protocolKit = await Safe.create({ provider, signer, safeAddress })
const protocolKit = await Safe.init({ provider, signer, safeAddress })

return protocolKit
}
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-kit/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function App() {
const safeAddress = safeAuthSignInResponse?.safes?.[index] || '0x'

// Wrap Web3Auth provider with ethers
const protocolKit = await Safe.create({
const protocolKit = await Safe.init({
provider: safeAuthPack?.getProvider() as Eip1193Provider,
safeAddress
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Monerium() {
;(async () => {
if (!authProvider || !selectedSafe) return

const protocolKit = await Safe.create({
const protocolKit = await Safe.init({
provider: authProvider,
safeAddress: selectedSafe,
isL1SafeSingleton: true
Expand Down
26 changes: 15 additions & 11 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class Safe {
* @throws "MultiSend contract is not deployed on the current network"
* @throws "MultiSendCallOnly contract is not deployed on the current network"
*/
static async create(config: SafeConfig): Promise<Safe> {
const safeSdk = new Safe()
await safeSdk.init(config)
return safeSdk
static async init(config: SafeConfig): Promise<Safe> {
const protocolKit = new Safe()
await protocolKit.#initializeProtocolKit(config)
return protocolKit
}

/**
Expand All @@ -111,9 +111,11 @@ class Safe {
* @throws "MultiSend contract is not deployed on the current network"
* @throws "MultiSendCallOnly contract is not deployed on the current network"
*/
private async init(config: SafeConfig): Promise<void> {
async #initializeProtocolKit(config: SafeConfig): Promise<Safe> {
const { provider, signer, isL1SafeSingleton, contractNetworks } = config

const safeSdk = new Safe()

this.#safeProvider = new SafeProvider({
provider,
signer
Expand Down Expand Up @@ -148,6 +150,8 @@ class Safe {
this.#safeProvider,
this.#contractManager.safeContract
)

return safeSdk
}

/**
Expand All @@ -170,30 +174,30 @@ class Safe {

// A new existing Safe is connected to the Signer
if (safeAddress) {
return await Safe.create({
return await Safe.init({
safeAddress,
...configProps
})
}

// A new predicted Safe is connected to the Signer
if (predictedSafe) {
return await Safe.create({
return await Safe.init({
predictedSafe,
...configProps
})
}

// The previous predicted Safe is connected to a new Signer
if (this.#predictedSafe) {
return await Safe.create({
return await Safe.init({
predictedSafe: this.#predictedSafe,
...configProps
})
}

// The previous existing Safe is connected to a new Signer
return await Safe.create({
return await Safe.init({
safeAddress: await this.getAddress(),
...configProps
})
Expand Down Expand Up @@ -1445,7 +1449,7 @@ class Safe {
*
* @returns The fallback Handler contract
*/
private async getFallbackHandlerContract(): Promise<CompatibilityFallbackHandlerContractType> {
async #getFallbackHandlerContract(): Promise<CompatibilityFallbackHandlerContractType> {
if (!this.#contractManager.safeContract) {
throw new Error('Safe is not deployed')
}
Expand Down Expand Up @@ -1494,7 +1498,7 @@ class Safe {
signature: SafeSignature[] | string = '0x'
): Promise<boolean> => {
const safeAddress = await this.getAddress()
const fallbackHandler = await this.getFallbackHandlerContract()
const fallbackHandler = await this.#getFallbackHandlerContract()

const signatureToCheck =
signature && Array.isArray(signature) ? buildSignatureBytes(signature) : signature
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol-kit/src/SafeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class SafeFactory {
#signer?: SafeFactoryConfig['signer']
#safeProvider!: SafeProvider

static async create({
static async init({
provider,
signer,
safeVersion = DEFAULT_SAFE_VERSION,
isL1SafeSingleton = false,
contractNetworks
}: SafeFactoryConfig): Promise<SafeFactory> {
const safeFactorySdk = new SafeFactory()
await safeFactorySdk.init({
await safeFactorySdk.#initializeSafeFactory({
provider,
signer,
safeVersion,
Expand All @@ -53,7 +53,7 @@ class SafeFactory {
return safeFactorySdk
}

private async init({
async #initializeSafeFactory({
provider,
signer,
safeVersion,
Expand Down Expand Up @@ -157,7 +157,7 @@ class SafeFactory {
if (!isContractDeployed) {
throw new Error('SafeProxy contract is not deployed on the current network')
}
const safe = await Safe.create({
const safe = await Safe.init({
provider: this.#provider,
signer: this.#signer,
safeAddress,
Expand Down
10 changes: 5 additions & 5 deletions packages/protocol-kit/tests/e2e/contractManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Safe contracts manager', () => {
}
}
chai.expect(
await Safe.create({
await Safe.init({
provider,
predictedSafe,
contractNetworks
Expand All @@ -63,7 +63,7 @@ describe('Safe contracts manager', () => {
const safeAddress = await safe.getAddress()
await chai
.expect(
Safe.create({
Safe.init({
provider,
safeAddress
})
Expand All @@ -75,7 +75,7 @@ describe('Safe contracts manager', () => {
const { contractNetworks, provider } = await setupTests()
await chai
.expect(
Safe.create({
Safe.init({
provider,
safeAddress: ZERO_ADDRESS,
contractNetworks
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('Safe contracts manager', () => {
const safeAddress = await safe.getAddress()
await chai
.expect(
Safe.create({
Safe.init({
provider,
safeAddress,
contractNetworks: customContractNetworks
Expand All @@ -122,7 +122,7 @@ describe('Safe contracts manager', () => {
it('should set the MultiSend contract available on the current network', async () => {
const { safe, chainId, contractNetworks, provider } = await setupTests()
const safeAddress = await safe.getAddress()
const safeSdk = await Safe.create({
const safeSdk = await Safe.init({
provider,
safeAddress,
contractNetworks
Expand Down
Loading

0 comments on commit 561e52b

Please sign in to comment.