UNPKG

@raiden_network/raiden-cli

Version:

Raiden Light Client standalone app with a REST API via HTTP

88 lines (87 loc) 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeApiV1Router = void 0; const express_1 = require("express"); const raiden_ts_1 = require("raiden-ts"); const channels_1 = require("./channels"); const connections_1 = require("./connections"); const payments_1 = require("./payments"); const pendingTransfers_1 = require("./pendingTransfers"); const testing_1 = require("./testing"); const tokens_1 = require("./tokens"); const userDeposit_1 = require("./userDeposit"); /** * Make a Raiden APIv1 router, with all its bound endpoints * * @param this - Cli object * @returns Set up router */ function makeApiV1Router() { const router = (0, express_1.Router)(); let status = 'syncing'; this.raiden.synced.then(() => { status = 'ready'; // register these routes only when finished syncing router.use('/channels', channels_1.makeChannelsRouter.call(this)); router.use('/tokens', tokens_1.makeTokensRouter.call(this)); router.use('/pending_transfers', pendingTransfers_1.makePendingTransfersRouter.call(this)); router.use('/connections', connections_1.makeConnectionsRouter.call(this)); router.use('/payments', payments_1.makePaymentsRouter.call(this)); router.use('/_testing', testing_1.makeTestingRouter.call(this)); router.use('/user_deposit', userDeposit_1.makeUserDepositRouter.call(this)); }); router.get('/version', (_request, response) => { response.json({ version: raiden_ts_1.Raiden.version }); }); router.get('/contracts', async (_request, response) => { const contracts = this.raiden.contractsInfo; response.json({ contracts_version: raiden_ts_1.Raiden.contractVersion, token_network_registry_address: contracts.TokenNetworkRegistry.address, secret_registry_address: contracts.SecretRegistry.address, service_registry_address: contracts.ServiceRegistry.address, user_deposit_address: contracts.UserDeposit.address, monitoring_service_address: contracts.MonitoringService.address, one_to_n_address: contracts.OneToN.address, user_deposit_token_address: await this.raiden.userDepositTokenAddress(), }); }); router.get('/address', async (_request, response) => { response.json({ our_address: this.raiden.address, eth_balance: (await this.raiden.getBalance()).toString(), block_number: await this.raiden.getBlockNumber(), network: this.raiden.network, }); }); router.get('/status', (_request, response) => { response.json({ status: this.raiden.started ? status : 'unavailable', blocks_to_sync: '0', // LC don't sync block-by-block, it syncs immediately once started }); }); router.post('/shutdown', async (_request, response) => { await this.raiden.stop(); response.json({ status: 'shutdown' }); }); // config endpoints aren't on Raiden spec, but are useful for CLI & SDK management router.get('/config', (_request, response) => { response.json(raiden_ts_1.RaidenConfig.encode(this.raiden.config)); }); router.patch('/config', (request, response) => { this.raiden.updateConfig(request.body); response.json(raiden_ts_1.RaidenConfig.encode(this.raiden.config)); }); router.get('/state.json', async (_request, response) => { response.writeHead(200, { 'Content-Type': 'application/json' }); response.write('['); let count = 0; for await (const row of this.raiden.dumpDatabase()) { response.write((count++ ? ',\n' : '\n') + JSON.stringify(row)); } response.write('\n]'); response.end(); }); return router; } exports.makeApiV1Router = makeApiV1Router;