-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploysol.py
More file actions
129 lines (107 loc) · 3.99 KB
/
deploysol.py
File metadata and controls
129 lines (107 loc) · 3.99 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
import json
import httpx
from typing import Any
from mcp.server.fastmcp import FastMCP
from web3 import Web3
from eth_account import Account
from solcx import install_solc, compile_standard
from dotenv import load_dotenv
import os
load_dotenv()
# 连接Sepolia测试链
w3 = Web3(Web3.HTTPProvider(os.getenv("WEB3_HTTP_PROVIDER")))
print("Connected:", w3.is_connected())
# 设置账户
private_key = os.getenv("WEB3_PRIVATE_KEY")
account = Account.from_key(private_key)
print(f"Using account: {account.address}")
# 编译合约
install_solc('0.8.0')
with open("SimpleStorage.sol", "r") as file:
contract_source = file.read()
compiled_sol = compile_standard({
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": contract_source}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "evm.bytecode"]}
}
}
})
# 提取ABI和字节码
abi = compiled_sol['contracts']['SimpleStorage.sol']['SimpleStorage']['abi']
bytecode = compiled_sol['contracts']['SimpleStorage.sol']['SimpleStorage']['evm']['bytecode']['object']
# 保存ABI
with open("abi.json", "w") as f:
json.dump(abi, f)
# 部署合约
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
nonce = w3.eth.get_transaction_count(account.address)
construct_txn = contract.constructor(42).build_transaction({
'chainId': w3.eth.chain_id,
'gas': 1000000,
'nonce': nonce,
'maxFeePerGas': w3.to_wei('25', 'gwei'),
'maxPriorityFeePerGas': w3.to_wei('2', 'gwei')
})
signed_txn = w3.eth.account.sign_transaction(construct_txn, private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.raw_transaction)
print(f"Deployment tx hash: {tx_hash.hex()}")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Contract deployed at: {tx_receipt.contractAddress}")
print(f"Transaction hash: {tx_hash.hex()}")
# ========== 新增服务注册合约部署 ==========
# 编译部署服务注册合约
with open("WertherserverRegistry.sol", "r") as file:
registry_source = file.read()
compiled_registry = compile_standard({
"language": "Solidity",
"sources": {"WertherserverRegistry.sol": {"content": registry_source}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "evm.bytecode"]}
}
}
})
# 提取注册合约ABI和字节码
registry_abi = compiled_registry['contracts']['WertherserverRegistry.sol']['WertherserverRegistry']['abi']
registry_bytecode = compiled_registry['contracts']['WertherserverRegistry.sol']['WertherserverRegistry']['evm']['bytecode']['object']
# 保存ABI
with open("registry_abi.json", "w") as f:
json.dump(registry_abi, f)
# 部署注册合约
registry_contract = w3.eth.contract(abi=registry_abi, bytecode=registry_bytecode)
construct_txn = registry_contract.constructor().build_transaction({
'chainId': w3.eth.chain_id,
'gas': 2000000,
'nonce': w3.eth.get_transaction_count(account.address),
'maxFeePerGas': w3.to_wei('25', 'gwei'),
'maxPriorityFeePerGas': w3.to_wei('2', 'gwei')
})
signed_txn = w3.eth.account.sign_transaction(construct_txn, private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.raw_transaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
# 注册天气服务
registry = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=registry_abi
)
register_tx = registry.functions.registerService(
"WeatherService", # 服务名称
"1.0" # 服务版本
).build_transaction({
'chainId': w3.eth.chain_id,
'gas': 200000,
'nonce': w3.eth.get_transaction_count(account.address),
'maxFeePerGas': w3.to_wei('25', 'gwei'),
'maxPriorityFeePerGas': w3.to_wei('2', 'gwei')
})
signed_register = w3.eth.account.sign_transaction(register_tx, private_key)
register_hash = w3.eth.send_raw_transaction(signed_register.raw_transaction)
w3.eth.wait_for_transaction_receipt(register_hash)
# 保存注册信息
with open("service_registry.json", "w") as f:
json.dump({
"address": tx_receipt.contractAddress,
"abi": registry_abi
}, f)