You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.0 KiB
89 lines
2.0 KiB
7 years ago
|
const ethAbi = require('ethereumjs-abi')
|
||
|
const ethUtil = require('ethereumjs-util')
|
||
|
const { TOKEN_TRANSFER_FUNCTION_SIGNATURE } = require('../send.constants')
|
||
7 years ago
|
|
||
|
function addHexPrefixToObjectValues (obj) {
|
||
|
return Object.keys(obj).reduce((newObj, key) => {
|
||
|
return { ...newObj, [key]: ethUtil.addHexPrefix(obj[key]) }
|
||
|
}, {})
|
||
|
}
|
||
|
|
||
6 years ago
|
function constructTxParams ({ selectedToken, data, to, amount, from, gas, gasPrice }) {
|
||
7 years ago
|
const txParams = {
|
||
6 years ago
|
data,
|
||
7 years ago
|
from,
|
||
|
value: '0',
|
||
|
gas,
|
||
|
gasPrice,
|
||
|
}
|
||
|
|
||
|
if (!selectedToken) {
|
||
|
txParams.value = amount
|
||
|
txParams.to = to
|
||
|
}
|
||
|
|
||
6 years ago
|
return addHexPrefixToObjectValues(txParams)
|
||
7 years ago
|
}
|
||
|
|
||
|
function constructUpdatedTx ({
|
||
|
amount,
|
||
6 years ago
|
data,
|
||
7 years ago
|
editingTransactionId,
|
||
|
from,
|
||
|
gas,
|
||
|
gasPrice,
|
||
|
selectedToken,
|
||
|
to,
|
||
|
unapprovedTxs,
|
||
|
}) {
|
||
6 years ago
|
const unapprovedTx = unapprovedTxs[editingTransactionId]
|
||
|
const txParamsData = unapprovedTx.txParams.data ? unapprovedTx.txParams.data : data
|
||
6 years ago
|
|
||
7 years ago
|
const editingTx = {
|
||
6 years ago
|
...unapprovedTx,
|
||
|
txParams: Object.assign(
|
||
|
unapprovedTx.txParams,
|
||
|
addHexPrefixToObjectValues({
|
||
|
data: txParamsData,
|
||
|
to,
|
||
|
from,
|
||
|
gas,
|
||
|
gasPrice,
|
||
|
value: amount,
|
||
|
})
|
||
|
),
|
||
7 years ago
|
}
|
||
|
|
||
|
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,
|
||
|
}))
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
if (typeof editingTx.txParams.data === 'undefined') {
|
||
|
delete editingTx.txParams.data
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
return editingTx
|
||
7 years ago
|
}
|
||
|
|
||
|
function addressIsNew (toAccounts, newAddress) {
|
||
6 years ago
|
const newAddressNormalized = newAddress.toLowerCase()
|
||
|
const foundMatching = toAccounts.some(({ address }) => address === newAddressNormalized)
|
||
|
return !foundMatching
|
||
7 years ago
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
addressIsNew,
|
||
|
constructTxParams,
|
||
|
constructUpdatedTx,
|
||
7 years ago
|
addHexPrefixToObjectValues,
|
||
7 years ago
|
}
|