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): Refactor and improve contract classes for Abitype #758

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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

This file was deleted.

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