2017-06-29 23:03:11 +00:00
|
|
|
// @flow
|
2017-07-04 03:21:19 +00:00
|
|
|
import type {
|
|
|
|
WalletAction,
|
2017-07-13 21:02:39 +00:00
|
|
|
SetWalletAction,
|
|
|
|
SetBalanceAction,
|
|
|
|
SetTokenBalancesAction
|
2017-07-04 03:21:19 +00:00
|
|
|
} from 'actions/wallet';
|
2017-07-13 21:02:39 +00:00
|
|
|
import { BaseWallet } from 'libs/wallet';
|
|
|
|
import { toEther } from 'libs/units';
|
|
|
|
import Big from 'big.js';
|
2017-06-29 23:03:11 +00:00
|
|
|
|
2017-07-03 23:59:27 +00:00
|
|
|
export type State = {
|
2017-07-04 03:21:19 +00:00
|
|
|
inst: ?BaseWallet,
|
2017-07-13 21:02:39 +00:00
|
|
|
// in ETH
|
|
|
|
balance: Big,
|
2017-07-04 03:21:19 +00:00
|
|
|
tokens: {
|
2017-07-13 21:02:39 +00:00
|
|
|
[string]: Big
|
2017-07-04 03:21:19 +00:00
|
|
|
}
|
2017-07-03 23:59:27 +00:00
|
|
|
};
|
2017-06-29 23:03:11 +00:00
|
|
|
|
2017-07-27 17:05:09 +00:00
|
|
|
export const INITIAL_STATE: State = {
|
2017-07-04 03:21:19 +00:00
|
|
|
inst: null,
|
2017-07-13 21:02:39 +00:00
|
|
|
balance: new Big(0),
|
2017-07-04 03:21:19 +00:00
|
|
|
tokens: {}
|
2017-07-03 23:59:27 +00:00
|
|
|
};
|
2017-06-29 23:03:11 +00:00
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
function setWallet(state: State, action: SetWalletAction): State {
|
|
|
|
return { ...state, inst: action.payload, balance: new Big(0), tokens: {} };
|
2017-07-03 23:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
function setBalance(state: State, action: SetBalanceAction): State {
|
|
|
|
const ethBalance = toEther(action.payload, 'wei');
|
|
|
|
return { ...state, balance: ethBalance };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTokenBalances(state: State, action: SetTokenBalancesAction): State {
|
|
|
|
return { ...state, tokens: { ...state.tokens, ...action.payload } };
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 21:02:13 +00:00
|
|
|
export function wallet(
|
2017-07-27 17:05:09 +00:00
|
|
|
state: State = INITIAL_STATE,
|
2017-07-16 21:02:13 +00:00
|
|
|
action: WalletAction
|
|
|
|
): State {
|
2017-07-04 03:21:19 +00:00
|
|
|
switch (action.type) {
|
2017-07-13 21:02:39 +00:00
|
|
|
case 'WALLET_SET':
|
|
|
|
return setWallet(state, action);
|
|
|
|
case 'WALLET_SET_BALANCE':
|
|
|
|
return setBalance(state, action);
|
|
|
|
case 'WALLET_SET_TOKEN_BALANCES':
|
|
|
|
return setTokenBalances(state, action);
|
2017-07-04 03:21:19 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|