Skip to content

Commit

Permalink
hide activate button if no config set up for chain
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Apr 18, 2024
1 parent 32c0568 commit c904e2c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
17 changes: 13 additions & 4 deletions src/components/SettingsTreasuriesBlockItemButton.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { TreasuryWallet } from '@/helpers/interfaces';
import { Network } from '@/plugins/oSnap/types';
import { getIsOsnapEnabled } from '@/plugins/oSnap/utils/getters';
import { ConfigError, getIsOsnapEnabled } from '@/plugins/oSnap/utils/getters';
const props = defineProps<{
treasury: TreasuryWallet;
Expand All @@ -17,13 +17,21 @@ const emit = defineEmits<{
}>();
const isOsnapEnabled = ref(false);
const isChainSupported = ref(true);
async function updateIsOsnapEnabled() {
if (!props.hasOsnapPlugin) return;
isOsnapEnabled.value = await getIsOsnapEnabled(
const isEnabled = await getIsOsnapEnabled(
props.treasury.network as Network,
props.treasury.address
);
).catch(e => {
if (e instanceof ConfigError) {
isChainSupported.value = false;
return false;
}
return false;
});
isOsnapEnabled.value = isEnabled;
}
onMounted(async () => {
Expand All @@ -47,12 +55,13 @@ onUnmounted(() => {
</div>
<div class="ml-auto mr-3">
<SettingsTreasuryActivateOsnapButton
v-show="hasOsnapPlugin"
v-if="hasOsnapPlugin && isChainSupported"
:is-osnap-enabled="isOsnapEnabled"
@click.stop="
!isViewOnly && emit('configureOsnap', treasuryIndex, isOsnapEnabled)
"
/>
<div v-else>Unsupported chain</div>
</div>
<BaseButtonIcon
v-show="!isViewOnly"
Expand Down
52 changes: 30 additions & 22 deletions src/plugins/oSnap/utils/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
} from '../types';
import { getPagedEvents } from './events';
import { shortenAddress, toChecksumAddress } from '@/helpers/utils';
import app from '../../../main';

/**
* Calls the Gnosis Safe Transaction API
Expand Down Expand Up @@ -100,6 +99,17 @@ function getDeployBlock(params: { network: Network; name: string }): number {
return 0;
}

export class ConfigError extends Error {
constructor(message: string, responsibleVar: string) {
super(message);
this.name = 'CONFIG_ERROR';
}
}

export function logIfErrorMessage(e: unknown, overrideMessage: string) {
console.error(e instanceof Error ? e.message : overrideMessage);
}

/**
* Fetches the subgraph url for a given contract on a given network.
*/
Expand All @@ -109,17 +119,17 @@ function getContractSubgraph(params: { network: Network; name: string }) {
contract.network === params.network && contract.name === params.name
);
if (results.length > 1)
throw new Error(
`Too many results finding ${params.name} subgraph on network ${params.network}`
throw new ConfigError(
`Too many results finding ${params.name} subgraph on network ${params.network}`,
'subgraph'
);
if (results.length < 1)
throw new Error(
`No results finding ${params.name} subgraph on network ${params.network}`
);
if (!results[0].subgraph)
throw new Error(
`No subgraph url defined for ${params.name} on network ${params.network}`

if (results.length < 1 || !results[0].subgraph)
throw new ConfigError(
`No subgraph url defined for ${params.name} on network ${params.network}`,
'subgraph'
);

return results[0].subgraph;
}

Expand Down Expand Up @@ -199,12 +209,11 @@ export const getModuleAddressForTreasury = async (
const result = await queryGql<Result>(subgraph, query);
return result?.safe?.optimisticGovernor?.id ?? '';
} catch (error) {
console.error(
error instanceof Error
? error.message
: `Unable to get module address for treasury ${shortenAddress(
treasuryAddress
)} on network ${network}`
logIfErrorMessage(
error,
`Unable to get module address for treasury ${shortenAddress(
treasuryAddress
)} on network ${network}`
);

throw error;
Expand Down Expand Up @@ -233,12 +242,11 @@ export const getIsOsnapEnabled = async (
const result = await queryGql<Result>(subgraph, query);
return result?.safe?.isOptimisticGovernorEnabled ?? false;
} catch (error) {
console.error(
error instanceof Error
? error.message
: `Unable to check if oSnap is enable for address ${shortenAddress(
safeAddress
)} on network ${network}`
logIfErrorMessage(
error,
`Unable to check if oSnap is enable for address ${shortenAddress(
safeAddress
)} on network ${network}`
);
throw error;
}
Expand Down

0 comments on commit c904e2c

Please sign in to comment.