@ethersphere/bee-js
Version:
Javascript client for Bee
95 lines (94 loc) • 4.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withdrawDAI = exports.withdrawBZZ = exports.getWalletBalance = exports.getChainState = exports.getReserveState = 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 typed_bytes_1 = require("../../utils/typed-bytes");
const workaround_1 = require("../../utils/workaround");
const RESERVE_STATE_ENDPOINT = 'reservestate';
const WALLET_ENDPOINT = 'wallet';
const CHAIN_STATE_ENDPOINT = 'chainstate';
/**
* Get state of reserve
*
* @param requestOptions Options for making requests
*/
async function getReserveState(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
url: `${RESERVE_STATE_ENDPOINT}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
commitment: cafe_utility_1.Types.asNumber(body.commitment, { name: 'commitment' }),
radius: cafe_utility_1.Types.asNumber(body.radius, { name: 'radius' }),
storageRadius: cafe_utility_1.Types.asNumber(body.storageRadius, { name: 'storageRadius' }),
};
}
exports.getReserveState = getReserveState;
/**
* Get state of reserve
*
* @param requestOptions Options for making requests
*/
async function getChainState(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
url: `${CHAIN_STATE_ENDPOINT}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
block: cafe_utility_1.Types.asNumber(body.block, { name: 'block' }),
chainTip: cafe_utility_1.Types.asNumber(body.chainTip, { name: 'chainTip' }),
totalAmount: (0, type_1.asNumberString)(body.totalAmount, { name: 'totalAmount' }),
currentPrice: (0, workaround_1.normalizeCurrentPrice)(cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' })),
};
}
exports.getChainState = getChainState;
/**
* Get wallet balances for xDai and BZZ of the node
*
* @param requestOptions Options for making requests
*/
async function getWalletBalance(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
url: `${WALLET_ENDPOINT}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
bzzBalance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.bzzBalance, { name: 'bzzBalance' })),
nativeTokenBalance: tokens_1.DAI.fromWei((0, type_1.asNumberString)(body.nativeTokenBalance, { name: 'nativeTokenBalance' })),
chainID: cafe_utility_1.Types.asNumber(body.chainID, { name: 'chainID' }),
chequebookContractAddress: cafe_utility_1.Types.asString(body.chequebookContractAddress, { name: 'chequebookContractAddress' }),
walletAddress: cafe_utility_1.Types.asString(body.walletAddress, { name: 'walletAddress' }),
};
}
exports.getWalletBalance = getWalletBalance;
async function withdrawBZZ(requestOptions, amount, address) {
const response = await (0, http_1.http)(requestOptions, {
method: 'post',
url: `${WALLET_ENDPOINT}/withdraw/bzz`,
responseType: 'json',
params: { amount: amount.toPLURString(), address: address.toHex() },
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asString(body.transactionHash, { name: 'transactionHash' }));
}
exports.withdrawBZZ = withdrawBZZ;
async function withdrawDAI(requestOptions, amount, address) {
const response = await (0, http_1.http)(requestOptions, {
method: 'post',
url: `${WALLET_ENDPOINT}/withdraw/nativetoken`,
responseType: 'json',
params: { amount: amount.toWeiString(), address: address.toHex() },
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asString(body.transactionHash, { name: 'transactionHash' }));
}
exports.withdrawDAI = withdrawDAI;