Skip to content

Commit

Permalink
fix: non zero gas limit in options breaks transfer function
Browse files Browse the repository at this point in the history
  • Loading branch information
comilao authored and petarTxFusion committed Apr 24, 2024
1 parent 41c1262 commit d739a60
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
67 changes: 67 additions & 0 deletions tests/integration/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,34 @@ def test_transfer_eth(self):

self.assertEqual(balance_after_transfer - balance_before_transfer, amount)

def test_transfer_eth_with_non_zero_gas_limit(self):
gas_limit = 500_000
options = TransactionOptions(gas_limit=gas_limit)
amount = 7_000_000_000
balance_before_transfer = self.zksync.zksync.get_balance(
Web3.to_checksum_address(self.address2)
)
tx_hash = self.wallet.transfer(
TransferTransaction(
to=Web3.to_checksum_address(self.address2),
token_address=ADDRESS_DEFAULT,
amount=amount,
options=options,
)
)

receipt = self.zksync.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)
self.assertNotEqual(receipt["gasUsed"], gas_limit)
tx = self.zksync.eth.get_transaction(tx_hash)
self.assertEqual(tx["gas"], gas_limit)
balance_after_transfer = self.zksync.zksync.get_balance(
Web3.to_checksum_address(self.address2)
)

self.assertEqual(balance_after_transfer - balance_before_transfer, amount)

def test_transfer_eth_paymaster(self):
amount = 1
paymaster_address = self.zksync.to_checksum_address(self.paymaster_address)
Expand Down Expand Up @@ -405,6 +433,45 @@ def test_transfer_token(self):
self.assertEqual(amount, sender_before - sender_after)
self.assertEqual(amount, balance_after - balance_before)

def test_transfer_token_with_non_zero_gas_limit(self):
gas_limit = 500_000
options = TransactionOptions(gas_limit=gas_limit)
amount = 5
_, l2_address = self.load_token()

sender_before = self.wallet.get_balance(token_address=l2_address)
balance_before = self.zksync.zksync.zks_get_balance(
self.address2,
token_address=l2_address,
block_tag=ZkBlockParams.LATEST.value,
)
tx_hash = self.wallet.transfer(
TransferTransaction(
to=Web3.to_checksum_address(self.address2),
token_address=Web3.to_checksum_address(l2_address),
amount=amount,
options=options,
)
)

result = self.zksync.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)
self.assertIsNotNone(result)
self.assertNotEqual(result["gasUsed"], gas_limit)
tx = self.zksync.eth.get_transaction(tx_hash)
self.assertEqual(tx["gas"], gas_limit)
sender_after = self.wallet.get_balance(token_address=l2_address)

balance_after = self.zksync.zksync.zks_get_balance(
self.address2,
token_address=l2_address,
block_tag=ZkBlockParams.LATEST.value,
)

self.assertEqual(amount, sender_before - sender_after)
self.assertEqual(amount, balance_after - balance_before)

def test_transfer_token_paymaster(self):
amount = 5
l1_address, l2_address = self.load_token()
Expand Down
10 changes: 6 additions & 4 deletions zksync2/account/wallet_l2.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def transfer(self, tx: TransferTransaction) -> HexStr:
tx_fun_call = self._zksync_web3.zksync.get_transfer_transaction(
tx, self._l1_account.address
)
if tx.options.gas_limit == 0:
tx_712 = tx_fun_call.tx712(
self._zksync_web3.zksync.zks_estimate_gas_transfer(tx_fun_call.tx)
)
estimated_gas = self._zksync_web3.zksync.zks_estimate_gas_transfer(
tx_fun_call.tx
)
tx_712 = tx_fun_call.tx712(
estimated_gas if tx.options.gas_limit == 0 else tx.options.gas_limit
)
signer = PrivateKeyEthSigner(self._l1_account, tx.options.chain_id)
signed_message = signer.sign_typed_data(tx_712.to_eip712_struct())

Expand Down

0 comments on commit d739a60

Please sign in to comment.