Skip to content

Commit

Permalink
Refactor total balance calculation
Browse files Browse the repository at this point in the history
Refactor bulkexport checkbox

Fix saturation and premium banner
  • Loading branch information
PeterBenc committed Jan 14, 2021
1 parent 73e3075 commit 589aab9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 43 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -14,5 +14,4 @@ app/dist/
server.cert
server.key
app/tests/.chrome/*
app/tests/*.module.wasm
.scannerwork
app/tests/*.module.wasm
45 changes: 30 additions & 15 deletions app/frontend/actions.ts
Expand Up @@ -37,7 +37,6 @@ import {localStorageVars} from './localStorage'

let wallet: ReturnType<typeof Wallet>
let account: ReturnType<typeof Account>
let cryptoProvider

const debounceEvent = (callback, time) => {
let interval
Expand Down Expand Up @@ -132,20 +131,28 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
const shouldExportPubKeyBulk = bulkExportPubKeys
const config = {...ADALITE_CONFIG, isShelleyCompatible, shouldExportPubKeyBulk}
try {
cryptoProvider = await ShelleyCryptoProviderFactory.getCryptoProvider(cryptoProviderType, {
walletSecretDef,
network: NETWORKS.SHELLEY[ADALITE_CONFIG.ADALITE_NETWORK],
config: ADALITE_CONFIG,
forceWebUsb,
})
const cryptoProvider = await ShelleyCryptoProviderFactory.getCryptoProvider(
cryptoProviderType,
{
walletSecretDef,
network: NETWORKS.SHELLEY[ADALITE_CONFIG.ADALITE_NETWORK],
config,
forceWebUsb, // TODO: into config
}
)

wallet = await Wallet({
config,
cryptoProvider,
})

const accountsInfo = await wallet.getAccountsInfo()
account = wallet.accounts[0] // THIS is not right
const {
accountsInfo,
totalRewardsBalance,
totalWalletBalance,
shouldShowSaturatedBanner,
} = await wallet.getAccountsInfo()
account = wallet.accounts[0] // TODO: Make accounts private

const conversionRatesPromise = getConversionRates(state)
const usingHwWallet = wallet.isHwWallet()
Expand All @@ -158,11 +165,13 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
const autoLogin = state.autoLogin
const ticker2Id = null
const shouldShowPremiumBanner =
state.shouldShowPremiumBanner && PREMIUM_MEMBER_BALANCE_TRESHHOLD < 0
// should work for full wallet balance
const isBigDelegator = BIG_DELEGATOR_THRESHOLD > 0 // todo
state.shouldShowPremiumBanner && PREMIUM_MEMBER_BALANCE_TRESHHOLD < totalWalletBalance
const isBigDelegator = totalWalletBalance > BIG_DELEGATOR_THRESHOLD
setState({
accounts: accountsInfo,
totalWalletBalance,
totalRewardsBalance,
shouldShowSaturatedBanner,
walletIsLoaded: true,
...accountsInfo[0],
loading: false,
Expand Down Expand Up @@ -203,18 +212,24 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
return true
}

//TODO reloadAccountInfo function

const reloadWalletInfo = async (state) => {
loadingAction(state, 'Reloading wallet info...')
try {
const accountsInfo = await wallet.getAccountsInfo()
const {
accountsInfo,
totalRewardsBalance,
totalWalletBalance,
shouldShowSaturatedBanner,
} = await wallet.getAccountsInfo()
const conversionRates = getConversionRates(state)

// timeout setting loading state, so that loading shows even if everything was cached
setTimeout(() => setState({loading: false}), 500)
setState({
accounts: accountsInfo,
totalWalletBalance,
totalRewardsBalance,
shouldShowSaturatedBanner,
...accountsInfo[account.accountIndex],
})
await fetchConversionRates(conversionRates)
Expand Down
17 changes: 6 additions & 11 deletions app/frontend/components/pages/accounts/accounts.tsx
Expand Up @@ -184,17 +184,10 @@ const Accounts = ({
shouldShowSendTransactionModal,
shouldShowDelegationModal,
selectedAccount,
totalWalletBalance,
totalRewardsBalance,
}) => {
const accountInfos = Object.values(accounts)
const totalBalance = accountInfos.reduce(
(a, {shelleyBalances}) =>
shelleyBalances.stakingBalance + shelleyBalances.nonStakingBalance + a,
0
)
const totalRewardBalance = accountInfos.reduce(
(a, {shelleyBalances}) => shelleyBalances.rewardsAccountBalance + a,
0
)
const firstAddressPerAccount = accountInfos.map((e: any) => e.visibleAddresses[0].address)
const InfoAlert = () => (
<Fragment>
Expand Down Expand Up @@ -236,14 +229,14 @@ const Accounts = ({
<div className="item">
<h2 className="card-title small-margin">Total balance</h2>
<div className="balance-amount">
{printAda(totalBalance as Lovelace)}
{printAda(totalWalletBalance as Lovelace)}
<AdaIcon />
</div>
</div>
<div className="item">
<h2 className="card-title small-margin">Total rewards balance</h2>
<div className="balance-amount">
{printAda(totalRewardBalance as Lovelace)}
{printAda(totalRewardsBalance as Lovelace)}
<AdaIcon />
</div>
</div>
Expand Down Expand Up @@ -293,6 +286,8 @@ export default connect(
shouldShowSendTransactionModal: state.shouldShowSendTransactionModal,
shouldShowDelegationModal: state.shouldShowDelegationModal,
selectedAccount: state.selectedAccount,
totalRewardsBalance: state.totalRewardsBalance,
totalWalletBalance: state.totalWalletBalance,
}),
actions
)(Accounts)
2 changes: 1 addition & 1 deletion app/frontend/components/pages/dashboard/dashboardPage.tsx
Expand Up @@ -220,7 +220,7 @@ export default connect(
isShelleyCompatible: state.isShelleyCompatible,
shouldShowNonShelleyCompatibleDialog: state.shouldShowNonShelleyCompatibleDialog,
shouldShowPremiumBanner: state.shouldShowPremiumBanner,
shouldShowSaturatedBanner: state.poolRecommendation.shouldShowSaturatedBanner,
shouldShowSaturatedBanner: state.shouldShowSaturatedBanner,
selectedAccount: state.selectedAccount,
}),
actions
Expand Down
16 changes: 8 additions & 8 deletions app/frontend/components/pages/login/hardwareAuth.tsx
Expand Up @@ -13,15 +13,15 @@ interface Props {
}

const LoadByHardwareWalletSection = ({loadWallet}: Props) => {
const [bulkExportPubKeys, setBulkExport] = useState(
const [dontBulkExportPubKeys, setBulkExport] = useState(
window.localStorage.getItem(localStorageVars.BULK_EXPORT) === 'true'
)
const toggleBulkExport = useCallback(
() => {
window.localStorage.setItem(localStorageVars.BULK_EXPORT, `${!bulkExportPubKeys}`)
setBulkExport(!bulkExportPubKeys)
window.localStorage.setItem(localStorageVars.BULK_EXPORT, `${!dontBulkExportPubKeys}`)
setBulkExport(!dontBulkExportPubKeys)
},
[bulkExportPubKeys]
[dontBulkExportPubKeys]
)

const TrezorAffiliateLink = (title) => (
Expand Down Expand Up @@ -61,7 +61,7 @@ const LoadByHardwareWalletSection = ({loadWallet}: Props) => {
onClick={() =>
loadWallet({
cryptoProviderType: CRYPTO_PROVIDER_TYPES.TREZOR,
bulkExportPubKeys: !bulkExportPubKeys,
bulkExportPubKeys: !dontBulkExportPubKeys,
})
}
>
Expand Down Expand Up @@ -93,7 +93,7 @@ const LoadByHardwareWalletSection = ({loadWallet}: Props) => {
onClick={() =>
loadWallet({
cryptoProviderType: CRYPTO_PROVIDER_TYPES.LEDGER,
bulkExportPubKeys: !bulkExportPubKeys,
bulkExportPubKeys: !dontBulkExportPubKeys,
})
}
>
Expand All @@ -110,7 +110,7 @@ const LoadByHardwareWalletSection = ({loadWallet}: Props) => {
loadWallet({
cryptoProviderType: CRYPTO_PROVIDER_TYPES.LEDGER,
forceWebUsb: true,
bulkExportPubKeys: !bulkExportPubKeys,
bulkExportPubKeys: !dontBulkExportPubKeys,
})
}
>
Expand All @@ -122,7 +122,7 @@ const LoadByHardwareWalletSection = ({loadWallet}: Props) => {
<label className="checkbox">
<input
type="checkbox"
checked={!bulkExportPubKeys}
checked={!dontBulkExportPubKeys}
onChange={toggleBulkExport}
className="checkbox-input"
/>
Expand Down
5 changes: 5 additions & 0 deletions app/frontend/state.ts
Expand Up @@ -144,11 +144,14 @@ export interface State {
status: string
shouldShowSaturatedBanner: boolean
}
shouldShowSaturatedBanner?: boolean
isBigDelegator: boolean
accounts: any
activeAccount: number
selectedAccount: number
targetAccount: number
totalWalletBalance: number
totalRewardsBalance: number
shouldShowSendTransactionModal: boolean
shouldShowDelegationModal: boolean
sendTransactionTitle: string
Expand Down Expand Up @@ -265,6 +268,8 @@ const initialState: State = {
activeAccount: 0,
selectedAccount: 0,
targetAccount: 0,
totalWalletBalance: 0,
totalRewardsBalance: 0,
shouldShowSendTransactionModal: false,
shouldShowDelegationModal: false,
sendTransactionTitle: '',
Expand Down
24 changes: 18 additions & 6 deletions app/frontend/wallet/wallet.ts
Expand Up @@ -71,14 +71,27 @@ const Wallet = ({config, cryptoProvider}) => {
return null
}

function getBalance() {
return 0
}

async function getAccountsInfo() {
await discoverAccounts()
const accountsInfo = await Promise.all(accounts.map((account) => account.getWalletInfo()))
return Object.assign({}, accountsInfo)
const totalWalletBalance = accountsInfo.reduce(
(a, {shelleyBalances}) =>
shelleyBalances.stakingBalance + shelleyBalances.nonStakingBalance + a,
0
)
const totalRewardsBalance = accountsInfo.reduce(
(a, {shelleyBalances}) => shelleyBalances.rewardsAccountBalance + a,
0
)
const shouldShowSaturatedBanner = accountsInfo.some(
({poolRecommendation}) => poolRecommendation.shouldShowSaturatedBanner
)
return {
accountsInfo: Object.assign({}, accountsInfo),
totalWalletBalance,
totalRewardsBalance,
shouldShowSaturatedBanner,
}
}

return {
Expand All @@ -88,7 +101,6 @@ const Wallet = ({config, cryptoProvider}) => {
getWalletSecretDef,
fetchTxInfo,
checkCryptoProviderVersion,
getBalance,
accounts,
loadNewAccount,
discoverAccounts,
Expand Down

0 comments on commit 589aab9

Please sign in to comment.