-
Notifications
You must be signed in to change notification settings - Fork 9
/
etherscanClass.py
111 lines (85 loc) · 3.75 KB
/
etherscanClass.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
import os
import requests
import traceback
import time
from dbClass import dbCalls
from dbPGClass import dbPGCalls
from otherClass import otherCalls
class etherscanCalls(object):
def __init__(self, config, db = None):
self.config = config
if db == None:
if self.config['main']['use-pg']:
self.db = dbPGCalls(config)
else:
self.db = dbCalls(config)
else:
self.db = db
self.otc = otherCalls(config, self.db)
self.apikey = self.config['other']['etherscan-apikey']
self.url = 'https://api.etherscan.io/api?'
self.lastScannedBlock = self.db.lastScannedBlock("ETH")
def currentBlock(self):
time.sleep(2)
url = self.url + 'module=proxy&action=eth_blockNumber&apikey=' + self.apikey
result = requests.get(url).json()
return int(result['result'], 16)
def getBlock(self, height):
time.sleep(2)
url = self.url + 'module=account&action=txlist&address=' + self.config['other']['gatewayAddress'] + '&startblock=' + str(height) + '&endblock=' + str((self.currentBlock() - self.config['other']['confirmations'])) + '&sort=asc&apikey=' + self.apikey
result = requests.get(url).json()
if result['status'] == '1':
result = {'transactions': result['result']}
else:
result = {'transactions': []}
return result
def currentBalance(self):
time.sleep(2)
try:
url = self.url + 'module=account&action=balance&address=' + self.config['other']['gatewayAddress'] + '&tag=latest&apikey=' + self.apikey
result = requests.get(url).json()
if result['status'] == '1':
balance = int(result['result'])
balance /= pow(10, self.config['other']['contract']['decimals'])
else:
balance = 0
except:
balance = self.otc.currentBalance()
return balance
def normalizeAddress(self, address):
return self.otc.normalizeAddress(address)
def validateAddress(self, address):
return self.otc.validateAddress(address)
def verifyTx(self, txId, sourceAddress = '', targetAddress = ''):
time.sleep(2)
if type(txId) == str:
txid = txId
else:
txid = txId.hex()
url = self.url + 'module=proxy&action=eth_getTransactionReceipt&txhash=' + txid + '&apikey=' + self.apikey
try:
verified = requests.get(url).json()['result']
if int(verified['status'], 16) == 1:
self.db.insVerified("ETH", txid, int(verified['blockNumber'], 16))
print('INFO: tx to eth verified!')
self.db.delTunnel(sourceAddress, targetAddress)
elif int(verified['status'], 16) == 0:
print('ERROR: tx failed to send!')
self.resendTx(txId)
except:
self.db.insVerified("ETH", txid, 0)
print('WARN: tx to eth not verified!')
def checkTx(self, tx):
#check the transaction
result = None
tx['to'] = self.normalizeAddress(tx['to'])
if tx['to'] == self.config['other']['gatewayAddress']:
sender = self.normalizeAddress(tx['from'])
amount = int(tx['value']) / 10 ** self.config['other']['decimals']
if not self.db.didWeSendTx(tx['hash']):
result = { 'sender': sender, 'function': 'transfer', 'recipient': tx['to'], 'amount': amount, 'id': tx['hash'] }
return result
def sendTx(self, targetAddress, amount, gasprice = None, gas = None):
return self.otc.sendTx(targetAddress, amount, gasprice, gas)
def resendTx(self, txId):
self.otc.resendTx(txId)