1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/ui/app/actions.js

798 lines
19 KiB
JavaScript
Raw Normal View History

var actions = {
GO_HOME: 'GO_HOME',
goHome: goHome,
2016-05-18 21:30:03 +02:00
// menu state
getNetworkStatus: 'getNetworkStatus',
// remote state
UPDATE_METAMASK_STATE: 'UPDATE_METAMASK_STATE',
updateMetamaskState: updateMetamaskState,
// intialize screen
2016-06-17 00:58:45 +02:00
AGREE_TO_DISCLAIMER: 'AGREE_TO_DISCLAIMER',
agreeToDisclaimer: agreeToDisclaimer,
CREATE_NEW_VAULT_IN_PROGRESS: 'CREATE_NEW_VAULT_IN_PROGRESS',
SHOW_CREATE_VAULT: 'SHOW_CREATE_VAULT',
SHOW_RESTORE_VAULT: 'SHOW_RESTORE_VAULT',
SHOW_INIT_MENU: 'SHOW_INIT_MENU',
SHOW_NEW_VAULT_SEED: 'SHOW_NEW_VAULT_SEED',
SHOW_INFO_PAGE: 'SHOW_INFO_PAGE',
RECOVER_FROM_SEED: 'RECOVER_FROM_SEED',
CLEAR_SEED_WORD_CACHE: 'CLEAR_SEED_WORD_CACHE',
clearSeedWordCache: clearSeedWordCache,
recoverFromSeed: recoverFromSeed,
unlockMetamask: unlockMetamask,
unlockFailed: unlockFailed,
showCreateVault: showCreateVault,
showRestoreVault: showRestoreVault,
showInitializeMenu: showInitializeMenu,
createNewVault: createNewVault,
createNewVaultInProgress: createNewVaultInProgress,
showNewVaultSeed: showNewVaultSeed,
showInfoPage: showInfoPage,
// seed recovery actions
REVEAL_SEED_CONFIRMATION: 'REVEAL_SEED_CONFIRMATION',
revealSeedConfirmation: revealSeedConfirmation,
requestRevealSeed: requestRevealSeed,
// unlock screen
UNLOCK_IN_PROGRESS: 'UNLOCK_IN_PROGRESS',
UNLOCK_FAILED: 'UNLOCK_FAILED',
UNLOCK_METAMASK: 'UNLOCK_METAMASK',
LOCK_METAMASK: 'LOCK_METAMASK',
tryUnlockMetamask: tryUnlockMetamask,
lockMetamask: lockMetamask,
unlockInProgress: unlockInProgress,
// error handling
displayWarning: displayWarning,
2016-06-30 20:30:31 +02:00
showWarning: showWarning, // alias
DISPLAY_WARNING: 'DISPLAY_WARNING',
HIDE_WARNING: 'HIDE_WARNING',
hideWarning: hideWarning,
// accounts screen
SET_SELECTED_ACCOUNT: 'SET_SELECTED_ACCOUNT',
SHOW_ACCOUNT_DETAIL: 'SHOW_ACCOUNT_DETAIL',
SHOW_ACCOUNTS_PAGE: 'SHOW_ACCOUNTS_PAGE',
SHOW_CONF_TX_PAGE: 'SHOW_CONF_TX_PAGE',
SHOW_CONF_MSG_PAGE: 'SHOW_CONF_MSG_PAGE',
REVEAL_ACCOUNT: 'REVEAL_ACCOUNT',
revealAccount: revealAccount,
2016-07-22 19:15:39 +02:00
SET_CURRENT_FIAT: 'SET_CURRENT_FIAT',
setCurrentFiat: setCurrentFiat,
// account detail screen
SHOW_SEND_PAGE: 'SHOW_SEND_PAGE',
showSendPage: showSendPage,
REQUEST_ACCOUNT_EXPORT: 'REQUEST_ACCOUNT_EXPORT',
requestExportAccount: requestExportAccount,
EXPORT_ACCOUNT: 'EXPORT_ACCOUNT',
exportAccount: exportAccount,
SHOW_PRIVATE_KEY: 'SHOW_PRIVATE_KEY',
showPrivateKey: showPrivateKey,
SAVE_ACCOUNT_LABEL: 'SAVE_ACCOUNT_LABEL',
saveAccountLabel: saveAccountLabel,
2016-08-10 22:43:01 +02:00
AGREE_TO_ETH_WARNING: 'AGREE_TO_ETH_WARNING',
agreeToEthWarning: agreeToEthWarning,
SHOW_ETH_WARNING: 'SHOW_ETH_WARNING',
showEthWarning: showEthWarning,
// tx conf screen
COMPLETED_TX: 'COMPLETED_TX',
TRANSACTION_ERROR: 'TRANSACTION_ERROR',
NEXT_TX: 'NEXT_TX',
PREVIOUS_TX: 'PREV_TX',
setSelectedAddress: setSelectedAddress,
signMsg: signMsg,
cancelMsg: cancelMsg,
sendTx: sendTx,
2016-05-06 23:46:04 +02:00
signTx: signTx,
cancelTx: cancelTx,
completedTx: completedTx,
txError: txError,
nextTx: nextTx,
previousTx: previousTx,
viewPendingTx: viewPendingTx,
VIEW_PENDING_TX: 'VIEW_PENDING_TX',
// app messages
showAccountDetail: showAccountDetail,
BACK_TO_ACCOUNT_DETAIL: 'BACK_TO_ACCOUNT_DETAIL',
backToAccountDetail: backToAccountDetail,
showAccountsPage: showAccountsPage,
showConfTxPage: showConfTxPage,
confirmSeedWords: confirmSeedWords,
// config screen
SHOW_CONFIG_PAGE: 'SHOW_CONFIG_PAGE',
SET_RPC_TARGET: 'SET_RPC_TARGET',
SET_PROVIDER_TYPE: 'SET_PROVIDER_TYPE',
USE_ETHERSCAN_PROVIDER: 'USE_ETHERSCAN_PROVIDER',
useEtherscanProvider: useEtherscanProvider,
showConfigPage: showConfigPage,
setRpcTarget: setRpcTarget,
setProviderType: setProviderType,
// hacky - need a way to get a reference to account manager
_setAccountManager: _setAccountManager,
// loading overlay
SHOW_LOADING: 'SHOW_LOADING_INDICATION',
HIDE_LOADING: 'HIDE_LOADING_INDICATION',
showLoadingIndication: showLoadingIndication,
hideLoadingIndication: hideLoadingIndication,
2016-08-10 22:43:01 +02:00
// buy Eth with coinbase
BUY_ETH: 'BUY_ETH',
buyEth: buyEth,
buyEthView: buyEthView,
BUY_ETH_VIEW: 'BUY_ETH_VIEW',
2016-08-10 22:43:01 +02:00
UPDATE_COINBASE_AMOUNT: 'UPDATE_COIBASE_AMOUNT',
updateCoinBaseAmount: updateCoinBaseAmount,
UPDATE_BUY_ADDRESS: 'UPDATE_BUY_ADDRESS',
updateBuyAddress: updateBuyAddress,
COINBASE_SUBVIEW: 'COINBASE_SUBVIEW',
coinBaseSubview: coinBaseSubview,
SHAPESHIFT_SUBVIEW: 'SHAPESHIFT_SUBVIEW',
shapeShiftSubview: shapeShiftSubview,
PAIR_UPDATE: 'PAIR_UPDATE',
pairUpdate: pairUpdate,
coinShiftRquest: coinShiftRquest,
SHOW_SUB_LOADING_INDICATION: 'SHOW_SUB_LOADING_INDICATION',
showSubLoadingIndication: showSubLoadingIndication,
HIDE_SUB_LOADING_INDICATION: 'HIDE_SUB_LOADING_INDICATION',
hideSubLoadingIndication: hideSubLoadingIndication,
// QR STUFF:
SHOW_QR: 'SHOW_QR',
showQrView: showQrView,
2016-08-18 19:40:35 +02:00
reshowQrCode: reshowQrCode,
SHOW_QR_VIEW: 'SHOW_QR_VIEW',
// FORGOT PASSWORD:
BACK_TO_INIT_MENU: 'BACK_TO_INIT_MENU',
goBackToInitView: goBackToInitView,
RECOVERY_IN_PROGRESS: 'RECOVERY_IN_PROGRESS',
BACK_TO_UNLOCK_VIEW: 'BACK_TO_UNLOCK_VIEW',
backToUnlockView: backToUnlockView,
}
module.exports = actions
var _accountManager = null
2016-06-21 22:18:32 +02:00
function _setAccountManager (accountManager) {
_accountManager = accountManager
}
2016-06-21 22:18:32 +02:00
function goHome () {
return {
2016-05-25 21:39:12 +02:00
type: actions.GO_HOME,
}
}
// async actions
2016-06-21 22:18:32 +02:00
function tryUnlockMetamask (password) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
2016-05-25 21:39:12 +02:00
dispatch(actions.unlockInProgress())
_accountManager.submitPassword(password, (err, selectedAccount) => {
dispatch(actions.hideLoadingIndication())
if (err) {
2016-05-25 21:39:12 +02:00
dispatch(actions.unlockFailed())
} else {
2016-05-25 21:39:12 +02:00
dispatch(actions.unlockMetamask(selectedAccount))
}
})
}
}
2016-06-21 22:18:32 +02:00
function createNewVault (password, entropy) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.createNewVaultInProgress())
_accountManager.createNewVault(password, entropy, (err, result) => {
2016-06-21 22:56:04 +02:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-05-25 21:39:12 +02:00
dispatch(actions.showNewVaultSeed(result))
})
}
}
2016-06-21 22:18:32 +02:00
function revealSeedConfirmation () {
return {
type: this.REVEAL_SEED_CONFIRMATION,
}
}
2016-06-21 22:18:32 +02:00
function requestRevealSeed (password) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
_accountManager.tryPassword(password, (err, seed) => {
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
_accountManager.recoverSeed((err, seed) => {
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.showNewVaultSeed(seed))
})
})
}
}
2016-06-21 22:18:32 +02:00
function recoverFromSeed (password, seed) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
// dispatch(actions.createNewVaultInProgress())
dispatch(actions.showLoadingIndication())
_accountManager.recoverFromSeed(password, seed, (err, metamaskState) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
var account = Object.keys(metamaskState.identities)[0]
2016-05-25 21:39:12 +02:00
dispatch(actions.unlockMetamask(account))
2016-06-21 22:18:32 +02:00
})
}
}
2016-06-21 22:18:32 +02:00
function showInfoPage () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_INFO_PAGE,
}
}
2016-06-21 22:18:32 +02:00
function setSelectedAddress (address) {
return (dispatch) => {
_accountManager.setSelectedAddress(address)
}
}
2016-06-21 22:18:32 +02:00
function revealAccount () {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
_accountManager.revealAccount((err) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
dispatch({
2016-05-25 21:39:12 +02:00
type: actions.REVEAL_ACCOUNT,
})
})
}
}
function setCurrentFiat (fiat) {
return (dispatch) => {
dispatch(this.showLoadingIndication())
_accountManager.setCurrentFiat(fiat, (data, err) => {
dispatch(this.hideLoadingIndication())
dispatch({
type: this.SET_CURRENT_FIAT,
value: {
currentFiat: data.currentFiat,
conversionRate: data.conversionRate,
conversionDate: data.conversionDate,
2016-07-22 19:15:39 +02:00
},
})
})
}
}
2016-06-21 22:18:32 +02:00
function signMsg (msgData) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
_accountManager.signMessage(msgData, (err) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
2016-05-25 21:39:12 +02:00
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.completedTx(msgData.metamaskId))
})
}
}
2016-06-21 22:18:32 +02:00
function signTx (txData) {
2016-05-06 23:46:04 +02:00
return (dispatch) => {
web3.eth.sendTransaction(txData, (err, data) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
2016-05-06 23:46:04 +02:00
2016-05-25 21:39:12 +02:00
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideWarning())
dispatch(actions.goHome())
2016-05-06 23:46:04 +02:00
})
2016-09-01 20:30:42 +02:00
dispatch(this.showConfTxPage())
2016-05-06 23:46:04 +02:00
}
}
2016-06-21 22:18:32 +02:00
function sendTx (txData) {
return (dispatch) => {
_accountManager.approveTransaction(txData.id, (err) => {
if (err) {
alert(err.message)
2016-05-25 21:39:12 +02:00
dispatch(actions.txError(err))
return console.error(err.message)
}
2016-05-25 21:39:12 +02:00
dispatch(actions.completedTx(txData.id))
})
}
}
2016-06-21 22:18:32 +02:00
function completedTx (id) {
return {
2016-05-25 21:39:12 +02:00
type: actions.COMPLETED_TX,
id,
}
}
2016-06-21 22:18:32 +02:00
function txError (err) {
return {
2016-05-25 21:39:12 +02:00
type: actions.TRANSACTION_ERROR,
message: err.message,
}
}
2016-06-21 22:18:32 +02:00
function cancelMsg (msgData) {
_accountManager.cancelMessage(msgData.id)
2016-05-25 21:39:12 +02:00
return actions.completedTx(msgData.id)
}
2016-06-21 22:18:32 +02:00
function cancelTx (txData) {
_accountManager.cancelTransaction(txData.id)
2016-05-25 21:39:12 +02:00
return actions.completedTx(txData.id)
}
//
// initialize screen
//
2016-06-21 22:18:32 +02:00
function showCreateVault () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_CREATE_VAULT,
}
}
2016-06-21 22:18:32 +02:00
function showRestoreVault () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_RESTORE_VAULT,
}
}
2016-06-21 22:18:32 +02:00
function showInitializeMenu () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_INIT_MENU,
}
}
2016-06-21 22:18:32 +02:00
function agreeToDisclaimer () {
2016-06-17 00:58:45 +02:00
return (dispatch) => {
dispatch(this.showLoadingIndication())
2016-06-17 01:40:55 +02:00
_accountManager.agreeToDisclaimer((err) => {
2016-06-21 22:56:04 +02:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-06-17 01:40:55 +02:00
dispatch(this.hideLoadingIndication())
2016-06-17 00:58:45 +02:00
dispatch({
type: this.AGREE_TO_DISCLAIMER,
})
})
}
}
2016-06-21 22:18:32 +02:00
function createNewVaultInProgress () {
return {
2016-05-25 21:39:12 +02:00
type: actions.CREATE_NEW_VAULT_IN_PROGRESS,
}
}
2016-06-21 22:18:32 +02:00
function showNewVaultSeed (seed) {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_NEW_VAULT_SEED,
value: seed,
}
}
function backToUnlockView () {
return {
type: actions.BACK_TO_UNLOCK_VIEW,
}
}
//
// unlock screen
//
2016-06-21 22:18:32 +02:00
function unlockInProgress () {
return {
2016-05-25 21:39:12 +02:00
type: actions.UNLOCK_IN_PROGRESS,
}
}
2016-06-21 22:18:32 +02:00
function unlockFailed () {
return {
2016-05-25 21:39:12 +02:00
type: actions.UNLOCK_FAILED,
}
}
2016-06-21 22:18:32 +02:00
function unlockMetamask (account) {
return {
2016-05-25 21:39:12 +02:00
type: actions.UNLOCK_METAMASK,
value: account,
}
}
2016-06-21 22:18:32 +02:00
function updateMetamaskState (newState) {
return {
2016-05-25 21:39:12 +02:00
type: actions.UPDATE_METAMASK_STATE,
value: newState,
}
}
2016-06-21 22:18:32 +02:00
function lockMetamask () {
return (dispatch) => {
_accountManager.setLocked((err) => {
2016-06-21 22:56:04 +02:00
dispatch(actions.hideLoadingIndication())
if (err) {
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 21:39:12 +02:00
type: actions.LOCK_METAMASK,
})
})
}
}
2016-06-21 22:18:32 +02:00
function showAccountDetail (address) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
_accountManager.setSelectedAddress(address, (err, address) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
2016-06-21 22:56:04 +02:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 21:39:12 +02:00
type: actions.SHOW_ACCOUNT_DETAIL,
value: address,
})
})
}
}
2016-06-21 22:18:32 +02:00
function backToAccountDetail (address) {
return {
2016-05-25 21:39:12 +02:00
type: actions.BACK_TO_ACCOUNT_DETAIL,
value: address,
}
}
2016-06-21 22:18:32 +02:00
function clearSeedWordCache (account) {
return {
2016-05-25 21:39:12 +02:00
type: actions.CLEAR_SEED_WORD_CACHE,
2016-05-12 07:21:10 +02:00
value: account,
}
}
2016-06-21 22:18:32 +02:00
function confirmSeedWords () {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
2016-05-12 07:21:10 +02:00
_accountManager.clearSeedWordCache((err, account) => {
2016-06-21 22:56:04 +02:00
dispatch(actions.hideLoadingIndication())
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-05-12 07:21:10 +02:00
console.log('Seed word cache cleared. ' + account)
2016-05-25 21:39:12 +02:00
dispatch(actions.showAccountDetail(account))
})
}
}
2016-06-21 22:18:32 +02:00
function showAccountsPage () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_ACCOUNTS_PAGE,
}
}
function showConfTxPage (transForward = true) {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_CONF_TX_PAGE,
transForward: transForward,
}
}
2016-06-21 22:18:32 +02:00
function nextTx () {
return {
2016-05-25 21:39:12 +02:00
type: actions.NEXT_TX,
}
}
2016-06-21 22:18:32 +02:00
function viewPendingTx (txId) {
return {
type: actions.VIEW_PENDING_TX,
value: txId,
}
}
2016-06-21 22:18:32 +02:00
function previousTx () {
return {
2016-05-25 21:39:12 +02:00
type: actions.PREVIOUS_TX,
}
}
2016-06-21 22:18:32 +02:00
function showConfigPage (transitionForward = true) {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_CONFIG_PAGE,
value: transitionForward,
}
}
function goBackToInitView () {
return {
type: actions.BACK_TO_INIT_MENU,
}
}
//
// config
//
2016-06-21 22:18:32 +02:00
function setRpcTarget (newRpc) {
_accountManager.setRpcTarget(newRpc)
return {
2016-05-25 21:39:12 +02:00
type: actions.SET_RPC_TARGET,
value: newRpc,
}
}
2016-06-21 22:18:32 +02:00
function setProviderType (type) {
_accountManager.setProviderType(type)
return {
2016-05-25 21:39:12 +02:00
type: actions.SET_PROVIDER_TYPE,
value: type,
}
}
2016-06-21 22:18:32 +02:00
function useEtherscanProvider () {
_accountManager.useEtherscanProvider()
return {
2016-05-25 21:39:12 +02:00
type: actions.USE_ETHERSCAN_PROVIDER,
}
}
2016-06-21 22:18:32 +02:00
function showLoadingIndication () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_LOADING,
}
}
2016-06-21 22:18:32 +02:00
function hideLoadingIndication () {
return {
2016-05-25 21:39:12 +02:00
type: actions.HIDE_LOADING,
}
}
2016-08-10 22:43:01 +02:00
function showSubLoadingIndication () {
return {
type: actions.SHOW_SUB_LOADING_INDICATION,
}
}
function hideSubLoadingIndication () {
return {
type: actions.HIDE_SUB_LOADING_INDICATION,
}
}
2016-06-30 20:30:31 +02:00
function showWarning (text) {
return this.displayWarning(text)
}
2016-06-21 22:18:32 +02:00
function displayWarning (text) {
return {
2016-05-25 21:39:12 +02:00
type: actions.DISPLAY_WARNING,
value: text,
}
}
2016-06-21 22:18:32 +02:00
function hideWarning () {
return {
2016-05-25 21:39:12 +02:00
type: actions.HIDE_WARNING,
}
}
2016-06-21 22:18:32 +02:00
function requestExportAccount () {
return {
2016-05-25 21:39:12 +02:00
type: actions.REQUEST_ACCOUNT_EXPORT,
}
}
2016-06-21 22:18:32 +02:00
function exportAccount (address) {
var self = this
2016-06-21 22:18:32 +02:00
return function (dispatch) {
dispatch(self.showLoadingIndication())
2016-06-21 22:18:32 +02:00
_accountManager.exportAccount(address, function (err, result) {
dispatch(self.hideLoadingIndication())
if (err) {
console.error(err)
return dispatch(self.displayWarning('Had a problem exporting the account.'))
}
dispatch(self.showPrivateKey(result))
})
}
}
2016-06-21 22:18:32 +02:00
function showPrivateKey (key) {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_PRIVATE_KEY,
value: key,
}
}
2016-06-21 22:18:32 +02:00
function saveAccountLabel (account, label) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
_accountManager.saveAccountLabel(account, label, (err) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
if (err) {
2016-05-25 21:39:12 +02:00
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 21:39:12 +02:00
type: actions.SAVE_ACCOUNT_LABEL,
value: { account, label },
})
})
}
}
2016-06-21 22:18:32 +02:00
function showSendPage () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_SEND_PAGE,
}
}
2016-08-10 22:43:01 +02:00
function agreeToEthWarning () {
return (dispatch) => {
_accountManager.agreeToEthWarning((err) => {
if (err) {
return dispatch(actions.showEthWarning(err.message))
}
dispatch({
type: actions.AGREE_TO_ETH_WARNING,
})
})
}
}
function showEthWarning () {
return {
type: actions.SHOW_ETH_WARNING,
}
}
function buyEth (address, amount) {
return (dispatch) => {
_accountManager.buyEth(address, amount)
dispatch({
type: actions.BUY_ETH,
})
}
}
function buyEthView (address) {
2016-08-10 22:43:01 +02:00
return {
type: actions.BUY_ETH_VIEW,
value: address,
2016-08-10 22:43:01 +02:00
}
}
function updateCoinBaseAmount (value) {
return {
type: actions.UPDATE_COINBASE_AMOUNT,
value,
}
}
function updateBuyAddress (value) {
return {
type: actions.UPDATE_BUY_ADDRESS,
value,
}
}
function coinBaseSubview () {
return {
type: actions.COINBASE_SUBVIEW,
}
}
function pairUpdate (coin) {
return (dispatch) => {
dispatch(actions.showSubLoadingIndication())
dispatch(actions.hideWarning())
shapeShiftRequest('marketinfo', {pair: `${coin.toLowerCase()}_eth`}, (mktResponse) => {
dispatch(actions.hideSubLoadingIndication())
dispatch({
type: actions.PAIR_UPDATE,
value: {
marketinfo: mktResponse,
},
})
})
}
}
function shapeShiftSubview (network) {
var pair = 'btc_eth'
2016-08-10 22:43:01 +02:00
return (dispatch) => {
dispatch(actions.showSubLoadingIndication())
shapeShiftRequest('marketinfo', {pair}, (mktResponse) => {
shapeShiftRequest('getcoins', {}, (response) => {
dispatch(actions.hideSubLoadingIndication())
if (mktResponse.error) return dispatch(actions.showWarning(mktResponse.error))
dispatch({
type: actions.SHAPESHIFT_SUBVIEW,
value: {
marketinfo: mktResponse,
coinOptions: response,
},
})
})
})
}
}
function coinShiftRquest (data, marketData) {
2016-08-10 22:43:01 +02:00
return (dispatch) => {
dispatch(actions.showLoadingIndication())
2016-08-10 22:43:01 +02:00
shapeShiftRequest('shift', { method: 'POST', data}, (response) => {
if (response.error) return dispatch(actions.showWarning(response.error))
var message = `
Deposit your ${response.depositType} to the address bellow:`
2016-08-18 19:40:35 +02:00
_accountManager.createShapeShiftTx(response.deposit, response.depositType)
dispatch(actions.showQrView(response.deposit, [message].concat(marketData)))
})
}
}
function showQrView (data, message) {
return {
type: actions.SHOW_QR_VIEW,
value: {
message: message,
data: data,
},
2016-08-10 22:43:01 +02:00
}
}
2016-08-18 19:40:35 +02:00
function reshowQrCode (data, coin) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
shapeShiftRequest('marketinfo', {pair: `${coin.toLowerCase()}_eth`}, (mktResponse) => {
if (mktResponse.error) return dispatch(actions.showWarning(mktResponse.error))
2016-08-19 00:20:26 +02:00
var message = [
2016-08-18 19:40:35 +02:00
`Deposit your ${coin} to the address bellow:`,
`Deposit Limit: ${mktResponse.limit}`,
`Deposit Minimum:${mktResponse.minimum}`,
]
dispatch(actions.hideLoadingIndication())
return dispatch(actions.showQrView(data, message))
2016-08-18 19:40:35 +02:00
})
}
}
2016-08-10 22:43:01 +02:00
function shapeShiftRequest (query, options, cb) {
var queryResponse, method
!options ? options = {} : null
options.method ? method = options.method : method = 'GET'
var requestListner = function (request) {
queryResponse = JSON.parse(this.responseText)
cb ? cb(queryResponse) : null
return queryResponse
}
var shapShiftReq = new XMLHttpRequest()
shapShiftReq.addEventListener('load', requestListner)
shapShiftReq.open(method, `https://shapeshift.io/${query}/${options.pair ? options.pair : ''}`, true)
if (options.method === 'POST') {
var jsonObj = JSON.stringify(options.data)
shapShiftReq.setRequestHeader('Content-Type', 'application/json')
return shapShiftReq.send(jsonObj)
} else {
return shapShiftReq.send()
}
}