Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Set default max_fee in account functions (#262)
Browse files Browse the repository at this point in the history
* Set default max_fee in account functions

* Test with mocks for account.send

* Merge with main
  • Loading branch information
ericglau authored Nov 3, 2022
1 parent e0669c7 commit c2152a9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/nile/core/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ def deploy_contract(
)

def send(
self, address_or_alias, method, calldata, max_fee, nonce=None, query_type=None
self,
address_or_alias,
method,
calldata,
max_fee=None,
nonce=None,
query_type=None,
):
"""Execute a query or invoke call for a tx going through an Account contract."""
# get target address with the right format
Expand Down Expand Up @@ -166,11 +172,13 @@ def send(
query_flag=query_type,
)

def simulate(self, address_or_alias, method, calldata, max_fee, nonce=None):
def simulate(self, address_or_alias, method, calldata, max_fee=None, nonce=None):
"""Simulate a tx going through an Account contract."""
return self.send(address_or_alias, method, calldata, max_fee, nonce, "simulate")

def estimate_fee(self, address_or_alias, method, calldata, max_fee, nonce=None):
def estimate_fee(
self, address_or_alias, method, calldata, max_fee=None, nonce=None
):
"""Estimate fee for a tx going through an Account contract."""
return self.send(
address_or_alias, method, calldata, max_fee, nonce, "estimate_fee"
Expand Down
47 changes: 43 additions & 4 deletions tests/commands/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,55 @@ def test_send_sign_transaction_and_execute(mock_target_address, mock_deploy):
)


@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
@patch(
"nile.core.account.Account._get_target_address", return_value=MOCK_TARGET_ADDRESS
)
@patch("nile.core.account.get_nonce", return_value=0)
@patch("nile.core.account.call_or_invoke")
def test_send_defaults(mock_call, mock_nonce, mock_target_address, mock_deploy):
account = Account(KEY, NETWORK)

send_args = [MOCK_TARGET_ADDRESS, "method", [1, 2, 3]]
calldata = ["111", "222", "333"]
sig_r, sig_s = [999, 888]
return_signature = [calldata, sig_r, sig_s]

# Mock sign_transaction
account.signer.sign_transaction = MagicMock(return_value=return_signature)

account.send(account.address, "method", [1, 2, 3])

account.signer.sign_transaction.assert_called_once_with(
calls=[send_args],
nonce=0,
sender=account.address,
max_fee=0,
version=TRANSACTION_VERSION,
)

mock_call.assert_called_with(
contract=account,
max_fee=str(0),
method="__execute__",
network=NETWORK,
params=calldata,
signature=[str(sig_r), str(sig_s)],
type="invoke",
query_flag=None,
)


@patch("nile.core.account.deploy", return_value=(MOCK_ADDRESS, MOCK_INDEX))
def test_estimate_fee(mock_deploy):
account = Account(KEY, NETWORK)
# Mock send
account.send = MagicMock()

account.estimate_fee(account.address, "method", [1, 2, 3], max_fee=0)
account.estimate_fee(account.address, "method", [1, 2, 3])

account.send.assert_called_once_with(
account.address, "method", [1, 2, 3], 0, None, "estimate_fee"
account.address, "method", [1, 2, 3], None, None, "estimate_fee"
)


Expand All @@ -198,10 +237,10 @@ def test_simulate(mock_deploy):
# Mock send
account.send = MagicMock()

account.simulate(account.address, "method", [1, 2, 3], max_fee=0)
account.simulate(account.address, "method", [1, 2, 3])

account.send.assert_called_once_with(
account.address, "method", [1, 2, 3], 0, None, "simulate"
account.address, "method", [1, 2, 3], None, None, "simulate"
)


Expand Down

0 comments on commit c2152a9

Please sign in to comment.