Skip to content
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

Use the new Soroban RPC simulation method when preparing transactions #77

Merged
merged 16 commits into from
May 18, 2023

Conversation

sreuland
Copy link
Contributor

@sreuland sreuland commented May 9, 2023

Adding support to 'prepare transaction' to apply new rpc simulation data, including multi-function auths, and suggested fees to the tx. Wanted to put the PR up asap get visibility and any course correction here if needed. working on unit tests now.

This depends on the rpc simulation changes - https://github.com/stellar/soroban-tools/pull/636/files

Closes #76

package.json Outdated
@@ -138,7 +138,7 @@
"axios": "0.25.0",
"es6-promise": "^4.2.4",
"lodash": "4.17.21",
"stellar-base": "8.2.2-soroban.12",
"stellar-base": "https://github.com/stellar/js-stellar-base\\#host_functions_ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change this to v9.0.0-soroban.1 when it's out, for now using this slightly newer stellar-base that has tweaks on txbuilder - js-stellar-base#604

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just going to unresolve this so you don't forget after you get approval

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sreuland sreuland changed the title use newer rpc simulation results on transaction prepare, apply n… use new rpc simulation on transaction prep May 9, 2023
src/transaction.ts Outdated Show resolved Hide resolved
src/transaction.ts Outdated Show resolved Hide resolved
src/transaction.ts Outdated Show resolved Hide resolved
src/transaction.ts Outdated Show resolved Hide resolved
yarn.lock Show resolved Hide resolved
@sreuland sreuland requested a review from Shaptic May 15, 2023 16:34
rawInvokeHostFunctionOp.functions.length !== simulation.results.length
) {
throw new Error(
"preflight simulation results do not contain same count of HostFunctions that InvokeHostFunctionOp in the transaction has.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the intricacies of the multi-op host functions yet: why is the length enforced to be 1 above, but that isn't enforced here? I guess it's implicit, but I just want to make sure.

Related: is there no way to simulate transactions with multiple invokeHostOperations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmkozh , can you confirm with the latest changes for multi-function InvokeHostFunctionOp, the limitation for a single InvokeHostFunctionOp in Tx is still being enforced? I thought have seen that mentioned in chat and was assuming that limitation on validation of tx here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh yeah sorry, you're right 🤦 one op per tx, but the op can have many fns. got it!

😆 too many layers 🍰

// surpass that value.
// 'classic' fees are measured as the product of tx.fee * 'number of operations', In soroban contract tx,
// there can only be single operation in the tx, so can safely make simplification of classic fee = tx.fee.
fee: Math.max(classicFeeNum + minResourceFeeNum, classicFeeNum).toString(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, when will this ever return the second case of the max? it seems like the first arg is strictly larger. I guess minResourceFeeNum could be negative, but that's... adversarial on the client's end?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the second argument to the Math.max function was supposed to be raw.fee

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good point, max is un-needed at this point, the incoming tx.fee has really become the 'suggested inclusion fee' when using server.prepareTransaction(tx, networkPassphrase).

I think we can only append minResourceFee to what user provides on incoming tx.fee and return the sum as the updated tx.fee on the returned transaction from server.prepareTransaction(tx, networkPassphrase). I've updated the js docs on that method to illustrate further.

src/transaction.ts Outdated Show resolved Hide resolved
src/transaction.ts Outdated Show resolved Hide resolved
const txnBuilder = new TransactionBuilder(source, {
// automatically update the 'classic' tx fee if min resource fees from simulation response
// surpass that value.
// 'classic' fees are measured as the product of tx.fee * 'number of operations', In soroban contract tx,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classic fees are classicFeeNum * operations num right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, on classic tx's that would be the way classic fees would result, with the limitation of only one operation in the soroban tx, have reduced to just classic fees = classicFeeNum(the parsed numeric value of user specified tx.fee).

Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really nice test coverage 👏

package.json Outdated
@@ -138,7 +138,7 @@
"axios": "0.25.0",
"es6-promise": "^4.2.4",
"lodash": "4.17.21",
"stellar-base": "8.2.2-soroban.12",
"stellar-base": "https://github.com/stellar/js-stellar-base\\#host_functions_ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just going to unresolve this so you don't forget after you get approval

rawInvokeHostFunctionOp.functions.length !== simulation.results.length
) {
throw new Error(
"preflight simulation results do not contain same count of HostFunctions that InvokeHostFunctionOp in the transaction has.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh yeah sorry, you're right 🤦 one op per tx, but the op can have many fns. got it!

😆 too many layers 🍰

@Shaptic Shaptic changed the title use new rpc simulation on transaction prep Use the new Soroban RPC simulation method when preparing transactions May 18, 2023
@sreuland sreuland merged commit f558bb8 into stellar:main May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

js-soroban-client: add support for fees
3 participants