@raiden_network/raiden-cli
Version:
Raiden Light Client standalone app with a REST API via HTTP
138 lines (137 loc) • 5.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryAsNumbers = exports.isInsuficientFundsError = exports.isConflictError = exports.isTransferFailedError = exports.isTransactionWouldFailError = exports.isInvalidParameterError = exports.validateOptionalAddressParameter = exports.validateAddressParameter = void 0;
const raiden_ts_1 = require("raiden-ts");
/**
* Validate bound string is a valid address in request's params
*
* @param this - Parameter name
* @param request - Check param on this request
* @param response - Fill this response in case of failure
* @param next - Callback
*/
function validateAddressParameter(request, response, next) {
const addressParameter = request.params[this];
if (!raiden_ts_1.Address.is(addressParameter)) {
response
.status(404)
.send(`The given address '${addressParameter}' is not valid eip55-encoded Ethereum address`);
}
else {
next();
}
}
exports.validateAddressParameter = validateAddressParameter;
/**
* Validate bound string is a valid address in request's params, optionally
*
* @param this - Parameter name
* @param request - Check param on this request
* @param response - Fill this response in case of failure
* @param next - Callback
*/
function validateOptionalAddressParameter(request, response, next) {
if (!request.params[this])
next();
else
validateAddressParameter.call(this, request, response, next);
}
exports.validateOptionalAddressParameter = validateOptionalAddressParameter;
/**
* Checks an error is an InvalidParameter error
*
* @param error - Error to test
* @returns True if error is one of InvalidParameter errors
*/
function isInvalidParameterError(error) {
return [
raiden_ts_1.ErrorCodes.DTA_NEGATIVE_NUMBER,
raiden_ts_1.ErrorCodes.DTA_NUMBER_TOO_LARGE,
raiden_ts_1.ErrorCodes.DTA_ARRAY_LENGTH_DIFFERENCE,
raiden_ts_1.ErrorCodes.DTA_UNENCODABLE_DATA,
raiden_ts_1.ErrorCodes.DTA_NON_POSITIVE_NUMBER,
raiden_ts_1.ErrorCodes.DTA_INVALID_ADDRESS,
raiden_ts_1.ErrorCodes.DTA_INVALID_TIMEOUT,
raiden_ts_1.ErrorCodes.DTA_INVALID_PAYMENT_ID,
raiden_ts_1.ErrorCodes.DTA_INVALID_PATH,
raiden_ts_1.ErrorCodes.DTA_INVALID_PFS,
raiden_ts_1.ErrorCodes.DTA_INVALID_DEPOSIT,
raiden_ts_1.ErrorCodes.DTA_INVALID_AMOUNT,
].includes(error.message);
}
exports.isInvalidParameterError = isInvalidParameterError;
/**
* This checks for a typical error message pattern that gets thrown by EthersJS.
* It occurs while trying to estimate the amount of gas for a transaction.
* For the use-case here this usually means that the parameter for the contracts
* function call lead to a failing require statement. An example would be
* insufficient tokens funds for depositing.
*
* @param error - Error to test
* @returns True if error is a TransactionWoulfFail error
*/
function isTransactionWouldFailError(error) {
return /always failing transaction/.test(error.message);
}
exports.isTransactionWouldFailError = isTransactionWouldFailError;
/**
* Checks if error is related to a failed transfer
*
* @param error - Error to test
* @returns True if error signals failed transfer
*/
function isTransferFailedError(error) {
return [
raiden_ts_1.ErrorCodes.XFER_ALREADY_COMPLETED,
raiden_ts_1.ErrorCodes.XFER_CHANNEL_CLOSED_PREMATURELY,
raiden_ts_1.ErrorCodes.XFER_EXPIRED,
raiden_ts_1.ErrorCodes.XFER_INVALID_SECRETREQUEST,
raiden_ts_1.ErrorCodes.XFER_REFUNDED,
raiden_ts_1.ErrorCodes.XFER_REGISTERSECRET_TX_FAILED,
].includes(error.message);
}
exports.isTransferFailedError = isTransferFailedError;
/**
* @param error - Error to test
* @returns True if error is a Conflict error
*/
function isConflictError(error) {
return ([
raiden_ts_1.ErrorCodes.RDN_UNKNOWN_TOKEN_NETWORK,
raiden_ts_1.ErrorCodes.CNL_INVALID_STATE,
raiden_ts_1.ErrorCodes.CNL_NO_OPEN_CHANNEL_FOUND,
raiden_ts_1.ErrorCodes.DTA_INVALID_DEPOSIT,
raiden_ts_1.ErrorCodes.DTA_INVALID_AMOUNT,
raiden_ts_1.ErrorCodes.UDC_WITHDRAW_NO_BALANCE,
raiden_ts_1.ErrorCodes.UDC_PLAN_WITHDRAW_EXCEEDS_AVAILABLE,
raiden_ts_1.ErrorCodes.UDC_WITHDRAW_NO_PLAN,
raiden_ts_1.ErrorCodes.UDC_WITHDRAW_TOO_LARGE,
].includes(error.message) || isTransactionWouldFailError(error));
}
exports.isConflictError = isConflictError;
/**
* @param error - Error to test
* @param error.message - Error message
* @param error.code - Error code
* @returns True if error is an InsufficientFunds error
*/
function isInsuficientFundsError(error) {
return (error.code === 'INSUFFICIENT_FUNDS' ||
[
raiden_ts_1.ErrorCodes.RDN_INSUFFICIENT_BALANCE,
raiden_ts_1.ErrorCodes.CNL_WITHDRAW_AMOUNT_TOO_LOW,
raiden_ts_1.ErrorCodes.CNL_WITHDRAW_AMOUNT_TOO_HIGH,
].includes(error.message));
}
exports.isInsuficientFundsError = isInsuficientFundsError;
const numRe = /^\d+$/;
/**
* Convert query parameters from strings to numbers
*
* @param query - query params mapping
* @returns mapping with numeric strings converted to numbers
*/
function queryAsNumbers(query) {
return Object.fromEntries(Object.entries(query).map(([k, v]) => [k, numRe.test(v) ? +v : v]));
}
exports.queryAsNumbers = queryAsNumbers;