Skip to content

Commit

Permalink
feat(provider): add zks_logProof
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Feb 18, 2024
1 parent b3737a6 commit 1f3442b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
56 changes: 55 additions & 1 deletion tests/integration/test_zksync_web3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexStr
from eth_utils import keccak
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.types import TxParams

from tests.contracts.utils import contract_path
from zksync2.core.types import Token, ZkBlockParams, EthBlockParams, ADDRESS_DEFAULT
from zksync2.core.types import (
Token,
ZkBlockParams,
EthBlockParams,
ADDRESS_DEFAULT,
StorageProof,
)
from zksync2.core.utils import pad_front_bytes, to_bytes, pad_back_bytes
from zksync2.manage_contracts.contract_encoder_base import (
ContractEncoder,
JsonConfiguration,
Expand All @@ -23,6 +31,7 @@
)
from zksync2.manage_contracts.utils import nonce_holder_abi_default
from zksync2.module.module_builder import ZkSyncBuilder
from zksync2.module.request_types import Transaction, EIP712Meta
from zksync2.signer.eth_signer import PrivateKeyEthSigner
from zksync2.transaction.transaction_builders import (
TxFunctionCall,
Expand Down Expand Up @@ -84,6 +93,51 @@ def test_send_money(self):
txn_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
self.assertEqual(txn_receipt["status"], 1)

def test_zks_l1_batch_number(self):
result = self.web3.zksync.zks_l1_batch_number()
self.assertGreater(result, 0)

def test_zks_get_l1_batch_block_range(self):
l1_batch_number = self.web3.zksync.zks_l1_batch_number()
result = self.web3.zksync.zks_get_l1_batch_block_range(l1_batch_number)
self.assertIsNotNone(result)

@skip
def test_zks_get_l1_batch_details(self):
l1_batch_number = self.web3.zksync.zks_l1_batch_number()
result = self.web3.zksync.zks_get_l1_batch_details(l1_batch_number)
self.assertIsNotNone(result)

def test_zks_estimate_gas_l1_to_l2(self):
meta = EIP712Meta(gas_per_pub_data=800)
result = self.web3.zksync.zks_estimate_gas_l1_to_l2(
{
"from": self.account.address,
"to": self.web3.zksync.zks_main_contract(),
"value": 7_000_000_000,
"eip712Meta": meta,
}
)
self.assertIsNotNone(result)

def test_zks_get_proof(self):
address_padded = pad_front_bytes(to_bytes(self.account.address), 32)

concatenated = pad_back_bytes(address_padded, 64)

storage_key = keccak(concatenated).hex()

l1_batch_number = self.web3.zksync.zks_l1_batch_number()
try:
result: StorageProof = self.web3.zksync.zks_get_proof(
ZkSyncAddresses.NONCE_HOLDER_ADDRESS.value,
[storage_key],
l1_batch_number,
)
self.assertIsNotNone(result)
except:
pass

# @skip("Integration test, used for develop purposes only")
def test_get_l1_balance(self):
eth_web3 = Web3(Web3.HTTPProvider(self.env.eth_server))
Expand Down
5 changes: 5 additions & 0 deletions zksync2/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def apply_l1_to_l2_alias(address: HexStr):
return add_0x_prefix(result)


def pad_back_bytes(bs: bytes, needed_length: int):
padded = bs + b"\0" * (needed_length - len(bs))
return padded


def undo_l1_to_l2_alias(address: HexStr):
result = int(address, 16) - int(L1_TO_L2_ALIAS_OFFSET, 16)
if result < 0:
Expand Down
25 changes: 15 additions & 10 deletions zksync2/module/zksync_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
TransactionOptions,
WithdrawTransaction,
ContractAccountInfo,
StorageProof,
)
from zksync2.core.utils import is_eth, MAX_PRIORITY_FEE_PER_GAS
from zksync2.manage_contracts.deploy_addresses import ZkSyncAddresses
Expand Down Expand Up @@ -89,6 +90,7 @@
zks_get_bridge_contracts_rpc = RPCEndpoint("zks_getBridgeContracts")
zks_get_l2_to_l1_msg_proof_prc = RPCEndpoint("zks_getL2ToL1MsgProof")
zks_get_l2_to_l1_log_proof_prc = RPCEndpoint("zks_getL2ToL1LogProof")
zks_get_proof_rpc = RPCEndpoint("zks_getProof")
eth_estimate_gas_rpc = RPCEndpoint("eth_estimateGas")
eth_get_transaction_receipt_rpc = RPCEndpoint("eth_getTransactionReceipt")
eth_get_transaction_by_hash_rpc = RPCEndpoint("eth_getTransactionByHash")
Expand Down Expand Up @@ -367,6 +369,13 @@ class ZkSync(Eth, ABC):
request_formatters=zksync_get_request_formatters,
result_formatters=zksync_get_result_formatters,
)
_zks_get_proof: Method[
Callable[[HexStr, List[HexStr], int], StorageProof]
] = Method(
zks_get_proof_rpc,
mungers=[default_root_munger],
request_formatters=zksync_get_request_formatters,
)
_zks_estimate_gas_l1_to_l2: Method[Callable[[Transaction], int]] = Method(
zks_estimate_gas_l1_to_l2_rpc,
mungers=[default_root_munger],
Expand Down Expand Up @@ -462,7 +471,7 @@ def __init__(self, web3: "Web3"):
self.bridge_addresses = None

def zks_l1_batch_number(self) -> int:
return self._zks_l1_batch_number()
return int(self._zks_l1_batch_number(), 16)

def zks_get_l1_batch_block_range(self, l1_batch_number: int) -> BlockRange:
return self._zks_get_l1_batch_block_range(l1_batch_number)
Expand All @@ -479,6 +488,9 @@ def zks_get_transaction_details(self, txHash: str) -> TransactionDetails:
def zks_estimate_gas_l1_to_l2(self, transaction: Transaction) -> int:
return int(self._zks_estimate_gas_l1_to_l2(transaction), 16)

def zks_get_proof(self, address: HexStr, key: List[HexStr], l1_batch_number: int):
return self._zks_get_proof(address, key, l1_batch_number)

def zks_estimate_gas_transfer(
self, transaction: Transaction, token_address: HexStr = ADDRESS_DEFAULT
) -> int:
Expand Down Expand Up @@ -718,14 +730,7 @@ def get_transfer_transaction(
Web3.to_checksum_address(tx.token_address), abi=get_erc20_abi()
)
call_data = contract.encodeABI("transfer", transfer_params)
paymaster_params = None
if tx.paymaster_params is not None:
paymaster_params = PaymasterParams(
**{
"paymaster": tx.paymaster_params.paymaster,
"paymaster_input": tx.paymaster_params.paymaster_input,
}
)

transaction = TxTransfer(
web3=self,
token=tx.token_address,
Expand All @@ -739,7 +744,7 @@ def get_transfer_transaction(
gas_price=tx.options.gas_price,
max_priority_fee_per_gas=tx.options.max_priority_fee_per_gas,
gas_per_pub_data=tx.gas_per_pub_data,
paymaster_params=paymaster_params,
paymaster_params=tx.paymaster_params,
)

return transaction
Expand Down

0 comments on commit 1f3442b

Please sign in to comment.