Skip to content

Commit

Permalink
feat: Ethers v6 Migration (#549)
Browse files Browse the repository at this point in the history
* Initial migration to Ethers v6, basic functionality in place

* Fix gas estimation parsing issue in Ethers v6

* Fix relay-kit tests

* Fix auth-kit tests

* Fix protocol-kit tests

* Update ethers doc links in .md files

* Add zkSyncEraCreate2Address to generates a zkSync Era address (#553)

* remove unnecesary cast to BigInt in EthersAdapter

* use AbstractSigner instead of Signer
  • Loading branch information
DaniSomoza authored Oct 25, 2023
1 parent 3643a51 commit 7496d7b
Show file tree
Hide file tree
Showing 123 changed files with 3,346 additions and 3,360 deletions.
2 changes: 1 addition & 1 deletion guides/integrating-the-safe-core-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ To integrate the [Safe Core SDK](https://github.com/safe-global/safe-core-sdk) i

### Instantiate an EthAdapter

First of all, we need to create an `EthAdapter`, which contains all the required utilities for the SDKs to interact with the blockchain. It acts as a wrapper for [web3.js](https://web3js.readthedocs.io/) or [ethers.js](https://docs.ethers.io/v5/) Ethereum libraries.
First of all, we need to create an `EthAdapter`, which contains all the required utilities for the SDKs to interact with the blockchain. It acts as a wrapper for [web3.js](https://web3js.readthedocs.io/) or [ethers.js](https://docs.ethers.org/v6/) Ethereum libraries.

Depending on the library used by the Dapp, there are two options:

Expand Down
2 changes: 1 addition & 1 deletion packages/account-abstraction-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"@safe-global/relay-kit": "^1.3.0",
"@safe-global/safe-core-sdk-types": "^2.3.0",
"ethereumjs-util": "^7.1.5",
"ethers": "^5.7.2"
"ethers": "^6.7.1"
}
}
2 changes: 1 addition & 1 deletion packages/api-kit/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@nomiclabs/hardhat-ethers'
import '@nomicfoundation/hardhat-ethers'
import '@nomiclabs/hardhat-web3'
import dotenv from 'dotenv'
import { HardhatUserConfig, HttpNetworkUserConfig } from 'hardhat/types'
Expand Down
5 changes: 3 additions & 2 deletions packages/api-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
],
"homepage": "https://github.com/safe-global/safe-core-sdk#readme",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomicfoundation/hardhat-ethers": "^3.0.4 ",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@types/chai": "^4.3.5",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
"@types/node-fetch": "^2.6.6",
"@types/sinon-chai": "^3.2.9",
"@types/yargs": "^16.0.1",
"chai": "^4.3.7",
Expand All @@ -54,7 +55,7 @@
"yargs": "^17.7.2"
},
"dependencies": {
"@ethersproject/abstract-signer": "^5.7.0",
"ethers": "^6.7.1",
"@safe-global/protocol-kit": "^1.3.0",
"@safe-global/safe-core-sdk-types": "^2.3.0",
"node-fetch": "^2.6.6"
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/src/types/safeTransactionServiceTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Signer } from 'ethers'
import {
SafeMultisigTransactionResponse,
SafeTransactionData
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/tests/e2e/addSafeDelegate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Signer } from 'ethers'
import SafeApiKit, { AddSafeDelegateProps } from '@safe-global/api-kit/index'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/tests/e2e/getSafeDelegates.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Signer } from 'ethers'
import SafeApiKit, { DeleteSafeDelegateProps } from '@safe-global/api-kit/index'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/tests/e2e/removeSafeDelegate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Signer } from 'ethers'
import SafeApiKit, { DeleteSafeDelegateProps } from '@safe-global/api-kit/index'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
Expand Down
3 changes: 1 addition & 2 deletions packages/api-kit/tests/endpoint/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getDefaultProvider } from '@ethersproject/providers'
import { Wallet } from '@ethersproject/wallet'
import { getDefaultProvider, Wallet } from 'ethers'
import SafeApiKit, {
AddSafeDelegateProps,
DeleteSafeDelegateProps,
Expand Down
16 changes: 11 additions & 5 deletions packages/api-kit/tests/utils/setupEthAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Provider } from '@ethersproject/providers'
import { AbstractSigner, Provider } from 'ethers'
import { EthAdapter } from '@safe-global/safe-core-sdk-types'
import {
EthersAdapter,
Expand All @@ -9,13 +8,20 @@ import {
} from '@safe-global/protocol-kit'
import { ethers, web3 } from 'hardhat'

export async function getEthAdapter(signerOrProvider: Signer | Provider): Promise<EthAdapter> {
export async function getEthAdapter(
signerOrProvider: AbstractSigner | Provider
): Promise<EthAdapter> {
let ethAdapter: EthAdapter
switch (process.env.ETH_LIB) {
case 'web3':
const signerAddress =
signerOrProvider instanceof Signer ? await signerOrProvider.getAddress() : undefined
const web3AdapterConfig: Web3AdapterConfig = { web3: web3 as any, signerAddress }
signerOrProvider instanceof AbstractSigner ? await signerOrProvider.getAddress() : undefined

const web3AdapterConfig: Web3AdapterConfig = {
web3,
signerAddress
}

ethAdapter = new Web3Adapter(web3AdapterConfig)
break
case 'ethers':
Expand Down
3 changes: 1 addition & 2 deletions packages/api-kit/tests/utils/setupServiceClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getDefaultProvider } from '@ethersproject/providers'
import { Wallet } from '@ethersproject/wallet'
import { getDefaultProvider, Wallet } from 'ethers'
import { EthAdapter } from '@safe-global/safe-core-sdk-types'
import SafeApiKit from '@safe-global/api-kit/index'
import config from '../utils/config'
Expand Down
1 change: 1 addition & 0 deletions packages/auth-kit/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@emotion/styled": "^11.10.5",
"@mui/material": "^5.11.7",
"@safe-global/safe-react-components": "^2.0.1",
"ethers": "^6.7.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
40 changes: 40 additions & 0 deletions packages/auth-kit/example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@adraffy/[email protected]":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@adraffy/ens-normalize/-/ens-normalize-1.10.0.tgz#d2a39395c587e092d77cbbc80acf956a54f38bf7"
integrity sha512-nA9XHtlAkYfJxY7bce8DcN7eKxWWCWkU+1GR9d+U6MbNpfwQp8TI7vqOsBsMcHoT4mBu2kypKoSKnghEzOOq5Q==

"@ampproject/remapping@^2.1.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d"
Expand Down Expand Up @@ -628,6 +633,18 @@
prop-types "^15.8.1"
react-is "^18.2.0"

"@noble/[email protected]":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
dependencies:
"@noble/hashes" "1.3.2"

"@noble/[email protected]":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@popperjs/core@^2.11.6":
version "2.11.6"
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.6.tgz#cee20bd55e68a1720bdab363ecf0c821ded4cd45"
Expand Down Expand Up @@ -676,6 +693,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2"
integrity sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ==

"@types/[email protected]":
version "18.15.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.13.tgz#f64277c341150c979e42b00e4ac289290c9df469"
integrity sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==

"@types/parse-json@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
Expand Down Expand Up @@ -732,6 +754,11 @@
magic-string "^0.27.0"
react-refresh "^0.14.0"

[email protected]:
version "4.0.0-beta.5"
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-4.0.0-beta.5.tgz#8d2452c52adedebc3a3e28465d858c11ca315873"
integrity sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==

ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
Expand Down Expand Up @@ -924,6 +951,19 @@ ethereum-blockies-base64@^1.0.2:
dependencies:
pnglib "0.0.1"

ethers@^6.7.1:
version "6.8.0"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-6.8.0.tgz#0a26f57e96fd697cefcfcef464e0c325689d1daf"
integrity sha512-zrFbmQRlraM+cU5mE4CZTLBurZTs2gdp2ld0nG/f3ecBK+x6lZ69KSxBqZ4NjclxwfTxl5LeNufcBbMsTdY53Q==
dependencies:
"@adraffy/ens-normalize" "1.10.0"
"@noble/curves" "1.2.0"
"@noble/hashes" "1.3.2"
"@types/node" "18.15.13"
aes-js "4.0.0-beta.5"
tslib "2.4.0"
ws "8.5.0"

find-root@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/find-root/-/find-root-1.1.0.tgz#abcfc8ba76f708c42a97b3d685b7e9450bfb9ce4"
Expand Down
5 changes: 2 additions & 3 deletions packages/auth-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@
},
"devDependencies": {
"eth-testing": "^1.14.0",
"ethers": "^5.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@safe-global/api-kit": "^1.3.0",
"@safe-global/protocol-kit": "^1.3.0",
"@walletconnect/client": "^1.8.0",
"@web3auth/base": "^4.3.0",
"@web3auth/modal": "^4.3.1",
"@web3auth/openlogin-adapter": "^4.3.0"
"@web3auth/openlogin-adapter": "^4.3.0",
"ethers": "^6.7.1"
}
}
15 changes: 8 additions & 7 deletions packages/auth-kit/src/AuthKitBasePack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Web3Provider } from '@ethersproject/providers'
import { ethers, Eip1193Provider } from 'ethers'
import SafeApiKit from '@safe-global/api-kit'

import type { AuthKitEthereumProvider, AuthKitSignInData } from './types'
import type { AuthKitSignInData } from './types'

export abstract class AuthKitBasePack {
safeAuthData?: AuthKitSignInData
Expand All @@ -27,7 +27,7 @@ export abstract class AuthKitBasePack {
* Get the provider instance based on the pack
* @returns The provider instance
*/
abstract getProvider(): AuthKitEthereumProvider | null
abstract getProvider(): Eip1193Provider | null

/**
* Get the user info from the provider
Expand Down Expand Up @@ -79,8 +79,9 @@ export abstract class AuthKitBasePack {
throw new Error('Provider is not defined')
}

const ethersProvider = new Web3Provider(authKitProvider)
const signer = ethersProvider.getSigner()
const ethersProvider = new ethers.BrowserProvider(authKitProvider)

const signer = await ethersProvider.getSigner()

return signer.getAddress()
}
Expand All @@ -92,11 +93,11 @@ export abstract class AuthKitBasePack {
throw new Error('Provider is not defined')
}

const ethersProvider = new Web3Provider(authKitProvider)
const ethersProvider = new ethers.BrowserProvider(authKitProvider)

const networkDetails = await ethersProvider.getNetwork()

return networkDetails.chainId
return Number(networkDetails.chainId)
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/auth-kit/src/packs/web3auth/Web3AuthModalPack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IAdapter, SafeEventEmitterProvider, UserInfo } from '@web3auth/base'
import { ModalConfig, Web3Auth, Web3AuthOptions } from '@web3auth/modal'
import { Eip1193Provider } from 'ethers'

import { getErrorMessage } from '@safe-global/auth-kit/lib/errors'
import { Web3AuthConfig, Web3AuthEvent, Web3AuthEventListener } from './types'
import { AuthKitBasePack } from '@safe-global/auth-kit/AuthKitBasePack'
import type { AuthKitEthereumProvider, AuthKitSignInData } from '@safe-global/auth-kit/types'
import type { AuthKitSignInData } from '@safe-global/auth-kit/types'

/**
* Web3AuthModalPack implements the SafeAuthClient interface for adapting the Web3Auth service provider
Expand Down Expand Up @@ -77,8 +78,8 @@ export class Web3AuthModalPack extends AuthKitBasePack {
return signInData
}

getProvider(): AuthKitEthereumProvider | null {
return this.#provider as AuthKitEthereumProvider
getProvider(): Eip1193Provider | null {
return this.#provider as Eip1193Provider
}

/**
Expand Down
6 changes: 0 additions & 6 deletions packages/auth-kit/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { ExternalProvider } from '@ethersproject/providers'

// We want to use a specific auth-kit type and avoid to return external library types in the abstract base pack class
// If we decide to change the external library, we will only need to change the type in this file
export type AuthKitEthereumProvider = ExternalProvider

export type AuthKitSignInData = {
eoa: string
safes?: string[]
Expand Down
2 changes: 1 addition & 1 deletion packages/onramp-kit/example/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@safe-global/safe-react-components": "^2.0.5",
"@web3auth/modal": "^4.3.1",
"@web3auth/openlogin-adapter": "^4.3.0",
"ethers": "^5.7.0",
"ethers": "^6.7.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.14.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Stripe() {
const stripeRootRef = useRef<HTMLDivElement>(null)

const handleCreateSession = async () => {
if (!isSessionValid(sessionId) && !ethers.utils.isAddress(walletAddress)) return
if (!isSessionValid(sessionId) && !ethers.isAddress(walletAddress)) return

if (stripeRootRef.current) {
stripeRootRef.current.innerHTML = ''
Expand Down
5 changes: 1 addition & 4 deletions packages/onramp-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
"access": "public"
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/hash": "^5.7.0",
"ethers": "^6.7.1",
"@monerium/sdk": "^2.3.0",
"@safe-global/api-kit": "^1.3.0",
"@safe-global/protocol-kit": "^1.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hashMessage } from '@ethersproject/hash'
import { hashMessage } from 'ethers'
import { Currency, PaymentStandard } from '@monerium/sdk'
import Safe, * as protocolKit from '@safe-global/protocol-kit'
import { OperationType } from '@safe-global/safe-core-sdk-types'
Expand Down
10 changes: 5 additions & 5 deletions packages/onramp-kit/src/packs/monerium/SafeMoneriumClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hashMessage } from '@ethersproject/hash'
import { arrayify } from '@ethersproject/bytes'
import { hashMessage, getBytes } from 'ethers'

import { Chain, IBAN, MoneriumClient, Networks, NewOrder, OrderKind } from '@monerium/sdk'
import Safe, { getSignMessageLibContract } from '@safe-global/protocol-kit'
import SafeApiKit from '@safe-global/api-kit'
Expand Down Expand Up @@ -39,7 +39,7 @@ export class SafeMoneriumClient extends MoneriumClient {
* @returns The Safe address
*/
async getSafeAddress(): Promise<string> {
return this.#safeSdk.getAddress()
return await this.#safeSdk.getAddress()
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ export class SafeMoneriumClient extends MoneriumClient {

const safeTransaction = await this.#safeSdk.createTransaction({
safeTransactionData: {
to: signMessageContract.getAddress(),
to: await signMessageContract.getAddress(),
value: '0',
data: txData,
operation: OperationType.DelegateCall
Expand Down Expand Up @@ -206,7 +206,7 @@ export class SafeMoneriumClient extends MoneriumClient {
messageHash,
'0x'
])
const msgBytes = arrayify(messageHash)
const msgBytes = getBytes(messageHash)

const eip1271BytesData = EIP_1271_BYTES_INTERFACE.encodeFunctionData('isValidSignature', [
msgBytes,
Expand Down
2 changes: 1 addition & 1 deletion packages/onramp-kit/src/packs/monerium/signatures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Interface } from '@ethersproject/abi'
import { Interface } from 'ethers'

// EIP-1271 magic values (https://eips.ethereum.org/EIPS/eip-1271)
// The four-byte code is defined as follows:
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The following steps show how to set up the Protocol Kit, deploy a new Safe, crea

### 1. Instantiate an EthAdapter

First of all, we need to create an `EthAdapter`, which contains all the required utilities for the SDKs to interact with the blockchain. It acts as a wrapper for [web3.js](https://web3js.readthedocs.io/) or [ethers.js](https://docs.ethers.io/v5/) Ethereum libraries.
First of all, we need to create an `EthAdapter`, which contains all the required utilities for the SDKs to interact with the blockchain. It acts as a wrapper for [web3.js](https://web3js.readthedocs.io/) or [ethers.js](https://docs.ethers.org/v6/) Ethereum libraries.

Depending on the library used by the Dapp, there are two options:

Expand Down
Loading

0 comments on commit 7496d7b

Please sign in to comment.