kuber-client
Version:
Javascript client library for kuber server
88 lines • 4.39 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KuberApiProvider = void 0;
const axios_1 = __importDefault(require("axios"));
const http_1 = require("../utils/http");
const cbor_rpc_1 = require("cbor-rpc");
const typeConverters_1 = require("../utils/typeConverters");
const KuberProvider_1 = require("./KuberProvider");
class KuberApiProvider extends KuberProvider_1.KuberProvider {
axios;
retry;
constructor(kuberApiUrl, apiKey, retry) {
super();
const cofig = { baseURL: kuberApiUrl, apiKey };
this.axios = axios_1.default.create(cofig);
this.retry = retry;
}
async queryUTxOByAddress(address) {
const request = `/api/v3/utxo?address=${address}`;
const response = await (0, http_1.get)(this.axios, "KuberApiProvider.queryUTxOByAddress", request, this.retry);
return (0, typeConverters_1.toUTxO)(response);
}
async queryUTxOByTxIn(txIn) {
const request = `/api/v3/utxo?txin=${encodeURIComponent(txIn)}`;
const response = await (0, http_1.get)(this.axios, "KuberApiProvider.queryUTxOByTxIn", request, this.retry);
return (0, typeConverters_1.toUTxO)(response);
}
async queryProtocolParameters() {
const request = `/api/v3/protocol-params`;
const response = await (0, http_1.get)(this.axios, "KuberApiProvider.queryProtocolParameters", request, this.retry);
return response;
}
async querySystemStart() {
const request = `/api/v3/genesis-params`;
return await (0, http_1.get)(this.axios, "KuberApiProvider.querySystemStart", request, this.retry);
}
async queryChainTip() {
const request = `/api/v3/chain-point`;
return await (0, http_1.get)(this.axios, "KuberApiProvider.queryChainTip", request, this.retry);
}
/**
* Build a transaction with kuber.
* The built transaction will already have exact min-fee, exact execution-units and extra change output if necessary.
* The transaction will be ready to be signed and submitted.
* **Note** It important to remember that the transaction returned by kuber api
* and the transaction returned by kuber-client might be different due to reason mentioned here
* <a href="https://github.com/Emurgo/cardano-serialization-lib/issues/429">cardano-serialization-lib/issues</a>
* @param cip30Instance Browser cip30 provider instance obtained with enable()
* @param buildRequest Object following Kuber's transaction builder JSON spec
* @param autoAddCollateral Add collateral from provider. Kuber automatically picks collateral.
* set this to true if you want to specify exact collateral utxo.
* @returns A new rejected Promise.
*/
async buildTx(txBuilder, submit = false) {
const request = `/api/v1/tx?submit=${submit}`;
return await (0, http_1.post)(this.axios, `KuberApiProvider.buildTx_&_submit=${submit}`, request, txBuilder, this.retry);
}
/**
* Submit a transaction with kuber's submit API. Note that kuber's submit api is limted to current era transaction only
* @param tx Browser Transaction to be submitted
* @param buildRequest Object following Kuber's transaction builder JSON spec
* @returns A new rejected Promise.
*/
async submitTx(cborString) {
const request = `/api/v1/tx/submit`;
const hasWitness = Object.keys(cbor_rpc_1.cborBackend.decode(Buffer.from(cborString, "hex"))[1]).length != 0;
const parsedKuberSubmitObject = {
tx: {
cborHex: cborString,
type: hasWitness ? "Witnessed Tx ConwayEra" : "Unwitnessed Tx ConwayEra",
description: "",
},
};
return await (0, http_1.post)(this.axios, "KuberApiProvider.submitTx", request, parsedKuberSubmitObject, this.retry);
}
async calculateMinFee(tx) {
return (0, http_1.post)(this.axios, "KuberApiProvider.calculateMinFee", "api/v1/tx/fee", tx, this.retry).then((res) => {
return res.text().then((txt) => {
return BigInt(txt);
});
});
}
}
exports.KuberApiProvider = KuberApiProvider;
//# sourceMappingURL=KuberApiProvider.js.map
;