Skip to content

Commit

Permalink
chore: improve network id typechecks (#2305)
Browse files Browse the repository at this point in the history
* rename stardustnetworkId into SupportedStardustNetworkId

* rename EvmNetworkId to SupportedEvmNetworkId

* use evm and stardust network ID into correct places

* move supported networks into constants

* PR fixes

* fix supported network id

* improve network typings

* fix circular dependency

* chore: use const

---------

Co-authored-by: Tuditi <[email protected]>
Co-authored-by: Tuditi <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 8101e6a commit cd77257
Show file tree
Hide file tree
Showing 117 changed files with 336 additions and 332 deletions.
4 changes: 2 additions & 2 deletions packages/desktop/components/NetworkCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Network,
NetworkHealth,
NetworkNamespace,
chainStatuses,
evmNetworkStatuses,
getDefaultExplorerUrl,
networkStatus,
setSelectedChain,
Expand All @@ -31,7 +31,7 @@
let status: NetworkHealth
const explorer = getDefaultExplorerUrl(network.id, ExplorerEndpoint.Address)
$: $networkStatus, $chainStatuses, $selectedAccount, setNetworkCardData()
$: $networkStatus, $evmNetworkStatuses, $selectedAccount, setNetworkCardData()
function onExplorerClick(): void {
if (!explorer || !address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { NetworkInput } from '@ui'
import { localize } from '@core/i18n'
import { getEvmTokenMetadata } from '@core/layer-2/utils'
import { NetworkId } from '@core/network'
import { EvmNetworkId } from '@core/network'
import { validateEthereumAddress } from '@core/utils'
import PopupTemplate from '../PopupTemplate.svelte'
import { closePopup } from '@desktop/auxiliary/popup'
Expand All @@ -13,7 +13,7 @@
let busy = false
let networkId: NetworkId
let networkId: EvmNetworkId
let tokenAddress: string
let tokenAddressError = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { TextInput } from '@bloomwalletio/ui'
import { NetworkInput } from '@ui'
import { localize } from '@core/i18n'
import { NetworkId } from '@core/network'
import { EvmNetworkId } from '@core/network'
import { validateEthereumAddress } from '@core/utils'
import PopupTemplate from '../PopupTemplate.svelte'
import { closePopup } from '@desktop/auxiliary/popup'
Expand All @@ -21,7 +21,7 @@
let busy = false
let networkId: NetworkId
let networkId: EvmNetworkId
let tokenAddress: string
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/components/popup/popups/SiwePopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { signMessage } from '@core/wallet/actions'
import { Table, Tabs, Text } from '@bloomwalletio/ui'
import { IAccountState } from '@core/account'
import { IEvmNetwork, NetworkId, getNameFromNetworkId } from '@core/network'
import { IEvmNetwork, EvmNetworkId, getNameFromNetworkId } from '@core/network'
import { AccountLabel, DappInfo, KeyValue, NetworkLabel } from '@ui'
import { checkActiveProfileAuth } from '@core/profile/actions'
import { LedgerAppName } from '@core/ledger'
Expand Down Expand Up @@ -39,7 +39,7 @@
let selectedTab = TABS[0]
let isBusy = false
const networkId = `eip155:${siweObject.chainId}` as NetworkId
const networkId: EvmNetworkId = `eip155:${siweObject.chainId}`
async function onConfirmClick(): Promise<void> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import { closePopup } from '@desktop/auxiliary/popup'
import { onDestroy } from 'svelte'
import PopupTemplate from '../PopupTemplate.svelte'
import { StardustNetworkId } from '@core/network/enums'
import { SupportedStardustNetworkId } from '@core/network/constants'
import { ledgerRaceConditionProtectionWrapper } from '@core/ledger'
import { StardustNetworkId } from '@core/network/types'
const { network, type } = $activeProfile
Expand All @@ -38,9 +39,9 @@
$: totalBalance = sumBalanceForAccounts($visibleActiveAccounts)
const networkSearchMethod: { [key in StardustNetworkId]?: () => Promise<void> } = {
[StardustNetworkId.Iota]: multiAddressSearch,
[StardustNetworkId.Shimmer]: singleAddressSearch,
[StardustNetworkId.Testnet]: singleAddressSearch,
[SupportedStardustNetworkId.Iota]: multiAddressSearch,
[SupportedStardustNetworkId.Shimmer]: singleAddressSearch,
[SupportedStardustNetworkId.Testnet]: singleAddressSearch,
}
async function singleAddressSearch(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { IAccountState, getAddressFromAccountForNetwork } from '@core/account'
import { selectedAccount } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import { NetworkId, NetworkNamespace, getEvmNetwork } from '@core/network'
import { EvmNetworkId, NetworkNamespace, getEvmNetwork } from '@core/network'
import { buildNftFromPersistedErc721Nft } from '@core/nfts'
import { addNftsToDownloadQueue, updateAllAccountNftsForAccount } from '@core/nfts/actions'
import { persistErc721Nft } from '@core/nfts/actions/persistErc721Nft'
Expand All @@ -26,14 +26,14 @@
let userAddress: string
let numberOfTasks: number | undefined
$: evmNetwork = getEvmNetwork(`${NetworkNamespace.Evm}:${$selectedCampaign.chainId}` as NetworkId)
$: evmNetwork = getEvmNetwork(`${NetworkNamespace.Evm}:${$selectedCampaign.chainId}` as EvmNetworkId)
$: ({ board: leaderboard, userPosition } = $campaignLeaderboards[$selectedCampaign.projectId]?.[
$selectedCampaign.id
] ?? { board: undefined, userPosition: undefined })
$: fetchAndPersistTideData($selectedAccount, evmNetwork?.id)
$: userNft = $ownedNfts.find((nft) => nft.id?.startsWith($selectedCampaign.address.toLowerCase()))
function fetchAndPersistTideData(account: IAccountState, networkId: NetworkId): void {
function fetchAndPersistTideData(account: IAccountState, networkId: EvmNetworkId): void {
if (networkId) {
userAddress = getAddressFromAccountForNetwork(account, networkId)?.toLowerCase()
void fetchAndPersistUserPosition(userAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { type IItem } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { IErc721Nft } from '@core/nfts'
import { ExplorerEndpoint, NetworkId, getDefaultExplorerUrl } from '@core/network'
import { ExplorerEndpoint, EvmNetworkId, getDefaultExplorerUrl } from '@core/network'
import CollectibleDetails from './CollectibleDetails.svelte'
import { buildUrl } from '@core/utils'
Expand All @@ -13,7 +13,7 @@
const address = contractMetadata.address
const explorerEndpoint = getExplorerEndpoint(networkId)
function getExplorerEndpoint(networkId: NetworkId): string | undefined {
function getExplorerEndpoint(networkId: EvmNetworkId): string | undefined {
const { baseUrl, endpoint } = getDefaultExplorerUrl(networkId, ExplorerEndpoint.Token)
const url = buildUrl({
origin: baseUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ProfileActionsMenu, SidebarTab } from '@components'
import { APP_STAGE, AppStage } from '@core/app'
import { localize } from '@core/i18n'
import { StardustNetworkId } from '@core/network'
import { SupportedStardustNetworkId } from '@core/network'
import { activeProfile, isSoftwareProfile } from '@core/profile/stores'
import {
DashboardRoute,
Expand Down Expand Up @@ -60,8 +60,8 @@
: []),
...(features?.campaigns?.enabled &&
profileFeatures?.campaigns &&
($activeProfile?.network?.id === StardustNetworkId.Shimmer ||
$activeProfile?.network?.id === StardustNetworkId.Testnet)
($activeProfile?.network?.id === SupportedStardustNetworkId.Shimmer ||
$activeProfile?.network?.id === SupportedStardustNetworkId.Testnet)
? [
{
icon: IconName.Trophy,
Expand All @@ -78,9 +78,9 @@
label: localize('tabs.buySell'),
route: DashboardRoute.BuySell,
onClick: openBuySell,
disabled: $activeProfile?.network?.id !== StardustNetworkId.Iota,
disabled: $activeProfile?.network?.id !== SupportedStardustNetworkId.Iota,
tooltip:
$activeProfile?.network?.id !== StardustNetworkId.Iota
$activeProfile?.network?.id !== SupportedStardustNetworkId.Iota
? localize('tabs.tooltips.buySell')
: '',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import {
MAX_NETWORK_NAME_LENGTH,
EvmNetworkType,
IIscpChainConfiguration,
IIscpEvmNetworkConfiguration,
ETHEREUM_COIN_TYPE,
NetworkId,
EvmNetworkId,
NetworkNamespace,
ChainId,
} from '@core/network'
import { activeProfile } from '@core/profile/stores'
import { getNetworkHrp } from '@core/profile/actions'
Expand All @@ -25,11 +25,11 @@
let explorerUrlError = ''
$: submitDisabled = !evmNetwork.name || !evmNetwork.aliasAddress || !evmNetwork.rpcEndpoint
const evmNetwork: IIscpChainConfiguration = {
const evmNetwork: IIscpEvmNetworkConfiguration = {
type: EvmNetworkType.Iscp,
id: '' as NetworkId,
id: '' as EvmNetworkId,
namespace: NetworkNamespace.Evm,
chainId: '' as EvmNetworkId,
chainId: '' as ChainId,
name: '',
aliasAddress: '',
rpcEndpoint: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
} else if (
selectedToken &&
isStardustNetwork(selectedToken.networkId) &&
!canAccountMakeStardustTransaction($selectedAccountIndex, selectedToken.networkId, $sendFlowParameters?.type)
!canAccountMakeStardustTransaction($selectedAccountIndex, $sendFlowParameters?.type)
) {
tokenError = localize('error.send.insufficientFundsTransaction')
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { DateTimePickerMenu } from '.'
import StorageDepositButton from './StorageDepositButton.svelte'
export let destinationNetworkId: NetworkId = undefined
export let destinationNetworkId: NetworkId | undefined = undefined
export let storageDeposit: bigint | undefined = undefined
export let transactionFee: bigint | undefined = undefined
export let giftStorageDeposit: boolean | undefined = undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { formatCurrency } from '@core/i18n'
import { getFiatValueFromTokenAmount, getMarketPriceForToken } from '@core/market/actions'
import { StardustNetworkId, TokenSupply, getActiveNetworkId } from '@core/network'
import { SupportedStardustNetworkId, TokenSupply, getActiveNetworkId } from '@core/network'
import { BASE_TOKEN_ID, ITokenWithBalance, formatTokenAmountBestMatch } from '@core/token'
import { truncateString } from '@core/utils'
import { PopupId, openPopup } from '@desktop/auxiliary/popup'
Expand All @@ -20,11 +20,11 @@
let tokenSupply: TokenSupply | '0'
switch (getActiveNetworkId()) {
case StardustNetworkId.Iota:
case SupportedStardustNetworkId.Iota:
tokenSupply = TokenSupply.Iota
break
case StardustNetworkId.Shimmer:
case StardustNetworkId.Testnet:
case SupportedStardustNetworkId.Shimmer:
case SupportedStardustNetworkId.Testnet:
tokenSupply = TokenSupply.Shimmer
break
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
import LoggedOutLayout from '@views/components/LoggedOutLayout.svelte'
import features from '@features/features'
import { login } from '@core/profile/actions'
import { StardustNetworkId } from '@core/network'
import { SupportedStardustNetworkId } from '@core/network'
import { handleError } from '@core/error/handlers'
import { onMount } from 'svelte'
const LOCALE_KEY = 'views.onboarding.completeOnboarding.finishOnboarding'
let isAppSetup = false
$: appName = $onboardingProfile?.network?.id === StardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
$: appName =
$onboardingProfile?.network?.id === SupportedStardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
async function onContinueClick(): Promise<void> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import { createFromLedgerRouter } from '..'
import { CreateFromLedgerRoute } from '../create-from-ledger-route.enum'
import { onboardingProfile } from '@contexts/onboarding'
import { StardustNetworkId } from '@core/network'
import { SupportedStardustNetworkId } from '@core/network'
$: isDisconnected = $ledgerConnectionState === LedgerConnectionState.Disconnected
$: isLocked = isDisconnected || $ledgerConnectionState === LedgerConnectionState.Locked
$: appName = $onboardingProfile?.network?.id === StardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
$: appName =
$onboardingProfile?.network?.id === SupportedStardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
$: isCorrectAppOpen =
$onboardingProfile?.network?.id === StardustNetworkId.Iota
$onboardingProfile?.network?.id === SupportedStardustNetworkId.Iota
? $ledgerConnectionState === LedgerConnectionState.IotaAppOpen
: $ledgerConnectionState === LedgerConnectionState.ShimmerAppOpen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { onMount } from 'svelte'
import { createFromLedgerRouter } from '../create-from-ledger-router'
import { onboardingProfile } from '@contexts/onboarding'
import { SupportedStardustNetworkId } from '@core/network/constants'
function onContinueClick(): void {
$createFromLedgerRouter.next()
Expand All @@ -32,7 +33,7 @@
}}
>
<content slot="content">
{#if $onboardingProfile?.network?.id === StardustNetworkId.Iota}
{#if $onboardingProfile?.network?.id === SupportedStardustNetworkId.Iota}
<icon-container class="bg-black">
<Icon name={IconName.Iota} size="lg" customColor="#ffffff" />
</icon-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import { LedgerAppName } from '@core/ledger'
import { get } from 'svelte/store'
import { onboardingProfile } from '@contexts/onboarding'
import { StardustNetworkId } from '@core/network'
import { SupportedStardustNetworkId } from '@core/network'
const ledgerAppName: LedgerAppName =
get(onboardingProfile)?.network?.id === StardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
get(onboardingProfile)?.network?.id === SupportedStardustNetworkId.Iota
? LedgerAppName.Iota
: LedgerAppName.Shimmer
const icon: IconName =
get(onboardingProfile)?.network?.id === StardustNetworkId.Iota ? IconName.Iota : IconName.Shimmer
get(onboardingProfile)?.network?.id === SupportedStardustNetworkId.Iota ? IconName.Iota : IconName.Shimmer
</script>

<Text type="body2" textColor="secondary" align="center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
} from '@contexts/onboarding'
import { localize } from '@core/i18n'
import {
NetworkId,
NetworkNamespace,
StardustNetworkId,
StardustNetworkName,
getDefaultClientOptions,
getDefaultPersistedNetwork,
Expand Down Expand Up @@ -39,7 +39,7 @@
function onContinueClick(): void {
if (selectedNetworkType !== OnboardingNetworkType.Custom) {
const networkName = getNetworkNameFromOnboardingNetworkType(selectedNetworkType)
const networkId: NetworkId = `${NetworkNamespace.Stardust}:${networkName}`
const networkId: StardustNetworkId = `${NetworkNamespace.Stardust}:${networkName}`
const network = getDefaultPersistedNetwork(networkId)
const clientOptions = getDefaultClientOptions(networkId)
updateOnboardingProfile({ network, clientOptions })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import { DEFAULT_SYNC_OPTIONS } from '@core/account/constants'
import { localize } from '@core/i18n'
import { LedgerAppName, checkOrConnectLedger, ledgerRaceConditionProtectionWrapper } from '@core/ledger'
import { StardustNetworkId } from '@core/network'
import { SupportedStardustNetworkId } from '@core/network/constants'
import { NetworkId } from '@core/network/types'
import { ProfileType } from '@core/profile'
import { RecoverAccountsPayload, createAccount, recoverAccounts } from '@core/profile-manager'
import { DEFAULT_ACCOUNT_RECOVERY_CONFIGURATION } from '@core/profile/constants'
Expand Down Expand Up @@ -34,10 +35,10 @@
let accountsBalances: IAccountBalance[] = []
const networkSearchMethod: { [key in StardustNetworkId]?: () => Promise<void> } = {
[StardustNetworkId.Iota]: multiAddressSearch,
[StardustNetworkId.Shimmer]: singleAddressSearch,
[StardustNetworkId.Testnet]: singleAddressSearch,
const networkSearchMethod: { [key in NetworkId]?: () => Promise<void> } = {
[SupportedStardustNetworkId.Iota]: multiAddressSearch,
[SupportedStardustNetworkId.Shimmer]: singleAddressSearch,
[SupportedStardustNetworkId.Testnet]: singleAddressSearch,
}
async function singleAddressSearch(): Promise<void> {
Expand Down Expand Up @@ -133,7 +134,7 @@
async function onFindBalancesClick(): Promise<void> {
if (type === ProfileType.Ledger) {
await checkOrConnectLedger(
network?.id === StardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
network?.id === SupportedStardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer
)
}
await findBalances()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
import { Table } from '@bloomwalletio/ui'
import { getFormattedTimeStamp, localize } from '@core/i18n'
import { NetworkId } from '@core/network'
import { EvmNetworkId } from '@core/network'
import { getBaseToken } from '@core/profile/actions'
import { formatTokenAmountBestMatch } from '@core/token'
import { NetworkLabel } from '@ui'
export let time: Date
export let destinationNetworkId: NetworkId
export let destinationNetworkId: EvmNetworkId
export let maxGasFee: bigint | undefined = undefined
export let transactionFee: bigint | undefined = undefined
Expand Down
Loading

0 comments on commit cd77257

Please sign in to comment.