Skip to content

Commit

Permalink
chore: lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Jan 19, 2024
1 parent 3e081fb commit 4e4fd56
Show file tree
Hide file tree
Showing 31 changed files with 2,283 additions and 1,417 deletions.
34 changes: 21 additions & 13 deletions scripts/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Deposits token to l2 so that tests can run
# Deposits token to l2 so that tests can run
import json
from pathlib import Path

Expand All @@ -15,40 +15,48 @@
def main():
zksync = ZkSyncBuilder.build("http://127.0.0.1:3050")
eth_web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
account: LocalAccount = Account.from_key("0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110")
account: LocalAccount = Account.from_key(
"0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110"
)
wallet = Wallet(zksync, eth_web3, account)
zksync_contract = eth_web3.eth.contract(
Web3.to_checksum_address(zksync.zksync.main_contract_address),
abi=zksync_abi_default())
abi=zksync_abi_default(),
)

deposit_token(wallet, eth_web3, zksync, zksync_contract)


def deposit_token(wallet: Wallet, eth_web3: Web3, zksync: Web3, zksync_contract):
amount = 50
l1_address = load_token()

tx_hash = wallet.deposit(DepositTransaction(Web3.to_checksum_address(l1_address),
amount,
wallet.address,
approve_erc20=True,
refund_recipient=wallet.address))
tx_hash = wallet.deposit(
DepositTransaction(
Web3.to_checksum_address(l1_address),
amount,
wallet.address,
approve_erc20=True,
refund_recipient=wallet.address,
)
)

l1_tx_receipt = eth_web3.eth.wait_for_transaction_receipt(tx_hash)

l2_hash = zksync.zksync.get_l2_hash_from_priority_op(l1_tx_receipt, zksync_contract)
zksync.zksync.wait_for_transaction_receipt(transaction_hash=l2_hash,
timeout=360,
poll_latency=10)
zksync.zksync.wait_for_transaction_receipt(
transaction_hash=l2_hash, timeout=360, poll_latency=10
)


def load_token():
directory = Path(__file__).parent.parent
path = directory / "tests/integration/token.json"

with open(path, 'r') as file:
with open(path, "r") as file:
data = json.load(file)
return data[0]["address"]


if __name__ == "__main__":
main()
main()
4 changes: 2 additions & 2 deletions scripts/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def wait_for_node():
raise Exception("Maximum retries exceeded.")


if __name__ == '__main__':
if __name__ == "__main__":
current_directory = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.join(current_directory, '..')
parent_directory = os.path.join(current_directory, "..")
sys.path.append(parent_directory)
try:
wait_for_node()
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ class TestEnvironment:
eth_server: str


LOCAL_ENV = TestEnvironment(EnvType.LOCAL_HOST, "http://127.0.0.1:3050", "http://127.0.0.1:8545")
LOCAL_ENV = TestEnvironment(
EnvType.LOCAL_HOST, "http://127.0.0.1:3050", "http://127.0.0.1:8545"
)
123 changes: 81 additions & 42 deletions tests/integration/test_paymaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,59 @@

from tests.integration.test_config import LOCAL_ENV
from tests.integration.test_zksync_contract import generate_random_salt
from zksync2.manage_contracts.contract_encoder_base import ContractEncoder, JsonConfiguration
from zksync2.manage_contracts.contract_encoder_base import (
ContractEncoder,
JsonConfiguration,
)
from zksync2.manage_contracts.deploy_addresses import ZkSyncAddresses
from zksync2.manage_contracts.paymaster_utils import PaymasterFlowEncoder
from zksync2.manage_contracts.precompute_contract_deployer import PrecomputeContractDeployer
from zksync2.manage_contracts.precompute_contract_deployer import (
PrecomputeContractDeployer,
)
from zksync2.manage_contracts.utils import nonce_holder_abi_default
from zksync2.module.module_builder import ZkSyncBuilder
from zksync2.core.types import Token, EthBlockParams, ZkBlockParams, PaymasterParams
from eth_account import Account
from eth_account.signers.local import LocalAccount
from zksync2.signer.eth_signer import PrivateKeyEthSigner
from tests.contracts.utils import contract_path
from zksync2.transaction.transaction_builders import TxFunctionCall, TxCreate2Contract, TxCreateAccount
from zksync2.transaction.transaction_builders import (
TxFunctionCall,
TxCreate2Contract,
TxCreateAccount,
)


# mint tx hash of Test coins:
# https://goerli.explorer.zksync.io/address/0xFC174650BDEbE4D94736442307D4D7fdBe799EeC#contract

class PaymasterTests(TestCase):

class PaymasterTests(TestCase):
ETH_TOKEN = Token.create_eth()
PRIVATE_KEY = bytes.fromhex("1f0245d47b3a84299aeb121ac33c2dbd1cdb3d3c2079b3240e63796e75ee8b70")
PRIVATE_KEY = bytes.fromhex(
"1f0245d47b3a84299aeb121ac33c2dbd1cdb3d3c2079b3240e63796e75ee8b70"
)
ETH_AMOUNT_BALANCE = 1
ETH_TEST_NET_AMOUNT_BALANCE = Decimal(1)

SERC20_TOKEN = Token(
Web3.to_checksum_address("0x" + "0" * 40),
Web3.to_checksum_address("0xFC174650BDEbE4D94736442307D4D7fdBe799EeC"),
"SERC20",
18)
18,
)

SALT = keccak(text="TestPaymaster")

def setUp(self) -> None:
self.env = LOCAL_ENV
self.web3 = ZkSyncBuilder.build(self.env.zksync_server)
self.account: LocalAccount = Account.from_key("0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110")
self.account: LocalAccount = Account.from_key(
"0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110"
)
self.chain_id = self.web3.zksync.chain_id
self.signer = PrivateKeyEthSigner(self.account, self.chain_id)
#self.custom_paymaster = ContractEncoder.from_json(self.web3, contract_path("CustomPaymaster.json"))
# self.custom_paymaster = ContractEncoder.from_json(self.web3, contract_path("CustomPaymaster.json"))

def _is_deployed(self):
return len(self.web3.zksync.get_code(self.paymaster_address)) > 0
Expand All @@ -64,15 +78,20 @@ def deploy_paymaster(self, token_address):
nonce = self.web3.zksync.get_transaction_count(
self.account.address, EthBlockParams.PENDING.value
)
nonce_holder = self.web3.zksync.contract(address=ZkSyncAddresses.NONCE_HOLDER_ADDRESS.value,
abi=nonce_holder_abi_default())
deployment_nonce = nonce_holder.functions.getDeploymentNonce(self.account.address).call(
{
"from": self.account.address
})
nonce_holder = self.web3.zksync.contract(
address=ZkSyncAddresses.NONCE_HOLDER_ADDRESS.value,
abi=nonce_holder_abi_default(),
)
deployment_nonce = nonce_holder.functions.getDeploymentNonce(
self.account.address
).call({"from": self.account.address})
deployer = PrecomputeContractDeployer(self.web3)
precomputed_address = deployer.compute_l2_create_address(self.account.address, deployment_nonce)
token_contract = ContractEncoder.from_json(self.web3, path.resolve(), JsonConfiguration.STANDARD)
precomputed_address = deployer.compute_l2_create_address(
self.account.address, deployment_nonce
)
token_contract = ContractEncoder.from_json(
self.web3, path.resolve(), JsonConfiguration.STANDARD
)
encoded_constructor = token_contract.encode_constructor(**constructor_arguments)
gas_price = self.web3.zksync.gas_price
create_account = TxCreateAccount(
Expand Down Expand Up @@ -104,20 +123,24 @@ def test_paymaster(self):
self.account.address, EthBlockParams.PENDING.value
)
random_salt = generate_random_salt()
token_contract = ContractEncoder.from_json(self.web3, path.resolve(), JsonConfiguration.STANDARD)
token_contract = ContractEncoder.from_json(
self.web3, path.resolve(), JsonConfiguration.STANDARD
)
abi = token_contract.abi
encoded_constructor = token_contract.encode_constructor(**constructor_arguments)

gas_price = self.web3.zksync.gas_price
create2_contract = TxCreate2Contract(web3=self.web3,
chain_id=chain_id,
nonce=nonce,
from_=self.account.address,
gas_limit=0,
gas_price=gas_price,
bytecode=token_contract.bytecode,
salt=random_salt,
call_data=encoded_constructor)
create2_contract = TxCreate2Contract(
web3=self.web3,
chain_id=chain_id,
nonce=nonce,
from_=self.account.address,
gas_limit=0,
gas_price=gas_price,
bytecode=token_contract.bytecode,
salt=random_salt,
call_data=encoded_constructor,
)
estimate_gas = self.web3.zksync.eth_estimate_gas(create2_contract.tx)
tx_712 = create2_contract.tx712(estimate_gas)
signed_message = signer.sign_typed_data(tx_712.to_eip712_struct())
Expand All @@ -129,12 +152,18 @@ def test_paymaster(self):
token_contract_address = tx_receipt["contractAddress"]
token_contract = self.web3.zksync.contract(token_contract_address, abi=abi)

mint_tx = token_contract.functions.mint(self.account.address, 15).build_transaction({
"nonce": self.web3.zksync.get_transaction_count(self.account.address, EthBlockParams.LATEST.value),
"from": self.account.address,
"maxPriorityFeePerGas": 1_000_000,
"maxFeePerGas": self.web3.zksync.gas_price,
})
mint_tx = token_contract.functions.mint(
self.account.address, 15
).build_transaction(
{
"nonce": self.web3.zksync.get_transaction_count(
self.account.address, EthBlockParams.LATEST.value
),
"from": self.account.address,
"maxPriorityFeePerGas": 1_000_000,
"maxFeePerGas": self.web3.zksync.gas_price,
}
)

signed = self.account.sign_transaction(mint_tx)
tx_hash = self.web3.zksync.send_raw_transaction(signed.rawTransaction)
Expand All @@ -146,7 +175,9 @@ def test_paymaster(self):

tx_func_call = TxFunctionCall(
chain_id=self.web3.zksync.chain_id,
nonce=self.web3.zksync.get_transaction_count(self.account.address, EthBlockParams.LATEST.value),
nonce=self.web3.zksync.get_transaction_count(
self.account.address, EthBlockParams.LATEST.value
),
from_=self.account.address,
to=paymaster_address,
value=self.web3.to_wei(1, "ether"),
Expand All @@ -164,24 +195,33 @@ def test_paymaster(self):
tx_hash, timeout=240, poll_latency=0.5
)

calladata = token_contract.encodeABI(fn_name="mint", args=[self.account.address, 7])
calladata = token_contract.encodeABI(
fn_name="mint", args=[self.account.address, 7]
)

paymaster_params = PaymasterParams(**{
"paymaster": paymaster_address,
"paymaster_input": self.web3.to_bytes(
hexstr=PaymasterFlowEncoder(self.web3).encode_approval_based(token_contract_address, 1, b''))
})
paymaster_params = PaymasterParams(
**{
"paymaster": paymaster_address,
"paymaster_input": self.web3.to_bytes(
hexstr=PaymasterFlowEncoder(self.web3).encode_approval_based(
token_contract_address, 1, b""
)
),
}
)

tx_func_call = TxFunctionCall(
chain_id=self.web3.zksync.chain_id,
nonce=self.web3.zksync.get_transaction_count(self.account.address, EthBlockParams.LATEST.value),
nonce=self.web3.zksync.get_transaction_count(
self.account.address, EthBlockParams.LATEST.value
),
from_=self.account.address,
to=token_contract_address,
data=calladata,
gas_limit=0, # Unknown at this state, estimation is done in next step
gas_price=gas_price,
max_priority_fee_per_gas=100_000_000,
paymaster_params=paymaster_params
paymaster_params=paymaster_params,
)
estimate_gas = self.web3.zksync.eth_estimate_gas(tx_func_call.tx)
tx_712 = tx_func_call.tx712(estimate_gas)
Expand All @@ -192,4 +232,3 @@ def test_paymaster(self):
self.web3.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)

Loading

0 comments on commit 4e4fd56

Please sign in to comment.