stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
259 lines (258 loc) • 10.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationCloudRpcHandler = void 0;
const tslib_1 = require("tslib");
const buffer_1 = require("buffer");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const axios_1 = tslib_1.__importDefault(require("axios"));
const errors_1 = require("./errors");
class ValidationCloudRpcHandler {
/**
*
* @param {NetworkConfig} networkConfig - The network to use.
* @param {string} apiKey - The API key to authenticate with Validation Cloud's API.
*
* @description - This rpc handler integrates directly with Validation Cloud's API. And uses their RPC infrastructure to carry out the RPC functions.
*
*/
constructor(networkConfig, apiKey) {
this.type = 'RpcHandler';
if (!apiKey) {
throw errors_1.VCRPCError.invalidApiKey();
}
this.networkConfig = networkConfig;
this.apiKey = apiKey;
this.baseUrl =
this.networkConfig.name === 'testnet'
? 'https://testnet.stellar.validationcloud.io/v1/'
: 'https://testnet.stellar.validationcloud.io/v1/'; // no support to mainnet yet
this.id = this.generateId();
}
generateId() {
const id = Math.floor(Math.random() * 100000).toString();
this.id = id;
return id;
}
fetch(payload) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const requestUrl = this.baseUrl + this.apiKey;
return yield axios_1.default
.post(requestUrl, payload, {
headers: {
'Content-Type': 'application/json',
},
})
.then((res) => {
return res.data;
})
.catch((error) => {
throw errors_1.VCRPCError.failedToInvokeVCApi(error, payload);
});
});
}
/**
*
* @param {string} txHash - The transaction hash to get.
*
* @returns {Promise<SorobanRpc.GetTransactionResponse | SorobanRpc.GetFailedTransactionResponse | SorobanRpc.GetMissingTransactionResponse | SorobanRpc.GetSuccessfulTransactionResponse>} The transaction response from the Soroban server.
*
* @description - Gets the transaction from the Soroban server.
*/
getTransaction(txHash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payload = {
jsonrpc: '2.0',
id: this.generateId(),
method: 'getTransaction',
params: {
hash: txHash,
},
};
const response = (yield this.fetch(payload));
const rawGetResponse = response.result;
if (rawGetResponse.status === 'NOT_FOUND') {
return rawGetResponse;
}
if (rawGetResponse.status === 'FAILED') {
return rawGetResponse;
}
//
// Necessary to parse the inner values
// as we need because they're returned as
// raw xdr strings.
//
if (rawGetResponse.status === 'SUCCESS') {
return Object.assign(Object.assign({}, rawGetResponse), { resultMetaXdr: stellar_sdk_1.xdr.TransactionMeta.fromXDR(buffer_1.Buffer.from(rawGetResponse.resultMetaXdr, 'base64'), 'raw'), resultXdr: stellar_sdk_1.xdr.TransactionResult.fromXDR(buffer_1.Buffer.from(rawGetResponse.resultXdr, 'base64'), 'raw') });
}
return rawGetResponse;
});
}
/**
* @returns {Promise<SorobanRpc.GetLatestLedgerResponse>} The latest ledger response from the Soroban server.
* @description - Gets the latest ledger from the Soroban RPC server.
* */
getLatestLedger() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'getLatestLedger',
};
const response = (yield this.fetch(payload));
return response.result;
});
}
/**
* @returns {Promise<SorobanRpc.GetHealthResponse>} The health response from the Soroban server.
* @description - Gets the health of the Soroban RPC server.
* */
getHealth() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'getHealth',
};
const response = (yield this.fetch(payload));
return response.result;
});
}
/**
* @returns {Promise<SorobanRpc.GetNetworkResponse>} The network response from the Soroban server.
* @description - Gets the network of the Soroban RPC server.
* */
getNetwork() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'getNetwork',
};
const response = (yield this.fetch(payload));
return response.result;
});
}
/**
*
* @description - Important: This integration might fail due to the RPC server build on VC. startLedger recently has been changed to a 'number'. Refer to https://github.com/stellar/js-stellar-sdk/issues/893
*
* @args {SorobanRpc.GetEventsRequest} request - The events request to get.
* @args {Api.EventFilter[]} request.filters - The filters to apply to the events.
* @args {number} request.startLedger - The start ledger to get the events from.
* @args {string} request.cursor - The cursor to get the events from.
* @args {number} request.limit - The limit of events to get.
*
*
* @returns {SorobanRpc.GetEventsResponse} The events response from the Soroban server.
*
* @description - Gets the events from the Soroban server.
*/
getEvents(request) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const cursor = request.cursor;
const limit = request.limit;
const pagination = { cursor, limit };
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'getEvents',
params: Object.assign(Object.assign({}, request), { pagination }),
};
const response = (yield this.fetch(payload));
return response.result;
});
}
/**
*
* @param {xdr.LedgerKey[]} keys - The keys to get the ledger entries for.
*
* @returns {SorobanRpc.GetLedgerEntriesResponse} The ledger entries response from the Soroban server.
*
* @description - Gets the ledger entries from the Soroban server.
*/
getLedgerEntries(...keys) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'getLedgerEntries',
params: {
keys: keys.map((key) => key.toXDR('base64')),
},
};
const response = (yield this.fetch(payload));
const rawEntries = response.result;
if (!rawEntries.entries) {
throw errors_1.VCRPCError.ledgerEntriesMissingFromRpcResponse(rawEntries);
}
const formattedEntries = Object.assign(Object.assign({}, rawEntries), { entries: rawEntries.entries.map((entry) => {
return Object.assign(Object.assign({}, entry), { key: stellar_sdk_1.xdr.LedgerKey.fromXDR(buffer_1.Buffer.from(entry.key, 'base64'), 'raw'), val: stellar_sdk_1.xdr.LedgerEntryData.fromXDR(buffer_1.Buffer.from(entry.xdr, 'base64'), 'raw') });
}) });
return formattedEntries;
});
}
/**
*
* @param {Transaction} tx - The transaction to simulate.
*
* @returns {Promise<SorobanRpc.SimulateTransactionResponse>} The transaction simulation response from the Soroban server.
*
* @description - Simulates the transaction on the Soroban server.
*/
simulateTransaction(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const txXdr = tx.toXDR();
const payload = {
jsonrpc: '2.0',
id: this.generateId(), // no need for tracking it currently
method: 'simulateTransaction',
params: {
transaction: txXdr,
},
};
const response = (yield this.fetch(payload));
const formattedResponse = stellar_sdk_1.rpc.parseRawSimulation(response.result);
return formattedResponse;
});
}
/**
*
* @param {Transaction} tx - The transaction to prepare.
*
* @returns {Promise<Transaction>} The prepared transaction.
*
* @description - Prepares the transaction on the Soroban server.
*/
prepareTransaction(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = (yield this.simulateTransaction(tx));
const assembledTx = stellar_sdk_1.rpc.assembleTransaction(tx, response);
return assembledTx.build();
});
}
/**
*
* @param {Transaction} tx - The transaction to submit.
*
* @returns {Promise<SorobanRpc.SendTransactionResponse>} The transaction submission response from the Soroban server.
*
* @description - Submits the transaction on the Soroban server.
*/
submitTransaction(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const txXdr = tx.toXDR();
const payload = {
jsonrpc: '2.0',
id: this.id,
method: 'sendTransaction',
params: {
transaction: txXdr,
},
};
const response = (yield this.fetch(payload));
const formattedResponse = response.result;
return formattedResponse;
});
}
}
exports.ValidationCloudRpcHandler = ValidationCloudRpcHandler;