@ethersphere/bee-js
Version:
Javascript client for Bee
110 lines (109 loc) • 5.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRedistributionState = exports.stake = exports.migrateStake = exports.withdrawSurplusStake = exports.getWithdrawableStake = exports.getStake = void 0;
const cafe_utility_1 = require("cafe-utility");
const headers_1 = require("../../utils/headers");
const http_1 = require("../../utils/http");
const tokens_1 = require("../../utils/tokens");
const type_1 = require("../../utils/type");
const typed_bytes_1 = require("../../utils/typed-bytes");
const STAKE_ENDPOINT = 'stake';
const REDISTRIBUTION_ENDPOINT = 'redistributionstate';
/**
* Gets the amount of staked BZZ
*
* @param requestOptions Options for making requests
*/
async function getStake(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
responseType: 'json',
url: `${STAKE_ENDPOINT}`,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.stakedAmount, { name: 'stakedAmount' }));
}
exports.getStake = getStake;
/**
* Gets the amount of withdrawable staked BZZ
*
* @param requestOptions Options for making requests
*/
async function getWithdrawableStake(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
responseType: 'json',
url: `${STAKE_ENDPOINT}/withdrawable`,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.withdrawableAmount, { name: 'withdrawableAmount' }));
}
exports.getWithdrawableStake = getWithdrawableStake;
async function withdrawSurplusStake(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'delete',
responseType: 'json',
url: `${STAKE_ENDPOINT}/withdrawable`,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
}
exports.withdrawSurplusStake = withdrawSurplusStake;
async function migrateStake(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'delete',
responseType: 'json',
url: STAKE_ENDPOINT,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
}
exports.migrateStake = migrateStake;
/**
* Stake given amount of tokens.
*
* @param requestOptions Options for making requests
* @param amount
*/
async function stake(requestOptions, amount, options) {
const repsonse = await (0, http_1.http)(requestOptions, {
method: 'post',
responseType: 'json',
url: `${STAKE_ENDPOINT}/${amount}`,
headers: (0, headers_1.prepareRequestHeaders)(null, options),
});
const body = cafe_utility_1.Types.asObject(repsonse.data, { name: 'response.data' });
return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
}
exports.stake = stake;
/**
* Get current status of node in redistribution game
*
* @param requestOptions Options for making requests
*/
async function getRedistributionState(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
responseType: 'json',
url: REDISTRIBUTION_ENDPOINT,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
minimumGasFunds: tokens_1.DAI.fromWei((0, type_1.asNumberString)(body.minimumGasFunds, { name: 'minimumGasFunds' })),
hasSufficientFunds: cafe_utility_1.Types.asBoolean(body.hasSufficientFunds, { name: 'hasSufficientFunds' }),
isFrozen: cafe_utility_1.Types.asBoolean(body.isFrozen, { name: 'isFrozen' }),
isFullySynced: cafe_utility_1.Types.asBoolean(body.isFullySynced, { name: 'isFullySynced' }),
phase: cafe_utility_1.Types.asString(body.phase, { name: 'phase' }),
round: cafe_utility_1.Types.asNumber(body.round, { name: 'round' }),
lastWonRound: cafe_utility_1.Types.asNumber(body.lastWonRound, { name: 'lastWonRound' }),
lastPlayedRound: cafe_utility_1.Types.asNumber(body.lastPlayedRound, { name: 'lastPlayedRound' }),
lastFrozenRound: cafe_utility_1.Types.asNumber(body.lastFrozenRound, { name: 'lastFrozenRound' }),
lastSelectedRound: cafe_utility_1.Types.asNumber(body.lastSelectedRound, { name: 'lastSelectedRound' }),
lastSampleDurationSeconds: cafe_utility_1.Types.asNumber(body.lastSampleDurationSeconds, { name: 'lastSampleDurationSeconds' }),
block: cafe_utility_1.Types.asNumber(body.block, { name: 'block' }),
reward: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.reward, { name: 'reward' })),
fees: tokens_1.DAI.fromWei((0, type_1.asNumberString)(body.fees, { name: 'fees' })),
isHealthy: cafe_utility_1.Types.asBoolean(body.isHealthy, { name: 'isHealthy' }),
};
}
exports.getRedistributionState = getRedistributionState;