raiden-api-client
Version:
Raiden Network API TypeScript client
262 lines (261 loc) • 10.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable-next-line: no-var-requires
require("isomorphic-fetch"); /* global fetch */
class RaidenClient {
/**
* Default client that can be constructed for interacting with the Etherscan API.
*
* @param baseUrl base url of the API endpoint (e.g; http://localhost:5001/api)
* @param version version of the API (e.g; v1)
*/
constructor(baseUrl, version = "v1") {
if (!baseUrl) {
throw new Error(`baseUrl is required`);
}
if (!version) {
throw new Error(`version is required`);
}
this.apiUrl = `${baseUrl}/${version}`;
}
// Node Information
getClientAddress() {
return __awaiter(this, void 0, void 0, function* () {
return this.call(`${this.apiUrl}/address`);
});
}
// Deploying
registerToken(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/tokens/${tokenAddress}`, "PUT", 201);
});
}
// Channels
getChannels() {
return __awaiter(this, void 0, void 0, function* () {
return this.call(`${this.apiUrl}/channels`);
});
}
getChannelsForTokenAddress(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/channels/${tokenAddress}`);
});
}
getChannelsForTokenAddressAndPartnerAddress(tokenAddress, partnerAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
return this.call(`${this.apiUrl}/channels/${tokenAddress}/${partnerAddress}`);
});
}
// Channel Management
createChannel(partnerAddress, tokenAddress, totalDeposit, settleTimeout) {
return __awaiter(this, void 0, void 0, function* () {
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (totalDeposit <= 0) {
throw new Error(`totalDeposit is required`);
}
if (settleTimeout <= 0) {
throw new Error(`settleTimeout is required`);
}
const body = { partner_address: partnerAddress, token_address: tokenAddress, total_deposit: totalDeposit, settle_timeout: settleTimeout };
return this.call(`${this.apiUrl}/channels`, "PUT", 201, body);
});
}
closeChannel(tokenAddress, partnerAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
const body = { state: "closed" };
return this.call(`${this.apiUrl}/channels/${tokenAddress}/${partnerAddress}`, "PATCH", 200, body);
});
}
depositChannel(tokenAddress, partnerAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
const body = { total_deposit: 100 };
return this.call(`${this.apiUrl}/channels/${tokenAddress}/${partnerAddress}`, "PATCH", 200, body);
});
}
withdrawChannel(tokenAddress, partnerAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
const body = { total_withdraw: 100 };
return this.call(`${this.apiUrl}/channels/${tokenAddress}/${partnerAddress}`, "PATCH", 200, body);
});
}
// Tokens
getTokens() {
return __awaiter(this, void 0, void 0, function* () {
return this.call(`${this.apiUrl}/tokens`);
});
}
getTokenNetworkForTokenAddress(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/tokens/${tokenAddress}`);
});
}
getPartnersForTokenAddress(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/tokens/${tokenAddress}/partners`);
});
}
// Transfers
getPendingTransfers() {
return __awaiter(this, void 0, void 0, function* () {
return this.call(`${this.apiUrl}/pending_transfers`);
});
}
getPendingTransfersForTokenAddress(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/pending_transfers/${tokenAddress}`);
});
}
getPendingTransfersForTokenAddressAndPartner(tokenAddress, partnerAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!partnerAddress) {
throw new Error(`partnerAddress is required`);
}
return this.call(`${this.apiUrl}/pending_transfers/${tokenAddress}/${partnerAddress}`);
});
}
// Connection Management
getConnections() {
return __awaiter(this, void 0, void 0, function* () {
return this.call(`${this.apiUrl}/connections`);
});
}
joinTokenNetwork(tokenAddress, funds, channelTarget, fundsTarget) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (funds <= 0) {
throw new Error(`funds is required`);
}
const body = { funds };
if (channelTarget && channelTarget > 0) {
body.initial_channel_target = channelTarget;
}
if (fundsTarget && fundsTarget > 0) {
body.joinable_funds_target = channelTarget;
}
return this.call(`${this.apiUrl}/connections/${tokenAddress}`, "PUT", 204, body);
});
}
leaveTokenNetwork(tokenAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
return this.call(`${this.apiUrl}/connections/${tokenAddress}`, "DELETE", 200);
});
}
// Payments
initiatePayment(tokenAddress, targetAddress, amount, identifier) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!targetAddress) {
throw new Error(`targetAddress is required`);
}
if (amount <= 0) {
throw new Error(`amount is required`);
}
if (identifier <= 0) {
throw new Error(`identifier is required`);
}
const body = { amount, identifier };
return this.call(`${this.apiUrl}/payments/${tokenAddress}/${targetAddress}`, "POST", 200, body);
});
}
// Querying
queryEvents(tokenAddress, targetAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (!tokenAddress) {
throw new Error(`tokenAddress is required`);
}
if (!targetAddress) {
throw new Error(`targetAddress is required`);
}
return this.call(`${this.apiUrl}/payments/${tokenAddress}/${targetAddress}`);
});
}
// Private
call(uri, method = "GET", statusCode = 200, body = {}) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(uri, {
body: JSON.stringify(body),
method
});
if (response.status !== statusCode) {
// console.log(`Error ${response.status} - ${response.statusText} | ${uri}`);
throw new Error(`invalid response: ${response.status}`);
}
if (response.status === 204) {
return {};
}
try {
// console.log(response);
return (yield response
.clone()
.json()
.catch(() => response.text()));
}
catch (err) {
throw new Error(`failed to read response: ${err.message}`);
}
});
}
}
exports.RaidenClient = RaidenClient;