Skip to content

Commit

Permalink
refactor: replace SUPPORTED objects with functions for more flexible …
Browse files Browse the repository at this point in the history
…access (#38)

* refactor: fallback to using strategy address on display

* refactor: expose helpers function to check if strategy is supported

Mapping objects are limiting us for off-chain network, functions
give us more flexibility.
  • Loading branch information
Sekhmet authored Feb 13, 2024
1 parent 548e9a7 commit d36da24
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 461 deletions.
2 changes: 1 addition & 1 deletion apps/ui/src/components/Modal/VotingPower.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const baseNetwork = computed(() =>
<a
:href="network.helpers.getExplorerUrl(strategy.address, 'strategy')"
target="_blank"
v-text="network.constants.STRATEGIES[strategy.address]"
v-text="network.constants.STRATEGIES[strategy.address] || strategy.address"
/>
<div class="text-skin-link">
{{ _n(Number(strategy.value) / 10 ** finalDecimals) }} {{ votingPowerSymbol }}
Expand Down
8 changes: 4 additions & 4 deletions apps/ui/src/components/ProposalVote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const start = getTsFromCurrent(props.proposal.network, props.proposal.start);
const isSupported = computed(() => {
const network = getNetwork(props.proposal.network);
const hasSupportedAuthenticator = props.proposal.space.authenticators.find(
authenticator => network.constants.SUPPORTED_AUTHENTICATORS[authenticator]
const hasSupportedAuthenticator = props.proposal.space.authenticators.find(authenticator =>
network.helpers.isAuthenticatorSupported(authenticator)
);
const hasSupportedStrategies = props.proposal.strategies.find(
strategy => network.constants.SUPPORTED_STRATEGIES[strategy]
const hasSupportedStrategies = props.proposal.strategies.find(strategy =>
network.helpers.isStrategySupported(strategy)
);
return hasSupportedAuthenticator && hasSupportedStrategies;
Expand Down
22 changes: 8 additions & 14 deletions apps/ui/src/networks/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ export async function buildMetadata(helpers: NetworkHelpers, config: StrategyCon
}

export function createStrategyPicker({
supportedAuthenticators,
supportedStrategies,
contractSupportedAuthenticators,
relayerAuthenticators,
helpers,
managerConnectors,
lowPriorityAuthenticators = []
}: {
supportedAuthenticators: Record<string, boolean | undefined>;
supportedStrategies: Record<string, boolean | undefined>;
contractSupportedAuthenticators: Record<string, boolean | undefined>;
relayerAuthenticators: Record<string, 'evm' | 'evm-tx' | 'starknet' | undefined>;
helpers: NetworkHelpers;
managerConnectors: Connector[];
lowPriorityAuthenticators?: ('evm' | 'evm-tx' | 'starknet')[];
}) {
Expand All @@ -84,12 +78,12 @@ export function createStrategyPicker({
const authenticatorsInfo = [...authenticators]
.filter(authenticator =>
isContract
? contractSupportedAuthenticators[authenticator]
: supportedAuthenticators[authenticator]
? helpers.isAuthenticatorContractSupported(authenticator)
: helpers.isAuthenticatorSupported(authenticator)
)
.sort((a, b) => {
const aRelayer = relayerAuthenticators[a];
const bRelayer = relayerAuthenticators[b];
const aRelayer = helpers.getRelayerAuthenticatorType(a);
const bRelayer = helpers.getRelayerAuthenticatorType(b);
const aLowPriority = aRelayer && lowPriorityAuthenticators.includes(aRelayer);
const bLowPriority = bRelayer && lowPriorityAuthenticators.includes(bRelayer);

Expand All @@ -116,7 +110,7 @@ export function createStrategyPicker({
return 0;
})
.map(authenticator => {
const relayerType = relayerAuthenticators[authenticator];
const relayerType = helpers.getRelayerAuthenticatorType(authenticator);

let connectors: Connector[] = [];
if (relayerType && ['evm', 'evm-tx'].includes(relayerType)) connectors = EVM_CONNECTORS;
Expand All @@ -136,7 +130,7 @@ export function createStrategyPicker({

const selectedStrategies = strategies
.map((strategy, index) => ({ address: strategy, index: strategiesIndicies[index] }) as const)
.filter(({ address }) => supportedStrategies[address]);
.filter(({ address }) => helpers.isStrategySupported(address));

if (!authenticatorInfo || (strategies.length !== 0 && selectedStrategies.length === 0)) {
throw new Error('Unsupported space');
Expand Down
7 changes: 1 addition & 6 deletions apps/ui/src/networks/evm/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type { Web3Provider } from '@ethersproject/providers';
import type {
Connector,
NetworkActions,
NetworkConstants,
NetworkHelpers,
SnapshotInfo,
StrategyConfig,
Expand All @@ -54,17 +53,13 @@ const CONFIGS: Record<number, EvmNetworkConfig> = {

export function createActions(
provider: Provider,
constants: NetworkConstants,
helpers: NetworkHelpers,
chainId: number
): NetworkActions {
const networkConfig = CONFIGS[chainId];

const pickAuthenticatorAndStrategies = createStrategyPicker({
supportedAuthenticators: constants.SUPPORTED_AUTHENTICATORS,
supportedStrategies: constants.SUPPORTED_STRATEGIES,
contractSupportedAuthenticators: constants.CONTRACT_SUPPORTED_AUTHENTICATORS,
relayerAuthenticators: constants.RELAYER_AUTHENTICATORS,
helpers,
managerConnectors: EVM_CONNECTORS
});

Expand Down
10 changes: 9 additions & 1 deletion apps/ui/src/networks/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export function createEvmNetwork(networkId: NetworkID): Network {
const constants = createConstants(networkId);

const helpers = {
isAuthenticatorSupported: (authenticator: string) =>
constants.SUPPORTED_AUTHENTICATORS[authenticator],
isAuthenticatorContractSupported: (authenticator: string) =>
constants.CONTRACT_SUPPORTED_AUTHENTICATORS[authenticator],
getRelayerAuthenticatorType: (authenticator: string) =>
constants.RELAYER_AUTHENTICATORS[authenticator],
isStrategySupported: (strategy: string) => constants.SUPPORTED_STRATEGIES[strategy],
isExecutorSupported: (executor: string) => constants.SUPPORTED_EXECUTORS[executor],
pin: pinGraph,
waitForTransaction: (txId: string) => provider.waitForTransaction(txId),
waitForSpace: (spaceAddress: string, interval = 5000): Promise<Space> =>
Expand Down Expand Up @@ -109,7 +117,7 @@ export function createEvmNetwork(networkId: NetworkID): Network {
hasReceive: false,
supportsSimulation: ['eth', 'gor', 'sep', 'matic', 'arb1'].includes(networkId),
managerConnectors: EVM_CONNECTORS,
actions: createActions(provider, constants, helpers, chainId),
actions: createActions(provider, helpers, chainId),
api,
constants,
helpers
Expand Down
Loading

0 comments on commit d36da24

Please sign in to comment.