2021-03-10 21:16:44 +01:00
|
|
|
import { TRANSACTION_TYPES } from '../../../../shared/constants/transaction';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { decimalToHex } from '../../helpers/utils/conversions.util';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
calcTokenValue,
|
|
|
|
getTokenAddressParam,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../helpers/utils/token-util';
|
|
|
|
import { getTokenData } from '../../helpers/utils/transactions.util';
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getCustomTxParamsData(
|
|
|
|
data,
|
|
|
|
{ customPermissionAmount, decimals },
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenData = getTokenData(data);
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-01-29 03:49:32 +01:00
|
|
|
if (!tokenData) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Invalid data');
|
2021-03-10 21:16:44 +01:00
|
|
|
} else if (tokenData.name !== TRANSACTION_TYPES.TOKEN_METHOD_APPROVE) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`Invalid data; should be 'approve' method, but instead is '${tokenData.name}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-29 03:49:32 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
let spender = getTokenAddressParam(tokenData);
|
2020-01-29 03:49:32 +01:00
|
|
|
if (spender.startsWith('0x')) {
|
2021-02-04 19:15:23 +01:00
|
|
|
spender = spender.substring(2);
|
2020-01-29 03:49:32 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Invalid data');
|
2020-01-29 03:49:32 +01:00
|
|
|
} else if (tokenValue.length !== 64) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
'Invalid token value; should be exactly 64 hex digits long (u256)',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-29 03:49:32 +01:00
|
|
|
}
|
2019-11-05 16:13:48 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
let customPermissionValue = decimalToHex(
|
|
|
|
calcTokenValue(customPermissionAmount, decimals),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-29 03:49:32 +01:00
|
|
|
if (customPermissionValue.length > 64) {
|
2021-02-04 19:15:23 +01:00
|
|
|
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
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
customPermissionValue = customPermissionValue.padStart(
|
|
|
|
tokenValue.length,
|
|
|
|
'0',
|
|
|
|
);
|
|
|
|
const customTxParamsData = `${signature}${spender}${customPermissionValue}`;
|
|
|
|
return customTxParamsData;
|
2019-11-05 16:13:48 +01:00
|
|
|
}
|