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): Add Abitype for Safe Contract 1.3.0 + Web3.js #633

Merged
12 changes: 6 additions & 6 deletions packages/protocol-kit/scripts/generateTypechainFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const safeContracts_V1_4_1 = [
`${safeContractsPath}/v1.4.1/simulate_tx_accessor.json`
].join(' ')
const safeContracts_V1_3_0 = [
`${safeContractsPath}/v1.3.0/gnosis_safe.json`,
// `${safeContractsPath}/v1.3.0/gnosis_safe.json`, // Remove contract 1.3.0 from typechain as it's migrated to Abitype
`${safeContractsPath}/v1.3.0/proxy_factory.json`,
`${safeContractsPath}/v1.3.0/multi_send.json`,
`${safeContractsPath}/v1.3.0/multi_send_call_only.json`,
Expand Down Expand Up @@ -103,15 +103,15 @@ function generateTypes(typechainTarget: string) {
generateTypechainFiles(
typechainTarget,
`${outDirSrc}${typechainTarget}/v1.4.1`,
safeContracts_V1_4_1
// Remove Safe Contract v1.4.1 for web3-v1
typechainTarget === 'web3-v1'
? safeContracts_V1_4_1.replace(`${safeContractsPath}/v1.4.1/safe.json `, '')
: safeContracts_V1_4_1
)
generateTypechainFiles(
typechainTarget,
`${outDirSrc}${typechainTarget}/v1.3.0`,
// removed Safe Contract v1.3.0 for ethers-v6
typechainTarget === 'ethers-v6'
? safeContracts_V1_3_0.replace(`${safeContractsPath}/v1.3.0/gnosis_safe.json `, '')
: safeContracts_V1_3_0
safeContracts_V1_3_0
)
generateTypechainFiles(
typechainTarget,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SafeContract_v1_3_0_Ethers
return this.contract.interface.encodeFunctionData(functionToEncode, args)
}

estimateGas: EstimateGasSafeFunction<SafeContract_v1_3_0_Abi> = (
estimateGas: EstimateGasSafeFunction<SafeContract_v1_3_0_Abi, EthersTransactionOptions> = (
functionToEstimate,
args,
options = {}
Expand Down
14 changes: 11 additions & 3 deletions packages/protocol-kit/src/adapters/web3/Web3Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,27 @@ class Web3Adapter implements EthAdapter {
safeVersion,
singletonDeployment,
customContractAddress,
customContractAbi
customContractAbi,
isL1SafeSingleton
}: GetContractProps): Promise<SafeContractWeb3> {
const chainId = await this.getChainId()
const contractAddress =
customContractAddress ?? singletonDeployment?.networkAddresses[chainId.toString()]
if (!contractAddress) {
throw new Error('Invalid SafeProxy contract address')
}
const safeContract = this.getContract(
const safeSingletonContract = this.getContract(
contractAddress,
customContractAbi ?? (singletonDeployment?.abi as AbiItem[])
)
return getSafeContractInstance(safeVersion, safeContract)
return getSafeContractInstance(
safeVersion,
safeSingletonContract,
contractAddress,
this,
customContractAbi,
isL1SafeSingleton
)
}

async getSafeProxyFactoryContract({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Contract from 'web3-eth-contract'
import { AbiItem } from 'web3-utils'

import Web3Adapter from '@safe-global/protocol-kit/adapters/web3/Web3Adapter'
import { SafeVersion } from 'packages/safe-core-sdk-types'
import SafeBaseContract from '@safe-global/protocol-kit/adapters/SafeBaseContract'

/**
* Abstract class SafeBaseContractWeb3 extends SafeBaseContract to specifically integrate with the Web3.js library.
* It is designed to be instantiated for different versions of the Safe contract.
*
* This abstract class sets up the Web3.js Contract object that interacts with a Safe contract version.
*
* Subclasses of SafeBaseContractWeb3 are expected to represent specific versions of the Safe contract.
*
* @template SafeContractAbiType - The ABI type specific to the version of the Safe contract, extending AbiItem.
* @extends SafeBaseContract<SafeContractAbiType> - Extends the generic SafeBaseContract with Web3-specific implementation.
*
* Example subclasses:
* - SafeContract_v1_4_1_Web3 extends SafeBaseContractWeb3<SafeContract_v1_4_1_Abi>
* - SafeContract_v1_3_0_Web3 extends SafeBaseContractWeb3<SafeContract_v1_3_0_Abi>
* - SafeContract_v1_2_0_Web3 extends SafeBaseContractWeb3<SafeContract_v1_2_0_Abi>
* - SafeContract_v1_1_1_Web3 extends SafeBaseContractWeb3<SafeContract_v1_1_1_Abi>
* - SafeContract_v1_0_0_Web3 extends SafeBaseContractWeb3<SafeContract_v1_0_0_Abi>
*/
abstract class SafeBaseContractWeb3<
SafeContractAbiType extends AbiItem[]
> extends SafeBaseContract<SafeContractAbiType> {
contract: Contract
adapter: Web3Adapter

/**
* @constructor
* Constructs an instance of SafeBaseContractWeb3.
*
* @param chainId - The chain ID of the contract.
* @param web3Adapter - An instance of Web3Adapter.
* @param defaultAbi - The default ABI for the Safe contract. It should be compatible with the specific version of the Safe contract.
* @param safeVersion - The version of the Safe contract.
* @param isL1SafeSingleton - A flag indicating if the contract is a L1 Safe Singleton.
* @param customContractAddress - Optional custom address for the contract. If not provided, the address is derived from the Safe deployments based on the chainId and safeVersion.
* @param customContractAbi - Optional custom ABI for the contract. If not provided, the ABI is derived from the Safe deployments or the defaultAbi is used.
*/
constructor(
chainId: bigint,
web3Adapter: Web3Adapter,
defaultAbi: SafeContractAbiType,
safeVersion: SafeVersion,
isL1SafeSingleton = false,
customContractAddress?: string,
customContractAbi?: SafeContractAbiType
) {
super(
chainId,
defaultAbi,
safeVersion,
isL1SafeSingleton,
customContractAddress,
customContractAbi
)

this.adapter = web3Adapter
this.contract = web3Adapter.getContract(this.contractAddress, this.contractAbi)
}
}

export default SafeBaseContractWeb3
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { toTxResult } from '@safe-global/protocol-kit/adapters/web3/utils'
import { Gnosis_safe as Safe_V1_0_0 } from '@safe-global/protocol-kit/typechain/src/web3-v1/v1.0.0/Gnosis_safe'
import { Gnosis_safe as Safe_V1_1_1 } from '@safe-global/protocol-kit/typechain/src/web3-v1/v1.1.1/Gnosis_safe'
import { Gnosis_safe as Safe_V1_2_0 } from '@safe-global/protocol-kit/typechain/src/web3-v1/v1.2.0/Gnosis_safe'
import { Gnosis_safe as Safe_V1_3_0 } from '@safe-global/protocol-kit/typechain/src/web3-v1/v1.3.0/Gnosis_safe'
import { Safe as Safe_V1_4_1 } from '@safe-global/protocol-kit/typechain/src/web3-v1/v1.4.1/Safe'
import {
SafeContract,
SafeSetupConfig,
Expand All @@ -17,9 +15,7 @@ import {
} from '@safe-global/safe-core-sdk-types'

abstract class SafeContractWeb3 implements SafeContract {
constructor(
public contract: Safe_V1_4_1 | Safe_V1_3_0 | Safe_V1_2_0 | Safe_V1_1_1 | Safe_V1_0_0
) {}
constructor(public contract: Safe_V1_2_0 | Safe_V1_1_1 | Safe_V1_0_0) {}

abstract setup(
setupConfig: SafeSetupConfig,
Expand Down

This file was deleted.

Loading