UNPKG

raiden-api-sdk

Version:

SDK for interacting with the Raiden API

99 lines (98 loc) 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var apis_1 = require("./apis"); var operators_1 = require("rxjs/operators"); var ajax_1 = require("rxjs/ajax"); var errors_1 = require("./errors"); /** * Creates an object used to initiate a payment * * @param address - The address of the token * @param amount - The amount you want to pay */ function NewToken(address, amount) { return { address: address, amount: amount, }; } exports.NewToken = NewToken; var Payments = /** @class */ (function () { function Payments(paymentsApi) { this.paymentsApi = paymentsApi; } Payments.create = function (config) { var paymentsApi = new apis_1.PaymentsApi(config); return new Payments(paymentsApi); }; /** * Initiate a payment * * @remarks * The request will only return once the payment either succeeded or failed. * * A payment can fail due to the expiration of a lock, the target being offline, * channels on the path to the target not having enough `settleTimeout` and * `revealTimeout` in order to allow the payment to be propagated safely, not enough funds etc. * @param token - Payment details * @param to - Address of the recipient * @param identifier - Identifier of the payment * * @since 0.100.3 * @throws {@link RaidenAPIError} * @see {@link https://raiden-network.readthedocs.io/en/latest/rest_api.html#post--api-(version)-payments-(token_address)-(target_address)} */ Payments.prototype.initiate = function (token, to, identifier) { return this.paymentsApi .pay({ tokenAddress: token.address, targetAddress: to, payment: { amount: token.amount, identifier: identifier, }, }) .pipe(operators_1.catchError(function (err) { if (err instanceof ajax_1.AjaxError) { return rxjs_1.throwError(new errors_1.RaidenAPIError(err)); } return rxjs_1.throwError(err); })); }; /** * Payment History * * @param tokenAddress - The token you want history for * @param targetAddress - The target address you want history for * * @since 0.100.3 * @throws {@link RaidenAPIError} * @see {@link https://raiden-network.readthedocs.io/en/latest/rest_api.html#get--api-v1-payments-(token_address)-(target_address)} */ Payments.prototype.history = function (tokenAddress, targetAddress) { var stream; if (tokenAddress && targetAddress) { stream = this.paymentsApi.getPaymentsByTokenForTarget({ tokenAddress: tokenAddress, targetAddress: targetAddress, }); } else if (tokenAddress) { stream = this.paymentsApi.getPaymentsByToken({ tokenAddress: tokenAddress, }); } else { stream = this.paymentsApi.getPayments(); } return stream.pipe(operators_1.catchError(function (err) { if (err instanceof ajax_1.AjaxError) { return rxjs_1.throwError(new errors_1.RaidenAPIError(err)); } return rxjs_1.throwError(err); })); }; return Payments; }()); exports.Payments = Payments;