2017-09-25 02:06:28 +00:00
|
|
|
import {
|
2017-07-13 21:02:39 +00:00
|
|
|
SetBalanceAction,
|
2017-09-25 02:06:28 +00:00
|
|
|
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-12 19:45:52 +00:00
|
|
|
import { Wei, TokenValue } from 'libs/units';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { BroadcastTransactionStatus } from 'libs/transaction';
|
2017-09-12 00:26:16 +00:00
|
|
|
import { IWallet } 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-12 19:45:52 +00:00
|
|
|
balance?: Wei | null;
|
2017-07-04 03:21:19 +00:00
|
|
|
tokens: {
|
2017-11-12 19:45:52 +00:00
|
|
|
[key: string]: TokenValue;
|
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-09-12 22:15:23 +00:00
|
|
|
balance: 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-09-12 22:15:23 +00:00
|
|
|
return { ...state, inst: action.payload, balance: null, tokens: {} };
|
2017-07-03 23:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
function setBalance(state: State, action: SetBalanceAction): State {
|
2017-11-12 19:45:52 +00:00
|
|
|
const weiBalance = action.payload;
|
|
|
|
return { ...state, balance: weiBalance };
|
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-10 03:30:20 +00:00
|
|
|
case TypeKeys.WALLET_SET_BALANCE:
|
2017-07-13 21:02:39 +00:00
|
|
|
return setBalance(state, action);
|
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
|
|
|
}
|