1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/app/actions.js

780 lines
18 KiB
JavaScript
Raw Normal View History

var actions = {
2016-10-20 21:07:53 +02:00
_setBackgroundConnection: _setBackgroundConnection,
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',
unlockMetamask: unlockMetamask,
unlockFailed: unlockFailed,
showCreateVault: showCreateVault,
showRestoreVault: showRestoreVault,
showInitializeMenu: showInitializeMenu,
createNewVaultAndKeychain: createNewVaultAndKeychain,
createNewVaultAndRestore: createNewVaultAndRestore,
createNewVaultInProgress: createNewVaultInProgress,
addNewKeyring,
addNewAccount,
showNewVaultSeed: showNewVaultSeed,
showInfoPage: showInfoPage,
2016-11-02 06:19:04 +01:00
// 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,
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',
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,
// tx conf screen
COMPLETED_TX: 'COMPLETED_TX',
TRANSACTION_ERROR: 'TRANSACTION_ERROR',
NEXT_TX: 'NEXT_TX',
PREVIOUS_TX: 'PREV_TX',
setSelectedAccount: setSelectedAccount,
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
confirmSeedWords: confirmSeedWords,
showAccountDetail: showAccountDetail,
BACK_TO_ACCOUNT_DETAIL: 'BACK_TO_ACCOUNT_DETAIL',
backToAccountDetail: backToAccountDetail,
showAccountsPage: showAccountsPage,
showConfTxPage: showConfTxPage,
// 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,
// 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,
2016-10-15 19:48:12 +02:00
// SHOWING KEYCHAIN
SHOW_NEW_KEYCHAIN: 'SHOW_NEW_KEYCHAIN',
showNewKeychain: showNewKeychain,
2016-10-21 01:44:31 +02:00
}
module.exports = actions
var background = null
2016-11-11 19:26:12 +01:00
function _setBackgroundConnection (backgroundConnection) {
background = backgroundConnection
}
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())
2016-11-16 02:13:33 +01:00
background.submitPassword(password, (err, newState) => {
dispatch(actions.hideLoadingIndication())
if (err) {
dispatch(actions.unlockFailed(err.message))
} else {
let selectedAccount
try {
selectedAccount = newState.metamask.selectedAccount
} catch (e) {}
2016-05-25 21:39:12 +02:00
dispatch(actions.unlockMetamask(selectedAccount))
}
})
}
}
function confirmSeedWords () {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.clearSeedWordCache((err, account) => {
dispatch(actions.hideLoadingIndication())
if (err) {
2016-11-08 01:02:02 +01:00
return dispatch(actions.displayWarning(err.message))
}
console.log('Seed word cache cleared. ' + account)
dispatch(actions.showAccountDetail(account))
})
}
}
function createNewVaultAndRestore (password, seed) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.createNewVaultAndRestore(password, seed, (err) => {
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
})
}
}
function createNewVaultAndKeychain (password) {
return (dispatch) => {
background.createNewVaultAndKeychain(password, (err) => {
2016-06-21 22:56:04 +02:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-06-21 22:18:32 +02:00
})
}
}
2016-11-02 06:19:04 +01:00
function revealSeedConfirmation () {
return {
type: this.REVEAL_SEED_CONFIRMATION,
}
}
function requestRevealSeed (password) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.submitPassword(password, (err) => {
2016-11-02 06:19:04 +01:00
if (err) return dispatch(actions.displayWarning(err.message))
background.placeSeedWords((err) => {
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideLoadingIndication())
})
2016-11-02 06:19:04 +01:00
})
}
}
2016-10-21 01:44:31 +02:00
function addNewKeyring (type, opts) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.addNewKeyring(type, opts, (err) => {
2016-10-21 01:44:31 +02:00
dispatch(this.hideLoadingIndication())
if (err) {
2016-10-21 02:24:03 +02:00
return dispatch(actions.showWarning(err))
2016-10-21 01:44:31 +02:00
}
})
}
}
function addNewAccount (ringNumber = 0) {
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.addNewAccount(ringNumber, (err) => {
dispatch(this.hideLoadingIndication())
if (err) {
return dispatch(actions.showWarning(err))
}
})
}
}
2016-06-21 22:18:32 +02:00
function showInfoPage () {
return {
2016-05-25 21:39:12 +02:00
type: actions.SHOW_INFO_PAGE,
}
}
function setSelectedAccount (address) {
return (dispatch) => {
background.setSelectedAccount(address)
}
}
function setCurrentFiat (fiat) {
return (dispatch) => {
dispatch(this.showLoadingIndication())
background.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())
background.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) => {
background.setGasMultiplier(txData.gasMultiplier, (err) => {
2016-05-25 21:39:12 +02:00
if (err) return dispatch(actions.displayWarning(err.message))
2016-10-14 01:53:32 +02:00
web3.eth.sendTransaction(txData, (err, data) => {
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideWarning())
dispatch(actions.goHome())
})
dispatch(this.showConfTxPage())
2016-05-06 23:46:04 +02:00
})
}
}
2016-06-21 22:18:32 +02:00
function sendTx (txData) {
return (dispatch) => {
background.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) {
background.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) {
background.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())
background.agreeToDisclaimer((err) => {
2016-06-21 22:56:04 +02:00
if (err) {
2016-11-08 01:02:02 +01:00
return dispatch(actions.displayWarning(err.message))
2016-06-21 22:56:04 +02:00
}
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,
}
}
2016-10-15 19:48:12 +02:00
function showNewKeychain () {
return {
2016-10-20 20:33:18 +02:00
type: actions.SHOW_NEW_KEYCHAIN,
2016-10-15 19:48:12 +02:00
}
}
//
// 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,
}
}
function unlockFailed (message) {
return {
2016-05-25 21:39:12 +02:00
type: actions.UNLOCK_FAILED,
value: message,
}
}
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) => {
background.setLocked((err) => {
2016-06-21 22:56:04 +02:00
dispatch(actions.hideLoadingIndication())
if (err) {
2016-11-08 01:02:02 +01:00
return dispatch(actions.displayWarning(err.message))
2016-06-21 22:56:04 +02:00
}
})
}
}
2016-06-21 22:18:32 +02:00
function showAccountDetail (address) {
return (dispatch) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.showLoadingIndication())
background.setSelectedAccount(address, (err, address) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
2016-06-21 22:56:04 +02:00
if (err) {
2016-11-08 01:02:02 +01:00
return dispatch(actions.displayWarning(err.message))
2016-06-21 22:56:04 +02:00
}
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 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) {
background.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) {
background.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 () {
background.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-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())
background.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())
background.saveAccountLabel(account, label, (err) => {
2016-05-25 21:39:12 +02:00
dispatch(actions.hideLoadingIndication())
if (err) {
2016-11-08 01:02:02 +01:00
return dispatch(actions.displayWarning(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 buyEth (address, amount) {
return (dispatch) => {
background.buyEth(address, amount)
2016-08-10 22:43:01 +02:00
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())
2016-11-08 01:02:02 +01:00
if (mktResponse.error) return dispatch(actions.displayWarning(mktResponse.error))
2016-08-10 22:43:01 +02:00
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) => {
2016-11-08 01:02:02 +01:00
if (response.error) return dispatch(actions.displayWarning(response.error))
var message = `
Deposit your ${response.depositType} to the address bellow:`
background.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) => {
2016-11-08 01:02:02 +01:00
if (mktResponse.error) return dispatch(actions.displayWarning(mktResponse.error))
2016-08-18 19:40:35 +02:00
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()
}
}