-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2750 from decentdao/release/v0.8.1
Release/v0.8.1
- Loading branch information
Showing
6 changed files
with
121 additions
and
119 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,110 @@ | ||
import { | ||
getSafeL2SingletonDeployment, | ||
getSafeSingletonDeployment, | ||
} from '@safe-global/safe-deployments'; | ||
import { useEffect, useState } from 'react'; | ||
import { isAddress } from 'viem'; | ||
import { useSafeAPI } from '../../providers/App/hooks/useSafeAPI'; | ||
|
||
/** | ||
* A hook which determines whether the provided Ethereum address is a Safe | ||
* smart contract address on the currently connected chain (chainId). | ||
* | ||
* The state can be either true/false or undefined, if a network call is currently | ||
* being performed to determine that status. | ||
* | ||
* @param address the address to check | ||
* @returns isSafe: whether the address is a Safe, | ||
* isSafeLoading: true/false whether the isSafe status is still being determined | ||
*/ | ||
import { Address, createPublicClient, http, isAddress, toHex } from 'viem'; | ||
import { getSafeContractDeploymentAddress } from '../../providers/NetworkConfig/networks/utils'; | ||
import { useNetworkConfigStore } from '../../providers/NetworkConfig/useNetworkConfigStore'; | ||
import { NetworkConfig } from '../../types/network'; | ||
|
||
const safeVersions = ['1.3.0', '1.4.1']; | ||
|
||
const safe130bytecode = | ||
'0x608060405273ffffffffffffffffffffffffffffffffffffffff600054167fa619486e0000000000000000000000000000000000000000000000000000000060003514156050578060005260206000f35b3660008037600080366000845af43d6000803e60008114156070573d6000fd5b3d6000f3fea2646970667358221220d1429297349653a4918076d650332de1a1068c5f3e07c5c82360c277770b955264736f6c63430007060033'; | ||
const safe141bytecode = | ||
'0x608060405273ffffffffffffffffffffffffffffffffffffffff600054167fa619486e0000000000000000000000000000000000000000000000000000000060003514156050578060005260206000f35b3660008037600080366000845af43d6000803e60008114156070573d6000fd5b3d6000f3fea264697066735822122003d1488ee65e08fa41e58e888a9865554c535f2c77126a82cb4c0f917f31441364736f6c63430007060033'; | ||
|
||
function getSafeSingleton(chainId: number, safeVersion: string): string | undefined { | ||
try { | ||
const singleton = getSafeContractDeploymentAddress( | ||
getSafeSingletonDeployment, | ||
safeVersion, | ||
chainId.toString(), | ||
); | ||
return singleton.toLowerCase().replace('0x', ''); | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
|
||
function getSafeL2Singleton(chainId: number, safeVersion: string): string | undefined { | ||
try { | ||
const singleton = getSafeContractDeploymentAddress( | ||
getSafeL2SingletonDeployment, | ||
safeVersion, | ||
chainId.toString(), | ||
); | ||
return singleton.toLowerCase().replace('0x', ''); | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
|
||
function getSafeSingletons(chainId: number): string[] { | ||
const safeSingletons = safeVersions | ||
.map(version => getSafeSingleton(chainId, version)) | ||
.filter(singleton => singleton != undefined); | ||
const safeL2Singletons = safeVersions | ||
.map(version => getSafeL2Singleton(chainId, version)) | ||
.filter(singleton => singleton != undefined); | ||
return [safeSingletons, safeL2Singletons].flat(); | ||
} | ||
|
||
export async function getIsSafe(address: Address, networkConfig: NetworkConfig): Promise<boolean> { | ||
try { | ||
const publicClient = createPublicClient({ | ||
chain: networkConfig.chain, | ||
transport: http(networkConfig.rpcEndpoint), | ||
}); | ||
|
||
const bytecode = (await publicClient.getBytecode({ address: address }))?.toLowerCase(); | ||
if (bytecode != safe130bytecode && bytecode != safe141bytecode) { | ||
return false; | ||
} | ||
|
||
const store = await publicClient.getStorageAt({ | ||
address: address, | ||
slot: toHex(0), | ||
}); | ||
|
||
if (store === undefined) { | ||
return false; | ||
} | ||
|
||
// We have the bytecode, let's just find if any of the single addresses are there | ||
const safeSingletons = getSafeSingletons(networkConfig.chain.id); | ||
return safeSingletons.some(singleton => store === `0x000000000000000000000000${singleton}`); | ||
} catch (error) { | ||
console.log(error); | ||
return false; | ||
} | ||
} | ||
|
||
export const useIsSafe = (address: string | undefined) => { | ||
const [isSafeLoading, setSafeLoading] = useState<boolean>(false); | ||
const [isSafe, setIsSafe] = useState<boolean | undefined>(); | ||
const safeAPI = useSafeAPI(); | ||
|
||
// currently connected chain | ||
const { chain, getConfigByChainId } = useNetworkConfigStore(); | ||
const networkConfig = getConfigByChainId(chain.id); | ||
|
||
useEffect(() => { | ||
setSafeLoading(true); | ||
setIsSafe(undefined); | ||
|
||
if (!address || !isAddress(address) || !safeAPI) { | ||
if (!address || !isAddress(address)) { | ||
setIsSafe(false); | ||
setSafeLoading(false); | ||
return; | ||
} | ||
|
||
safeAPI | ||
.getSafeCreationInfo(address) | ||
getIsSafe(address, networkConfig) | ||
.then(setIsSafe) | ||
.then(() => setIsSafe(true)) | ||
.catch(() => setIsSafe(false)) | ||
.finally(() => setSafeLoading(false)); | ||
}, [address, safeAPI]); | ||
}, [address, networkConfig]); | ||
|
||
return { isSafe, isSafeLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters