2017-11-15 03:44:55 +00:00
|
|
|
import { SetBalanceFullfilledAction } from 'actions/wallet/actionTypes';
|
2017-09-25 02:06:28 +00:00
|
|
|
import {
|
|
|
|
SetTokenBalancesAction,
|
|
|
|
SetWalletAction,
|
2017-11-10 03:30:20 +00:00
|
|
|
WalletAction,
|
|
|
|
TypeKeys
|
2017-07-04 03:21:19 +00:00
|
|
|
} from 'actions/wallet';
|
2017-11-15 03:44:55 +00:00
|
|
|
import { TokenValue } from 'libs/units';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { BroadcastTransactionStatus } from 'libs/transaction';
|
2017-11-15 03:44:55 +00:00
|
|
|
import { IWallet, Balance } from 'libs/wallet';
|
2017-09-05 21:28:19 +00:00
|
|
|
import { getTxFromBroadcastTransactionStatus } from 'selectors/wallet';
|
Sidebar refactor / style update (#173)
* Convert bootstrap to sass instead of checked in and less
* Darken body, adjust header.
* First pass at tab styles, each tab will need a lot of individual love tho.
* Update footer to main site content, improve responsiveness.
* Missing key added.
* Fix dropdowns.
* Convert GenerateWallet HTML over, still needs styling.
* Send form.
* Current rates styled.
* CurrencySwap form styles.
* SwapInfoHeader styled.
* Finish up swap restyling, minor usability improvements for mobile.
* Fix up notifications / alert customizations
* Import v3 variables.
* Fix notification spacing.
* Align input height base with buttons.
* Revert height base, add additional bootstrap overrides.
* Grid overrides.
* Move overrides to their own folder. Adjust naming.
* Fix inconsistencies.
* Style generate wallet pt 1.
* Style generate wallet pt 2
* Style generate wallet pt 3
* Fix swap
* Added some missing overries, fixed the fallout.
* Remove header text, indicate alpha version.
* Fix radio / checkbox weights.
* Bind => arrow
* Convert simpledropdown to proper form select, instead of weirdly implemented nonfuncitoning dropdown.
* Fix token balances buttons, footr icons.
* Break out files, style up account info.
* Style up token balances.
* Equivalent values styling.
* Sidebar promos.
* Fix up delete button and add custom form.
* Even spacing.
* Unlog
* Convert Big types to Ether types
* Fix test to expect Ether instead of Big
2017-09-08 19:26:51 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface State {
|
|
|
|
inst?: IWallet | null;
|
2017-07-13 21:02:39 +00:00
|
|
|
// in ETH
|
2017-11-15 03:44:55 +00:00
|
|
|
balance: Balance | { wei: null };
|
2017-07-04 03:21:19 +00:00
|
|
|
tokens: {
|
2017-11-30 05:35:17 +00:00
|
|
|
[key: string]: {
|
|
|
|
balance: TokenValue;
|
|
|
|
error: string | null;
|
|
|
|
};
|
2017-09-25 02:06:28 +00:00
|
|
|
};
|
|
|
|
transactions: BroadcastTransactionStatus[];
|
|
|
|
}
|
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-11-15 03:44:55 +00:00
|
|
|
balance: { isPending: false, wei: null },
|
2017-08-31 04:00:31 +00:00
|
|
|
tokens: {},
|
|
|
|
transactions: []
|
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 {
|
2017-11-15 03:44:55 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
inst: action.payload,
|
|
|
|
balance: INITIAL_STATE.balance,
|
|
|
|
tokens: INITIAL_STATE.tokens
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setBalancePending(state: State): State {
|
|
|
|
return { ...state, balance: { ...state.balance, isPending: true } };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setBalanceFullfilled(
|
|
|
|
state: State,
|
|
|
|
action: SetBalanceFullfilledAction
|
|
|
|
): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
balance: { wei: action.payload, isPending: false }
|
|
|
|
};
|
2017-07-03 23:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 03:44:55 +00:00
|
|
|
function setBalanceRejected(state: State): State {
|
|
|
|
return { ...state, balance: { ...state.balance, isPending: false } };
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setTokenBalances(state: State, action: SetTokenBalancesAction): State {
|
|
|
|
return { ...state, tokens: { ...state.tokens, ...action.payload } };
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:00:31 +00:00
|
|
|
function handleUpdateTxArray(
|
2017-09-25 02:06:28 +00:00
|
|
|
transactions: BroadcastTransactionStatus[],
|
2017-09-05 21:28:19 +00:00
|
|
|
broadcastStatusTx: BroadcastTransactionStatus,
|
2017-08-31 04:00:31 +00:00
|
|
|
isBroadcasting: boolean,
|
|
|
|
successfullyBroadcast: boolean
|
2017-09-25 02:06:28 +00:00
|
|
|
): BroadcastTransactionStatus[] {
|
2017-08-31 04:00:31 +00:00
|
|
|
return transactions.map(item => {
|
|
|
|
if (item === broadcastStatusTx) {
|
|
|
|
return { ...item, isBroadcasting, successfullyBroadcast };
|
|
|
|
} else {
|
|
|
|
return { ...item };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleTxBroadcastCompleted(
|
|
|
|
state: State,
|
|
|
|
signedTx: string,
|
|
|
|
successfullyBroadcast: boolean
|
2017-09-25 02:06:28 +00:00
|
|
|
): BroadcastTransactionStatus[] {
|
2017-09-05 21:28:19 +00:00
|
|
|
const existingTx = getTxFromBroadcastTransactionStatus(
|
2017-08-31 04:00:31 +00:00
|
|
|
state.transactions,
|
|
|
|
signedTx
|
|
|
|
);
|
|
|
|
if (existingTx) {
|
|
|
|
const isBroadcasting = false;
|
|
|
|
return handleUpdateTxArray(
|
|
|
|
state.transactions,
|
|
|
|
existingTx,
|
|
|
|
isBroadcasting,
|
|
|
|
successfullyBroadcast
|
|
|
|
);
|
|
|
|
} else {
|
2017-09-05 21:28:19 +00:00
|
|
|
return state.transactions;
|
2017-08-31 04:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleBroadcastTxRequested(state: State, signedTx: string) {
|
2017-09-05 21:28:19 +00:00
|
|
|
const existingTx = getTxFromBroadcastTransactionStatus(
|
2017-08-31 04:00:31 +00:00
|
|
|
state.transactions,
|
|
|
|
signedTx
|
|
|
|
);
|
|
|
|
const isBroadcasting = true;
|
|
|
|
const successfullyBroadcast = false;
|
|
|
|
if (!existingTx) {
|
|
|
|
return state.transactions.concat([
|
|
|
|
{
|
|
|
|
signedTx,
|
|
|
|
isBroadcasting,
|
|
|
|
successfullyBroadcast
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return handleUpdateTxArray(
|
|
|
|
state.transactions,
|
|
|
|
existingTx,
|
|
|
|
isBroadcasting,
|
|
|
|
successfullyBroadcast
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_SET:
|
2017-07-13 21:02:39 +00:00
|
|
|
return setWallet(state, action);
|
2017-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_RESET:
|
2017-10-10 23:08:55 +00:00
|
|
|
return INITIAL_STATE;
|
2017-11-15 03:44:55 +00:00
|
|
|
case TypeKeys.WALLET_SET_BALANCE_PENDING:
|
|
|
|
return setBalancePending(state);
|
|
|
|
case TypeKeys.WALLET_SET_BALANCE_FULFILLED:
|
|
|
|
return setBalanceFullfilled(state, action);
|
|
|
|
case TypeKeys.WALLET_SET_BALANCE_REJECTED:
|
|
|
|
return setBalanceRejected(state);
|
2017-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_SET_TOKEN_BALANCES:
|
2017-07-13 21:02:39 +00:00
|
|
|
return setTokenBalances(state, action);
|
2017-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_BROADCAST_TX_REQUESTED:
|
2017-08-31 04:00:31 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
transactions: handleBroadcastTxRequested(state, action.payload.signedTx)
|
|
|
|
};
|
2017-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_BROADCAST_TX_SUCCEEDED:
|
2017-08-31 04:00:31 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
transactions: handleTxBroadcastCompleted(
|
|
|
|
state,
|
|
|
|
action.payload.signedTx,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
};
|
2017-11-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_BROADCAST_TX_FAILED:
|
2017-08-31 04:00:31 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
transactions: handleTxBroadcastCompleted(
|
|
|
|
state,
|
|
|
|
action.payload.signedTx,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
};
|
2017-07-04 03:21:19 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|