2019-11-05 16:13:48 +01:00
|
|
|
import { decimalToHex } from '../../helpers/utils/conversions.util'
|
|
|
|
import { calcTokenValue } from '../../helpers/utils/token-util.js'
|
2020-01-29 03:49:32 +01:00
|
|
|
import { getTokenData } from '../../helpers/utils/transactions.util'
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-01-29 03:49:32 +01:00
|
|
|
export function getCustomTxParamsData (data, { customPermissionAmount, decimals }) {
|
|
|
|
const tokenData = getTokenData(data)
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-01-29 03:49:32 +01:00
|
|
|
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 = tokenData.params[0].value
|
|
|
|
if (spender.startsWith('0x')) {
|
|
|
|
spender = spender.substring(2)
|
|
|
|
}
|
|
|
|
const [signature, tokenValue] = data.split(spender)
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-01-29 03:49:32 +01:00
|
|
|
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)')
|
|
|
|
}
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-01-29 03:49:32 +01:00
|
|
|
let customPermissionValue = decimalToHex(calcTokenValue(customPermissionAmount, decimals))
|
|
|
|
if (customPermissionValue.length > 64) {
|
|
|
|
throw new Error('Custom value is larger than u256')
|
2019-11-05 16:13:48 +01:00
|
|
|
}
|
2020-01-29 03:49:32 +01:00
|
|
|
|
|
|
|
customPermissionValue = customPermissionValue.padStart(tokenValue.length, '0')
|
|
|
|
const customTxParamsData = `${signature}${spender}${customPermissionValue}`
|
|
|
|
return customTxParamsData
|
2019-11-05 16:13:48 +01:00
|
|
|
}
|