File size: 2,001 Bytes
efeddde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path

from dotenv import load_dotenv
from solcx import compile_standard, install_solc
from web3 import Web3

load_dotenv()

RPC = os.getenv("SEPOLIA_RPC_URL")
PRIVATE_KEY = os.getenv("PRIVATE_KEY")

if not RPC or not PRIVATE_KEY:
    raise RuntimeError("Set SEPOLIA_RPC_URL and PRIVATE_KEY in .env")

contract_path = Path("contracts/ProvenanceRegistry.sol")
source = contract_path.read_text(encoding="utf-8")

install_solc("0.8.20")

compiled = compile_standard(
    {
        "language": "Solidity",
        "sources": {
            "ProvenanceRegistry.sol": {"content": source}
        },
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "evm.bytecode"]
                }
            }
        },
    },
    solc_version="0.8.20",
)

artifact = compiled["contracts"]["ProvenanceRegistry.sol"]["ProvenanceRegistry"]
abi = artifact["abi"]
bytecode = artifact["evm"]["bytecode"]["object"]

w3 = Web3(Web3.HTTPProvider(RPC))
if not w3.is_connected():
    raise RuntimeError("RPC connection failed.")

account = w3.eth.account.from_key(PRIVATE_KEY)
contract = w3.eth.contract(abi=abi, bytecode=bytecode)

nonce = w3.eth.get_transaction_count(account.address)
tx = contract.constructor().build_transaction({
    "from": account.address,
    "nonce": nonce,
    "chainId": w3.eth.chain_id,
    "gas": 1000000,
    "maxFeePerGas": w3.to_wei("30", "gwei"),
    "maxPriorityFeePerGas": w3.to_wei("1", "gwei"),
})

signed = account.sign_transaction(tx)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

address = receipt.contractAddress
print("Contract deployed:", address)
print("Transaction:", tx_hash.hex())

abi_path = Path("app/blockchain/abi.json")
abi_path.parent.mkdir(parents=True, exist_ok=True)
abi_path.write_text(json.dumps(abi, indent=2), encoding="utf-8")

print("\nAdd this to .env:")
print(f"CONTRACT_ADDRESS={address}")