aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/pages/send/send-footer/send-footer.utils.js
diff options
context:
space:
mode:
authorpldespaigne <pl.despaigne@gmail.com>2019-05-31 00:22:55 +0800
committerpldespaigne <pl.despaigne@gmail.com>2019-05-31 00:22:55 +0800
commit9a658ee53d1f75ce07c33581ac1189fa8c4fd173 (patch)
treeea92ef1971ffaa72c29bf16904906bc1841654c7 /ui/app/pages/send/send-footer/send-footer.utils.js
parent9b87aaae1907eb04ca0a4055b5bb2c863e56aa39 (diff)
parent681f3f67b89b64fc837df1103198b641c7e7b2d6 (diff)
downloadtangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar.gz
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar.bz2
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar.lz
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar.xz
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.tar.zst
tangerine-wallet-browser-9a658ee53d1f75ce07c33581ac1189fa8c4fd173.zip
merge
Diffstat (limited to 'ui/app/pages/send/send-footer/send-footer.utils.js')
-rw-r--r--ui/app/pages/send/send-footer/send-footer.utils.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/ui/app/pages/send/send-footer/send-footer.utils.js b/ui/app/pages/send/send-footer/send-footer.utils.js
new file mode 100644
index 000000000..91ac29014
--- /dev/null
+++ b/ui/app/pages/send/send-footer/send-footer.utils.js
@@ -0,0 +1,88 @@
+const ethAbi = require('ethereumjs-abi')
+const ethUtil = require('ethereumjs-util')
+const { TOKEN_TRANSFER_FUNCTION_SIGNATURE } = require('../send.constants')
+
+function addHexPrefixToObjectValues (obj) {
+ return Object.keys(obj).reduce((newObj, key) => {
+ return { ...newObj, [key]: ethUtil.addHexPrefix(obj[key]) }
+ }, {})
+}
+
+function constructTxParams ({ selectedToken, data, to, amount, from, gas, gasPrice }) {
+ const txParams = {
+ data,
+ from,
+ value: '0',
+ gas,
+ gasPrice,
+ }
+
+ if (!selectedToken) {
+ txParams.value = amount
+ txParams.to = to
+ }
+
+ return addHexPrefixToObjectValues(txParams)
+}
+
+function constructUpdatedTx ({
+ amount,
+ data,
+ editingTransactionId,
+ from,
+ gas,
+ gasPrice,
+ selectedToken,
+ to,
+ unapprovedTxs,
+}) {
+ const unapprovedTx = unapprovedTxs[editingTransactionId]
+ const txParamsData = unapprovedTx.txParams.data ? unapprovedTx.txParams.data : data
+
+ const editingTx = {
+ ...unapprovedTx,
+ txParams: Object.assign(
+ unapprovedTx.txParams,
+ addHexPrefixToObjectValues({
+ data: txParamsData,
+ to,
+ from,
+ gas,
+ gasPrice,
+ value: amount,
+ })
+ ),
+ }
+
+ if (selectedToken) {
+ const data = TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(
+ ethAbi.rawEncode(['address', 'uint256'], [to, ethUtil.addHexPrefix(amount)]),
+ x => ('00' + x.toString(16)).slice(-2)
+ ).join('')
+
+ Object.assign(editingTx.txParams, addHexPrefixToObjectValues({
+ value: '0',
+ to: selectedToken.address,
+ data,
+ }))
+ }
+
+ if (typeof editingTx.txParams.data === 'undefined') {
+ delete editingTx.txParams.data
+ }
+
+ return editingTx
+}
+
+function addressIsNew (toAccounts, newAddress) {
+ const newAddressNormalized = newAddress.toLowerCase()
+ const foundMatching = toAccounts.some(({ address }) => address === newAddressNormalized)
+ return !foundMatching
+}
+
+module.exports = {
+ addressIsNew,
+ constructTxParams,
+ constructUpdatedTx,
+ addHexPrefixToObjectValues,
+}