Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show All 12 Lines | import { | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
} from '@utils/cashMethods'; | } from '@utils/cashMethods'; | ||||
import { isValidCashtabSettings } from '@utils/validation'; | import { isValidCashtabSettings } from '@utils/validation'; | ||||
import localforage from 'localforage'; | import localforage from 'localforage'; | ||||
import { currency } from '@components/Common/Ticker'; | import { currency } from '@components/Common/Ticker'; | ||||
import isEmpty from 'lodash.isempty'; | import isEmpty from 'lodash.isempty'; | ||||
import isEqual from 'lodash.isequal'; | import isEqual from 'lodash.isequal'; | ||||
import { | import { | ||||
xecReceivedNotification, | |||||
eTokenReceivedNotification, | |||||
} from '@components/Common/Notifications'; | |||||
import { | |||||
CashReceivedNotificationIcon, | CashReceivedNotificationIcon, | ||||
TokenReceivedNotificationIcon, | TokenReceivedNotificationIcon, | ||||
} from '@components/Common/CustomIcons'; | } from '@components/Common/CustomIcons'; | ||||
const useWallet = () => { | const useWallet = () => { | ||||
const [wallet, setWallet] = useState(false); | const [wallet, setWallet] = useState(false); | ||||
const [cashtabSettings, setCashtabSettings] = useState(false); | const [cashtabSettings, setCashtabSettings] = useState(false); | ||||
const [fiatPrice, setFiatPrice] = useState(null); | const [fiatPrice, setFiatPrice] = useState(null); | ||||
const [apiError, setApiError] = useState(false); | const [apiError, setApiError] = useState(false); | ||||
const [checkFiatInterval, setCheckFiatInterval] = useState(null); | const [checkFiatInterval, setCheckFiatInterval] = useState(null); | ||||
const { | const { | ||||
getBCH, | getBCH, | ||||
▲ Show 20 Lines • Show All 862 Lines • ▼ Show 20 Lines | if ( | ||||
previousBalances && | previousBalances && | ||||
balances && | balances && | ||||
'totalBalance' in previousBalances && | 'totalBalance' in previousBalances && | ||||
'totalBalance' in balances && | 'totalBalance' in balances && | ||||
new BigNumber(balances.totalBalance) | new BigNumber(balances.totalBalance) | ||||
.minus(previousBalances.totalBalance) | .minus(previousBalances.totalBalance) | ||||
.gt(0) | .gt(0) | ||||
) { | ) { | ||||
notification.success({ | xecReceivedNotification( | ||||
message: 'Transaction received', | balances, | ||||
description: ( | previousBalances, | ||||
<Paragraph> | cashtabSettings, | ||||
+{' '} | fiatPrice, | ||||
{parseFloat( | ); | ||||
Number( | |||||
balances.totalBalance - | |||||
previousBalances.totalBalance, | |||||
).toFixed(currency.cashDecimals), | |||||
).toLocaleString()}{' '} | |||||
{currency.ticker}{' '} | |||||
{cashtabSettings && | |||||
cashtabSettings.fiatCurrency && | |||||
`(${ | |||||
currency.fiatCurrencies[ | |||||
cashtabSettings.fiatCurrency | |||||
].symbol | |||||
}${( | |||||
Number( | |||||
balances.totalBalance - | |||||
previousBalances.totalBalance, | |||||
) * fiatPrice | |||||
).toFixed( | |||||
currency.cashDecimals, | |||||
)} ${cashtabSettings.fiatCurrency.toUpperCase()})`} | |||||
</Paragraph> | |||||
), | |||||
duration: 3, | |||||
icon: <CashReceivedNotificationIcon />, | |||||
style: { width: '100%' }, | |||||
}); | |||||
} | } | ||||
// Parse for incoming eToken transactions | // Parse for incoming eToken transactions | ||||
if ( | if ( | ||||
tokens && | tokens && | ||||
tokens[0] && | tokens[0] && | ||||
tokens[0].balance && | tokens[0].balance && | ||||
previousTokens && | previousTokens && | ||||
Show All 39 Lines | ) { | ||||
const receivedSlpTicker = | const receivedSlpTicker = | ||||
tokens[receivedTokenObjectIndex].info.tokenTicker; | tokens[receivedTokenObjectIndex].info.tokenTicker; | ||||
const receivedSlpName = | const receivedSlpName = | ||||
tokens[receivedTokenObjectIndex].info.tokenName; | tokens[receivedTokenObjectIndex].info.tokenName; | ||||
//console.log(`receivedSlpQty`, receivedSlpQty); | //console.log(`receivedSlpQty`, receivedSlpQty); | ||||
// Notification if you received SLP | // Notification if you received SLP | ||||
if (receivedSlpQty > 0) { | if (receivedSlpQty > 0) { | ||||
notification.success({ | eTokenReceivedNotification( | ||||
message: `${currency.tokenTicker} transaction received: ${receivedSlpTicker}`, | currency, | ||||
description: ( | receivedSlpTicker, | ||||
<Paragraph> | receivedSlpQty, | ||||
You received {receivedSlpQty} {receivedSlpName} | receivedSlpName, | ||||
</Paragraph> | ); | ||||
), | |||||
duration: 3, | |||||
icon: <TokenReceivedNotificationIcon />, | |||||
style: { width: '100%' }, | |||||
}); | |||||
} | } | ||||
// | // | ||||
} else { | } else { | ||||
// If tokens[i].balance > previousTokens[i].balance, a new SLP tx of an existing token has been received | // If tokens[i].balance > previousTokens[i].balance, a new SLP tx of an existing token has been received | ||||
// Note that tokens[i].balance is of type BigNumber | // Note that tokens[i].balance is of type BigNumber | ||||
for (let i = 0; i < tokens.length; i += 1) { | for (let i = 0; i < tokens.length; i += 1) { | ||||
if (tokens[i].balance.gt(previousTokens[i].balance)) { | if (tokens[i].balance.gt(previousTokens[i].balance)) { | ||||
// Received this token | // Received this token | ||||
// console.log(`previousTokenId`, previousTokens[i].tokenId); | // console.log(`previousTokenId`, previousTokens[i].tokenId); | ||||
Show All 9 Lines | ) { | ||||
} | } | ||||
const receivedSlpQty = tokens[i].balance.minus( | const receivedSlpQty = tokens[i].balance.minus( | ||||
previousTokens[i].balance, | previousTokens[i].balance, | ||||
); | ); | ||||
const receivedSlpTicker = tokens[i].info.tokenTicker; | const receivedSlpTicker = tokens[i].info.tokenTicker; | ||||
const receivedSlpName = tokens[i].info.tokenName; | const receivedSlpName = tokens[i].info.tokenName; | ||||
notification.success({ | eTokenReceivedNotification( | ||||
message: `${currency.tokenTicker} transaction received: ${receivedSlpTicker}`, | currency, | ||||
description: ( | receivedSlpTicker, | ||||
<Paragraph> | receivedSlpQty, | ||||
You received {receivedSlpQty.toString()}{' '} | receivedSlpName, | ||||
{receivedSlpName} | ); | ||||
</Paragraph> | |||||
), | |||||
duration: 3, | |||||
icon: <TokenReceivedNotificationIcon />, | |||||
style: { width: '100%' }, | |||||
}); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Update wallet every 10s | // Update wallet every 10s | ||||
useAsyncTimeout(async () => { | useAsyncTimeout(async () => { | ||||
const wallet = await getWallet(); | const wallet = await getWallet(); | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |