Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol-kit): Migrate CompatibilityFallbackHandler contract to Abitype #747

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
dd5e412
Refactor SafeProxyFactory contract types
tmjssz Mar 27, 2024
60c0d2a
Merge branch 'Abitype-1_3_0-safe-contract' into Abitype-Compatibility…
tmjssz Mar 27, 2024
ff4a9c5
Define base type + class for CompatibilityFallbackHandler contract
tmjssz Mar 27, 2024
ef8d893
Create adapter-specific base classes for CompatibilityFallbackHandler…
tmjssz Mar 27, 2024
9aeed20
Define version-specific types for CompatibilityFallbackHandler contra…
tmjssz Mar 27, 2024
2e5a729
Implement CompatibilityFallbackHandler contract for Ethers.js
tmjssz Mar 27, 2024
ffd320f
Implement CompatibilityFallbackHandler contract for Web3.js
tmjssz Mar 28, 2024
1853e39
Remove CompatibilityFallbackHandler contract from typechain generatio…
tmjssz Mar 28, 2024
fa7e59a
Remove comment
tmjssz Mar 28, 2024
f31d624
Refactor CreateCallBaseContract type to use the common BaseContract type
tmjssz Mar 28, 2024
ee50e1e
Refactor MultiSendContract type to use the common BaseContract type
tmjssz Mar 28, 2024
4b57840
Refactor SafeBaseContract type to use the common BaseContract type
tmjssz Mar 28, 2024
8929c3d
Refactor SignMessageLibBaseContract type to use the common BaseContra…
tmjssz Mar 28, 2024
0e9e473
Refactor SimulateTxAccessorBaseContract type to use the common BaseCo…
tmjssz Mar 28, 2024
6763ce7
fix: typo
dasanra Apr 1, 2024
e42e429
Use AbiTypes in SignMessageLib and SimulateTxAccessor
dasanra Apr 2, 2024
f33a4d0
feat(protocol-kit): refactor and improve contract classes for Abitype…
tmjssz Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions packages/protocol-kit/scripts/generateTypechainFiles.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { execSync } from 'child_process'
import { existsSync, mkdirSync, readdir } from 'fs'
import path from 'path'

// Directories where the Typechain files will be generated
const outDirSrc = 'typechain/src/'
const typeChainDirectorySrcPath = path.join(__dirname, `../${outDirSrc}`)

const outDirBuild = 'dist/typechain/src/'
const typeChainDirectoryBuildPath = path.join(__dirname, `../${outDirBuild}`)

const outDirTests = 'typechain/tests/'

// Contract list for which the Typechain files will be generated
// Will be included in dist/ folder
const safeContractsPath = '../../node_modules/@safe-global/safe-deployments/dist/assets'

const safeContracts_V1_4_1 = [
`${safeContractsPath}/v1.4.1/compatibility_fallback_handler.json`
].join(' ')
const safeContracts_V1_3_0 = [
`${safeContractsPath}/v1.3.0/compatibility_fallback_handler.json`
].join(' ')

// Won't be included in dist/ folder
const safeContractsTestV1_4_1Path =
'../../node_modules/@safe-global/safe-contracts-v1.4.1/build/artifacts/contracts'
Expand Down Expand Up @@ -55,47 +38,7 @@ function generateTypechainFiles(
console.log(`Generated typechain ${typechainVersion} at ${outDir}`)
}

// Copy Typechain files with the right extension (.d.ts -> .ts) allows them to be included in the build folder
function moveTypechainFiles(inDir: string, outDir: string): void {
readdir(`${inDir}`, (error, files) => {
if (error) {
console.log(error)
}
if (!existsSync(`${outDir}`)) {
mkdirSync(`${outDir}`, { recursive: true })
}
files.forEach((file) => {
const pattern = /.d.ts/
if (!file.match(pattern)) {
return
}
execSync(`cp ${inDir}/${file} ${outDir}/${file}`)
})
})
}

// Contracts v1.0.0 + v1.1.1 + v1.2.0 are migrated to Abitype already, so they're not included in here
function generateTypes(typechainTarget: string) {
// Src
generateTypechainFiles(
typechainTarget,
`${outDirSrc}${typechainTarget}/v1.4.1`,
safeContracts_V1_4_1
)
generateTypechainFiles(
typechainTarget,
`${outDirSrc}${typechainTarget}/v1.3.0`,
safeContracts_V1_3_0
)
moveTypechainFiles(
`${typeChainDirectorySrcPath}${typechainTarget}/v1.4.1`,
`${typeChainDirectoryBuildPath}${typechainTarget}/v1.4.1`
)
moveTypechainFiles(
`${typeChainDirectorySrcPath}${typechainTarget}/v1.3.0`,
`${typeChainDirectoryBuildPath}${typechainTarget}/v1.3.0`
)

// Tests
generateTypechainFiles(
typechainTarget,
Expand Down
61 changes: 61 additions & 0 deletions packages/protocol-kit/src/adapters/BaseContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { contractName, getContractDeployment } from '@safe-global/protocol-kit/contracts/config'
import { SafeVersion } from '@safe-global/safe-core-sdk-types'

/**
* Abstract class BaseContract serves as a base for creating a contract for a specific adapter (Ethers.js, Web3.js, or viem.js)
* This class is designed to be extended by adapter-specific abstract classes, such as BaseContractEthers, BaseContractWeb3, and BaseContractViem.
* It includes the core logic for selecting the appropriate ABI and the address from contract deployments.
*
* @template ContractAbiType - The ABI associated with the contract.
*
* Example subclasses extending this base class:
* - BaseContractEthers<ContractAbiType> extends BaseContract<ContractAbiType>
* - BaseContractWeb3<ContractAbiType> extends BaseContract<ContractAbiType>
* - BaseContractViem<ContractAbiType> extends BaseContract<ContractAbiType>
*/
abstract class BaseContract<ContractAbiType> {
contractAbi: ContractAbiType
contractAddress: string

abstract contractName: contractName
abstract safeVersion: SafeVersion

abstract contract: unknown // This needs to be implemented for each adapter.
abstract adapter: unknown // This needs to be implemented for each adapter.

/**
* Constructs a new BaseContract instance.
*
* @param contractName - The contract name.
* @param chainId - The chain ID of the contract.
* @param defaultAbi - The hardcoded ABI of the contract.
* @param safeVersion - The version of the contract.
* @param customContractAddress - Optional custom address for the contract.
* @param customContractAbi - Optional custom ABI for the contract.
* @throws Will throw an error if the contract address is invalid.
*/
constructor(
contractName: contractName,
chainId: bigint,
defaultAbi: ContractAbiType,
safeVersion: SafeVersion,
customContractAddress?: string,
customContractAbi?: ContractAbiType
) {
const deployment = getContractDeployment(safeVersion, chainId, contractName)

const contractAddress = customContractAddress || deployment?.defaultAddress

if (!contractAddress) {
throw new Error('Invalid contract address')
}

this.contractAddress = contractAddress
this.contractAbi =
customContractAbi ||
(deployment?.abi as ContractAbiType) || // this cast is required because abi is set as any[] in safe-deployments
defaultAbi // if no customAbi and no abi is present in the safe-deployments we use our hardcoded abi
}
}

export default BaseContract
61 changes: 0 additions & 61 deletions packages/protocol-kit/src/adapters/CreateCallBaseContract.ts

This file was deleted.

61 changes: 0 additions & 61 deletions packages/protocol-kit/src/adapters/MultiSendBaseContract.ts

This file was deleted.

This file was deleted.

69 changes: 0 additions & 69 deletions packages/protocol-kit/src/adapters/SafeBaseContract.ts

This file was deleted.

Loading
Loading