Skip to content

Commit

Permalink
API: Support updated simulate endpoint (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos authored Apr 19, 2023
1 parent 44db7b2 commit c1120d5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
2 changes: 1 addition & 1 deletion algosdk/atomic_transaction_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def simulate(
)

simulation_result = cast(
Dict[str, Any], client.simulate_transactions(self.signed_txns)
Dict[str, Any], client.simulate_raw_transactions(self.signed_txns)
)
# Only take the first group in the simulate response
txn_group: Dict[str, Any] = simulation_result["txn-groups"][0]
Expand Down
50 changes: 25 additions & 25 deletions algosdk/v2client/algod.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from urllib.request import Request, urlopen

from algosdk import constants, encoding, error, transaction, util
from algosdk.v2client import models

AlgodResponseType = Union[Dict[str, Any], bytes]

Expand Down Expand Up @@ -598,48 +599,47 @@ def get_block_hash(

def simulate_transactions(
self,
txns: "Iterable[transaction.GenericSignedTransaction]",
request: models.SimulateRequest,
**kwargs: Any,
) -> AlgodResponseType:
"""
Simulate a list of a signed transaction objects being sent to the network.
Simulate transactions being sent to the network.
Args:
txns (SignedTransaction[] or MultisigTransaction[]):
transactions to send
request_header (dict, optional): additional header for request
request (models.SimulateRequest): Simulation request object
headers (dict, optional): additional header for request
Returns:
Dict[str, Any]: results from simulation of transaction group
Dict[str, Any]: results from simulation of transactions
"""
serialized = []
for txn in txns:
serialized.append(base64.b64decode(encoding.msgpack_encode(txn)))

return self.simulate_raw_transaction(
base64.b64encode(b"".join(serialized)), **kwargs
body = base64.b64decode(encoding.msgpack_encode(request))
req = "/transactions/simulate"
headers = util.build_headers_from(
kwargs.get("headers", False),
{"Content-Type": "application/msgpack"},
)
kwargs["headers"] = headers
return self.algod_request("POST", req, data=body, **kwargs)

def simulate_raw_transaction(self, txn, **kwargs):
def simulate_raw_transactions(
self, txns: "Sequence[transaction.GenericSignedTransaction]", **kwargs
):
"""
Simulate a transaction group
Simulate a transaction group being sent to the network.
Args:
txn (str): transaction to send, encoded in base64
request_header (dict, optional): additional header for request
txns (Sequence[transaction.GenericSignedTransaction]): transaction group to simulate
headers (dict, optional): additional header for request
Returns:
Dict[str, Any]: results from simulation of transaction group
Dict[str, Any]: results from simulation of transactions
"""
txn = base64.b64decode(txn)
req = "/transactions/simulate"
headers = util.build_headers_from(
kwargs.get("headers", False),
{"Content-Type": "application/x-binary"},
request = models.SimulateRequest(
txn_groups=[
models.SimulateRequestTransactionGroup(txns=list(txns))
]
)
kwargs["headers"] = headers

return self.algod_request("POST", req, data=txn, **kwargs)
return self.simulate_transactions(request, **kwargs)


def _specify_round_string(
Expand Down
7 changes: 7 additions & 0 deletions algosdk/v2client/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@
from algosdk.v2client.models.dryrun_source import DryrunSource
from algosdk.v2client.models.teal_key_value import TealKeyValue
from algosdk.v2client.models.teal_value import TealValue
from algosdk.v2client.models.simulate_request import (
SimulateRequest,
SimulateRequestTransactionGroup,
)

__all__ = [
"Account",
"AccountParticipation",
"Application",
"ApplicationLocalState",
"ApplicationParams",
"ApplicationStateSchema",
Expand All @@ -45,4 +50,6 @@
"DryrunSource",
"TealKeyValue",
"TealValue",
"SimulateRequest",
"SimulateRequestTransactionGroup",
]
32 changes: 32 additions & 0 deletions algosdk/v2client/models/simulate_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List, Dict, Any, TYPE_CHECKING

if TYPE_CHECKING:
from algosdk import transaction


class SimulateRequestTransactionGroup(object):
txns: "List[transaction.GenericSignedTransaction]"

def __init__(
self, *, txns: "List[transaction.GenericSignedTransaction]"
) -> None:
self.txns = txns

def dictify(self) -> Dict[str, Any]:
return {"txns": [txn.dictify() for txn in self.txns]}


class SimulateRequest(object):
txn_groups: List[SimulateRequestTransactionGroup]

def __init__(
self, *, txn_groups: List[SimulateRequestTransactionGroup]
) -> None:
self.txn_groups = txn_groups

def dictify(self) -> Dict[str, Any]:
return {
"txn-groups": [
txn_group.dictify() for txn_group in self.txn_groups
]
}
2 changes: 1 addition & 1 deletion tests/steps/other_v2_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ def get_block_hash(context, round):

@when("I simulate the transaction")
def simulate_transaction(context):
context.simulate_response = context.app_acl.simulate_transactions(
context.simulate_response = context.app_acl.simulate_raw_transactions(
[context.stx]
)

Expand Down

0 comments on commit c1120d5

Please sign in to comment.