2021-11-24 16:06:03 -08:00
|
|
|
import { createStore as reduxCreateStore } from 'redux'
|
|
|
|
|
// Wallet from localStorage
|
|
|
|
|
import { getWalletInfo, setWalletInfo } from '../components/localWallet'
|
|
|
|
|
// import BchWallet from 'minimal-slp-wallet'
|
|
|
|
|
const BchWallet = typeof window !== 'undefined' ? window.SlpWallet : null
|
|
|
|
|
|
2022-02-02 16:18:42 -08:00
|
|
|
const siteConfig = require('../components/site-config')
|
|
|
|
|
|
2021-11-24 16:06:03 -08:00
|
|
|
const reducer = (state, action) => {
|
|
|
|
|
// Update walletInfo state property
|
|
|
|
|
if (action.type === 'SET_WALLET_INFO') {
|
|
|
|
|
const walletInfo = action.value
|
|
|
|
|
|
|
|
|
|
// persist JWT when create or import wallet
|
|
|
|
|
if (state.walletInfo.JWT && !walletInfo.JWT) {
|
|
|
|
|
walletInfo.JWT = state.walletInfo.JWT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setWalletInfo(walletInfo) // Add wallet to local storage
|
|
|
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
|
walletInfo: walletInfo
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update bchBalance state property
|
|
|
|
|
if (action.type === 'UPDATE_BALANCE') {
|
|
|
|
|
// Convert satoshis to bch
|
|
|
|
|
const { myBalance, currentRate } = action.value
|
|
|
|
|
console.log(`currentRate: ${currentRate}`)
|
|
|
|
|
|
|
|
|
|
const satoshis = myBalance
|
|
|
|
|
const bch = satoshis / 100000000
|
|
|
|
|
|
|
|
|
|
const bchBalance = Number(bch.toFixed(8))
|
|
|
|
|
const _usdBalance = bchBalance * (currentRate / 100)
|
|
|
|
|
const usdBalance = Number(_usdBalance.toFixed(2)) // usd balance
|
|
|
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
|
bchBalance: { bchBalance, usdBalance },
|
|
|
|
|
currentRate
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// Adds the minimal-slp-wallet instance to the Redux state
|
|
|
|
|
if (action.type === 'SET_BCH_WALLET') {
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
|
bchWallet: action.value
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get or update tokens information
|
|
|
|
|
if (action.type === 'SET_TOKENS_INFO') {
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
|
tokensInfo: action.value
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fuctionality to change between the different sections of the menu
|
|
|
|
|
// a "data" property can be added with information
|
|
|
|
|
// that can be handled by the other menu components
|
|
|
|
|
if (action.type === 'MENU_NAVIGATION') {
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
|
menuNavigation: {
|
|
|
|
|
changeTo: action.value.changeTo || state.menuNavigation.changeTo,
|
|
|
|
|
data: action.value.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wallet info from local storage
|
|
|
|
|
const localStorageInfo = getWalletInfo()
|
|
|
|
|
|
|
|
|
|
// Creates an instance of minimal-slp-wallet, with
|
|
|
|
|
// the local storage information if it exists
|
|
|
|
|
const instanceWallet = () => {
|
|
|
|
|
try {
|
|
|
|
|
if (!localStorageInfo.mnemonic) return null
|
|
|
|
|
|
2021-12-21 11:46:51 -04:00
|
|
|
const bchjsOptions = getBchjsOptions()
|
|
|
|
|
|
|
|
|
|
const bchWalletLib = new BchWallet(localStorageInfo.mnemonic, bchjsOptions)
|
2022-02-02 16:18:42 -08:00
|
|
|
|
2021-12-21 11:46:51 -04:00
|
|
|
// Update bchjs instances of minimal-slp-wallet libraries
|
|
|
|
|
bchWalletLib.tokens.sendBch.bchjs = new bchWalletLib.BCHJS(bchjsOptions)
|
|
|
|
|
bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions)
|
|
|
|
|
|
|
|
|
|
return bchWalletLib
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 16:18:42 -08:00
|
|
|
const getBchjsOptions = () => {
|
2021-12-21 11:46:51 -04:00
|
|
|
try {
|
2022-02-02 16:18:42 -08:00
|
|
|
const _interface = localStorageInfo.interface || siteConfig.interface
|
2021-12-21 11:46:51 -04:00
|
|
|
|
2021-11-24 16:06:03 -08:00
|
|
|
const jwtToken = localStorageInfo.JWT
|
|
|
|
|
const restURL = localStorageInfo.selectedServer
|
|
|
|
|
const bchjsOptions = {}
|
2022-02-02 16:18:42 -08:00
|
|
|
|
|
|
|
|
if (_interface === 'consumer-api') {
|
2021-12-21 11:46:51 -04:00
|
|
|
bchjsOptions.interface = _interface
|
2022-02-24 14:58:51 -04:00
|
|
|
bchjsOptions.restURL = restURL
|
2021-12-21 11:46:51 -04:00
|
|
|
return bchjsOptions
|
|
|
|
|
}
|
2021-11-24 16:06:03 -08:00
|
|
|
|
|
|
|
|
if (jwtToken) {
|
|
|
|
|
bchjsOptions.apiToken = jwtToken
|
|
|
|
|
}
|
2021-12-21 11:46:51 -04:00
|
|
|
|
2021-11-24 16:06:03 -08:00
|
|
|
if (restURL) {
|
|
|
|
|
bchjsOptions.restURL = restURL
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 11:46:51 -04:00
|
|
|
if (_interface === 'rest-api') {
|
|
|
|
|
bchjsOptions.interface = _interface
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 16:06:03 -08:00
|
|
|
// Assign the tx fee based on environment variable
|
|
|
|
|
const FEE = process.env.FEE ? process.env.FEE : 1
|
|
|
|
|
bchjsOptions.fee = FEE
|
|
|
|
|
console.log(`Using ${bchjsOptions.fee} sats per byte for tx fees.`)
|
|
|
|
|
|
2021-12-21 11:46:51 -04:00
|
|
|
return bchjsOptions
|
2021-11-24 16:06:03 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initial state
|
|
|
|
|
const initialState = {
|
|
|
|
|
walletInfo: localStorageInfo, // Object wallet info
|
|
|
|
|
bchBalance: { bchBalance: 0, usdBalance: 0 }, // Wallet Balance
|
|
|
|
|
bchWallet: instanceWallet(), // minimal-slp-wallet instance
|
|
|
|
|
tokensInfo: [],
|
|
|
|
|
currentRate: 0,
|
|
|
|
|
menuNavigation: {
|
|
|
|
|
changeTo: () => {},
|
|
|
|
|
data: null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let createStore
|
|
|
|
|
typeof window !== 'undefined'
|
|
|
|
|
? (createStore = () =>
|
|
|
|
|
reduxCreateStore(
|
|
|
|
|
reducer,
|
|
|
|
|
initialState,
|
|
|
|
|
/** Redux DevTools
|
|
|
|
|
* https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=es
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
window.__REDUX_DEVTOOLS_EXTENSION__ &&
|
|
|
|
|
window.__REDUX_DEVTOOLS_EXTENSION__()
|
|
|
|
|
))
|
|
|
|
|
: (createStore = () => reduxCreateStore(reducer, initialState))
|
|
|
|
|
export default createStore
|