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

refactor: persisted tokens #2368

Merged
merged 35 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
27f4bb8
create persisted dapps store
MarkNerdi Apr 22, 2024
e386fee
add verified icon to dapp list
MarkNerdi Apr 22, 2024
9e3ce13
add migrations
MarkNerdi Apr 23, 2024
f8fea83
add verification icon to edit + details drawer
MarkNerdi Apr 23, 2024
7404122
add comment + fix profile version
MarkNerdi Apr 23, 2024
b9c0295
PR fixes
MarkNerdi Apr 23, 2024
837f381
Merge branch 'develop' into 1976-persist-verification-state-for-dapp
Tuditi Apr 23, 2024
9c1b17a
refactor token balance
MarkNerdi Apr 24, 2024
9c7a7ac
add network to get persisted token
MarkNerdi Apr 24, 2024
3e41756
cleanup loadAssets function
MarkNerdi Apr 24, 2024
bb994c7
refactor loadTokens to load from all networks
MarkNerdi Apr 24, 2024
f45a610
add base token to chain config
MarkNerdi Apr 26, 2024
4c1528f
improve getting basetoken
MarkNerdi Apr 26, 2024
8b41563
Merge commit '24f53083ffedee8442b774f32c7198b0df09049f' into 1976-per…
MarkNerdi Apr 26, 2024
cf0026b
update migrations
MarkNerdi Apr 26, 2024
2f243cc
cleanup
MarkNerdi Apr 26, 2024
70e8fb5
Merge commit '6cb23435b3b36b1eee935c4ecf7a64ba3beffc1d' into refactor…
MarkNerdi Apr 26, 2024
d1ab4a7
Merge commit 'cf0026b1ad70d641fe98357366b7cd09160d235d' into refactor…
MarkNerdi Apr 26, 2024
240d7d4
add migrations to reset persisted tokens
MarkNerdi Apr 26, 2024
0a31ed8
Merge commit '0bc3f5a17a5ff8509f2ef88cde5107bb72c72076' into refactor…
MarkNerdi Apr 27, 2024
0c6a9b7
resolve PR comments
MarkNerdi Apr 29, 2024
5f5e7b7
make migration
MarkNerdi Apr 29, 2024
74541fb
update migration
MarkNerdi Apr 29, 2024
6a8261e
update migration for chain config
MarkNerdi Apr 29, 2024
6eefd79
use URL constructor
MarkNerdi Apr 29, 2024
79e66e5
fix chainconfiguration
MarkNerdi Apr 29, 2024
1533ad3
catch uncaught error
MarkNerdi Apr 29, 2024
942eb01
only check for owner on relevant network
MarkNerdi Apr 29, 2024
8182c7f
Merge commit '196a43ef10f4a50018a8194b725966a95f23031d' into refactor…
MarkNerdi Apr 29, 2024
4a44e60
fix condition
MarkNerdi Apr 29, 2024
3ec0890
Merge commit 'a5681bc65e2501897798ba6a5d5c6471205b5af0' into refactor…
MarkNerdi Apr 29, 2024
6167f1d
Merge commit '04fa5bd7db55fc46cba8310112e60b2fcecfbeef' into refactor…
MarkNerdi Apr 30, 2024
2e1d068
Merge remote-tracking branch 'origin/develop' into refactor/refactor-…
Tuditi Apr 30, 2024
860fbcd
fix: correctly destructure persisted token
Tuditi Apr 30, 2024
b854d99
Merge branch 'develop' into refactor/refactor-persisted-tokens
Tuditi Apr 30, 2024
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
8 changes: 4 additions & 4 deletions packages/desktop/components/menus/TokenActionsMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
}

function onUnverifyClick(): void {
unverifyToken(token.id, NotVerifiedStatus.Skipped)
unverifyToken(token.networkId, token.id, NotVerifiedStatus.Skipped)
updatePopupProps({
token: { ...token, verification: { verified: false, status: NotVerifiedStatus.Skipped } },
})
menu?.close()
}

function onVerifyClick(): void {
verifyToken(token.id, VerifiedStatus.SelfVerified)
verifyToken(token.networkId, token.id, VerifiedStatus.SelfVerified)
updatePopupProps({
token: { ...token, verification: { verified: true, status: VerifiedStatus.SelfVerified } },
})
menu?.close()
}

function onUnhideClick(): void {
unhideToken(token.id)
unhideToken(token.networkId, token.id)
hideActivitiesForHiddenTokens()
updatePopupProps({
token: { ...token, hidden: false },
Expand All @@ -48,7 +48,7 @@
}

function onHideClick(): void {
hideToken(token.id)
hideToken(token.networkId, token.id)
hideActivitiesForHiddenTokens()
updatePopupProps({
token: { ...token, hidden: true },
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/components/menus/TokenListMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { showNotification } from '@auxiliary/notification'
import { IconName, Menu } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { refreshAccountTokensForActiveProfile } from '@core/token/actions'
import { loadTokensForAllAccountBalances } from '@core/token/actions'
import { PopupId, closePopup, openPopup } from '../../lib/auxiliary/popup'
import { fetchL2BalanceForAllAccounts } from '@core/layer-2'

Expand All @@ -27,7 +27,7 @@
}

function refreshTokenMetadata(): void {
refreshAccountTokensForActiveProfile(true)
loadTokensForAllAccountBalances(true)
showNotification({
variant: 'success',
text: localize('notifications.refreshTokenMetadata.success'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import { Error } from '@bloomwalletio/ui'
import { handleError } from '@core/error/handlers/handleError'
import PopupTemplate from '../PopupTemplate.svelte'
import { getBaseToken } from '@core/profile/actions'

let isBusy = false
let error: string | undefined

const network = getL1Network()

async function onConfirmClick(): Promise<void> {
error = undefined
try {
Expand All @@ -33,7 +34,7 @@
<PopupTemplate
title={localize('popups.faucetRequest.title')}
description={localize('popups.faucetRequest.body', {
values: { token: getBaseToken().name, network: getL1Network()?.name },
values: { token: network.baseToken.name, network: network.name },
})}
backButton={{
text: localize('actions.cancel'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { checkActiveProfileAuth, getBaseToken, loadAccounts } from '@core/profile/actions'
import { activeAccounts, activeProfile, getActiveProfileId, visibleActiveAccounts } from '@core/profile/stores'
import { formatTokenAmountBestMatch } from '@core/token'
import { refreshAccountTokensForActiveProfile } from '@core/token/actions'
import { loadTokensForAllAccountBalances } from '@core/token/actions'
import { closePopup } from '@desktop/auxiliary/popup'
import { onDestroy } from 'svelte'
import PopupTemplate from '../PopupTemplate.svelte'
Expand Down Expand Up @@ -134,7 +134,7 @@
onDestroy(async () => {
if (hasUsedWalletFinder) {
const profileId = getActiveProfileId()
await refreshAccountTokensForActiveProfile()
await loadTokensForAllAccountBalances()
await generateAndStoreActivitiesForAllAccounts(profileId)
loadNftsForActiveProfile()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import { SendFlowRoute, SendFlowRouter, sendFlowRouter } from '@views/dashboard/send-flow'
import PopupTemplate from '../PopupTemplate.svelte'

export let token: ITokenWithBalance | undefined
export let token: ITokenWithBalance
export let activityId: string | undefined = undefined

$: isNewToken = token?.verification?.status === NotVerifiedStatus.New
$: isNewToken = token.verification?.status === NotVerifiedStatus.New

function onSkipClick(): void {
unverifyToken(token?.id, NotVerifiedStatus.Skipped)
unverifyToken(token.networkId, token.id, NotVerifiedStatus.Skipped)
if (activityId) {
openPopup({
id: PopupId.ActivityDetails,
Expand All @@ -31,7 +31,7 @@
}

function onVerifyClick(): void {
verifyToken(token?.id, VerifiedStatus.SelfVerified)
verifyToken(token.networkId, token.id, VerifiedStatus.SelfVerified)
if (activityId) {
openPopup({
id: PopupId.ActivityDetails,
Expand All @@ -45,7 +45,7 @@
}

function onSendClick(): void {
const sendFlowType = token?.id === BASE_TOKEN_ID ? SendFlowType.BaseCoinTransfer : SendFlowType.TokenTransfer
const sendFlowType = token.id === BASE_TOKEN_ID ? SendFlowType.BaseCoinTransfer : SendFlowType.TokenTransfer
setSendFlowParameters({
type: sendFlowType,
[sendFlowType]: {
Expand All @@ -62,7 +62,7 @@
</script>

<PopupTemplate
title={token?.metadata?.name}
title={token.metadata?.name}
backButton={isNewToken
? {
text: localize('actions.skip'),
Expand All @@ -75,7 +75,7 @@
}}
>
<div slot="menu">
{#if token?.standard === TokenStandard.Irc30 || token?.standard === TokenStandard.Erc20}
{#if token.standard === TokenStandard.Irc30 || token.standard === TokenStandard.Erc20}
<TokenActionsMenu {token} />
{/if}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
tokens?.baseCoin?.balance.total ?? BigInt(0),
tokens?.baseCoin?.metadata
)
$: fiatBalance =
formatCurrency(getFiatValueFromTokenAmount(BigInt(tokens?.baseCoin?.balance.total ?? 0), tokens?.baseCoin)) ??
''
$: fiatBalance = tokens
? formatCurrency(getFiatValueFromTokenAmount(BigInt(tokens?.baseCoin?.balance.total ?? 0), tokens.baseCoin))
: ''
$: address = getAddressFromAccountForNetwork(account, network.id)

$: hasTokens = tokens?.nativeTokens?.length > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,32 @@
$: $selectedAccountTokens, (token = getTokenFromActivity(activity))

function getAmount(_activity: Activity): string {
if (_activity.namespace === NetworkNamespace.Stardust) {
if (_activity.type === StardustActivityType.Basic || _activity.type === StardustActivityType.Foundry) {
const { type, namespace, sourceNetworkId, direction, action } = _activity

if (namespace === NetworkNamespace.Stardust) {
if (type === StardustActivityType.Basic || type === StardustActivityType.Foundry) {
const { rawAmount, tokenId } = _activity.tokenTransfer ?? _activity.baseTokenTransfer ?? {}

return getFormattedAmountFromActivity(rawAmount, tokenId, _activity.direction, _activity.action)
} else if (_activity.type === StardustActivityType.Governance) {
return getFormattedAmountFromActivity(rawAmount, tokenId, sourceNetworkId, direction, action)
} else if (type === StardustActivityType.Governance) {
const isVotingPowerActivity =
_activity.governanceAction === StardustGovernanceAction.DecreaseVotingPower ||
_activity.governanceAction === StardustGovernanceAction.IncreaseVotingPower

return isVotingPowerActivity ? getFormattedVotingPowerFromGovernanceActivity(_activity) : '-'
} else if (_activity.type === StardustActivityType.Nft) {
} else if (type === StardustActivityType.Nft) {
return '1 ' + localize('general.nft')
} else {
return '-'
}
} else if (_activity.namespace === NetworkNamespace.Evm) {
if (_activity.type === EvmActivityType.CoinTransfer) {
} else if (namespace === NetworkNamespace.Evm) {
if (type === EvmActivityType.CoinTransfer) {
return getFormattedAmountFromActivity(
_activity.baseTokenTransfer.rawAmount,
BASE_TOKEN_ID,
_activity.direction,
_activity.action
sourceNetworkId,
direction,
action
)
} else if (isEvmTokenActivity(_activity)) {
if (
Expand All @@ -57,8 +60,9 @@
return getFormattedAmountFromActivity(
_activity.tokenTransfer.rawAmount,
_activity.tokenTransfer.tokenId,
_activity.direction,
_activity.action
sourceNetworkId,
direction,
action
)
}
} else {
Expand All @@ -70,27 +74,29 @@
}

function getFormattedMarketPrice(_activity: Activity): string | undefined {
if (_activity.namespace === NetworkNamespace.Stardust) {
const { type, namespace } = _activity

if (namespace === NetworkNamespace.Stardust) {
if (
[StardustActivityType.Basic, StardustActivityType.Governance, StardustActivityType.Foundry].includes(
_activity.type
type
) &&
token
) {
const amount = _activity.tokenTransfer?.rawAmount ?? _activity.baseTokenTransfer.rawAmount

const marketPrice = getFiatValueFromTokenAmount(amount, token)
return marketPrice ? formatCurrency(marketPrice) : '-'
} else if (_activity.type === StardustActivityType.Nft) {
} else if (type === StardustActivityType.Nft) {
return '-'
} else {
return undefined
}
} else if (_activity.namespace === NetworkNamespace.Evm) {
if (_activity.type === EvmActivityType.CoinTransfer) {
} else if (namespace === NetworkNamespace.Evm) {
if (type === EvmActivityType.CoinTransfer) {
const marketPrice = getFiatValueFromTokenAmount(_activity.baseTokenTransfer.rawAmount, token)
return marketPrice ? formatCurrency(marketPrice) : '-'
} else if (_activity.type === EvmActivityType.TokenTransfer) {
} else if (type === EvmActivityType.TokenTransfer) {
if (
_activity.tokenTransfer?.standard === TokenStandard.Erc20 ||
_activity.tokenTransfer?.standard === TokenStandard.Irc30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{#if selectedTab.key === PopupTab.Transaction}
<EvmGenericInformation
time={activity.time}
sourceNetworkId={activity.sourceNetworkId}
destinationNetworkId={activity.destinationNetworkId}
maxGasFee={activity.maxGasFee}
transactionFee={activity.transactionFee}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import { Table } from '@bloomwalletio/ui'
import { getFormattedTimeStamp, localize } from '@core/i18n'
import { NetworkId } from '@core/network'
import { getBaseToken } from '@core/profile/actions'
import { NetworkId, getNetwork } from '@core/network'
import { formatTokenAmountBestMatch } from '@core/token'
import { NetworkLabel } from '@ui'

export let time: Date
export let sourceNetworkId: NetworkId
export let destinationNetworkId: NetworkId
export let maxGasFee: bigint | undefined = undefined
export let transactionFee: bigint | undefined = undefined
Expand All @@ -17,7 +17,7 @@
$: formattedTransactionFee = formatAmount(transactionFee)

function formatAmount(amount: bigint | undefined): string | undefined {
return amount ? formatTokenAmountBestMatch(amount, getBaseToken()) : undefined
return amount ? formatTokenAmountBestMatch(amount, getNetwork(sourceNetworkId)?.baseToken) : undefined
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
case StardustActivityType.Foundry:
tabs = getTabItems([PopupTab.Transaction, PopupTab.Foundry, PopupTab.Token])
token = activity.tokenTransfer
? getPersistedToken(activity.tokenTransfer?.tokenId)?.metadata
? getPersistedToken(activity.sourceNetworkId, activity.tokenTransfer?.tokenId)?.metadata
: undefined
break
case StardustActivityType.Consolidation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import { openUrlInBrowser } from '@core/app'
import { time } from '@core/app/stores'
import { getFormattedTimeStamp, localize } from '@core/i18n'
import { ExplorerEndpoint, getDefaultExplorerUrl } from '@core/network'
import { getBaseToken } from '@core/profile/actions'
import { ExplorerEndpoint, getDefaultExplorerUrl, getNetwork } from '@core/network'
import { formatTokenAmountBestMatch } from '@core/token'
import { buildUrl } from '@core/utils'
import { getTimeDifference } from '@core/utils/time'
Expand All @@ -32,7 +31,7 @@
}

function formatAmount(amount: bigint | undefined): string | undefined {
return amount ? formatTokenAmountBestMatch(amount, getBaseToken()) : undefined
return amount ? formatTokenAmountBestMatch(amount, getNetwork(activity.sourceNetworkId)?.baseToken) : undefined
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

let metadata: IIrc30Metadata | undefined
$: metadata = <IIrc30Metadata>(
getPersistedToken(activity.tokenTransfer?.tokenId ?? activity.baseTokenTransfer.tokenId)?.metadata
getPersistedToken(
activity.sourceNetworkId,
activity.tokenTransfer?.tokenId ?? activity.baseTokenTransfer.tokenId
)?.metadata
)
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@ import { IPersistedToken } from '@core/token/interfaces'
import { getOrRequestTokenFromPersistedTokens } from '@core/token/actions'
import { addPersistedToken } from '@core/token/stores'
import { get } from 'svelte/store'
import { StardustActivityType } from '../enums'
import { allAccountActivities } from '../stores'
import { NetworkId } from '@core/network/types'
import { getTokenIdFromActivity } from '../utils'

export async function loadAssetsForAllActivities(account: IAccountState): Promise<void> {
const accountActivities = get(allAccountActivities)[account.index]

const persistedTokens: IPersistedToken[] = []
const tokens: { [networkId: NetworkId]: IPersistedToken[] } = {}
for (const activity of accountActivities) {
try {
if (activity.type === StardustActivityType.Basic || activity.type === StardustActivityType.Foundry) {
const tokenId = activity.tokenTransfer?.tokenId ?? activity.baseTokenTransfer?.tokenId
const token = await getOrRequestTokenFromPersistedTokens(tokenId, activity.sourceNetworkId, false)
if (token) {
persistedTokens.push(token)
const tokenId = getTokenIdFromActivity(activity)
if (!tokenId) {
continue
}

const token = await getOrRequestTokenFromPersistedTokens(tokenId, activity.sourceNetworkId, false)
if (token) {
if (!tokens[activity.sourceNetworkId]) {
tokens[activity.sourceNetworkId] = []
}
tokens[activity.sourceNetworkId].push(token)
}
} catch (err) {
console.error(err)
}
}
addPersistedToken(...persistedTokens)

for (const networkId of Object.keys(tokens)) {
addPersistedToken(networkId as NetworkId, ...tokens[networkId])
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { syncBalance } from '@core/account/actions/syncBalance'
import { updateNftInAllAccountNftsForAccount } from '@core/nfts/actions'
import { refreshAccountTokensForActiveProfile } from '@core/token/actions'
import { loadTokensForAllAccountBalances } from '@core/token/actions'
import { StardustActivityAsyncStatus, ActivityDirection, StardustActivityType } from '../enums'
import { allAccountActivities } from '../stores'
import { getAsyncStatus } from '../utils/helper'
Expand Down Expand Up @@ -55,6 +55,6 @@ export function setAsyncStatusOfAccountActivities(time: Date): void {
syncBalance(accountIndex)
}
if (balancesToUpdate.length) {
void refreshAccountTokensForActiveProfile()
void loadTokensForAllAccountBalances()
}
}
Loading
Loading