-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically decode XDR for getLedgerEntries
and friends
#154
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e869ee
Add xdr parsing to getLedgerEntries
Shaptic fc88602
Move parsers to a separate file
Shaptic 04d0b2f
Fixup other usage after breaking change
Shaptic 043c2f8
Properly handle new response schema in other methods
Shaptic 9e0997e
Add docs for xdr field, export durability
Shaptic 964ef25
Fix-up all tests
Shaptic cc389af
Fixup last test plus code fmt pass
Shaptic ec1cbe4
There's no reason to export parsers
Shaptic 458da87
Fixup changelog link
Shaptic 50a6942
Add import to non-exported testing function
Shaptic 5d7b434
Ok I am a fool
Shaptic e7ed703
Feedback: renames, better docs
Shaptic 4ab61c6
Use correct tslib
Shaptic 47e445e
Add usage warning to testing-only function
Shaptic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { xdr, SorobanDataBuilder } from 'stellar-base'; | ||
import { SorobanRpc } from './soroban_rpc'; | ||
|
||
export function parseLedgerEntries( | ||
Shaptic marked this conversation as resolved.
Show resolved
Hide resolved
Shaptic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raw: SorobanRpc.RawGetLedgerEntriesResponse | ||
): SorobanRpc.GetLedgerEntriesResponse { | ||
return { | ||
latestLedger: raw.latestLedger, | ||
entries: (raw.entries ?? []).map(rawEntry => { | ||
if (!rawEntry.key || !rawEntry.xdr) { | ||
throw new TypeError(`invalid ledger entry: ${rawEntry}`); | ||
} | ||
|
||
return { | ||
lastModifiedLedgerSeq: rawEntry.lastModifiedLedgerSeq, | ||
key: xdr.LedgerKey.fromXDR(rawEntry.key, 'base64'), | ||
val: xdr.LedgerEntryData.fromXDR(rawEntry.xdr, 'base64'), | ||
}; | ||
}) | ||
}; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
export function parseRawSimulation( | ||
sim: | ||
| SorobanRpc.SimulateTransactionResponse | ||
| SorobanRpc.RawSimulateTransactionResponse | ||
): SorobanRpc.SimulateTransactionResponse { | ||
const looksRaw = SorobanRpc.isSimulationRaw(sim); | ||
if (!looksRaw) { | ||
// Gordon Ramsey in shambles | ||
return sim; | ||
} | ||
|
||
// shared across all responses | ||
let base: SorobanRpc.BaseSimulateTransactionResponse = { | ||
_parsed: true, | ||
id: sim.id, | ||
latestLedger: sim.latestLedger, | ||
events: sim.events?.map( | ||
evt => 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: SorobanRpc.RawSimulateTransactionResponse, | ||
partial: SorobanRpc.BaseSimulateTransactionResponse | ||
): | ||
| SorobanRpc.SimulateTransactionRestoreResponse | ||
| SorobanRpc.SimulateTransactionSuccessResponse { | ||
|
||
// success type: might have a result (if invoking) and... | ||
const success: SorobanRpc.SimulateTransactionSuccessResponse = { | ||
...partial, | ||
transactionData: new 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?.length ?? 0 > 0) && | ||
{ | ||
result: sim.results!.map(row => { | ||
return { | ||
auth: (row.auth ?? []).map((entry) => | ||
xdr.SorobanAuthorizationEntry.fromXDR(entry, 'base64')), | ||
// if return value is missing ("falsy") we coalesce to void | ||
retval: !!row.xdr | ||
? xdr.ScVal.fromXDR(row.xdr, 'base64') | ||
: 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 SorobanDataBuilder( | ||
sim.restorePreamble!.transactionData | ||
), | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once it's available in the wild, nothing prevents clients from using in their production side code, should comment just be removed or not exported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't avoid exporting because it would mean it isn't available for browser testing, unfortunately :( you're right that nothing is stopping people from using it if they want. I added a comment to that effect in 47e445e.