Skip to content

Commit

Permalink
fix(safe): momoize request of getting code (snapshot-labs#4052)
Browse files Browse the repository at this point in the history
* fix(safe): momoize request of getting code

* fix(safe): add extra check for safe contracts
  • Loading branch information
Todmy authored Jul 10, 2023
1 parent be90667 commit 119ec38
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/composables/useGnosis.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { getInstance } from '@snapshot-labs/lock/plugins/vue3';
import { ExtendedSpace } from '@/helpers/interfaces';
import utils from '@snapshot-labs/snapshot.js/src/utils';
import { computedAsync } from '@vueuse/core';
import { computedAsync, useMemoize } from '@vueuse/core';
import { Contract } from '@ethersproject/contracts';

const defaultNetwork = import.meta.env.VITE_DEFAULT_NETWORK;

const getSafeVersion = useMemoize(
async (networkKey: string, account: string) => {
const provider = utils.getProvider(networkKey);
const code = await provider.getCode(account);

if (code === '0x') return undefined;

const abi = ['function VERSION() view returns (string)'];
const contract = new Contract(account, abi, provider);
return contract.VERSION([]);
}
);

export function useGnosis(space?: ExtendedSpace) {
const { web3 } = useWeb3();

Expand All @@ -15,11 +29,15 @@ export function useGnosis(space?: ExtendedSpace) {

const spaceNetworkKey = computed(() => space?.network);

const isContract = computedAsync(async () => {
const isSafeContract = computedAsync(async () => {
if (!web3.value.account) return false;
const provider = utils.getProvider(networkKey.value);
const code = await provider.getCode(web3.value.account);
return code !== '0x';

const safeVersion = await getSafeVersion(
networkKey.value,
web3.value.account
);

return typeof safeVersion === 'string';
}, false);

const isGnosisSafe = computed(
Expand All @@ -28,7 +46,7 @@ export function useGnosis(space?: ExtendedSpace) {
web3.value?.walletConnectType === 'WalletConnect Safe App' ||
web3.value?.walletConnectType === 'Den' ||
connectorName.value === 'gnosis' ||
isContract.value
isSafeContract.value
);

const isGnosisAndNotDefaultNetwork = computed(() => {
Expand Down

0 comments on commit 119ec38

Please sign in to comment.