-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a compatibility shim for simulate 3.15 endpoints
- Loading branch information
1 parent
0a2d176
commit 0668358
Showing
4 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,73 @@ | ||
import base64 | ||
from typing import Any | ||
|
||
from algosdk import encoding | ||
from algosdk.atomic_transaction_composer import ( | ||
AtomicTransactionComposer, | ||
AtomicTransactionComposerStatus, | ||
SimulateABIResult, | ||
SimulateAtomicTransactionResponse, | ||
) | ||
from algosdk.error import AtomicTransactionComposerError | ||
from algosdk.v2client.algod import AlgodClient | ||
|
||
|
||
def simulate_atc_315(atc: AtomicTransactionComposer, client: AlgodClient) -> SimulateAtomicTransactionResponse: | ||
""" | ||
Ported from algosdk 2.1.2 | ||
Send the transaction group to the `simulate` endpoint and wait for results. | ||
An error will be thrown if submission or execution fails. | ||
The composer's status must be SUBMITTED or lower before calling this method, | ||
since execution is only allowed once. | ||
Returns: | ||
SimulateAtomicTransactionResponse: Object with simulation results for this | ||
transaction group, a list of txIDs of the simulated transactions, | ||
an array of results for each method call transaction in this group. | ||
If a method has no return value (void), then the method results array | ||
will contain None for that method's return value. | ||
""" | ||
|
||
if atc.status > AtomicTransactionComposerStatus.SUBMITTED: | ||
raise AtomicTransactionComposerError( # type: ignore[no-untyped-call] | ||
"AtomicTransactionComposerStatus must be submitted or lower to simulate a group" | ||
) | ||
|
||
signed_txns = atc.gather_signatures() | ||
txn = b"".join( | ||
base64.b64decode(encoding.msgpack_encode(txn)) for txn in signed_txns # type: ignore[no-untyped-call] | ||
) | ||
simulation_result = client.algod_request( | ||
"POST", "/transactions/simulate", data=txn, headers={"Content-Type": "application/x-binary"} | ||
) | ||
assert isinstance(simulation_result, dict) | ||
|
||
# Only take the first group in the simulate response | ||
txn_group: dict[str, Any] = simulation_result["txn-groups"][0] | ||
txn_results = txn_group["txn-results"] | ||
|
||
# Parse out abi results | ||
results = [] | ||
for method_index, method in atc.method_dict.items(): | ||
tx_info = txn_results[method_index]["txn-result"] | ||
|
||
result = atc.parse_result(method, atc.tx_ids[method_index], tx_info) | ||
sim_result = SimulateABIResult( | ||
tx_id=result.tx_id, | ||
raw_value=result.raw_value, | ||
return_value=result.return_value, | ||
decode_error=result.decode_error, | ||
tx_info=result.tx_info, | ||
method=result.method, | ||
) | ||
results.append(sim_result) | ||
|
||
return SimulateAtomicTransactionResponse( | ||
version=simulation_result.get("version", 0), | ||
failure_message=txn_group.get("failure-message", ""), | ||
failed_at=txn_group.get("failed-at"), | ||
simulate_response=simulation_result, | ||
tx_ids=atc.tx_ids, | ||
results=results, | ||
) |
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
0668358
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.
Coverage Report