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.
28 lines
1.1 KiB
28 lines
1.1 KiB
import { decimalToHex } from '../../helpers/utils/conversions.util'
|
|
import { calcTokenValue } from '../../helpers/utils/token-util.js'
|
|
|
|
export function getCustomTxParamsData (data, { customPermissionAmount, tokenAmount, decimals }) {
|
|
if (customPermissionAmount) {
|
|
const tokenValue = decimalToHex(calcTokenValue(tokenAmount, decimals))
|
|
|
|
const re = new RegExp('(^.+)' + tokenValue + '$')
|
|
const matches = re.exec(data)
|
|
|
|
if (!matches || !matches[1]) {
|
|
return data
|
|
}
|
|
let dataWithoutCurrentAmount = matches[1]
|
|
const customPermissionValue = decimalToHex(calcTokenValue(Number(customPermissionAmount), decimals))
|
|
|
|
const differenceInLengths = customPermissionValue.length - tokenValue.length
|
|
const zeroModifier = dataWithoutCurrentAmount.length - differenceInLengths
|
|
if (differenceInLengths > 0) {
|
|
dataWithoutCurrentAmount = dataWithoutCurrentAmount.slice(0, zeroModifier)
|
|
} else if (differenceInLengths < 0) {
|
|
dataWithoutCurrentAmount = dataWithoutCurrentAmount.padEnd(zeroModifier, 0)
|
|
}
|
|
|
|
const customTxParamsData = dataWithoutCurrentAmount + customPermissionValue
|
|
return customTxParamsData
|
|
}
|
|
}
|
|
|