-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_favorites.py
96 lines (81 loc) · 2.83 KB
/
deploy_favorites.py
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
# web3
from web3 import Web3, HTTPProvider
from eth_account import Account
from vyper import compile_code
# python
from dotenv import load_dotenv
import os
import sys
import getpass
from pathlib import Path
import json
# Load environment variables
load_dotenv()
RPC_URL = os.getenv("RPC_URL")
MY_ANVIL_ADDRESS = os.getenv("MY_ANVIL_ADDRESS")
TENDERLY_RPC_URL = os.getenv("TENDERLY_RPC_URL")
METAMASK_ADDRESS = os.getenv("METAMASK_ADDRESS")
# Get args
CHAIN_ID = int(sys.argv[1]) if len(sys.argv) > 1 else 31337
# Get keystore account
KEYSTORE_PATH = Path(".keystore.json")
def network_helper(chain_id: int) -> tuple([str, str]):
address = None
rpc_url = None
if chain_id == 735711155111:
# Tenderly
address = METAMASK_ADDRESS
rpc_url = TENDERLY_RPC_URL
else:
# Anvil
address = MY_ANVIL_ADDRESS
rpc_url = RPC_URL
return (address, rpc_url)
def main() -> None:
# No chain id passed in
if CHAIN_ID == None:
print("Please pass in a chain id")
return
elif CHAIN_ID != 735711155111 and CHAIN_ID != 31337:
print("Please pass in a valid chain id")
return
print(f"Deploying to chain id: {CHAIN_ID}")
# Run on the correct network
print("Let's read in the Vyper code and deploy it to the blockchain!")
(address, rpc_url) = network_helper(CHAIN_ID)
w3 = Web3(Web3.HTTPProvider(rpc_url))
with open("favorites.vy", "r") as favorites_file:
favorites_code = favorites_file.read()
compilation_details = compile_code(
favorites_code, output_formats=["bytecode", "abi"]
)
# Create the contract in Python
favorites_contract = w3.eth.contract(
abi=compilation_details["abi"], bytecode=compilation_details["bytecode"]
)
# Submit the transaction that deploys the contract
nonce = w3.eth.get_transaction_count(address)
print("Building the transaction...")
transaction = favorites_contract.constructor().build_transaction(
{
"gasPrice": w3.eth.gas_price,
"from": address,
"nonce": nonce,
}
)
# Decrypt account
if not KEYSTORE_PATH.exists():
print("Please run encrypt_key.py first")
return
with KEYSTORE_PATH.open("r") as fp:
encrypted_account = json.load(fp)
passphrase = getpass.getpass("Enter passphrase of your wallet account:")
private_key = Account.decrypt(encrypted_account, passphrase)
print("Signing transaction")
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.raw_transaction)
print(f"My tx hash: {tx_hash}")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Contract deployed to {tx_receipt.contractAddress}")
if __name__ == "__main__":
main()