Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add network switcher for starknet wallet #716

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/ui/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
import updateLocale from 'dayjs/plugin/updateLocale';
import sha3 from 'js-sha3';
import { validateAndParseAddress } from 'starknet';
import {
constants as starknetConstants,
validateAndParseAddress
} from 'starknet';
import networks from '@/helpers/networks.json';
import { VotingPowerItem } from '@/stores/votingPowers';
import { Choice, Proposal, SpaceMetadata } from '@/types';
Expand Down Expand Up @@ -379,6 +382,26 @@ export async function verifyNetwork(
}
}

export async function verifyStarknetNetwork(
web3: any,
chainId: starknetConstants.StarknetChainId
) {
if (!web3.provider.request) return;

try {
await web3.provider.request({
type: 'wallet_switchStarknetChain',
params: {
chainId
}
});
} catch (e) {
if (!e.message.toLowerCase().includes('not implemented')) {
throw new Error(e.message);
}
}
}

/**
* This function creates ERC1155 metadata object for space. external_url is stored
* at top level same as OpenSea, other extra properties are stored in the
Expand Down
29 changes: 25 additions & 4 deletions apps/ui/src/networks/starknet/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import {
starknetMainnet,
starknetSepolia
} from '@snapshot-labs/sx';
import { Account, AllowArray, Call, CallData, RpcProvider } from 'starknet';
import {
Account,
AllowArray,
Call,
CallData,
RpcProvider,
constants as starknetConstants
} from 'starknet';
import { executionCall, MANA_URL } from '@/helpers/mana';
import { getProvider } from '@/helpers/provider';
import { convertToMetaTransactions } from '@/helpers/transactions';
import { createErc1155Metadata, verifyNetwork } from '@/helpers/utils';
import {
createErc1155Metadata,
verifyNetwork,
verifyStarknetNetwork
} from '@/helpers/utils';
import {
EVM_CONNECTORS,
STARKNET_CONNECTORS
Expand Down Expand Up @@ -54,7 +65,11 @@ export function createActions(
chainId,
l1ChainId,
ethUrl
}: { chainId: string; l1ChainId: number; ethUrl: string }
}: {
chainId: starknetConstants.StarknetChainId;
l1ChainId: number;
ethUrl: string;
}
): NetworkActions {
const networkConfig = CONFIGS[networkId];
if (!networkConfig) throw new Error(`Unsupported network ${networkId}`);
Expand Down Expand Up @@ -230,6 +245,8 @@ export function createActions(

if (relayerType && ['evm', 'evm-tx'].includes(relayerType)) {
await verifyNetwork(web3, l1ChainId);
} else {
await verifyStarknetNetwork(web3, chainId);
}

let selectedExecutionStrategy;
Expand Down Expand Up @@ -331,6 +348,8 @@ export function createActions(

if (relayerType && ['evm', 'evm-tx'].includes(relayerType)) {
await verifyNetwork(web3, l1ChainId);
} else {
await verifyStarknetNetwork(web3, chainId);
}

let selectedExecutionStrategy;
Expand Down Expand Up @@ -407,6 +426,8 @@ export function createActions(

if (relayerType && ['evm', 'evm-tx'].includes(relayerType)) {
await verifyNetwork(web3, l1ChainId);
} else {
await verifyStarknetNetwork(web3, chainId);
}

const strategiesWithMetadata = await Promise.all(
Expand Down Expand Up @@ -467,7 +488,7 @@ export function createActions(
convertToMetaTransactions(proposal.executions[0].transactions)
);

return executionCall('stark', chainId, 'execute', {
return executionCall('stark', chainId as string, 'execute', {
space: proposal.space.id,
proposalId: proposal.proposal_id,
executionParams: executionData.executionParams
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/networks/starknet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createApi } from '../common/graphqlApi';

type Metadata = {
name: string;
chainId: string;
chainId: starknetConstants.StarknetChainId;
baseChainId: number;
baseNetworkId: NetworkID;
rpcUrl: string;
Expand Down
Loading