@ethersphere/bee-js
Version:
Javascript client for Bee
86 lines (85 loc) • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPastDueConsumptionPeerBalance = exports.getPastDueConsumptionBalances = exports.getPeerBalance = exports.getAllBalances = void 0;
const cafe_utility_1 = require("cafe-utility");
const http_1 = require("../../utils/http");
const tokens_1 = require("../../utils/tokens");
const type_1 = require("../../utils/type");
const balancesEndpoint = 'balances';
const consumedEndpoint = 'consumed';
/**
* Get the balances with all known peers including prepaid services
*
* @param requestOptions Options for making requests
*/
async function getAllBalances(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
url: balancesEndpoint,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
const balances = cafe_utility_1.Types.asArray(body.balances, { name: 'balances' }).map(x => cafe_utility_1.Types.asObject(x, { name: 'balance' }));
return {
balances: balances.map(x => ({
peer: cafe_utility_1.Types.asString(x.peer, { name: 'peer' }),
balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(x.balance, { name: 'balance' })),
})),
};
}
exports.getAllBalances = getAllBalances;
/**
* Get the balances with a specific peer including prepaid services
*
* @param requestOptions Options for making requests
* @param address Swarm address of peer
*/
async function getPeerBalance(requestOptions, address) {
const response = await (0, http_1.http)(requestOptions, {
url: `${balancesEndpoint}/${address}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
peer: cafe_utility_1.Types.asString(body.peer, { name: 'peer' }),
balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.balance, { name: 'balance' })),
};
}
exports.getPeerBalance = getPeerBalance;
/**
* Get the past due consumption balances with all known peers
*
* @param requestOptions Options for making requests
*/
async function getPastDueConsumptionBalances(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
url: consumedEndpoint,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
const balances = cafe_utility_1.Types.asArray(body.balances, { name: 'balances' }).map(x => cafe_utility_1.Types.asObject(x, { name: 'balance' }));
return {
balances: balances.map(x => ({
peer: cafe_utility_1.Types.asString(x.peer, { name: 'peer' }),
balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(x.balance, { name: 'balance' })),
})),
};
}
exports.getPastDueConsumptionBalances = getPastDueConsumptionBalances;
/**
* Get the past due consumption balance with a specific peer
*
* @param requestOptions Options for making requests
* @param address Swarm address of peer
*/
async function getPastDueConsumptionPeerBalance(requestOptions, address) {
const response = await (0, http_1.http)(requestOptions, {
url: `${consumedEndpoint}/${address}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
peer: cafe_utility_1.Types.asString(body.peer, { name: 'peer' }),
balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.balance, { name: 'balance' })),
};
}
exports.getPastDueConsumptionPeerBalance = getPastDueConsumptionPeerBalance;