-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeployment.py
More file actions
85 lines (65 loc) · 2.56 KB
/
deployment.py
File metadata and controls
85 lines (65 loc) · 2.56 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
from eth_utils import address
from web3 import Web3
import os
from solcx import compile_standard
import solcx
from dotenv import load_dotenv
import json
load_dotenv()
solcx.install_solc('0.7.6')
class Deployer:
def __init__(self, RPCURL, CHAINID, ADDRESS, CONTRACT_NAME):
self._RPCURL = RPCURL
self._CHAINID = CHAINID
self._ADDRESS = ADDRESS
self._PRIVATE_KEY = os.getenv('PRIVATE_KEY')
self._CONTRACT_NAME = CONTRACT_NAME
def compile(self):
name = self._CONTRACT_NAME
with open(f"./{name}.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {f"{name}.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {
"*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
}
}
},
},
solc_version='0.7.6',
)
with open(f"compiled_{name}.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
self._bytecode = compiled_sol["contracts"][f"{name}.sol"][name]["evm"]["bytecode"]["object"]
# get abi
self._abi = json.loads(
compiled_sol["contracts"][f"{name}.sol"][name]["metadata"]
)["output"]["abi"]
def sign(self, provider, transaction):
signed_tx = provider.eth.account.signTransaction(
transaction, private_key=self._PRIVATE_KEY,)
return signed_tx
def send(self, provider, signed_tx):
tx_hash = provider.eth.account.send_raw_transaction(
signed_tx.rawTransaction)
tx_receipt = provider.eth.wait_for_transaction_receipt(tx_hash)
return tx_receipt
def deploy_Trader(self, router, token0, token1, fee):
# set up connection
w3 = Web3(Web3.WebsocketProvider(self._RPCURL))
self.compile()
# initialize contract
Trader = w3.eth.contract(abi=self._abi, bytecode=self._bytecode)
nonce = w3.eth.getTransactionCount(self._ADDRESS)
# set up transaction from constructor which executes when firstly
transaction = Trader.constructor(router, token0, token1, fee).buildTransaction(
{"chainId": self._CHAINID, "from": self._ADDRESS, "nonce": nonce}
)
signed_tx = self.sign(w3, transaction)
tx_receipt = self.send(w3, signed_tx)
return tx_receipt