@ethersphere/bee-js
Version:
Javascript client for Bee
91 lines (90 loc) • 4.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cancelTransaction = exports.rebroadcastTransaction = exports.getTransaction = exports.getAllTransactions = void 0;
const cafe_utility_1 = require("cafe-utility");
const http_1 = require("../../utils/http");
const type_1 = require("../../utils/type");
const typed_bytes_1 = require("../../utils/typed-bytes");
const transactionsEndpoint = 'transactions';
/**
* Get list of all pending transactions
*
* @param requestOptions Options for making requests
*/
async function getAllTransactions(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
url: transactionsEndpoint,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
const pendingTransactions = cafe_utility_1.Types.asArray(body.pendingTransactions, { name: 'pendingTransactions' });
return pendingTransactions.map(toTransaction);
}
exports.getAllTransactions = getAllTransactions;
/**
* Get information for specific pending transactions
*
* @param requestOptions Options for making requests
* @param transactionHash Hash of the transaction
*/
async function getTransaction(requestOptions, transactionHash) {
const response = await (0, http_1.http)(requestOptions, {
url: `${transactionsEndpoint}/${transactionHash}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return toTransaction(body);
}
exports.getTransaction = getTransaction;
function toTransaction(value) {
const object = cafe_utility_1.Types.asObject(value, { name: 'transaction' });
return {
transactionHash: new typed_bytes_1.TransactionId(cafe_utility_1.Types.asString(object.transactionHash, { name: 'transactionHash' })),
to: cafe_utility_1.Types.asString(object.to, { name: 'to' }),
nonce: cafe_utility_1.Types.asNumber(object.nonce, { name: 'nonce' }),
gasPrice: (0, type_1.asNumberString)(object.gasPrice, { name: 'gasPrice' }),
gasLimit: cafe_utility_1.Types.asNumber(object.gasLimit, { name: 'gasLimit' }),
data: cafe_utility_1.Types.asString(object.data, { name: 'data' }),
created: cafe_utility_1.Types.asString(object.created, { name: 'created' }),
description: cafe_utility_1.Types.asString(object.description, { name: 'description' }),
value: (0, type_1.asNumberString)(object.value, { name: 'value' }),
};
}
/**
* Rebroadcast existing transaction
*
* @param requestOptions Options for making requests
* @param transactionHash Hash of the transaction
*/
async function rebroadcastTransaction(requestOptions, transactionHash) {
const response = await (0, http_1.http)(requestOptions, {
method: 'post',
url: `${transactionsEndpoint}/${transactionHash}`,
responseType: 'json',
});
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.rebroadcastTransaction = rebroadcastTransaction;
/**
* Cancel existing transaction
*
* @param requestOptions Options for making requests
* @param transactionHash Hash of the transaction
* @param gasPrice Optional gas price
*/
async function cancelTransaction(requestOptions, transactionHash, gasPrice) {
const headers = {};
if (gasPrice) {
headers['gas-price'] = gasPrice.toString();
}
const response = await (0, http_1.http)(requestOptions, {
method: 'delete',
headers,
url: `${transactionsEndpoint}/${transactionHash}`,
responseType: 'json',
});
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.cancelTransaction = cancelTransaction;