Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/paymaster transfer withdraw #58

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions scripts/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Deposits token to l2 so that tests can run
# Creates paymaster and crown token
import json
import os
import sys
Expand All @@ -13,23 +14,29 @@ def main():
current_directory = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.join(current_directory, "..")
sys.path.append(parent_directory)
SALT = "0x293328ad84b118194c65a0dc0defdb6483740d3163fd99b260907e15f2e2f642"

from zksync2.account.wallet import Wallet
from zksync2.manage_contracts.utils import zksync_abi_default
from zksync2.module.module_builder import ZkSyncBuilder
from zksync2.signer.eth_signer import PrivateKeyEthSigner

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"
)

chain_id = zksync.zksync.chain_id
signer = PrivateKeyEthSigner(account, chain_id)
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(),
)

deposit_token(wallet, eth_web3, zksync, zksync_contract)
setup_paymaster(eth_web3, zksync, wallet, signer, SALT)


def deposit_token(wallet, eth_web3: Web3, zksync: Web3, zksync_contract):
Expand All @@ -56,6 +63,141 @@ def deposit_token(wallet, eth_web3: Web3, zksync: Web3, zksync_contract):
)


def setup_paymaster(provider_l1, provider_l2, wallet, signer, salt):
from zksync2.core.types import EthBlockParams, TransferTransaction
from zksync2.manage_contracts.contract_encoder_base import (
JsonConfiguration,
ContractEncoder,
)
from zksync2.manage_contracts.deploy_addresses import ZkSyncAddresses

directory = Path(__file__).parent.parent
path = directory / "tests/contracts/Token.json"

token_contract = ContractEncoder.from_json(
provider_l2, path.resolve(), JsonConfiguration.STANDARD
)
abi = token_contract.abi

token_address = deploy_crown_token(
provider_l2, wallet, signer, salt, token_contract
)
token_contract = provider_l2.zksync.contract(token_address, abi=abi)

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

signed = wallet.sign_transaction(mint_tx)
tx_hash = provider_l2.eth.send_raw_transaction(signed.rawTransaction)
provider_l2.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)

paymaster_address = deploy_paymaster(
provider_l2, wallet, token_address, signer, salt
)
faucet_hash = wallet.transfer(
TransferTransaction(
to=paymaster_address,
amount=provider_l2.to_wei(1, "ether"),
token_address=ZkSyncAddresses.ETH_ADDRESS.value,
)
)

provider_l2.zksync.wait_for_transaction_receipt(
faucet_hash, timeout=240, poll_latency=0.5
)


def deploy_crown_token(provider_l2, wallet, signer, salt, token_contract):
from zksync2.core.types import EthBlockParams
from zksync2.core.utils import to_bytes
from zksync2.transaction.transaction_builders import TxCreate2Contract

constructor_arguments = {"name_": "Ducat", "symbol_": "Ducat", "decimals_": 18}
chain_id = provider_l2.zksync.chain_id
nonce = provider_l2.zksync.get_transaction_count(
wallet.address, EthBlockParams.PENDING.value
)
encoded_constructor = token_contract.encode_constructor(**constructor_arguments)

gas_price = provider_l2.zksync.gas_price
create2_contract = TxCreate2Contract(
web3=provider_l2,
chain_id=chain_id,
nonce=nonce,
from_=wallet.address,
gas_limit=0,
gas_price=gas_price,
bytecode=token_contract.bytecode,
salt=to_bytes(salt),
call_data=encoded_constructor,
)
estimate_gas = provider_l2.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())
msg = tx_712.encode(signed_message)
tx_hash = provider_l2.zksync.send_raw_transaction(msg)
tx_receipt = provider_l2.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)

return tx_receipt["contractAddress"]


def deploy_paymaster(provider_l2: Web3, wallet, token_address, signer, salt):
from zksync2.core.types import EthBlockParams
from zksync2.core.utils import to_bytes
from zksync2.manage_contracts.contract_encoder_base import (
JsonConfiguration,
ContractEncoder,
)
from zksync2.transaction.transaction_builders import TxCreate2Contract

directory = Path(__file__).parent.parent
path = directory / "tests/contracts/Paymaster.json"
token_address = provider_l2.to_checksum_address(token_address)
constructor_arguments = {"_erc20": token_address}

chain_id = provider_l2.zksync.chain_id
nonce = provider_l2.zksync.get_transaction_count(
wallet.address, EthBlockParams.PENDING.value
)
token_contract = ContractEncoder.from_json(
provider_l2, path.resolve(), JsonConfiguration.STANDARD
)
encoded_constructor = token_contract.encode_constructor(**constructor_arguments)
gas_price = provider_l2.zksync.gas_price
create_account = TxCreate2Contract(
web3=provider_l2,
chain_id=chain_id,
gas_limit=0,
nonce=nonce,
from_=wallet.address,
gas_price=gas_price,
bytecode=token_contract.bytecode,
call_data=encoded_constructor,
salt=to_bytes(salt),
)
estimate_gas = provider_l2.zksync.eth_estimate_gas(create_account.tx)
tx_712 = create_account.tx712(estimate_gas)
signed_message = signer.sign_typed_data(tx_712.to_eip712_struct())
msg = tx_712.encode(signed_message)
tx_hash = provider_l2.zksync.send_raw_transaction(msg)
tx_receipt = provider_l2.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)
return tx_receipt["contractAddress"]


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