1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 00:28:06 +01:00
metamask-extension/ui/app/pages/confirm-approve/confirm-approve.util.js
Erik Marks 3aaa41ef44
Replace abi-decoder with ethers (#9290)
* replace abi-decoder with ethers

* handle transaction parsing errors

* update token param getter function names

* add docstrings
2020-08-21 19:29:19 -07:00

34 lines
1.3 KiB
JavaScript

import { decimalToHex } from '../../helpers/utils/conversions.util'
import { calcTokenValue, getTokenAddressParam } from '../../helpers/utils/token-util'
import { getTokenData } from '../../helpers/utils/transactions.util'
export function getCustomTxParamsData (data, { customPermissionAmount, decimals }) {
const tokenData = getTokenData(data)
if (!tokenData) {
throw new Error('Invalid data')
} else if (tokenData.name !== 'approve') {
throw new Error(`Invalid data; should be 'approve' method, but instead is '${tokenData.name}'`)
}
let spender = getTokenAddressParam(tokenData)
if (spender.startsWith('0x')) {
spender = spender.substring(2)
}
const [signature, tokenValue] = data.split(spender)
if (!signature || !tokenValue) {
throw new Error('Invalid data')
} else if (tokenValue.length !== 64) {
throw new Error('Invalid token value; should be exactly 64 hex digits long (u256)')
}
let customPermissionValue = decimalToHex(calcTokenValue(customPermissionAmount, decimals))
if (customPermissionValue.length > 64) {
throw new Error('Custom value is larger than u256')
}
customPermissionValue = customPermissionValue.padStart(tokenValue.length, '0')
const customTxParamsData = `${signature}${spender}${customPermissionValue}`
return customTxParamsData
}