Skip to content

Commit

Permalink
fix: migrate to published SDK (safe-global#4393)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook authored Oct 18, 2024
1 parent 6e5250c commit 72c04ca
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 144 deletions.
8 changes: 3 additions & 5 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect'
import { TextEncoder, TextDecoder } from 'util'
import { Headers, Request, Response } from 'node-fetch'
import { Request } from 'node-fetch'

jest.mock('@web3-onboard/coinbase', () => jest.fn())
jest.mock('@web3-onboard/injected-wallets', () => ({ ProviderLabel: { MetaMask: 'MetaMask' } }))
jest.mock('@web3-onboard/keystone/dist/index', () => jest.fn())
jest.mock('@web3-onboard/ledger/dist/index', () => jest.fn())
jest.mock('@web3-onboard/trezor', () => jest.fn())
jest.mock('@web3-onboard/walletconnect', () => jest.fn())
jest.mock('safe-client-gateway-sdk')
jest.mock('@safe-global/safe-client-gateway-sdk')

const mockOnboardState = {
chains: [],
Expand Down Expand Up @@ -71,7 +71,5 @@ Object.defineProperty(Uint8Array, Symbol.hasInstance, {
},
})

// These are required for safe-client-gateway-sdk
// These are required for @safe-global/safe-client-gateway-sdk
globalThis.Request = Request
globalThis.Response = Response
globalThis.Headers = Headers
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@safe-global/protocol-kit": "^4.1.1",
"@safe-global/safe-apps-sdk": "^9.1.0",
"@safe-global/safe-deployments": "^1.37.10",
"@safe-global/safe-client-gateway-sdk": "1.58.0-next-25bba61",
"@safe-global/safe-gateway-typescript-sdk": "3.22.3-beta.15",
"@safe-global/safe-modules-deployments": "^2.2.1",
"@sentry/react": "^7.91.0",
Expand Down Expand Up @@ -91,7 +92,6 @@
"react-hook-form": "7.41.1",
"react-papaparse": "^4.0.2",
"react-redux": "^9.1.2",
"safe-client-gateway-sdk": "git+https://github.com/safe-global/safe-client-gateway-sdk.git#v1.53.0-next-7344903",
"semver": "^7.6.3",
"zodiac-roles-deployments": "^2.2.5"
},
Expand Down
215 changes: 93 additions & 122 deletions src/features/multichain/hooks/__tests__/useSafeCreationData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PendingSafeStatus, type UndeployedSafe } from '@/store/slices'
import { PayMethod } from '@/features/counterfactual/PayNowPayLater'
import { chainBuilder } from '@/tests/builders/chains'
import * as sdk from '@/services/tx/tx-sender/sdk'
import * as cgwSdk from 'safe-client-gateway-sdk'
import * as cgwSdk from '@safe-global/safe-client-gateway-sdk'
import * as web3 from '@/hooks/wallets/web3'
import { encodeMultiSendData, type SafeProvider } from '@safe-global/protocol-kit'
import { Safe__factory, Safe_proxy_factory__factory } from '@/types/contracts'
Expand Down Expand Up @@ -210,10 +210,7 @@ describe('useSafeCreationData', () => {
})

it('should throw an error if creation data cannot be found', async () => {
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
response: new Response(),
data: undefined,
} as any)
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue(undefined as any)

const safeAddress = faker.finance.ethereumAddress()
const chainInfos = [chainBuilder().with({ chainId: '1', l2: false }).build()]
Expand All @@ -228,15 +225,13 @@ describe('useSafeCreationData', () => {

it('should throw an error if Safe creation data is incomplete', async () => {
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: null,
setupData: null,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: null,
setupData: null,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand All @@ -252,15 +247,13 @@ describe('useSafeCreationData', () => {

it('should throw an error if Safe setupData is empty', async () => {
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: faker.finance.ethereumAddress(),
setupData: '0x',
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: faker.finance.ethereumAddress(),
setupData: '0x',
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand All @@ -287,15 +280,13 @@ describe('useSafeCreationData', () => {
])

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.1.1' })?.defaultAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.1.1' })?.defaultAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand Down Expand Up @@ -326,15 +317,13 @@ describe('useSafeCreationData', () => {
])

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: faker.finance.ethereumAddress(),
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: faker.finance.ethereumAddress(),
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand Down Expand Up @@ -365,15 +354,13 @@ describe('useSafeCreationData', () => {
])

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand All @@ -400,15 +387,13 @@ describe('useSafeCreationData', () => {
])

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

const safeAddress = faker.finance.ethereumAddress()
Expand All @@ -435,15 +420,13 @@ describe('useSafeCreationData', () => {
])

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: faker.finance.ethereumAddress(),
transactionHash: faker.string.hexadecimal({ length: 64 }),
masterCopy: getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue(undefined)
Expand Down Expand Up @@ -476,15 +459,13 @@ describe('useSafeCreationData', () => {
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down Expand Up @@ -518,15 +499,13 @@ describe('useSafeCreationData', () => {
const mockFactoryAddress = faker.finance.ethereumAddress()
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress!
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down Expand Up @@ -583,15 +562,13 @@ describe('useSafeCreationData', () => {
const mockFactoryAddress = faker.finance.ethereumAddress()
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress!
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down Expand Up @@ -637,15 +614,13 @@ describe('useSafeCreationData', () => {
const mockFactoryAddress = faker.finance.ethereumAddress()
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress!
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down Expand Up @@ -701,15 +676,13 @@ describe('useSafeCreationData', () => {
const mockFactoryAddress = fakerChecksummedAddress()
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.3.0' })?.defaultAddress!
jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: fakerChecksummedAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: fakerChecksummedAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down Expand Up @@ -777,15 +750,13 @@ describe('useSafeCreationData', () => {
const mockMasterCopyAddress = getSafeSingletonDeployment({ version: '1.4.1' })?.defaultAddress!

jest.spyOn(cgwSdk, 'getCreationTransaction').mockResolvedValue({
data: {
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
},
response: new Response(),
created: new Date(Date.now()).toISOString(),
creator: faker.finance.ethereumAddress(),
factoryAddress: mockFactoryAddress,
transactionHash: mockTxHash,
masterCopy: mockMasterCopyAddress,
setupData,
saltNonce: faker.string.hexadecimal({ length: 64 }),
})

jest.spyOn(web3, 'createWeb3ReadOnly').mockReturnValue({
Expand Down
13 changes: 8 additions & 5 deletions src/features/multichain/hooks/useSafeCreationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createWeb3ReadOnly } from '@/hooks/wallets/web3'
import { type UndeployedSafe, selectRpc, type ReplayedSafeProps, selectUndeployedSafes } from '@/store/slices'
import { Safe__factory, Safe_proxy_factory__factory } from '@/types/contracts'
import { sameAddress } from '@/utils/addresses'
import { getCreationTransaction } from 'safe-client-gateway-sdk'
import { getCreationTransaction } from '@safe-global/safe-client-gateway-sdk'
import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'
import { useAppSelector } from '@/store'
import { determineMasterCopyVersion, isPredictedSafeProps } from '@/features/counterfactual/utils'
Expand Down Expand Up @@ -91,10 +91,12 @@ const getCreationDataForChain = async (
return undeployedCreationData
}

const { data: creation } = await getCreationTransaction({
path: {
chainId: chain.chainId,
safeAddress,
const creation = await getCreationTransaction({
params: {
path: {
chainId: chain.chainId,
safeAddress,
},
},
})

Expand Down Expand Up @@ -172,6 +174,7 @@ export const useSafeCreationData = (safeAddress: string, chains: ChainInfo[]): A

try {
const creationData = await getCreationDataForChain(chain, undeployedSafe, safeAddress, customRpc)
console.log({ creationData })
return creationData
} catch (err) {
lastError = asError(err)
Expand Down
Loading

0 comments on commit 72c04ca

Please sign in to comment.