UNPKG

@raiden_network/raiden-cli

Version:

Raiden Light Client standalone app with a REST API via HTTP

135 lines (134 loc) 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeUserDepositRouter = void 0; const ethers_1 = require("ethers"); const express_1 = require("express"); const validation_1 = require("../utils/validation"); const MalformedNumberValue = new Error('A provided number value was not decodable'); const MalformedRequestFormat = new Error('The provided JSON is in some way malformed'); const TotalDepositTooLowError = new Error('The provided total_deposit is not higher than the previous total_deposit'); function parseAsBigNumber(input) { try { return ethers_1.BigNumber.from(input); } catch { throw MalformedNumberValue; } } function getTotalDepositAmount(request) { return request.body.total_deposit?.toString(); } function getPlannedWithdrawAmount(request) { return request.body.planned_withdraw_amount?.toString(); } function getWithdrawAmount(request) { return request.body.withdraw_amount?.toString(); } function shouldDeposit(request) { return (getTotalDepositAmount(request) !== undefined && getPlannedWithdrawAmount(request) === undefined && getWithdrawAmount(request) === undefined); } function shouldPlanWithdraw(request) { return (getTotalDepositAmount(request) === undefined && getPlannedWithdrawAmount(request) !== undefined && getWithdrawAmount(request) === undefined); } function shouldWithdraw(request) { return (getTotalDepositAmount(request) === undefined && getPlannedWithdrawAmount(request) === undefined && getWithdrawAmount(request) !== undefined); } async function deposit(totalDeposit) { const totalDepositTarget = parseAsBigNumber(totalDeposit); const currentTotalDeposit = await this.raiden.getUDCTotalDeposit(); const depositDifference = totalDepositTarget.sub(currentTotalDeposit); if (depositDifference.lte(ethers_1.constants.Zero)) throw TotalDepositTooLowError; const transactionHash = await this.raiden.depositToUDC(depositDifference); return { transaction_hash: transactionHash }; } async function planWithdraw(amount) { const transactionHash = await this.raiden.planUDCWithdraw(amount); const withdrawPlan = await this.raiden.getUDCWithdrawPlan(); return { transaction_hash: transactionHash, planned_withdrawable_after: withdrawPlan.withdrawableAfter, // The plan must exists here. }; } async function withdraw(amount) { const transactionHash = await this.raiden.withdrawFromUDC(amount); return { transaction_hash: transactionHash }; } async function determineAndExecuteRequestedInteraction(request) { if (shouldDeposit(request)) { const totalDeposit = getTotalDepositAmount(request); // Can't be undefined here. return await deposit.call(this, totalDeposit); } else if (shouldPlanWithdraw(request)) { const amount = getPlannedWithdrawAmount(request); // Can't be undefined here. return await planWithdraw.call(this, amount); } else if (shouldWithdraw(request)) { const amount = getWithdrawAmount(request); // Can't be undefined here. return await withdraw.call(this, amount); } else { throw MalformedRequestFormat; } } async function getUDCInfo(_request, response) { const balance = await this.raiden.getUDCCapacity(); const totalDeposit = await this.raiden.getUDCTotalDeposit(); const udcTokenAddress = await this.raiden.userDepositTokenAddress(); const udcAddress = this.raiden.contractsInfo.UserDeposit.address; response.json({ udc_token_address: udcTokenAddress, udc_address: udcAddress, balance: balance.toString(), total_deposit: totalDeposit.toString(), }); } async function getUDCWithdrawPlan(_request, response) { const withdrawPlan = await this.raiden.getUDCWithdrawPlan(); if (withdrawPlan !== undefined) { response.json({ ...withdrawPlan, amount: withdrawPlan.amount.toString() }); } else { response.send(); } } async function interactWithUDC(request, response) { try { const interactionResponse = await determineAndExecuteRequestedInteraction.call(this, request); response.json(interactionResponse); } catch (error) { if (error === MalformedNumberValue || error === MalformedRequestFormat) { response.status(400).send(error.message); } else if ((0, validation_1.isInsuficientFundsError)(error)) { response.status(402).send(error.message); } else if ((0, validation_1.isInvalidParameterError)(error) || (0, validation_1.isConflictError)(error) || error === TotalDepositTooLowError) { response.status(409).send(error.message); } else { response.status(500).send(error.message); } } } /** * @param this - Cli object * @returns Router instance */ function makeUserDepositRouter() { const router = (0, express_1.Router)(); router.get('/', getUDCInfo.bind(this)); router.post('/', interactWithUDC.bind(this)); router.get('/withdraw_plan', getUDCWithdrawPlan.bind(this)); return router; } exports.makeUserDepositRouter = makeUserDepositRouter;