Skip to content

Commit fbd7788

Browse files
committed
Add withdrawals collector script
1 parent 48623bb commit fbd7788

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Run Collect Withdrawals Script
2+
3+
on:
4+
schedule:
5+
# Every 15 minutes (UTC)
6+
- cron: "*/15 * * * *"
7+
workflow_dispatch: # allow manual trigger from Actions tab
8+
9+
jobs:
10+
run-script:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: "3.11"
21+
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install -r requirements.txt
26+
27+
- name: Run Collect Withdrawals Script
28+
env:
29+
PK: ${{ secrets.MN_COLLECTOR_PK }}
30+
INFURA_KEY: ${{ secrets.INFURA_KEY }}
31+
run: python scripts/ticker.py
32+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ requests
77
pathlib
88
pytest-xdist
99
git+https://github.com/starkware-libs/starkware-starknet-utils.git@main#egg=test_utils&subdirectory=python
10+
web3==5.31.3

scripts/tikcer.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3.9
2+
3+
import os
4+
import argparse
5+
from web3 import Web3, Account, HTTPProvider, eth
6+
from web3.middleware import (
7+
buffered_gas_estimate_middleware,
8+
construct_sign_and_send_raw_middleware,
9+
)
10+
11+
node_uri_pat = "https://{chain}.infura.io/v3/{infura_key}"
12+
batcher_abi = [{
13+
'inputs': [],
14+
'name': 'tick',
15+
'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}],
16+
'stateMutability': 'nonpayable',
17+
'type': 'function'
18+
}]
19+
20+
default_batcher_address="0x613d088F2e5a2ED91635016483dAFa3cd47a8964"
21+
22+
def new_wallet_account(w3: Web3, eth_private_key):
23+
return new_wallet_account_list(w3, [eth_private_key])[0]
24+
25+
26+
def new_wallet_account_list(w3: Web3, eth_private_keys, name=None):
27+
"""
28+
Creates new eth accounts and registers them (including the private keys).
29+
The new accounts may have no funds.
30+
This function is more efficient than calling new_wallet_account() many times
31+
as it creates only one middleware layer.
32+
"""
33+
accounts = [Account.from_key(eth_private_key) for eth_private_key in eth_private_keys]
34+
35+
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(accounts), name=name)
36+
w3.middleware_onion.remove("gas_estimate")
37+
w3.middleware_onion.add(buffered_gas_estimate_middleware, name="gas_estimate")
38+
return [account.address for account in accounts]
39+
40+
41+
42+
def parse_args():
43+
parser = argparse.ArgumentParser(
44+
description="Collect pending withdrawals waiting for l1_recipient"
45+
)
46+
parser.add_argument(
47+
"--chain",
48+
type=str,
49+
choices=["mainnet", "sepolia"],
50+
default="mainnet",
51+
help="Select Ethereum chain (default: mainnet)",
52+
)
53+
parser.add_argument("--batcher_contract", type=str, default=default_batcher_address, help="L1 withdrawal batching contract")
54+
args = parser.parse_args()
55+
args.batcher_contract = Web3.toChecksumAddress(args.batcher_contract)
56+
return args
57+
58+
def main():
59+
args = parse_args()
60+
account = Account.from_key(os.environ["PK"])
61+
node_uri = node_uri_pat.format(chain=args.chain, infura_key=os.environ["INFURA_KEY"])
62+
w3 = Web3(HTTPProvider(node_uri))
63+
64+
assert(w3.isConnected())
65+
w3.eth.default_account = new_wallet_account(w3, account.key)
66+
batcher = w3.eth.contract(address=args.batcher_contract, abi=batcher_abi)
67+
print(w3.eth.default_account, batcher.address)
68+
69+
pending_funds = batcher.functions.tick().call()
70+
if pending_funds:
71+
print(f"Awaiting {pending_funds//10**6} withdrawal waiting.")
72+
th = batcher.functions.tick().transact()
73+
print(f"Pending withdrawal collected, tx hash: {th.hex()}")
74+
else:
75+
print("nothing to tick")
76+
77+
if __name__ == '__main__':
78+
main()

0 commit comments

Comments
 (0)