-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanchor_deploy.py
More file actions
59 lines (49 loc) · 1.79 KB
/
anchor_deploy.py
File metadata and controls
59 lines (49 loc) · 1.79 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
import os, json
from dotenv import load_dotenv
from web3 import Web3
from solcx import install_solc, set_solc_version, compile_source
load_dotenv()
RPC = os.environ["WEB3_RPC_URL"]
PK = os.environ["PRIVATE_KEY"]
SRC = r"""// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract CreditAnchor {
event Anchored(uint256 indexed blockId, bytes32 indexed root, address indexed caller);
mapping(uint256 => bytes32) public roots;
function anchor(uint256 blockId, bytes32 root) external returns (bool) {
require(roots[blockId] == bytes32(0), "already anchored");
roots[blockId] = root;
emit Anchored(blockId, root, msg.sender);
return true;
}
}
"""
def main():
# 1) compile
install_solc("0.8.20")
set_solc_version("0.8.20")
compiled = compile_source(SRC, output_values=["abi", "bin"])
_, c = list(compiled.items())[0]
abi, bytecode = c["abi"], c["bin"]
# 2) connect + account
w3 = Web3(Web3.HTTPProvider(RPC))
acct = w3.eth.account.from_key(PK)
# 3) build & send deploy tx
Contract = w3.eth.contract(abi=abi, bytecode=bytecode)
tx = Contract.constructor().build_transaction({
"from": acct.address,
"nonce": w3.eth.get_transaction_count(acct.address),
"maxFeePerGas": w3.to_wei("20", "gwei"),
"maxPriorityFeePerGas": w3.to_wei("1", "gwei"),
"chainId": w3.eth.chain_id,
})
signed = w3.eth.account.sign_transaction(tx, PK)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
rcpt = w3.eth.wait_for_transaction_receipt(tx_hash)
addr = rcpt.contractAddress
print("Contract deployed at:", addr)
print("Tx:", tx_hash.hex())
# save ABI for later
with open("CreditAnchor.abi.json", "w") as f: json.dump(abi, f)
if __name__ == "__main__":
main()