-
Notifications
You must be signed in to change notification settings - Fork 4
/
bridge.py
125 lines (111 loc) · 4.63 KB
/
bridge.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
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
from time import sleep
from web3 import Web3, HTTPProvider
from json import load, dump
from os import path
# initialize contracts
def initializeContractFactory(_w3, _path, _address):
abi = getAbi(_path)
contract = _w3.eth.contract(
abi=abi,
address=_address
)
return contract
# get private key from json file
def getPrivateKey():
privateKeyPath = "/privateKey.json"
if not path.exists(privateKeyPath):
with open("./privateKey.json") as file:
return load(file)["key"]
else:
print("Private key was not found")
exit()
# get contract address from json file
def getContractAddress():
with open("./contracts.json") as file:
return load(file)
# get last processed block from json file
def getLastProcessedBlock(_key):
if not path.exists(_key):
writeDataBase({"blockNumber": 0}, _key)
return 0
with open(_key) as f:
return load(f)['blockNumber']
# write _data in external _file
def writeDataBase(_data, _file):
with open(_file, 'w') as out:
dump(_data, out)
# get abi for contracts at specified path
def getAbi(_path):
with open(_path) as _file:
abi = load(_file)
return abi
def main(_w3Home, _w3Foreign):
homeBridge = initializeContractFactory(_w3Home, "../abi/bridge_abi.json",
Web3.toChecksumAddress(getContractAddress()["HomeBridge"]))
foreignBridge = initializeContractFactory(_w3Foreign, "../abi/bridge_abi.json",
Web3.toChecksumAddress(getContractAddress()["ForeignBridge"]))
acct = _w3Home.eth.account.privateKeyToAccount(getPrivateKey())
# infinite loop which processes both networks in row
while True:
print([])
filterHome = {
"fromBlock": getLastProcessedBlock(lastProcessedHomeBlockPath),
"toBlock": "latest",
"address": homeBridge.address
}
logs = _w3Home.eth.getLogs(filterHome)
for i in logs:
receipt = _w3Home.eth.getTransactionReceipt(i['transactionHash'])
events = homeBridge.events.UserRequestForSignature().processReceipt(receipt)
for ev in events:
nonce = _w3Foreign.eth.getTransactionCount(acct.address)
tx_foreign = {
"gas": 7000000,
"gasPrice": gasPrice,
"nonce": nonce
}
tx = foreignBridge.functions.transferApproved(
ev.args['_from'],
ev.args['_tokenVIN'],
ev.args['_data'],
ev.transactionHash
).buildTransaction(tx_foreign)
signed_tx = acct.signTransaction(tx)
tx_hash = _w3Foreign.eth.sendRawTransaction(signed_tx.rawTransaction)
_w3Foreign.eth.waitForTransactionReceipt(tx_hash)
print(tx_hash.hex())
writeDataBase({'blockNumber': receipt.blockNumber + 1}, lastProcessedHomeBlockPath)
filterForeign = {
"fromBlock": getLastProcessedBlock(lastProcessedForeignBlockPath),
"toBlock": "latest",
"address": foreignBridge.address
}
logs = _w3Foreign.eth.getLogs(filterForeign)
for i in logs:
receipt = _w3Foreign.eth.getTransactionReceipt(i['transactionHash'])
events = foreignBridge.events.UserRequestForSignature().processReceipt(receipt)
for ev in events:
nonce = _w3Home.eth.getTransactionCount(acct.address)
tx_home = {
"gas": 7000000,
"gasPrice": gasPrice,
"nonce": nonce
}
tx = homeBridge.functions.transferApproved(
ev.args['_from'],
ev.args['_tokenVIN'],
ev.args['_data'],
ev.transactionHash
).buildTransaction(tx_home)
signed_tx = acct.signTransaction(tx)
tx_hash = _w3Home.eth.sendRawTransaction(signed_tx.rawTransaction)
_w3Home.eth.waitForTransactionReceipt(tx_hash)
print(tx_hash.hex())
writeDataBase({'blockNumber': receipt.blockNumber + 1}, lastProcessedForeignBlockPath)
sleep(5)
w3Home = Web3(HTTPProvider("https://sokol.poa.network/"))
w3Foreign = Web3(HTTPProvider("https://kovan.infura.io/mew"))
lastProcessedHomeBlockPath = "../additional_data/homeLastProcessedBlock.json"
lastProcessedForeignBlockPath = "../additional_data/foreignLastProcessedBlock.json"
gasPrice = Web3.toWei(1, "gwei")
main(w3Home, w3Foreign)