biketrack-app/node_modules/@ethersphere/bee-js/dist/cjs/modules/debug/chequebook.js

168 lines
5.7 KiB
JavaScript
Raw Normal View History

2022-07-11 10:27:11 +02:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 http_1 = require("../../utils/http");
const chequebookEndpoint = 'chequebook';
/**
* Get the address of the chequebook contract used
*
* @param ky Ky debug instance
*/
function getChequebookAddress(ky) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, http_1.http)(ky, {
path: chequebookEndpoint + '/address',
responseType: 'json',
});
return response.data;
});
}
exports.getChequebookAddress = getChequebookAddress;
/**
* Get the balance of the chequebook
*
* @param ky Ky debug instance
*/
function getChequebookBalance(ky) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, http_1.http)(ky, {
path: chequebookEndpoint + '/balance',
responseType: 'json',
});
return response.data;
});
}
exports.getChequebookBalance = getChequebookBalance;
/**
* Get last cashout action for the peer
*
* @param ky Ky debug instance
* @param peer Swarm address of peer
*/
function getLastCashoutAction(ky, peer) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, http_1.http)(ky, {
path: chequebookEndpoint + `/cashout/${peer}`,
responseType: 'json',
});
return response.data;
});
}
exports.getLastCashoutAction = getLastCashoutAction;
/**
* Cashout the last cheque for the peer
*
* @param ky Ky debug instance
* @param peer Swarm address of peer
* @param options
*/
function cashoutLastCheque(ky, peer, options) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {};
if (options === null || options === void 0 ? void 0 : options.gasPrice) {
headers['gas-price'] = options.gasPrice.toString();
}
if (options === null || options === void 0 ? void 0 : options.gasLimit) {
headers['gas-limit'] = options.gasLimit.toString();
}
const response = yield (0, http_1.http)(ky, {
method: 'post',
path: chequebookEndpoint + `/cashout/${peer}`,
responseType: 'json',
headers,
});
return response.data.transactionHash;
});
}
exports.cashoutLastCheque = cashoutLastCheque;
/**
* Get last cheques for the peer
*
* @param ky Ky debug instance
* @param peer Swarm address of peer
*/
function getLastChequesForPeer(ky, peer) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, http_1.http)(ky, {
path: chequebookEndpoint + `/cheque/${peer}`,
responseType: 'json',
});
return response.data;
});
}
exports.getLastChequesForPeer = getLastChequesForPeer;
/**
* Get last cheques for all peers
*
* @param ky Ky debug instance
*/
function getLastCheques(ky) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, http_1.http)(ky, {
path: chequebookEndpoint + '/cheque',
responseType: 'json',
});
return response.data;
});
}
exports.getLastCheques = getLastCheques;
/**
* Deposit tokens from overlay address into chequebook
*
* @param ky Ky debug instance
* @param amount Amount of tokens to deposit
* @param gasPrice Gas Price in WEI for the transaction call
* @return string Hash of the transaction
*/
function depositTokens(ky, amount, gasPrice) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {};
if (gasPrice) {
headers['gas-price'] = gasPrice.toString();
}
const response = yield (0, http_1.http)(ky, {
method: 'post',
path: chequebookEndpoint + '/deposit',
responseType: 'json',
searchParams: { amount: amount.toString(10) },
headers,
});
return response.data.transactionHash;
});
}
exports.depositTokens = depositTokens;
/**
* Withdraw tokens from the chequebook to the overlay address
*
* @param ky Ky debug instance
* @param amount Amount of tokens to withdraw
* @param gasPrice Gas Price in WEI for the transaction call
* @return string Hash of the transaction
*/
function withdrawTokens(ky, amount, gasPrice) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {};
if (gasPrice) {
headers['gas-price'] = gasPrice.toString();
}
const response = yield (0, http_1.http)(ky, {
method: 'post',
path: chequebookEndpoint + '/withdraw',
responseType: 'json',
searchParams: { amount: amount.toString(10) },
headers,
});
return response.data.transactionHash;
});
}
exports.withdrawTokens = withdrawTokens;