Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion packages/shared/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { persistent } from 'shared/lib/helpers'
import { _ } from 'shared/lib/i18n'
import type { HistoryData, PriceData } from 'shared/lib/marketData'
import { HistoryDataProps } from 'shared/lib/marketData'
import { showSystemNotification } from 'shared/lib/notifications'
import { showAppNotification, showSystemNotification } from 'shared/lib/notifications'
import { activeProfile, updateProfile } from 'shared/lib/profile'
import { formatUnit } from 'shared/lib/units'
import { get, writable, Writable } from 'svelte/store'
Expand Down Expand Up @@ -574,3 +574,27 @@ export const getWalletBalanceHistory = (accountsBalanceHistory: BalanceHistory):
})
return balanceHistory
}

/**
* Sync the accounts
*/
export function syncAccounts() {
isSyncing.set(true)
api.syncAccounts({
onSuccess(syncAccountsResponse) {
const syncedAccounts = syncAccountsResponse.payload

updateAccounts(syncedAccounts)

isSyncing.set(false)
},
onError(err) {
isSyncing.set(false)
const locale = get(_) as (string) => string
showAppNotification({
type: 'error',
message: locale(err.error),
})
},
})
}
30 changes: 3 additions & 27 deletions packages/shared/routes/dashboard/settings/views/Advanced.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { Button, Checkbox, Dropdown, Radio, Text } from 'shared/components'
import { developerMode } from 'shared/lib/app'
import { DEFAULT_NODE, DEFAULT_NODES } from 'shared/lib/network'
import { showAppNotification } from 'shared/lib/notifications'
import { openPopup } from 'shared/lib/popup'
import { activeProfile, updateProfile } from 'shared/lib/profile'
import type { Node } from 'shared/lib/typings/client'
import { api, updateAccounts, wallet, WalletAccount } from 'shared/lib/wallet'
import { api, isSyncing, syncAccounts, wallet, WalletAccount } from 'shared/lib/wallet'
import { get } from 'svelte/store'
import { showAppNotification } from 'shared/lib/notifications'

export let locale

Expand Down Expand Up @@ -155,30 +155,6 @@
function handleErrorLogClick() {
openPopup({ type: 'errorLog' })
}

function resyncAccounts() {
const _sync = () => {
api.syncAccounts({
onSuccess(syncAccountsResponse) {
const syncedAccounts = syncAccountsResponse.payload

updateAccounts(syncedAccounts)
},
onError(err) {
showAppNotification({
type: 'error',
message: locale(err.error),
})
},
})
}

if ($activeProfile.isStrongholdLocked) {
openPopup({ type: 'password', props: { onSuccess: _sync } })
} else {
_sync()
}
}
</script>

<div>
Expand Down Expand Up @@ -236,7 +212,7 @@
<section id="resyncAccounts" class="w-3/4">
<Text type="h4" classes="mb-3">{locale('views.settings.resyncAccounts.title')}</Text>
<Text type="p" secondary classes="mb-5">{locale('views.settings.resyncAccounts.description')}</Text>
<Button classes="w-1/4" onClick={resyncAccounts}>{locale('actions.syncAll')}</Button>
<Button classes="w-1/4" onClick={syncAccounts} disabled={$isSyncing}>{locale('actions.syncAll')}</Button>
</section>
<hr class="border-t border-gray-100 w-full border-solid pb-5 mt-5 justify-center" />
<section id="errorLog" class="w-3/4">
Expand Down
58 changes: 25 additions & 33 deletions packages/shared/routes/dashboard/wallet/Wallet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import { deepLinkRequestActive } from 'shared/lib/deepLinking'
import { priceData } from 'shared/lib/marketData'
import { DEFAULT_NODE, DEFAULT_NODES, network } from 'shared/lib/network'
import { showAppNotification } from 'shared/lib/notifications'
import { openPopup } from 'shared/lib/popup'
import { activeProfile, updateProfile } from 'shared/lib/profile'
import { walletRoute } from 'shared/lib/router'
import { WalletRoutes } from 'shared/lib/typings/routes'
import { formatUnit } from 'shared/lib/units'
import { showAppNotification } from 'shared/lib/notifications'
import {
AccountMessage,
api,
Expand All @@ -24,8 +24,8 @@
initialiseListeners,
isTransferring,
selectedAccountId,
syncAccounts,
transferState,
updateAccounts,
updateBalanceOverview,
wallet,
WalletAccount,
Expand Down Expand Up @@ -128,32 +128,40 @@
function getAccounts() {
api.getAccounts({
onSuccess(accountsResponse) {
syncAccounts()

const _totalBalance = {
balance: 0,
incoming: 0,
outgoing: 0,
const _continue = () => {
accountsLoaded.set(true)
syncAccounts()
}

for (const [idx, storedAccount] of accountsResponse.payload.entries()) {
if (accountsResponse.payload.length === 0) {
_continue()
} else {
const totalBalance = {
balance: 0,
incoming: 0,
outgoing: 0,
}

for (const [idx, storedAccount] of accountsResponse.payload.entries()) {
getAccountMeta(storedAccount.id, (err, meta) => {
if (!err) {
_totalBalance.balance += meta.balance
_totalBalance.incoming += meta.incoming
_totalBalance.outgoing += meta.outgoing
totalBalance.balance += meta.balance
totalBalance.incoming += meta.incoming
totalBalance.outgoing += meta.outgoing

const account = prepareAccountInfo(storedAccount, meta)
accounts.update((accounts) => [...accounts, account])

if (idx === accountsResponse.payload.length - 1) {
updateBalanceOverview(_totalBalance.balance, _totalBalance.incoming, _totalBalance.outgoing)
updateBalanceOverview(totalBalance.balance, totalBalance.incoming, totalBalance.outgoing)
_continue()
}
} else {
console.error(err)
}
})
}
}
},
onError(err) {
showAppNotification({
Expand Down Expand Up @@ -213,24 +221,6 @@
})
}

function syncAccounts() {
api.syncAccounts({
onSuccess(syncAccountsResponse) {
const syncedAccounts = syncAccountsResponse.payload

updateAccounts(syncedAccounts)

accountsLoaded.set(true)
},
onError(err) {
showAppNotification({
type: 'error',
message: locale(err.error),
})
},
})
}

function onCreateAccount(alias, completeCallback) {
const _create = () =>
api.createAccount(
Expand Down Expand Up @@ -372,8 +362,10 @@
{
messages: [
Object.assign({}, response.payload, {
incoming: isReceiverAccount
}), ..._account.messages],
incoming: isReceiverAccount,
}),
..._account.messages,
],
}
)
}
Expand Down
32 changes: 15 additions & 17 deletions packages/shared/routes/dashboard/wallet/views/WalletActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,24 @@
<div data-label="accounts" class="w-full h-full flex flex-col flex-no-wrap justify-start mb-6">
<div class="flex flex-row mb-6 justify-between items-center">
<Text type="h5">{locale('general.accounts')}</Text>
<Button onClick={handleCreateClick} secondary small icon="plus" disabled={!$accountsLoaded}>
<Button onClick={handleCreateClick} secondary small icon="plus">
{locale('actions.create')}
</Button>
</div>
{#if $accountsLoaded}
{#if $accounts.length > 0}
<div class="grid grid-cols-{$accounts.length <= 2 ? $accounts.length : '3'} gap-2 w-full flex-auto">
{#each $accounts as account}
<AccountTile
color={account.color}
name={account.alias}
balance={account.balance}
balanceEquiv={account.balanceEquiv}
size={$accounts.length >= 3 ? 's' : $accounts.length === 2 ? 'm' : 'l'}
onClick={() => handleAccountClick(account.id)} />
{/each}
</div>
{:else}
<Text>{locale('general.no_accounts')}</Text>
{/if}
{#if $accounts.length > 0}
<div class="grid grid-cols-{$accounts.length <= 2 ? $accounts.length : '3'} gap-2 w-full flex-auto">
{#each $accounts as account}
<AccountTile
color={account.color}
name={account.alias}
balance={account.balance}
balanceEquiv={account.balanceEquiv}
size={$accounts.length >= 3 ? 's' : $accounts.length === 2 ? 'm' : 'l'}
onClick={() => handleAccountClick(account.id)} />
{/each}
</div>
{:else}
<Text>{locale('general.no_accounts')}</Text>
{/if}
</div>
{#if $accounts.length > 0}
Expand Down
43 changes: 22 additions & 21 deletions packages/shared/routes/dashboard/wallet/views/WalletHistory.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang="typescript">
import { ActivityRow, Text } from 'shared/components'
import { ActivityRow, Icon, Text } from 'shared/components'
import { accountRoute, walletRoute } from 'shared/lib/router'
import { AccountRoutes, WalletRoutes } from 'shared/lib/typings/routes'
import type { AccountMessage, WalletAccount } from 'shared/lib/wallet'
import { selectedAccountId, selectedMessage } from 'shared/lib/wallet'
import { AccountMessage, isSyncing, selectedAccountId, selectedMessage, syncAccounts, WalletAccount } from 'shared/lib/wallet'
import { getContext } from 'svelte'
import type { Readable, Writable } from 'svelte/store'
import { get } from 'svelte/store'
Expand All @@ -12,7 +11,6 @@

const accounts = getContext<Writable<WalletAccount[]>>('walletAccounts')
const transactions = getContext<Readable<AccountMessage[]>>('walletTransactions')
const accountsLoaded = getContext<Writable<boolean>>('walletAccountsLoaded')

function handleTransactionClick(transaction) {
const sourceAccount = get(accounts).find((acc) => acc.index === transaction.account)
Expand All @@ -28,21 +26,24 @@
</script>

<div data-label="latest-transactions" class="h-full p-8 flex-grow flex flex-col">
<Text type="h4" classes="mb-5">{locale('general.latest_transactions')}</Text>
{#if $accountsLoaded}
<div class="overflow-y-auto flex-auto h-1 space-y-2">
{#if $transactions?.length}
{#each $transactions as transaction}
<ActivityRow
{...transaction}
onClick={() => handleTransactionClick(transaction)}
color={$accounts.find((acc) => acc.index === transaction.account)?.color} />
{/each}
{:else}
<div class="h-full flex flex-col items-center justify-center text-center">
<Text secondary>{locale('general.no_recent_history')}</Text>
</div>
{/if}
</div>
{/if}
<div class="w-full flex flex-row justify-between items-start">
<Text type="p" smaler bold classes="mb-5">{locale('general.latest_transactions')}</Text>
<button on:click={syncAccounts} class:pointer-events-none={$isSyncing}>
<Icon icon="refresh" classes="{$isSyncing && 'animate-spin'} text-gray-500 dark:text-white" />
</button>
</div>
<div class="overflow-y-auto flex-auto h-1 space-y-2">
{#if $transactions?.length}
{#each $transactions as transaction}
<ActivityRow
{...transaction}
onClick={() => handleTransactionClick(transaction)}
color={$accounts.find((acc) => acc.index === transaction.account)?.color} />
{/each}
{:else}
<div class="h-full flex flex-col items-center justify-center text-center">
<Text secondary>{locale('general.no_recent_history')}</Text>
</div>
{/if}
</div>
</div>