"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseRawEvents = parseRawEvents;
exports.parseRawLedgerEntries = parseRawLedgerEntries;
exports.parseRawSendTransaction = parseRawSendTransaction;
exports.parseRawSimulation = parseRawSimulation;
var _stellarBase = require("stellar-base");
var _soroban_rpc = require("./soroban_rpc");
function parseRawSendTransaction(r) {
const errResult = r.errorResultXdr;
delete r.errorResultXdr;
if (!!errResult) {
return {
...r,
errorResult: _stellarBase.xdr.TransactionResult.fromXDR(errResult, 'base64')
};
}
return {
...r
};
}
function parseRawEvents(r) {
return {
latestLedger: r.latestLedger,
events: (r.events ?? []).map(evt => {
const clone = {
...evt
};
delete clone.contractId; // `as any` hack because contractId field isn't optional
// the contractId may be empty so we omit the field in that case
return {
...clone,
...(evt.contractId !== '' && {
contractId: new _stellarBase.Contract(evt.contractId)
}),
topic: evt.topic.map(topic => _stellarBase.xdr.ScVal.fromXDR(topic, 'base64')),
value: _stellarBase.xdr.ScVal.fromXDR(evt.value, 'base64')
};
})
};
}
function parseRawLedgerEntries(raw) {
return {
latestLedger: raw.latestLedger,
entries: (raw.entries ?? []).map(rawEntry => {
if (!rawEntry.key || !rawEntry.xdr) {
throw new TypeError(`invalid ledger entry: ${JSON.stringify(rawEntry)}`);
}
return {
lastModifiedLedgerSeq: rawEntry.lastModifiedLedgerSeq,
key: _stellarBase.xdr.LedgerKey.fromXDR(rawEntry.key, 'base64'),
val: _stellarBase.xdr.LedgerEntryData.fromXDR(rawEntry.xdr, 'base64'),
...(rawEntry.liveUntilLedgerSeq !== undefined && {
liveUntilLedgerSeq: rawEntry.liveUntilLedgerSeq
})
};
})
};
}
/**
* Converts a raw response schema into one with parsed XDR fields and a
* simplified interface.
*
* @param raw the raw response schema (parsed ones are allowed, best-effort
* detected, and returned untouched)
*
* @returns the original parameter (if already parsed), parsed otherwise
*
* @warning This API is only exported for testing purposes and should not be
* relied on or considered "stable".
*/
function parseRawSimulation(sim) {
var _sim$events;
const looksRaw = _soroban_rpc.SorobanRpc.isSimulationRaw(sim);
if (!looksRaw) {
// Gordon Ramsey in shambles
return sim;
}
// shared across all responses
let base = {
_parsed: true,
id: sim.id,
latestLedger: sim.latestLedger,
events: ((_sim$events = sim.events) === null || _sim$events === void 0 ? void 0 : _sim$events.map(evt => _stellarBase.xdr.DiagnosticEvent.fromXDR(evt, 'base64'))) ?? []
};
// error type: just has error string
if (typeof sim.error === 'string') {
return {
...base,
error: sim.error
};
}
return parseSuccessful(sim, base);
}
function parseSuccessful(sim, partial) {
var _sim$results;
// success type: might have a result (if invoking) and...
const success = {
...partial,
transactionData: new _stellarBase.SorobanDataBuilder(sim.transactionData),
minResourceFee: sim.minResourceFee,
cost: sim.cost,
...(
// coalesce 0-or-1-element results[] list into a single result struct
// with decoded fields if present
(((_sim$results = sim.results) === null || _sim$results === void 0 ? void 0 : _sim$results.length) ?? 0 > 0) && {
result: sim.results.map(row => {
return {
auth: (row.auth ?? []).map(entry => _stellarBase.xdr.SorobanAuthorizationEntry.fromXDR(entry, 'base64')),
// if return value is missing ("falsy") we coalesce to void
retval: !!row.xdr ? _stellarBase.xdr.ScVal.fromXDR(row.xdr, 'base64') : _stellarBase.xdr.ScVal.scvVoid()
};
})[0]
})
};
if (!sim.restorePreamble || sim.restorePreamble.transactionData === '') {
return success;
}
// ...might have a restoration hint (if some state is expired)
return {
...success,
restorePreamble: {
minResourceFee: sim.restorePreamble.minResourceFee,
transactionData: new _stellarBase.SorobanDataBuilder(sim.restorePreamble.transactionData)
}
};
}