-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·284 lines (237 loc) · 8.97 KB
/
Copy pathdeploy.py
File metadata and controls
executable file
·284 lines (237 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""
Oak Protocol Deployment Script for Arbitrum Sepolia Testnet
This script:
1. Compiles the Rust contract to WASM
2. Deploys to Arbitrum Sepolia using cargo-stylus
3. Initializes the contract with owner and treasury addresses
Prerequisites:
- Rust and cargo-stylus installed
- Private key or mnemonic in environment variables
- Sufficient ETH on Arbitrum Sepolia for deployment
"""
import os
import subprocess
import sys
from pathlib import Path
from typing import Optional
# Arbitrum Sepolia RPC endpoint
ARBITRUM_SEPOLIA_RPC = "https://sepolia-rollup.arbitrum.io/rpc"
# Stylus program ID (will be set after deployment)
PROGRAM_ID_ENV = "STYLUS_PROGRAM_ID"
def check_prerequisites() -> bool:
"""Check if required tools are installed."""
print("🔍 Checking prerequisites...")
# Check Rust
try:
subprocess.run(["rustc", "--version"], check=True, capture_output=True)
print("✅ Rust installed")
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ Rust not found. Install from https://rustup.rs/")
return False
# Check cargo-stylus
try:
result = subprocess.run(
["cargo", "stylus", "--version"],
check=True,
capture_output=True,
text=True
)
print(f"✅ cargo-stylus installed: {result.stdout.strip()}")
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ cargo-stylus not found. Install with: cargo install --force cargo-stylus")
return False
# Check wasm32 target
try:
subprocess.run(
["rustup", "target", "list", "--installed"],
check=True,
capture_output=True
)
result = subprocess.run(
["rustup", "target", "list", "--installed"],
check=True,
capture_output=True,
text=True
)
if "wasm32-unknown-unknown" not in result.stdout:
print("⚠️ Installing wasm32-unknown-unknown target...")
subprocess.run(
["rustup", "target", "add", "wasm32-unknown-unknown"],
check=True
)
print("✅ wasm32-unknown-unknown target available")
except subprocess.CalledProcessError:
print("❌ Failed to check/install wasm32 target")
return False
return True
def compile_contract() -> bool:
"""Compile the Rust contract to WASM."""
print("\n🔨 Compiling contract to WASM...")
try:
result = subprocess.run(
["cargo", "build", "--target", "wasm32-unknown-unknown", "--release"],
check=True,
capture_output=True,
text=True
)
print("✅ Contract compiled successfully")
# Check if WASM file exists
wasm_path = Path("target/wasm32-unknown-unknown/release/oak_protocol.wasm")
if wasm_path.exists():
size_kb = wasm_path.stat().st_size / 1024
print(f"📦 WASM size: {size_kb:.2f} KB")
return True
else:
print("⚠️ WASM file not found at expected location")
return False
except subprocess.CalledProcessError as e:
print(f"❌ Compilation failed: {e.stderr}")
return False
def deploy_contract(
private_key: Optional[str] = None,
mnemonic: Optional[str] = None,
rpc_url: str = ARBITRUM_SEPOLIA_RPC
) -> Optional[str]:
"""
Deploy the contract to Arbitrum Sepolia.
Returns the deployed contract address or None on failure.
"""
print("\n🚀 Deploying contract to Arbitrum Sepolia...")
if not private_key and not mnemonic:
print("❌ Either PRIVATE_KEY or MNEMONIC environment variable must be set")
return None
# Build cargo-stylus deploy command
cmd = [
"cargo", "stylus", "deploy",
"--wasm-file", "target/wasm32-unknown-unknown/release/oak_protocol.wasm",
"--network", "sepolia",
"--rpc-url", rpc_url,
]
if private_key:
cmd.extend(["--private-key", private_key])
elif mnemonic:
cmd.extend(["--mnemonic", mnemonic])
try:
print("⏳ Deployment in progress (this may take a few minutes)...")
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True
)
# Parse output for contract address
output = result.stdout + result.stderr
print(output)
# Look for contract address in output
# cargo-stylus typically outputs: "Contract deployed at: 0x..."
for line in output.split("\n"):
if "deployed" in line.lower() or "address" in line.lower():
print(f"✅ {line}")
# Try to extract address
if "0x" in line:
parts = line.split("0x")
if len(parts) > 1:
address = "0x" + parts[1].split()[0][:40]
return address
print("⚠️ Could not parse contract address from output")
return None
except subprocess.CalledProcessError as e:
print(f"❌ Deployment failed: {e.stderr}")
return None
def initialize_contract(
contract_address: str,
owner_address: str,
treasury_address: str,
private_key: Optional[str] = None,
mnemonic: Optional[str] = None,
rpc_url: str = ARBITRUM_SEPOLIA_RPC
) -> bool:
"""
Initialize the deployed contract with owner and treasury addresses.
"""
print(f"\n⚙️ Initializing contract at {contract_address}...")
print(f" Owner: {owner_address}")
print(f" Treasury: {treasury_address}")
# Use cargo-stylus to call init function
cmd = [
"cargo", "stylus", "call",
"--address", contract_address,
"--function", "init",
"--args", f"{owner_address},{treasury_address}",
"--network", "sepolia",
"--rpc-url", rpc_url,
]
if private_key:
cmd.extend(["--private-key", private_key])
elif mnemonic:
cmd.extend(["--mnemonic", mnemonic])
try:
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True
)
print("✅ Contract initialized successfully")
print(result.stdout)
return True
except subprocess.CalledProcessError as e:
print(f"❌ Initialization failed: {e.stderr}")
return False
def main():
"""Main deployment flow."""
print("=" * 60)
print("🌳 Oak Protocol Deployment Script")
print("=" * 60)
# Check prerequisites
if not check_prerequisites():
sys.exit(1)
# Compile contract
if not compile_contract():
sys.exit(1)
# Get deployment credentials
private_key = os.getenv("PRIVATE_KEY")
mnemonic = os.getenv("MNEMONIC")
if not private_key and not mnemonic:
print("\n❌ Error: Set PRIVATE_KEY or MNEMONIC environment variable")
print(" Example: export PRIVATE_KEY=0x...")
sys.exit(1)
# Get addresses for initialization
owner_address = os.getenv("OWNER_ADDRESS")
treasury_address = os.getenv("TREASURY_ADDRESS")
if not owner_address or not treasury_address:
print("\n⚠️ Warning: OWNER_ADDRESS and TREASURY_ADDRESS not set")
print(" Contract will be deployed but not initialized")
print(" Set them to auto-initialize:")
print(" export OWNER_ADDRESS=0x...")
print(" export TREASURY_ADDRESS=0x...")
# Deploy contract
contract_address = deploy_contract(private_key=private_key, mnemonic=mnemonic)
if not contract_address:
print("\n❌ Deployment failed")
sys.exit(1)
print(f"\n✅ Contract deployed at: {contract_address}")
# Initialize if addresses provided
if owner_address and treasury_address:
if initialize_contract(contract_address, owner_address, treasury_address,
private_key=private_key, mnemonic=mnemonic):
print("\n" + "=" * 60)
print("🎉 Deployment Complete!")
print("=" * 60)
print(f"Contract Address: {contract_address}")
print(f"Owner: {owner_address}")
print(f"Treasury: {treasury_address}")
print("\nNext steps:")
print("1. Verify contract on Arbiscan")
print("2. Add liquidity to the pool")
print("3. Test commit-reveal swaps")
else:
print("\n⚠️ Contract deployed but initialization failed")
print(" You can initialize manually using the interaction scripts")
else:
print("\n⚠️ Contract deployed but not initialized")
print(" Set OWNER_ADDRESS and TREASURY_ADDRESS and run:")
print(f" python3 scripts/interaction.py init {contract_address} {owner_address} {treasury_address}")
if __name__ == "__main__":
main()