"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseRawEvents = parseRawEvents;
exports.parseRawLedgerEntries = parseRawLedgerEntries;
exports.parseRawSendTransaction = parseRawSendTransaction;
exports.parseRawSimulation = parseRawSimulation;
var _stellarBase = require("@stellar/stellar-base");
var _api = require("./api");
function parseRawSendTransaction(r) {
const {
errorResultXdr,
diagnosticEventsXdr
} = r;
delete r.errorResultXdr;
delete r.diagnosticEventsXdr;
if (errorResultXdr) {
return {
...r,
...(diagnosticEventsXdr !== undefined && diagnosticEventsXdr.length > 0 && {
diagnosticEvents: diagnosticEventsXdr.map(evt => _stellarBase.xdr.DiagnosticEvent.fromXDR(evt, 'base64'))
}),
errorResult: _stellarBase.xdr.TransactionResult.fromXDR(errorResultXdr, '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
})
};
})
};
}
function parseSuccessful(sim, partial) {
// 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
// eslint-disable-next-line no-self-compare
(sim.results?.length ?? 0 > 0) && {
result: sim.results.map(row => ({
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]
}),
// eslint-disable-next-line no-self-compare
...((sim.stateChanges?.length ?? 0 > 0) && {
stateChanges: sim.stateChanges?.map(entryChange => ({
type: entryChange.type,
key: _stellarBase.xdr.LedgerKey.fromXDR(entryChange.key, 'base64'),
before: entryChange.before ? _stellarBase.xdr.LedgerEntry.fromXDR(entryChange.before, 'base64') : null,
after: entryChange.after ? _stellarBase.xdr.LedgerEntry.fromXDR(entryChange.after, 'base64') : null
}))
})
};
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)
}
};
}
/**
* Converts a raw response schema into one with parsed XDR fields and a
* simplified interface.
* Warning: This API is only exported for testing purposes and should not be
* relied on or considered "stable".
*
* @param {Api.SimulateTransactionResponse|Api.RawSimulateTransactionResponse} sim the raw response schema (parsed ones are allowed, best-effort
* detected, and returned untouched)
*
* @returns the original parameter (if already parsed), parsed otherwise
*
*/
function parseRawSimulation(sim) {
const looksRaw = _api.Api.isSimulationRaw(sim);
if (!looksRaw) {
// Gordon Ramsey in shambles
return sim;
}
// shared across all responses
const base = {
_parsed: true,
id: sim.id,
latestLedger: sim.latestLedger,
events: 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);
}