import { BigNumber } from '@ethersproject/bignumber' ; import { SafeVersion } from '../contracts/config' ; import { getGnosisSafeProxyFactoryContractInstance , getMultiSendContractInstance , getSafeContractInstance } from '../contracts/contractInstancesWeb3' ; import GnosisSafeContractWeb3 from '../contracts/GnosisSafe/GnosisSafeContractWeb3' ; import GnosisSafeProxyFactoryWeb3Contract from '../contracts/GnosisSafeProxyFactory/GnosisSafeProxyFactoryWeb3Contract' ; import MultiSendWeb3Contract from '../contracts/MultiSend/MultiSendWeb3Contract' ; import { getMultiSendContractDeployment , getSafeContractDeployment , getSafeProxyFactoryContractDeployment } from '../contracts/safeDeploymentContracts' ; import { AbiItem } from '../types' ; import EthAdapter , { EthAdapterTransaction , GetSafeContractProps } from './EthAdapter' ; export interface Web3AdapterConfig { /** web3 - Web3 library */ web3 : any ; /** signerAddress - Address of the signer */ signerAddress : string ; } class Web3Adapter implements EthAdapter { #web3 : any ; #signerAddress : string ; constructor ( { web3 , signerAddress } ) { if ( ! web3 ) { throw new Error ( 'web3 property missing from options' ) ; } this . #web3 = web3 ; this . #signerAddress = signerAddress ; } isAddress ( address ) { return this . #web3 . utils . isAddress ( address ) ; } async getBalance ( address ) { return BigNumber . from ( await this . #web3 . eth . getBalance ( address ) ) ; } async getChainId ( ) { return this . #web3 . eth . getChainId ( ) ; } getSafeContract ( { safeVersion , chainId , isL1SafeMasterCopy , customContractAddress } ) { const safeSingletonDeployment = getSafeContractDeployment ( safeVersion , chainId , isL1SafeMasterCopy ) ; const contractAddress = customContractAddress ?? safeSingletonDeployment ?. networkAddresses [ chainId ] ; if ( ! contractAddress ) { throw new Error ( 'Invalid Safe Proxy contract address' ) ; } const safeContract = this . getContract ( contractAddress , safeSingletonDeployment ?. abi as AbiItem [ ] ) ; return getSafeContractInstance ( safeVersion , safeContract ) ; } getMultiSendContract ( safeVersion , chainId , customContractAddress ? ) { const multiSendDeployment = getMultiSendContractDeployment ( safeVersion , chainId ) ; const contractAddress = customContractAddress ?? multiSendDeployment ?. networkAddresses [ chainId ] ; if ( ! contractAddress ) { throw new Error ( 'Invalid Multi Send contract addresss' ) ; } const multiSendContract = this . getContract ( contractAddress , multiSendDeployment ?. abi as AbiItem [ ] ) ; return getMultiSendContractInstance ( safeVersion , multiSendContract ) ; } getSafeProxyFactoryContract ( safeVersion , chainId , customContractAddress ? ) { const proxyFactoryDeployment = getSafeProxyFactoryContractDeployment ( safeVersion , chainId ) ; const contractAddress = customContractAddress ?? proxyFactoryDeployment ?. networkAddresses [ chainId ] ; if ( ! contractAddress ) { throw new Error ( 'Invalid Safe Proxy Factory contract address' ) ; } const proxyFactoryContract = this . getContract ( contractAddress , proxyFactoryDeployment ?. abi as AbiItem [ ] ) ; return getGnosisSafeProxyFactoryContractInstance ( safeVersion , proxyFactoryContract ) ; } getContract ( address , abi ) { return new this . #web3 . eth . Contract ( abi , address ) ; } async getContractCode ( address ) { return this . #web3 . eth . getCode ( address ) ; } async getTransaction ( transactionHash ) { return this . #web3 . eth . getTransaction ( transactionHash ) ; } async getSignerAddress ( ) { return this . #signerAddress ; } signMessage ( message ) { return this . #web3 . eth . sign ( message , this . #signerAddress ) ; } estimateGas ( transaction , options ? ) { return this . #web3 . eth . estimateGas ( transaction , options ) ; } call ( transaction ) { return this . #web3 . eth . call ( transaction ) ; } } export default Web3Adapter ;