nifty-wallet/ui/app/actions.js

730 lines
17 KiB
JavaScript
Raw Normal View History

var actions = {
GO_HOME: 'GO_HOME',
goHome: goHome,
2016-05-18 12:30:03 -07:00
// menu state
getNetworkStatus: 'getNetworkStatus',
// remote state
UPDATE_METAMASK_STATE: 'UPDATE_METAMASK_STATE',
updateMetamaskState: updateMetamaskState,
// intialize screen
2016-06-16 15:58:45 -07: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 11:30:31 -07: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,
// 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 13:43:01 -07: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 14:46:04 -07: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 13:43:01 -07:00
// buy Eth with coinbase
BUY_ETH: 'BUY_ETH',
buyEth: buyEth,
buyEthSubview: buyEthSubview,
BUY_ETH_SUBVIEW: 'BUY_ETH_SUBVIEW',
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,
COIN_SHIFT_REQUEST: 'COIN_SHIFT_REQUEST',
coinShiftRquest: coinShiftRquest,
SHOW_SUB_LOADING_INDICATION: 'SHOW_SUB_LOADING_INDICATION',
showSubLoadingIndication: showSubLoadingIndication,
HIDE_SUB_LOADING_INDICATION: 'HIDE_SUB_LOADING_INDICATION',
hideSubLoadingIndication: hideSubLoadingIndication,
}
module.exports = actions
var _accountManager = null
2016-06-21 13:18:32 -07:00
function _setAccountManager (accountManager) {
_accountManager = accountManager
}
2016-06-21 13:18:32 -07:00
function goHome () {
return {
2016-05-25 12:39:12 -07:00
type: actions.GO_HOME,
}
}
// async actions
2016-06-21 13:18:32 -07:00
function tryUnlockMetamask (password) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.unlockInProgress())
_accountManager.submitPassword(password, (err, selectedAccount) => {
if (err) {
2016-05-25 12:39:12 -07:00
dispatch(actions.unlockFailed())
} else {
2016-05-25 12:39:12 -07:00
dispatch(actions.unlockMetamask(selectedAccount))
}
})
}
}
2016-06-21 13:18:32 -07:00
function createNewVault (password, entropy) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.createNewVaultInProgress())
_accountManager.createNewVault(password, entropy, (err, result) => {
2016-06-21 13:56:04 -07:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-05-25 12:39:12 -07:00
dispatch(actions.showNewVaultSeed(result))
})
}
}
2016-06-21 13:18:32 -07:00
function revealSeedConfirmation () {
return {
type: this.REVEAL_SEED_CONFIRMATION,
}
}
2016-06-21 13:18:32 -07: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 13:18:32 -07:00
function recoverFromSeed (password, seed) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
// dispatch(actions.createNewVaultInProgress())
dispatch(actions.showLoadingIndication())
_accountManager.recoverFromSeed(password, seed, (err, metamaskState) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
var account = Object.keys(metamaskState.identities)[0]
2016-05-25 12:39:12 -07:00
dispatch(actions.unlockMetamask(account))
2016-06-21 13:18:32 -07:00
})
}
}
2016-06-21 13:18:32 -07:00
function showInfoPage () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_INFO_PAGE,
}
}
2016-06-21 13:18:32 -07:00
function setSelectedAddress (address) {
return (dispatch) => {
_accountManager.setSelectedAddress(address)
}
}
2016-06-21 13:18:32 -07:00
function revealAccount () {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
_accountManager.revealAccount((err) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
if (err) return dispatch(actions.displayWarning(err.message))
dispatch({
2016-05-25 12:39:12 -07:00
type: actions.REVEAL_ACCOUNT,
})
})
}
}
2016-06-21 13:18:32 -07:00
function signMsg (msgData) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
_accountManager.signMessage(msgData, (err) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
2016-05-25 12:39:12 -07:00
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.completedTx(msgData.metamaskId))
})
}
}
2016-06-21 13:18:32 -07:00
function signTx (txData) {
2016-05-06 14:46:04 -07:00
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
2016-05-06 14:46:04 -07:00
web3.eth.sendTransaction(txData, (err, data) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
2016-05-06 14:46:04 -07:00
2016-05-25 12:39:12 -07:00
if (err) return dispatch(actions.displayWarning(err.message))
dispatch(actions.hideWarning())
dispatch(actions.goHome())
2016-05-06 14:46:04 -07:00
})
}
}
2016-06-21 13:18:32 -07:00
function sendTx (txData) {
return (dispatch) => {
_accountManager.approveTransaction(txData.id, (err) => {
if (err) {
alert(err.message)
2016-05-25 12:39:12 -07:00
dispatch(actions.txError(err))
return console.error(err.message)
}
2016-05-25 12:39:12 -07:00
dispatch(actions.completedTx(txData.id))
})
}
}
2016-06-21 13:18:32 -07:00
function completedTx (id) {
return {
2016-05-25 12:39:12 -07:00
type: actions.COMPLETED_TX,
id,
}
}
2016-06-21 13:18:32 -07:00
function txError (err) {
return {
2016-05-25 12:39:12 -07:00
type: actions.TRANSACTION_ERROR,
message: err.message,
}
}
2016-06-21 13:18:32 -07:00
function cancelMsg (msgData) {
_accountManager.cancelMessage(msgData.id)
2016-05-25 12:39:12 -07:00
return actions.completedTx(msgData.id)
}
2016-06-21 13:18:32 -07:00
function cancelTx (txData) {
_accountManager.cancelTransaction(txData.id)
2016-05-25 12:39:12 -07:00
return actions.completedTx(txData.id)
}
//
// initialize screen
//
2016-06-21 13:18:32 -07:00
function showCreateVault () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_CREATE_VAULT,
}
}
2016-06-21 13:18:32 -07:00
function showRestoreVault () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_RESTORE_VAULT,
}
}
2016-06-21 13:18:32 -07:00
function showInitializeMenu () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_INIT_MENU,
}
}
2016-06-21 13:18:32 -07:00
function agreeToDisclaimer () {
2016-06-16 15:58:45 -07:00
return (dispatch) => {
dispatch(this.showLoadingIndication())
2016-06-16 16:40:55 -07:00
_accountManager.agreeToDisclaimer((err) => {
2016-06-21 13:56:04 -07:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-06-16 16:40:55 -07:00
dispatch(this.hideLoadingIndication())
2016-06-16 15:58:45 -07:00
dispatch({
type: this.AGREE_TO_DISCLAIMER,
})
})
}
}
2016-06-21 13:18:32 -07:00
function createNewVaultInProgress () {
return {
2016-05-25 12:39:12 -07:00
type: actions.CREATE_NEW_VAULT_IN_PROGRESS,
}
}
2016-06-21 13:18:32 -07:00
function showNewVaultSeed (seed) {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_NEW_VAULT_SEED,
value: seed,
}
}
//
// unlock screen
//
2016-06-21 13:18:32 -07:00
function unlockInProgress () {
return {
2016-05-25 12:39:12 -07:00
type: actions.UNLOCK_IN_PROGRESS,
}
}
2016-06-21 13:18:32 -07:00
function unlockFailed () {
return {
2016-05-25 12:39:12 -07:00
type: actions.UNLOCK_FAILED,
}
}
2016-06-21 13:18:32 -07:00
function unlockMetamask (account) {
return {
2016-05-25 12:39:12 -07:00
type: actions.UNLOCK_METAMASK,
value: account,
}
}
2016-06-21 13:18:32 -07:00
function updateMetamaskState (newState) {
return {
2016-05-25 12:39:12 -07:00
type: actions.UPDATE_METAMASK_STATE,
value: newState,
}
}
2016-06-21 13:18:32 -07:00
function lockMetamask () {
return (dispatch) => {
_accountManager.setLocked((err) => {
2016-06-21 13:56:04 -07:00
dispatch(actions.hideLoadingIndication())
if (err) {
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 12:39:12 -07:00
type: actions.LOCK_METAMASK,
})
})
}
}
2016-06-21 13:18:32 -07:00
function showAccountDetail (address) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
_accountManager.setSelectedAddress(address, (err, address) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
2016-06-21 13:56:04 -07:00
if (err) {
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 12:39:12 -07:00
type: actions.SHOW_ACCOUNT_DETAIL,
value: address,
})
})
}
}
2016-06-21 13:18:32 -07:00
function backToAccountDetail (address) {
return {
2016-05-25 12:39:12 -07:00
type: actions.BACK_TO_ACCOUNT_DETAIL,
value: address,
}
}
2016-06-21 13:18:32 -07:00
function clearSeedWordCache (account) {
return {
2016-05-25 12:39:12 -07:00
type: actions.CLEAR_SEED_WORD_CACHE,
2016-05-11 22:21:10 -07:00
value: account,
}
}
2016-06-21 13:18:32 -07:00
function confirmSeedWords () {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
2016-05-11 22:21:10 -07:00
_accountManager.clearSeedWordCache((err, account) => {
2016-06-21 13:56:04 -07:00
dispatch(actions.hideLoadingIndication())
if (err) {
return dispatch(actions.showWarning(err.message))
}
2016-05-11 22:21:10 -07:00
console.log('Seed word cache cleared. ' + account)
2016-05-25 12:39:12 -07:00
dispatch(actions.showAccountDetail(account))
})
}
}
2016-06-21 13:18:32 -07:00
function showAccountsPage () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_ACCOUNTS_PAGE,
}
}
2016-06-21 13:18:32 -07:00
function showConfTxPage () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_CONF_TX_PAGE,
}
}
2016-06-21 13:18:32 -07:00
function nextTx () {
return {
2016-05-25 12:39:12 -07:00
type: actions.NEXT_TX,
}
}
2016-06-21 13:18:32 -07:00
function viewPendingTx (txId) {
return {
type: actions.VIEW_PENDING_TX,
value: txId,
}
}
2016-06-21 13:18:32 -07:00
function previousTx () {
return {
2016-05-25 12:39:12 -07:00
type: actions.PREVIOUS_TX,
}
}
2016-06-21 13:18:32 -07:00
function showConfigPage (transitionForward = true) {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_CONFIG_PAGE,
value: transitionForward,
}
}
//
// config
//
2016-06-21 13:18:32 -07:00
function setRpcTarget (newRpc) {
_accountManager.setRpcTarget(newRpc)
return {
2016-05-25 12:39:12 -07:00
type: actions.SET_RPC_TARGET,
value: newRpc,
}
}
2016-06-21 13:18:32 -07:00
function setProviderType (type) {
_accountManager.setProviderType(type)
return {
2016-05-25 12:39:12 -07:00
type: actions.SET_PROVIDER_TYPE,
value: type,
}
}
2016-06-21 13:18:32 -07:00
function useEtherscanProvider () {
_accountManager.useEtherscanProvider()
return {
2016-05-25 12:39:12 -07:00
type: actions.USE_ETHERSCAN_PROVIDER,
}
}
2016-06-21 13:18:32 -07:00
function showLoadingIndication () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_LOADING,
}
}
2016-06-21 13:18:32 -07:00
function hideLoadingIndication () {
return {
2016-05-25 12:39:12 -07:00
type: actions.HIDE_LOADING,
}
}
2016-08-10 13:43:01 -07:00
function showSubLoadingIndication () {
return {
type: actions.SHOW_SUB_LOADING_INDICATION,
}
}
function hideSubLoadingIndication () {
return {
type: actions.HIDE_SUB_LOADING_INDICATION,
}
}
2016-06-30 11:30:31 -07:00
function showWarning (text) {
return this.displayWarning(text)
}
2016-06-21 13:18:32 -07:00
function displayWarning (text) {
return {
2016-05-25 12:39:12 -07:00
type: actions.DISPLAY_WARNING,
value: text,
}
}
2016-06-21 13:18:32 -07:00
function hideWarning () {
return {
2016-05-25 12:39:12 -07:00
type: actions.HIDE_WARNING,
}
}
2016-06-21 13:18:32 -07:00
function requestExportAccount () {
return {
2016-05-25 12:39:12 -07:00
type: actions.REQUEST_ACCOUNT_EXPORT,
}
}
2016-06-21 13:18:32 -07:00
function exportAccount (address) {
var self = this
2016-06-21 13:18:32 -07:00
return function (dispatch) {
dispatch(self.showLoadingIndication())
2016-06-21 13:18:32 -07: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 13:18:32 -07:00
function showPrivateKey (key) {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_PRIVATE_KEY,
value: key,
}
}
2016-06-21 13:18:32 -07:00
function saveAccountLabel (account, label) {
return (dispatch) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.showLoadingIndication())
_accountManager.saveAccountLabel(account, label, (err) => {
2016-05-25 12:39:12 -07:00
dispatch(actions.hideLoadingIndication())
if (err) {
2016-05-25 12:39:12 -07:00
return dispatch(actions.showWarning(err.message))
}
dispatch({
2016-05-25 12:39:12 -07:00
type: actions.SAVE_ACCOUNT_LABEL,
value: { account, label },
})
})
}
}
2016-06-21 13:18:32 -07:00
function showSendPage () {
return {
2016-05-25 12:39:12 -07:00
type: actions.SHOW_SEND_PAGE,
}
}
2016-08-10 13:43:01 -07: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 buyEthSubview () {
return {
type: actions.BUY_ETH_SUBVIEW,
}
}
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
network === 'classic' ? pair = 'btc_etc' : pair = 'btc_eth'
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) {
return (dispatch) => {
dispatch(actions.showSubLoadingIndication())
shapeShiftRequest('shift', { method: 'POST', data}, (response) => {
dispatch(actions.hideSubLoadingIndication())
if (response.error) return dispatch(actions.showWarning(response.error))
dispatch({
type: actions.COIN_SHIFT_REQUEST,
value: {
response: response,
},
})
})
}
}
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()
}
}