UNPKG

@ethersphere/bee-js

Version:
187 lines (186 loc) 8.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withdrawTokens = exports.depositTokens = exports.getLastCheques = exports.getLastChequesForPeer = exports.cashoutLastCheque = exports.getLastCashoutAction = exports.getChequebookBalance = exports.getChequebookAddress = 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 chequebookEndpoint = 'chequebook'; /** * Get the address of the chequebook contract used * * @param requestOptions Options for making requests */ async function getChequebookAddress(requestOptions) { const response = await (0, http_1.http)(requestOptions, { url: chequebookEndpoint + '/address', responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { chequebookAddress: cafe_utility_1.Types.asString(body.chequebookAddress, { name: 'chequebookAddress' }), }; } exports.getChequebookAddress = getChequebookAddress; /** * Get the balance of the chequebook * * @param requestOptions Options for making requests */ async function getChequebookBalance(requestOptions) { const response = await (0, http_1.http)(requestOptions, { url: chequebookEndpoint + '/balance', responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { availableBalance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.availableBalance, { name: 'availableBalance' })), totalBalance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.totalBalance, { name: 'totalBalance' })), }; } exports.getChequebookBalance = getChequebookBalance; /** * Get last cashout action for the peer * * @param requestOptions Options for making requests * @param peer Swarm address of peer */ async function getLastCashoutAction(requestOptions, peer) { const response = await (0, http_1.http)(requestOptions, { url: chequebookEndpoint + `/cashout/${peer}`, 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' }), uncashedAmount: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.uncashedAmount, { name: 'uncashedAmount' })), transactionHash: cafe_utility_1.Types.asNullableString(body.transactionHash), lastCashedCheque: cafe_utility_1.Types.asNullable(x => asCheque(x), body.lastCashedCheque), result: cafe_utility_1.Types.asNullable(x => asCashoutResult(x), body.result), }; } exports.getLastCashoutAction = getLastCashoutAction; /** * Cashout the last cheque for the peer * * @param requestOptions Options for making requests * @param peer Swarm address of peer * @param options */ async function cashoutLastCheque(requestOptions, peer, options) { const response = await (0, http_1.http)(requestOptions, { method: 'post', url: chequebookEndpoint + `/cashout/${peer}`, responseType: 'json', headers: (0, headers_1.prepareRequestHeaders)(null, options), }); 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.cashoutLastCheque = cashoutLastCheque; /** * Get last cheques for the peer * * @param requestOptions Options for making requests * @param peer Swarm address of peer */ async function getLastChequesForPeer(requestOptions, peer) { const response = await (0, http_1.http)(requestOptions, { url: chequebookEndpoint + `/cheque/${peer}`, 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' }), lastreceived: cafe_utility_1.Types.asNullable(x => asCheque(x), body.lastreceived), lastsent: cafe_utility_1.Types.asNullable(x => asCheque(x), body.lastsent), }; } exports.getLastChequesForPeer = getLastChequesForPeer; /** * Get last cheques for all peers * * @param requestOptions Options for making requests */ async function getLastCheques(requestOptions) { const response = await (0, http_1.http)(requestOptions, { url: chequebookEndpoint + '/cheque', responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); const lastcheques = cafe_utility_1.Types.asArray(body.lastcheques, { name: 'lastcheques' }).map(x => cafe_utility_1.Types.asObject(x, { name: 'lastcheque' })); return { lastcheques: lastcheques.map(x => ({ peer: cafe_utility_1.Types.asString(x.peer, { name: 'peer' }), lastreceived: cafe_utility_1.Types.asNullable(y => asCheque(y), x.lastreceived), lastsent: cafe_utility_1.Types.asNullable(y => asCheque(y), x.lastsent), })), }; } exports.getLastCheques = getLastCheques; function asCheque(x) { const object = cafe_utility_1.Types.asObject(x, { name: 'cheque' }); return { beneficiary: new typed_bytes_1.EthAddress(cafe_utility_1.Types.asString(object.beneficiary, { name: 'beneficiary' })), chequebook: new typed_bytes_1.EthAddress(cafe_utility_1.Types.asString(object.chequebook, { name: 'chequebook' })), payout: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(object.payout, { name: 'payout' })), }; } function asCashoutResult(x) { const object = cafe_utility_1.Types.asObject(x, { name: 'cashout result' }); return { recipient: cafe_utility_1.Types.asString(object.recipient, { name: 'recipient' }), lastPayout: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(object.lastPayout, { name: 'lastPayout' })), bounced: cafe_utility_1.Types.asBoolean(object.bounced, { name: 'bounced' }), }; } /** * Deposit tokens from overlay address into chequebook * * @param requestOptions Options for making requests * @param amount Amount of tokens to deposit * @param gasPrice Gas Price in WEI for the transaction call * @return string Hash of the transaction */ async function depositTokens(requestOptions, amount, gasPrice) { const headers = {}; if (gasPrice) { headers['gas-price'] = gasPrice.toString(); } const response = await (0, http_1.http)(requestOptions, { method: 'post', url: chequebookEndpoint + '/deposit', responseType: 'json', params: { amount: amount.toString(10) }, headers, }); 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.depositTokens = depositTokens; /** * Withdraw tokens from the chequebook to the overlay address * * @param requestOptions Options for making requests * @param amount Amount of tokens to withdraw * @param gasPrice Gas Price in WEI for the transaction call * @return string Hash of the transaction */ async function withdrawTokens(requestOptions, amount, gasPrice) { const headers = {}; if (gasPrice) { headers['gas-price'] = gasPrice.toString(); } const response = await (0, http_1.http)(requestOptions, { method: 'post', url: chequebookEndpoint + '/withdraw', responseType: 'json', params: { amount: amount.toString(10) }, headers, }); 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.withdrawTokens = withdrawTokens;