Skip to content

Commit

Permalink
Make account part of wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBenc committed Jan 14, 2021
1 parent f034edd commit 580f517
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
39 changes: 10 additions & 29 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>
const accounts = new Map()
let cryptoProvider

const debounceEvent = (callback, time) => {
Expand Down Expand Up @@ -141,14 +140,7 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
isShelleyCompatible,
})

const newAccount = await Account({
config: ADALITE_CONFIG,
cryptoProvider,
isShelleyCompatible,
accountIndex: 0,
})
accounts.set(0, newAccount)
account = accounts.get(0)
account = wallet.accounts[0]

const walletInfo = await account.getWalletInfo()
const conversionRatesPromise = getConversionRates(state)
Expand Down Expand Up @@ -1165,35 +1157,24 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
})
}

const loadWalletInfo = async (state) => {
const walletInfo = await account.getWalletInfo()
const loadNewAccount = async (state: State, accountIndex: number) => {
wallet.loadNewAccount(accountIndex)
const walletInfo = await wallet.accounts[accountIndex].getWalletInfo()
setState({
accounts: {
...state.accounts,
[account.accountIndex]: walletInfo,
[accountIndex]: walletInfo,
},
})
stopLoadingAction(state, {})
}

const loadNewAccount = async (state: State, accountIndex: number) => {
const setAccount = async (state: State, accountIndex: number) => {
loadingAction(state, 'Loading account')
const newWallet = await Account({
config: ADALITE_CONFIG,
cryptoProvider,
isShelleyCompatible: true,
accountIndex,
})
accounts.set(accountIndex, newWallet)
account = accounts.get(accountIndex)
await loadWalletInfo(state)
}

const setWalletInfo = async (state, accountIndex: number) => {
if (!accounts.has(accountIndex)) {
if (!wallet.accounts[accountIndex]) {
await loadNewAccount(state, accountIndex)
}
account = accounts.get(accountIndex)
stopLoadingAction(state, {})
account = wallet.accounts[accountIndex]
const newState = getState()
const walletInfo = newState.accounts[accountIndex]
setState({
Expand All @@ -1209,7 +1190,7 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
loadingAction,
stopLoadingAction,
setAuthMethod,
setWalletInfo,
setAccount,
loadWallet,
logout,
exportJsonWallet,
Expand Down
4 changes: 2 additions & 2 deletions app/frontend/components/pages/accounts/accounts.tsx
Expand Up @@ -4,15 +4,15 @@ import actions from '../../../actions'
import range from '../../../../frontend/wallet/helpers/range'
import {ACCOUNT_COUNT} from '../../../../frontend/wallet/constants'

const Accounts = ({setWalletInfo}) => (
const Accounts = ({setAccount}) => (
<div className="card" style={'width: 100%;'}>
<h2 className="card-title small-margin" />
<div className="account-dropdown">
{range(0, ACCOUNT_COUNT).map((i) => (
<a
key={i}
onClick={() => {
setWalletInfo(i)
setAccount(i)
}}
>
Account {i}
Expand Down
4 changes: 1 addition & 3 deletions app/frontend/wallet/account.ts
@@ -1,5 +1,4 @@
import AddressManager from './address-manager'
import BlockchainExplorer from './blockchain-explorer'
import PseudoRandom from './helpers/PseudoRandom'
import {MAX_INT32} from './constants'
import NamedError from '../helpers/NamedError'
Expand Down Expand Up @@ -123,6 +122,7 @@ const Account = ({
randomChangeSeed,
cryptoProvider,
isShelleyCompatible,
blockchainExplorer,
accountIndex,
}: any) => {
const {
Expand All @@ -137,8 +137,6 @@ const Account = ({

generateNewSeeds()

const blockchainExplorer = BlockchainExplorer(config)

const myAddresses = MyAddresses({
accountIndex,
cryptoProvider,
Expand Down
24 changes: 24 additions & 0 deletions app/frontend/wallet/wallet.ts
@@ -1,8 +1,30 @@
import {Account} from './account'
import BlockchainExplorer from './blockchain-explorer'

const Wallet = ({config, cryptoProvider, isShelleyCompatible}) => {
const blockchainExplorer = BlockchainExplorer(config)

const accounts: Array<ReturnType<typeof Account>> = []

accounts[0] = Account({
config,
isShelleyCompatible,
cryptoProvider,
blockchainExplorer,
accountIndex: 0,
})

function loadNewAccount(accountIndex: number) {
const newAccount = Account({
config,
isShelleyCompatible,
cryptoProvider,
blockchainExplorer,
accountIndex,
})
accounts.push(newAccount)
}

function isHwWallet() {
return cryptoProvider.isHwWallet()
}
Expand Down Expand Up @@ -48,6 +70,8 @@ const Wallet = ({config, cryptoProvider, isShelleyCompatible}) => {
fetchTxInfo,
checkCryptoProviderVersion,
getBalance,
accounts,
loadNewAccount,
}
}

Expand Down

0 comments on commit 580f517

Please sign in to comment.